[FIX] #593
This commit is contained in:
@@ -1,57 +1,57 @@
|
||||
$(document).ready(function () {
|
||||
|
||||
// this function is used to generate csrf token
|
||||
function getCookie(name) {
|
||||
let cookieValue = null;
|
||||
if (document.cookie && document.cookie !== "") {
|
||||
const cookies = document.cookie.split(";");
|
||||
for (let i = 0; i < cookies.length; i++) {
|
||||
const cookie = cookies[i].trim();
|
||||
// Does this cookie string begin with the name we want?
|
||||
if (cookie.substring(0, name.length + 1) === (name + "=")) {
|
||||
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
||||
break;
|
||||
}
|
||||
// this function is used to generate csrf token
|
||||
function getCookie(name) {
|
||||
let cookieValue = null;
|
||||
if (document.cookie && document.cookie !== "") {
|
||||
const cookies = document.cookie.split(";");
|
||||
for (let i = 0; i < cookies.length; i++) {
|
||||
const cookie = cookies[i].trim();
|
||||
// Does this cookie string begin with the name we want?
|
||||
if (cookie.substring(0, name.length + 1) === (name + "=")) {
|
||||
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return cookieValue;
|
||||
}
|
||||
}
|
||||
return cookieValue;
|
||||
}
|
||||
|
||||
|
||||
$select = $("#id_period").select2({
|
||||
minimumResultsForSearch: -1,
|
||||
}).on("change", function () {
|
||||
let $this = $(this)
|
||||
periodChange($this)
|
||||
});
|
||||
$select = $("#id_period").select2({
|
||||
minimumResultsForSearch: -1,
|
||||
}).on("change", function () {
|
||||
let $this = $(this)
|
||||
periodChange($this)
|
||||
});
|
||||
|
||||
$select.data('select2').$selection.addClass('oh-select--lg--custom'); //adding css class for the select
|
||||
$select.data('select2').$selection.addClass('oh-select--lg--custom'); //adding css class for the select
|
||||
|
||||
// period assigning to dates
|
||||
function periodChange(period_data) {
|
||||
// period assigning to dates
|
||||
function periodChange(period_data) {
|
||||
var period_id = period_data.val();
|
||||
|
||||
var period_id = period_data.val()
|
||||
$.ajax({
|
||||
url: '/pms/period-change',
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
data: JSON.stringify(period_id),
|
||||
// Check if period_id is not "create_new_period" before making the request
|
||||
if (period_id !== "create_new_period") {
|
||||
$.ajax({
|
||||
url: '/pms/period-change',
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
data: JSON.stringify(period_id),
|
||||
headers: {
|
||||
"X-Requested-With": "XMLHttpRequest",
|
||||
"X-CSRFToken": getCookie("csrftoken"),
|
||||
},
|
||||
success: (data) => {
|
||||
// Adding data to start and end date
|
||||
$('#id_start_date').val(data.start_date);
|
||||
$('#id_end_date').val(data.end_date);
|
||||
},
|
||||
error: (error) => {
|
||||
console.log('Error', error);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
headers: {
|
||||
"X-Requested-With": "XMLHttpRequest",
|
||||
"X-CSRFToken": getCookie("csrftoken"),
|
||||
|
||||
},
|
||||
success: (data) => {
|
||||
// adding data to start and end date
|
||||
$('#id_start_date').val(data.start_date)
|
||||
$('#id_end_date').val(data.end_date)
|
||||
|
||||
|
||||
},
|
||||
error: (error) => {
|
||||
console.log('Error', error);
|
||||
}
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
@@ -12,116 +12,106 @@
|
||||
</div>
|
||||
{% endif %}
|
||||
<span class="oh-modal__dialog-title" id="addEmployeeModalLabel">{% trans "Create Period" %}</span>
|
||||
<form onsubmit="event.preventDefault()" hx-target="#periodModalTarget" id="periodForm">
|
||||
{% csrf_token %}
|
||||
<section>
|
||||
<div id="periodContainer">
|
||||
<div class="my-3" id="keyResultCard">
|
||||
<div class="">
|
||||
|
||||
<div class="oh-input__group">
|
||||
<label class="oh-input__label mt-0" for="{{form.period_name.id_for_label}}">{% trans "Title" %}</label>
|
||||
{{ form.period_name }}
|
||||
{{ form.period_name.errors }}
|
||||
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-12 col-sm-12 col-md-6 col-lg-6">
|
||||
<div class="oh-input__group">
|
||||
<label class="oh-input__label" for="{{form.start_date.id_for_label}}">{% trans "Start Date" %}</label>
|
||||
{{form.start_date}}
|
||||
|
||||
<div id="error-container"></div>
|
||||
|
||||
<form onsubmit="event.preventDefault()" hx-target="#periodModalTarget" id="periodForm" class="oh-profile-section">
|
||||
{% csrf_token %}
|
||||
<section>
|
||||
<div id="periodContainer">
|
||||
<div class="my-3" id="keyResultCard">
|
||||
<div class="">
|
||||
<div class="oh-input__group">
|
||||
<label class="oh-input__label mt-0" for="{{form.period_name.id_for_label}}">{% trans "Title" %}</label>
|
||||
{{ form.period_name }}
|
||||
{{ form.period_name.errors }}
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-12 col-sm-12 col-md-6 col-lg-6">
|
||||
<div class="oh-input__group">
|
||||
<label class="oh-input__label" for="{{form.start_date.id_for_label}}">{% trans "Start Date" %}</label>
|
||||
{{form.start_date}}
|
||||
<div id="error-container"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-12 col-sm-12 col-md-6 col-lg-6">
|
||||
<div class="oh-input__group">
|
||||
<label class="oh-input__label" for="{{form.end_date.id_for_label}}">{% trans "End Date" %}</label>
|
||||
{{form.end_date}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-12 col-sm-12 col-md-6 col-lg-6">
|
||||
<div class="oh-input__group">
|
||||
<label class="oh-input__label" for="{{form.end_date.id_for_label}}">{% trans "End Date" %}</label>
|
||||
{{form.end_date}}
|
||||
|
||||
<div class="oh-modal__dialog-footer p-0">
|
||||
<button type="submit" class="oh-btn oh-btn--secondary oh-btn--shadow ">
|
||||
{% trans "Save" %}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="oh-modal__dialog-footer pe-0">
|
||||
<button type="submit" class="oh-btn oh-btn--secondary oh-btn--shadow ">
|
||||
{% trans "Save" %}
|
||||
</button>
|
||||
</div>
|
||||
</section>
|
||||
</section>
|
||||
</form>
|
||||
<script>
|
||||
$(document).ready(function (e) {
|
||||
function getCookie(name) {
|
||||
let cookieValue = null;
|
||||
if (document.cookie && document.cookie !== "") {
|
||||
const cookies = document.cookie.split(";");
|
||||
for (let i = 0; i < cookies.length; i++) {
|
||||
const cookie = cookies[i].trim();
|
||||
if (cookie.substring(0, name.length + 1) === name + "=") {
|
||||
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return cookieValue;
|
||||
}
|
||||
$("#periodForm").submit(function(e){
|
||||
|
||||
e.preventDefault();
|
||||
var formData = $(this).serialize();
|
||||
|
||||
formData["csrfmiddlewaretoken"] = getCookie("csrftoken");
|
||||
$.ajax({
|
||||
type:"post",
|
||||
url:"create-period",
|
||||
data:formData,
|
||||
beforeSend: function () {
|
||||
$(".errorlist").remove();
|
||||
},
|
||||
success:function(data){
|
||||
|
||||
if (data.errors){
|
||||
var errorDataJson = JSON.parse(data.errors)
|
||||
key = Object.keys(errorDataJson)[0]
|
||||
|
||||
if (key == 'period_name'){
|
||||
var nameerror = errorDataJson["period_name"]
|
||||
var errorMessage = nameerror[0].message;
|
||||
function getCookie(name) {
|
||||
let cookieValue = null;
|
||||
if (document.cookie && document.cookie !== "") {
|
||||
const cookies = document.cookie.split(";");
|
||||
for (let i = 0; i < cookies.length; i++) {
|
||||
const cookie = cookies[i].trim();
|
||||
if (cookie.substring(0, name.length + 1) === name + "=") {
|
||||
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
var dateerror = errorDataJson["__all__"]
|
||||
var errorMessage = dateerror[0].message;
|
||||
}
|
||||
|
||||
$('#error-container').html('<div class="error text-danger">' + errorMessage + '</div>');
|
||||
|
||||
}
|
||||
else{
|
||||
var newOption = $("<option></option>")
|
||||
.val(data.id)
|
||||
.text(data.name);
|
||||
$("#id_period option[value='create_new_period']").before(newOption);
|
||||
$("#id_period").val(data.id).trigger("change");
|
||||
// Close the second modal and reset the form
|
||||
$('#PeriodModal').removeClass('oh-modal--show');
|
||||
var message = `
|
||||
<div class="oh-alert-container">
|
||||
<div class="oh-alert oh-alert--animated oh-alert--success">
|
||||
Period created successfully
|
||||
</div>
|
||||
</div>
|
||||
`
|
||||
$("#message").html(message);
|
||||
document.periodForm.reset();
|
||||
return cookieValue;
|
||||
}
|
||||
},
|
||||
});
|
||||
$("#periodForm").submit(function (e) {
|
||||
|
||||
e.preventDefault();
|
||||
var formData = $(this).serialize();
|
||||
|
||||
formData["csrfmiddlewaretoken"] = getCookie("csrftoken");
|
||||
$.ajax({
|
||||
type: "post",
|
||||
url: "create-period",
|
||||
data: formData,
|
||||
beforeSend: function () {
|
||||
$(".errorlist").remove();
|
||||
},
|
||||
success: function (data) {
|
||||
if (data.errors) {
|
||||
var errorDataJson = JSON.parse(data.errors);
|
||||
key = Object.keys(errorDataJson)[0];
|
||||
if (key == 'period_name') {
|
||||
var nameerror = errorDataJson["period_name"];
|
||||
var errorMessage = nameerror[0].message;
|
||||
}
|
||||
else {
|
||||
var dateerror = errorDataJson["__all__"];
|
||||
var errorMessage = dateerror[0].message;
|
||||
}
|
||||
$('#error-container').html('<div class="error text-danger">' + errorMessage + '</div>');
|
||||
}
|
||||
|
||||
else {
|
||||
var newOption = $("<option></option>")
|
||||
.val(data.id)
|
||||
.text(data.name);
|
||||
$("#id_period option[value='create_new_period']").before(newOption);
|
||||
$("#id_period").val(data.id).trigger("change");
|
||||
// Close the second modal and reset the form
|
||||
$('#PeriodModal').removeClass('oh-modal--show');
|
||||
var message = `
|
||||
<div class="oh-alert-container">
|
||||
<div class="oh-alert oh-alert--animated oh-alert--success">
|
||||
Period created successfully
|
||||
</div>
|
||||
</div>
|
||||
`
|
||||
$("#message").html(message);
|
||||
$("#periodForm")[0].reset();
|
||||
}
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
</script>
|
||||
|
||||
13
pms/views.py
13
pms/views.py
@@ -33,6 +33,7 @@ from base.methods import (
|
||||
paginator_qry,
|
||||
sortby,
|
||||
)
|
||||
from base.models import Company
|
||||
from employee.models import Employee, EmployeeWorkInformation
|
||||
from horilla.decorators import (
|
||||
hx_request_required,
|
||||
@@ -2733,11 +2734,19 @@ def create_period(request):
|
||||
This is an ajax method to return json response to create stage related
|
||||
to the project in the task-all form fields
|
||||
"""
|
||||
company_id = request.session.get("selected_company")
|
||||
companies = (
|
||||
Company.objects.filter(id=company_id)
|
||||
if company_id != "all"
|
||||
else Company.objects.all()
|
||||
)
|
||||
|
||||
if request.method == "GET":
|
||||
form = PeriodForm()
|
||||
form = PeriodForm(initial={"company_id": companies})
|
||||
if request.method == "POST":
|
||||
form = PeriodForm(request.POST)
|
||||
data = request.POST.copy()
|
||||
data.setlist("company_id", list(companies.values_list("id", flat=True)))
|
||||
form = PeriodForm(data)
|
||||
if form.is_valid():
|
||||
instance = form.save()
|
||||
return JsonResponse(
|
||||
|
||||
Reference in New Issue
Block a user