[FIX] BASE: Fixed import not showing in empty page

This commit is contained in:
Horilla
2025-02-05 11:20:06 +05:30
parent cae2a816ec
commit 545efb29a1
2 changed files with 518 additions and 208 deletions

View File

@@ -1,5 +1,4 @@
import calendar
import datetime as dt
import sys
from datetime import date, datetime, timedelta
@@ -66,7 +65,6 @@ def work_type_rotate_after(rotating_work_work_type):
date_today = datetime.now()
switch_date = rotating_work_work_type.next_change_date
if switch_date.strftime("%Y-%m-%d") == date_today.strftime("%Y-%m-%d"):
# calculate the next work type switch date
new_date = date_today + timedelta(days=rotating_work_work_type.rotate_after_day)
update_rotating_work_type_assign(rotating_work_work_type, new_date)
return
@@ -79,7 +77,6 @@ def work_type_rotate_weekend(rotating_work_type):
date_today = datetime.now()
switch_date = rotating_work_type.next_change_date
if switch_date.strftime("%Y-%m-%d") == date_today.strftime("%Y-%m-%d"):
# calculate the next work type switch date
day = datetime.now().strftime("%A").lower()
switch_day = rotating_work_type.rotate_every_weekend
if day == switch_day:
@@ -96,7 +93,6 @@ def work_type_rotate_every(rotating_work_type):
switch_date = rotating_work_type.next_change_date
day_date = rotating_work_type.rotate_every
if switch_date.strftime("%Y-%m-%d") == date_today.strftime("%Y-%m-%d"):
# calculate the next work type switch date
if day_date == switch_date.strftime("%d").lstrip("0"):
new_date = date_today.replace(month=date_today.month + 1)
update_rotating_work_type_assign(rotating_work_type, new_date)
@@ -134,6 +130,7 @@ def update_rotating_shift_assign(rotating_shift, new_date):
"""
from django.contrib.auth.models import User
next_shift_index = 0
employee = rotating_shift.employee_id
employee_work_info = employee.employee_work_info
rotating_shift_id = rotating_shift.rotating_shift_id
@@ -174,25 +171,23 @@ def update_rotating_shift_assign(rotating_shift, new_date):
return
def shift_rotate_after_day(rotating_shift, today=datetime.now()):
def shift_rotate_after_day(rotating_shift, today):
"""
This method for rotate shift based on after day
"""
switch_date = rotating_shift.next_change_date
if switch_date.strftime("%Y-%m-%d") == today.strftime("%Y-%m-%d"):
# calculate the next work type switch date
if switch_date == today:
new_date = today + timedelta(days=rotating_shift.rotate_after_day)
update_rotating_shift_assign(rotating_shift, new_date)
return
def shift_rotate_weekend(rotating_shift, today=datetime.now()):
def shift_rotate_weekend(rotating_shift, today):
"""
This method for rotate shift based on weekend
"""
switch_date = rotating_shift.next_change_date
if switch_date.strftime("%Y-%m-%d") == today.strftime("%Y-%m-%d"):
# calculate the next work type switch date
if switch_date == today:
day = today.strftime("%A").lower()
switch_day = rotating_shift.rotate_every_weekend
if day == switch_day:
@@ -201,20 +196,19 @@ def shift_rotate_weekend(rotating_shift, today=datetime.now()):
return
def shift_rotate_every(rotating_shift, today=datetime.now()):
def shift_rotate_every(rotating_shift, today):
"""
This method for rotate shift based on every month
"""
switch_date = rotating_shift.next_change_date
day_date = rotating_shift.rotate_every
if switch_date.strftime("%Y-%m-%d") == today.strftime("%Y-%m-%d"):
# calculate the next work type switch date
if switch_date == today:
if day_date == switch_date.strftime("%d").lstrip("0"):
new_date = today.replace(month=today.month + 1)
update_rotating_shift_assign(rotating_shift, new_date)
elif day_date == "last":
year = today.strftime("%Y")
month = today.strftime("%m")
year = today.year
month = today.month
last_day = calendar.monthrange(int(year), int(month) + 1)[1]
new_date = datetime(int(year), int(month) + 1, last_day)
update_rotating_shift_assign(rotating_shift, new_date)
@@ -229,18 +223,29 @@ def rotate_shift():
from base.models import RotatingShiftAssign
rotating_shifts = RotatingShiftAssign.objects.filter(is_active=True)
today = datetime.date
for rotating_shift in rotating_shifts:
today = datetime.now().date()
r_shifts = rotating_shifts.filter(start_date=today)
rotating_shifts_modified = None
for r_shift in r_shifts:
emp_shift = rotating_shifts.filter(
employee_id=r_shift.employee_id, start_date__lte=today
).exclude(id=r_shift.id)
rotating_shifts_modified = rotating_shifts.exclude(
id__in=emp_shift.values_list("id", flat=True)
)
emp_shift.update(is_active=False)
for rotating_shift in rotating_shifts_modified:
based_on = rotating_shift.based_on
# after day condition
if based_on == "after":
shift_rotate_after_day(rotating_shift)
shift_rotate_after_day(rotating_shift, today)
# weekly condition
elif based_on == "weekly":
shift_rotate_weekend(rotating_shift)
shift_rotate_weekend(rotating_shift, today)
# monthly condition
elif based_on == "monthly":
shift_rotate_every(rotating_shift)
shift_rotate_every(rotating_shift, today)
return
@@ -417,8 +422,8 @@ def recurring_holiday():
for recurring_holiday in recurring_holidays:
start_date = recurring_holiday.start_date
end_date = recurring_holiday.end_date
new_start_date = dt.date(start_date.year + 1, start_date.month, start_date.day)
new_end_date = dt.date(end_date.year + 1, end_date.month, end_date.day)
new_start_date = date(start_date.year + 1, start_date.month, start_date.day)
new_end_date = date(end_date.year + 1, end_date.month, end_date.day)
# Checking that end date is not none
if end_date is None:
# checking if that start date is day before today
@@ -438,7 +443,7 @@ if not any(
# Add jobs with next_run_time set to the end of the previous job
try:
scheduler.add_job(rotate_shift, "interval", minutes=5, id="job1")
scheduler.add_job(rotate_shift, "interval", hours=4, id="job1")
except:
pass
@@ -446,7 +451,7 @@ if not any(
scheduler.add_job(
rotate_work_type,
"interval",
minutes=5,
hours=4,
id="job2",
)
except:
@@ -456,7 +461,7 @@ if not any(
scheduler.add_job(
undo_shift,
"interval",
minutes=5,
hours=4,
id="job3",
)
except:
@@ -466,7 +471,7 @@ if not any(
scheduler.add_job(
switch_shift,
"interval",
minutes=5,
hours=4,
id="job4",
)
except:
@@ -476,7 +481,7 @@ if not any(
scheduler.add_job(
undo_work_type,
"interval",
minutes=5,
hours=4,
id="job6",
)
except:
@@ -486,7 +491,7 @@ if not any(
scheduler.add_job(
switch_work_type,
"interval",
minutes=5,
hours=4,
id="job5",
)
except:

View File

@@ -1,187 +1,492 @@
{% load basefilters %} {% load i18n %}
{% load static basefilters %} {% load i18n %}
<section class="oh-wrapper oh-main__topbar" x-data="{searchShow: false}">
<div class="oh-main__titlebar oh-main__titlebar--left">
<h1 class="oh-main__titlebar-title fw-bold">
{% trans "Rotating Shift Assign" %}
</h1>
<a
class="oh-main__titlebar-search-toggle"
role="button"
aria-label="Toggle Search"
@click="searchShow = !searchShow"
>
<ion-icon
name="search-outline"
class="oh-main__titlebar-serach-icon"
></ion-icon>
</a>
</div>
<form
hx-get='{% url "rotating-shift-assign-view" %}'
id="filterForm"
hx-target="#view-container"
class="d-flex"
>
<div class="oh-main__titlebar oh-main__titlebar--right">
{% if rshift_all %}
<div
class="oh-input-group oh-input__search-group"
:class="searchShow ? 'oh-input__search-group--show' : ''"
<div class="oh-main__titlebar oh-main__titlebar--left">
<h1 class="oh-main__titlebar-title fw-bold">
{% trans "Rotating Shift Assign" %}
</h1>
<a
class="oh-main__titlebar-search-toggle"
role="button"
aria-label="Toggle Search"
@click="searchShow = !searchShow"
>
<ion-icon
name="search-outline"
class="oh-input-group__icon oh-input-group__icon--left"
></ion-icon>
<input
type="text"
name="search"
class="oh-input oh-input__icon"
aria-label="Search Input"
placeholder="{% trans 'Search' %}"
onkeyup="$('.filterButton')[0].click()"
/>
</div>
{% endif %}
<div class="oh-main__titlebar-button-container">
{% if rshift_all %}
<div class="oh-dropdown" x-data="{open: false}">
<button class="oh-btn ml-2" @click="open = !open" onclick="event.preventDefault()">
<ion-icon name="filter" class="mr-1"></ion-icon>{% trans "Filter" %}
<div id="filterCount"></div>
</button>
<div
class="oh-dropdown__menu oh-dropdown__menu--right oh-dropdown__filter p-4"
x-show="open"
@click.outside="open = false"
style="display: none"
>
{% include 'base/rotating_shift/filters.html' %}
</div>
</div>
<div class="oh-dropdown" x-data="{open: false}">
<button class="oh-btn ml-2" @click="open = !open" onclick="event.preventDefault()">
<ion-icon name="library-outline" class="mr-1"></ion-icon>{% trans "Group By" %}
<div id="filterCount"></div>
</button>
<div
class="oh-dropdown__menu oh-dropdown__menu--right oh-dropdown__filter p-4"
x-show="open"
@click.outside="open = false"
style="display: none"
>
<div class="oh-accordion">
<label>{% trans "Group By" %}</label>
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-6">
<div class="oh-input-group">
<label class="oh-label">{% trans "Field" %}</label>
</div>
</div>
<div class="col-sm-12 col-md-12 col-lg-6">
<div class="oh-input-group">
<select
class="oh-select mt-1 w-100"
id="id_field"
name="field"
class="select2-selection select2-selection--single"
>
{% for field in gp_fields %}
<option value="{{ field.0 }}">{% trans field.1 %}</option>
{% endfor %}
</select>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="oh-dropdown" x-data="{open: false}">
<button
class="oh-btn oh-btn--dropdown oh-btn ml-2"
@click="open = !open"
@click.outside="open = false"
onclick="event.preventDefault()"
>
{% trans "Actions" %}
</button>
<div
class="oh-dropdown__menu oh-dropdown__menu--right"
x-show="open"
style="display: none"
>
<ul class="oh-dropdown__items">
{% if perms.base.change_rotatingshiftassign %}
<li class="oh-dropdown__item">
<a
href="#"
class="oh-dropdown__link"
data-toggle="oh-modal-toggle"
data-target="#objectCreateModal"
hx-get="{% url 'rotating-shift-assign-info-export' %}"
hx-target="#objectCreateModalTarget"
>{% trans "Export" %}</a
>
</li>
{% endif %}
{% if perms.base.change_rotatingshiftassign or request.user|is_reportingmanager %}
<li class="oh-dropdown__item">
<a
href="#"
class="oh-dropdown__link"
id="archiveRotatingShiftAssign"
>{% trans "Archive" %}</a
>
</li>
{% endif %}
{% if perms.base.change_rotatingshiftassign or request.user|is_reportingmanager %}
<li class="oh-dropdown__item">
<a
href="#"
class="oh-dropdown__link"
id="unArchiveRotatingShiftAssign"
>{% trans "Un-Archive" %}</a
>
</li>
{% endif %}
{% if request.user|is_reportingmanager %}
<li class="oh-dropdown__item">
<a
href="#"
class="oh-dropdown__link oh-dropdown__link--danger"
data-action="delete"
id="deleteRotatingShiftAssign"
>{% trans "Delete" %}</a
>
</li>
{% endif %}
</ul>
</div>
</div>
{% endif %}
{% if perms.base.add_rotatingshiftassign or request.user|is_reportingmanager %}
<div class="oh-btn-group ml-2">
<div class="oh-dropdown" x-data="{open: false}">
<button
class="oh-btn oh-btn--secondary oh-btn--shadow"
hx-get="{% url 'rotating-shift-assign-add' %}"
hx-target="#objectCreateModalTarget"
data-toggle="oh-modal-toggle"
data-target="#objectCreateModal"
>
{% trans "Assign" %}
</button>
</div>
</div>
{% endif %}
</div>
<ion-icon
name="search-outline"
class="oh-main__titlebar-serach-icon"
></ion-icon>
</a>
</div>
</form>
<form
hx-get='{% url "rotating-shift-assign-view" %}'
id="filterForm"
hx-target="#view-container"
class="d-flex"
>
<div class="oh-main__titlebar oh-main__titlebar--right">
{% if rshift_all %}
<div
class="oh-input-group oh-input__search-group"
:class="searchShow ? 'oh-input__search-group--show' : ''"
>
<ion-icon
name="search-outline"
class="oh-input-group__icon oh-input-group__icon--left"
></ion-icon>
<input
type="text"
name="search"
class="oh-input oh-input__icon"
aria-label="Search Input"
placeholder="{% trans 'Search' %}"
onkeyup="$('.filterButton')[0].click()"
/>
</div>
{% endif %}
<div class="oh-main__titlebar-button-container">
{% if rshift_all %}
<div class="oh-dropdown" x-data="{open: false}">
<button
class="oh-btn ml-2"
@click="open = !open"
onclick="event.preventDefault()"
>
<ion-icon name="filter" class="mr-1"></ion-icon>
{% trans "Filter" %}
<div id="filterCount"></div>
</button>
<div
class="oh-dropdown__menu oh-dropdown__menu--right oh-dropdown__filter p-4"
x-show="open"
@click.outside="open = false"
style="display: none"
>
{% include 'base/rotating_shift/filters.html' %}
</div>
</div>
<div class="oh-dropdown" x-data="{open: false}">
<button
class="oh-btn ml-2"
@click="open = !open"
onclick="event.preventDefault()"
>
<ion-icon name="library-outline" class="mr-1"></ion-icon
>{% trans "Group By" %}
<div id="filterCount"></div>
</button>
<div
class="oh-dropdown__menu oh-dropdown__menu--right oh-dropdown__filter p-4"
x-show="open"
@click.outside="open = false"
style="display: none"
>
<div class="oh-accordion">
<label>{% trans "Group By" %}</label>
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-6">
<div class="oh-input-group">
<label class="oh-label"
>{% trans "Field" %}</label
>
</div>
</div>
<div class="col-sm-12 col-md-12 col-lg-6">
<div class="oh-input-group">
<select
class="oh-select mt-1 w-100"
id="id_field"
name="field"
class="select2-selection select2-selection--single"
>
{% for field in gp_fields %}
<option value="{{ field.0 }}">
{% trans field.1 %}
</option>
{% endfor %}
</select>
</div>
</div>
</div>
</div>
</div>
</div>
{% endif %}
<div class="oh-dropdown" x-data="{open: false}">
<button
class="oh-btn oh-btn--dropdown oh-btn ml-2"
@click="open = !open"
@click.outside="open = false"
onclick="event.preventDefault()"
>
{% trans "Actions" %}
</button>
<div
class="oh-dropdown__menu oh-dropdown__menu--right"
x-show="open"
style="display: none"
>
<ul class="oh-dropdown__items">
<li class="oh-dropdown__item">
<a
href="#"
data-toggle="oh-modal-toggle"
data-target="#shiftImport"
class="oh-dropdown__link"
onclick="template_download(event)"
>{% trans "Import" %}</a
>
</li>
{% if rshift_all %}
{% if perms.base.change_rotatingshiftassign %}
<li class="oh-dropdown__item">
<a
href="#"
class="oh-dropdown__link"
data-toggle="oh-modal-toggle"
data-target="#objectCreateModal"
hx-get="{% url 'rotating-shift-assign-info-export' %}"
hx-target="#objectCreateModalTarget"
>{% trans "Export" %}</a
>
</li>
{% endif %}
{% if perms.base.change_rotatingshiftassign or request.user|is_reportingmanager %}
<li class="oh-dropdown__item">
<a
href="#"
class="oh-dropdown__link"
id="archiveRotatingShiftAssign"
>{% trans "Archive" %}</a
>
</li>
{% endif %}
{% if perms.base.change_rotatingshiftassign or request.user|is_reportingmanager %}
<li class="oh-dropdown__item">
<a
href="#"
class="oh-dropdown__link"
id="unArchiveRotatingShiftAssign"
>{% trans "Un-Archive" %}</a
>
</li>
{% endif %}
{% if request.user|is_reportingmanager %}
<li class="oh-dropdown__item">
<a
href="#"
class="oh-dropdown__link oh-dropdown__link--danger"
data-action="delete"
id="deleteRotatingShiftAssign"
>{% trans "Delete" %}</a
>
</li>
{% endif %}
{% endif %}
</ul>
</div>
</div>
{% if perms.base.add_rotatingshiftassign or request.user|is_reportingmanager %}
<div class="oh-btn-group ml-2">
<div class="oh-dropdown" x-data="{open: false}">
<button
class="oh-btn oh-btn--secondary oh-btn--shadow"
hx-get="{% url 'rotating-shift-assign-add' %}"
hx-target="#objectCreateModalTarget"
data-toggle="oh-modal-toggle"
data-target="#objectCreateModal"
>
{% trans "Assign" %}
</button>
</div>
</div>
{% endif %}
</div>
</div>
</form>
</section>
<div
class="oh-modal"
id="shiftImport"
role="dialog"
aria-labelledby="shiftImport"
aria-hidden="true"
>
<div class="oh-modal__dialog">
<div class="oh-modal__dialog-header">
<h2 class="oh-modal__dialog-title" id="shiftImportLavel">
{% trans "Import Rotating Shift" %}
</h2>
<button class="oh-modal__close" aria-label="Close">
<ion-icon name="close-outline"></ion-icon>
</button>
<div
class="oh-modal__dialog-body p-0 pt-2"
id="shiftImportModalBody"
>
<form
action="#"
id="shiftImportForm"
enctype="multipart/form-data"
>
<div
class="oh-modal__dialog-body mr-5"
id="uploading"
style="display: none"
>
<div class="loader-container">
<div class="loader"></div>
<div class="loader-text">
{% trans "Uploading..." %}
</div>
</div>
</div>
<div id="error-container" style="color: red"></div>
<div class="modal-body">
{% csrf_token %}
<div class="oh-dropdown__import-form">
<label
class="oh-dropdown__import-label"
for="shiftImportFile"
>
<ion-icon
name="cloud-upload"
class="oh-dropdown__import-form-icon md hydrated"
role="img"
aria-label="cloud upload"
></ion-icon>
<span class="oh-dropdown__import-form-title"
>{% trans "Upload a File" %}</span
>
<span class="oh-dropdown__import-form-text"
>{% trans "Drag and drop files here" %}
</span
>
</label>
<input
type="file"
name="file"
id="shiftImportFile"
/>
<div class="d-inline float-end">
<a
href="#"
style="
text-decoration: none;
display: inline-block;
"
class="oh-dropdown__link"
onclick="template_download(event)"
data-toggle="oh-modal-toggle"
data-target="#shiftImport"
>
<ion-icon
name="cloud-download-outline"
style="
font-size: 20px;
vertical-align: middle;
"
></ion-icon>
<span>{% trans "Download Template" %}</span>
</a>
</div>
</div>
</div>
<div class="modal-footer d-flex flex-row-reverse">
<input
type="submit"
class="oh-btn oh-btn--small oh-btn--secondary w-100 mt-3"
value="{% trans 'Upload' %}"
/>
</div>
</form>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$('#id_field').on('change',function(){
$('.filterButton')[0].click();
})
});
$(document).ready(function () {
$("#id_field").on("change", function () {
$(".filterButton")[0].click();
});
var form = $("#shiftImportForm");
form.on("submit", function () {
event.preventDefault();
$(".oh-dropdown__import-form").css("display", "none");
$("#uploading").css("display", "block");
var formData = new FormData();
var fileInput = document.querySelector("#shiftImportFile");
formData.append("file", fileInput.files[0]);
$.ajax({
type: "POST",
url: "{% url 'rotating-shift-assign-info-import' %}",
dataType: "binary",
data: formData,
processData: false,
contentType: false,
headers: {
"X-CSRFToken": getCookie("csrftoken"),
},
xhrFields: {
responseType: "blob",
},
success: function (response, textStatus, xhr) {
var errorCount = xhr.getResponseHeader("X-Error-Count");
if (
typeof response === "object" &&
response.type == "application/json"
) {
var reader = new FileReader();
reader.onload = function () {
var json = JSON.parse(reader.result);
if (json.success_count > 0) {
Swal.fire({
text: `${json.success_count} Employees Imported Successfully`,
icon: "success",
showConfirmButton: false,
timer: 3000,
timerProgressBar: true,
}).then(function () {
window.location.reload();
});
}
};
reader.readAsText(response);
return;
}
if (!$(".file-xlsx-validation").length) {
swal.fire({
text: `You have ${errorCount} errors. Do you want to download the error list?`,
icon: "error",
showCancelButton: true,
showDenyButton: true,
confirmButtonText:
"Download error list & Skip Import",
denyButtonText:
"Downlod error list & Continue Import",
cancelButtonText: "Cancel",
confirmButtonColor: "#d33",
denyButtonColor: "#008000",
customClass: {
container: "custom-swal-container",
},
}).then((result) => {
if (result.isConfirmed) {
const file = new Blob([response], { type: "text/csv" });
const url = URL.createObjectURL(file);
const link = document.createElement("a");
link.href = url;
link.download = "ImportError.csv";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
link.click();
window.location.reload();
} else if (result.isDenied) {
formData.append("create_rotating_shift", true);
$.ajax({
type: "POST",
url: "{% url 'rotating-shift-assign-info-import' %}",
dataType: "binary",
data: formData,
processData: false,
contentType: false,
headers: {
"X-CSRFToken": getCookie("csrftoken"),
},
xhrFields: {
responseType: "blob",
},
success: function (
response,
textStatus,
xhr
) {
Swal.fire({
text: `Rotating Shifts Imported Successfully`,
icon: "success",
showConfirmButton: false,
timer: 3000,
timerProgressBar: true,
}).then(function () {
const file = new Blob([response], { type: "text/csv" });
const url = URL.createObjectURL(file);
const link = document.createElement("a");
link.href = url;
link.download = "ImportError.csv";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
link.click();
window.location.reload();
});
return;
},
});
} else {
$(".oh-dropdown__import-form").css(
"display",
"block"
);
$("#uploading").css("display", "none");
}
});
}
},
error: function (xhr, textStatus, errorThrown) {
console.error("Error downloading file:", errorThrown);
},
});
});
});
function template_download(e) {
e.preventDefault();
var languageCode = null;
getCurrentLanguageCode(function (code) {
languageCode = code;
var confirmMessage = downloadMessages[languageCode];
Swal.fire({
text: confirmMessage,
icon: "question",
showCancelButton: true,
confirmButtonColor: "#008000",
cancelButtonColor: "#d33",
confirmButtonText: "Confirm",
}).then(function (result) {
if (result.isConfirmed) {
$.ajax({
url: "{% static 'import_templates/shift_schedule_template.csv' %}",
method: "GET",
xhrFields: {
responseType: "blob",
},
success: function (data) {
var blob = new Blob([data], { type: "text/csv" });
var link = $("<a>")
.attr("href", window.URL.createObjectURL(blob))
.attr("download", "shift_schedule_template.csv")
.appendTo("body");
link[0].click();
link.remove();
},
error: function (xhr, status, error) {
console.error("Failed to download file:", error);
},
});
}
});
});
}
</script>