diff --git a/pms/static/src/period/period.js b/pms/static/src/period/period.js index e0c8ed42c..06b1a562b 100644 --- a/pms/static/src/period/period.js +++ b/pms/static/src/period/period.js @@ -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); - } - }) - } }); diff --git a/pms/templates/okr/create_period.html b/pms/templates/okr/create_period.html index fb170fd0f..7cf79b3e9 100644 --- a/pms/templates/okr/create_period.html +++ b/pms/templates/okr/create_period.html @@ -12,116 +12,106 @@ {% endif %} {% trans "Create Period" %} -
- {% csrf_token %} -
-
-
-
- -
- - {{ form.period_name }} - {{ form.period_name.errors }} - -
-
-
-
- - {{form.start_date}} - -
- + + {% csrf_token %} +
+
+
+
+
+ + {{ form.period_name }} + {{ form.period_name.errors }} +
+
+
+
+ + {{form.start_date}} +
+
+
+
+
+ + {{form.end_date}} +
+
+
+
-
-
-
- - {{form.end_date}} - + -
-
-
- -
- -
+
diff --git a/pms/views.py b/pms/views.py index a37f63cc7..7c94307cc 100644 --- a/pms/views.py +++ b/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(