[UPDT] Delete method updation

This commit is contained in:
Horilla
2023-09-20 15:02:36 +05:30
parent 3ee10bf880
commit fb6f85335c
7 changed files with 91 additions and 41 deletions

View File

@@ -119,6 +119,7 @@ class AttendanceUpdateForm(ModelForm):
"requested_data",
"is_validate_request",
"is_validate_request_approved",
"attendance_overtime_approve"
]
model = Attendance
widgets = {

View File

@@ -74,7 +74,7 @@ class AttendanceActivity(models.Model):
employee_id = models.ForeignKey(
Employee,
on_delete=models.CASCADE,
on_delete=models.PROTECT,
related_name="employee_attendance_activities",
verbose_name=_("Employee"),
)
@@ -108,7 +108,7 @@ class Attendance(models.Model):
employee_id = models.ForeignKey(
Employee,
on_delete=models.CASCADE,
on_delete=models.PROTECT,
null=True,
related_name="employee_attendances",
verbose_name=_("Employee"),
@@ -372,7 +372,7 @@ class AttendanceOverTime(models.Model):
employee_id = models.ForeignKey(
Employee,
on_delete=models.CASCADE,
on_delete=models.PROTECT,
related_name="employee_overtime",
verbose_name=_("Employee"),
)
@@ -437,7 +437,7 @@ class AttendanceLateComeEarlyOut(models.Model):
]
attendance_id = models.ForeignKey(
Attendance, on_delete=models.CASCADE, related_name="late_come_early_out"
Attendance, on_delete=models.PROTECT, related_name="late_come_early_out"
)
employee_id = models.ForeignKey(
Employee,

View File

@@ -113,10 +113,10 @@
<a hx-get="{% url 'attendance-update' attendance.id %}" hx-target='#updateAttendanceModalBody' hx-swap='innerHTML' data-toggle='oh-modal-toggle' data-target='#updateAttendanceModal' class="oh-btn oh-btn--light-bkg w-100" title="{% trans 'Edit' %}"><ion-icon name="create-outline"></ion-icon></a>
{% endif %}
{% if perms.attendance.delete_attendance %}
<form hx-post="{% url 'attendance-delete' attendance.id %}?{{pd}}&opage={{overtime_attendances.number}}&vpage={{validate_attendances.number}}&page={{attendances.number}}" hx-confirm="Are you sure want to delete this attendance?" hx-target="#tab_contents" method='post'>
{% csrf_token %}
<button type='submit' class="oh-btn oh-btn--danger-outline oh-btn--light-bkg w-100" title="{% trans 'Remove' %}"><ion-icon name="trash-outline"></ion-icon></button>
</form>
{% comment %} <form hx-post="{% url 'attendance-delete' attendance.id %}?{{pd}}&opage={{overtime_attendances.number}}&vpage={{validate_attendances.number}}&page={{attendances.number}}" hx-confirm="Are you sure want to delete this attendance?" hx-target="#tab_contents" method='post'>
{% csrf_token %} {% endcomment %}
<button data-id={{attendance.id}} class="oh-btn oh-btn--danger-outline oh-btn--light-bkg w-100 deletebutton" title="{% trans 'Remove' %}"><ion-icon name="trash-outline"></ion-icon></button>
{% comment %} </form> {% endcomment %}
{% endif %}
</div>
</div>
@@ -276,10 +276,10 @@
<a hx-get="{% url 'attendance-update' attendance.id %}" hx-target='#updateAttendanceModalBody' hx-swap='innerHTML' data-toggle='oh-modal-toggle' data-target='#updateAttendanceModal' class="oh-btn oh-btn--light-bkg w-100" title="{% trans 'Edit' %}"><ion-icon name="create-outline"></ion-icon></a>
{% endif %}
{% if perms.attendance.delete_attendance %}
<form hx-post="{% url 'attendance-delete' attendance.id %}?{{pd}}&opage={{overtime_attendances.number}}&vpage={{validate_attendances.number}}&page={{attendances.number}}" hx-confirm="Are you sure want to delete this attendance?" hx-target="#tab_contents" method='post'>
{% csrf_token %}
<button type='submit' class="oh-btn oh-btn--danger-outline oh-btn--light-bkg w-100" title="{% trans 'Remove' %}"><ion-icon name="trash-outline"></ion-icon></button>
</form>
{% comment %} <form hx-post="{% url 'attendance-delete' attendance.id %}?{{pd}}&opage={{overtime_attendances.number}}&vpage={{validate_attendances.number}}&page={{attendances.number}}" hx-confirm="Are you sure want to delete this attendance?" hx-target="#tab_contents" method='post'>
{% csrf_token %} {% endcomment %}
<button data-id={{attendance.id}} class="oh-btn oh-btn--danger-outline oh-btn--light-bkg w-100 deletebutton" title="{% trans 'Remove' %}"><ion-icon name="trash-outline"></ion-icon></button>
{% comment %} </form> {% endcomment %}
{% endif %}
</div>
</div>
@@ -439,10 +439,10 @@
<a hx-get="{% url 'attendance-update' attendance.id %}" hx-target='#updateAttendanceModalBody' hx-swap='innerHTML' data-toggle='oh-modal-toggle' data-target='#updateAttendanceModal' class="oh-btn oh-btn--light-bkg w-100" title="{% trans 'Edit' %}"><ion-icon name="create-outline"></ion-icon></a>
{% endif %}
{% if perms.attendance.delete_attendance %}
<form hx-post="{% url 'attendance-delete' attendance.id %}?{{pd}}&opage={{overtime_attendances.number}}&vpage={{validate_attendances.number}}&page={{attendances.number}}" hx-confirm="Are you sure want to delete this attendance?" hx-target="#tab_contents" method='post'>
{% csrf_token %}
<button type='submit' class="oh-btn oh-btn--danger-outline oh-btn--light-bkg w-100" title="{% trans 'Remove' %}"><ion-icon name="trash-outline"></ion-icon></button>
</form>
{% comment %} <form hx-post="{% url 'attendance-delete' attendance.id %}?{{pd}}&opage={{overtime_attendances.number}}&vpage={{validate_attendances.number}}&page={{attendances.number}}" hx-confirm="Are you sure want to delete this attendance?" hx-target="#tab_contents" method='post'>
{% csrf_token %} {% endcomment %}
<button data-id={{attendance.id}} class="oh-btn oh-btn--danger-outline oh-btn--light-bkg w-100 deletebutton" title="{% trans 'Remove' %}"><ion-icon name="trash-outline"></ion-icon></button>
{% comment %} </form> {% endcomment %}
{% endif %}
</div>
</div>
@@ -540,6 +540,28 @@ aria-hidden="true"
}
});
$(document).ready(function () {
$('.deletebutton').click(function(){
var id = $(this).attr('data-id');
var url = `/attendance/attendance-delete/${id}/`;
// Create a form element
var form = $('<form></form>');
form.attr('method', 'POST');
form.attr('action', url);
// Create a hidden input field for the CSRF token
var csrf_token = $('input[name="csrfmiddlewaretoken"]').val();
var csrf_input = $('<input type="hidden" name="csrfmiddlewaretoken">');
csrf_input.val(csrf_token);
form.append(csrf_input);
// Append the form to the body and submit it
$(document.body).append(form);
form.submit();
});
var activeTab = localStorage.getItem('activeTabAttendance')
if (activeTab != null) {
var tab = $(`[data-target="${activeTab}"]`)

View File

@@ -92,10 +92,10 @@
<div class="oh-sticky-table__td">
<div class="oh-btn-group">
{% if perms.attendance.change_attendance or request.user|is_reportingmanager %}
<a hx-get="{% url 'attendance-update' attendance.id %}" hx-target='#updateAttendanceModalBody' hx-swap='innerHTML' data-toggle='oh-modal-toggle' data-target='#updateAttendanceModal' class="oh-btn oh-btn--light-bkg w-100" title="{% trans 'Edit' %}"><ion-icon name="create-outline"></ion-icon></a>
<a hx-get="{% url 'attendance-update' attendance.id %}" hx-target='#updateAttendanceModalBody' hx-swap='innerHTML' data-toggle='oh-modal-toggle' data-target='#updateAttendanceModal' class="oh-btn oh-btn--light-bkg w-50" title="{% trans 'Edit' %}"><ion-icon name="create-outline"></ion-icon></a>
{% endif %}
{% if perms.attendance.delete_attendance %}
<form action="{% url 'attendance-delete' attendance.id %}" onsubmit="return confirm('{% trans "Are you sure want to delete this attendance?" %}')" hx-target="#tab_contents" method='post'>
<form action="{% url 'attendance-delete' attendance.id %}" onsubmit="return confirm('{% trans "Are you sure want to delete this attendance?" %}')" hx-target="#tab_contents" method='post' class='w-50'>
{% csrf_token %}
<button type='submit' class="oh-btn oh-btn--danger-outline oh-btn--light-bkg w-100" title="{% trans 'Remove' %}"><ion-icon name="trash-outline"></ion-icon></button>
</form>
@@ -243,10 +243,10 @@
<div class="oh-sticky-table__td">
<div class="oh-btn-group">
{% if perms.attendance.change_attendance or request.user|is_reportingmanager %}
<a hx-get="{% url 'attendance-update' attendance.id %}" hx-target='#updateAttendanceModalBody' hx-swap='innerHTML' data-toggle='oh-modal-toggle' data-target='#updateAttendanceModal' class="oh-btn oh-btn--light-bkg w-100" title="{% trans 'Edit' %}"><ion-icon name="create-outline"></ion-icon></a>
<a hx-get="{% url 'attendance-update' attendance.id %}" hx-target='#updateAttendanceModalBody' hx-swap='innerHTML' data-toggle='oh-modal-toggle' data-target='#updateAttendanceModal' class="oh-btn oh-btn--light-bkg w-50" title="{% trans 'Edit' %}"><ion-icon name="create-outline"></ion-icon></a>
{% endif %}
{% if perms.attendance.delete_attendance %}
<form action="{% url 'attendance-delete' attendance.id %}" onsubmit="return confirm('{% trans "Are you sure want to delete this attendance?" %}')" hx-target="#tab_contents" method='post'>
<form action="{% url 'attendance-delete' attendance.id %}" onsubmit="return confirm('{% trans "Are you sure want to delete this attendance?" %}')" hx-target="#tab_contents" method='post' class='w-50'>
{% csrf_token %}
<button type='submit' class="oh-btn oh-btn--danger-outline oh-btn--light-bkg w-100" title="{% trans 'Remove' %}"><ion-icon name="trash-outline"></ion-icon></button>
</form>
@@ -404,10 +404,10 @@
<div class="oh-sticky-table__td">
<div class="oh-btn-group">
{% if perms.attendance.change_attendance or request.user|is_reportingmanager %}
<a hx-get="{% url 'attendance-update' attendance.id %}" hx-target='#updateAttendanceModalBody' hx-swap='innerHTML' data-toggle='oh-modal-toggle' data-target='#updateAttendanceModal' class="oh-btn oh-btn--light-bkg w-100" title="{% trans 'Edit' %}"><ion-icon name="create-outline"></ion-icon></a>
<a hx-get="{% url 'attendance-update' attendance.id %}" hx-target='#updateAttendanceModalBody' hx-swap='innerHTML' data-toggle='oh-modal-toggle' data-target='#updateAttendanceModal' class="oh-btn oh-btn--light-bkg w-50" title="{% trans 'Edit' %}"><ion-icon name="create-outline"></ion-icon></a>
{% endif %}
{% if perms.attendance.delete_attendance %}
<form action="{% url 'attendance-delete' attendance.id %}" onsubmit="return confirm('{% trans "Are you sure want to delete this attendance?" %}')" hx-target="#tab_contents" method='post'>
<form action="{% url 'attendance-delete' attendance.id %}" onsubmit="return confirm('{% trans "Are you sure want to delete this attendance?" %}')" hx-target="#tab_contents" method='post' class='w-50'>
{% csrf_token %}
<button type='submit' class="oh-btn oh-btn--danger-outline oh-btn--light-bkg w-100" title="{% trans 'Remove' %}"><ion-icon name="trash-outline"></ion-icon></button>
</form>

View File

@@ -56,24 +56,23 @@
>
</div>
</div>
<div class="oh-sticky-table__td">{{ot.month|capfirst}}</div>
<div class="oh-sticky-table__td">{% with month=ot.month|capfirst %}{% trans month %}{% endwith %}</div>
<div class="oh-sticky-table__td">{{ot.year}}</div>
<div class="oh-sticky-table__td">{{ot.hour_account}}</div>
<div class="oh-sticky-table__td">{{ot.overtime}}</div>
<div class="oh-sticky-table__td">
<div class="oh-btn-group">
{% if perms.recruitment.change_attendanceovertime or request.user|is_reportingmanager %}
<a hx-get="{% url 'attendance-overtime-update' ot.id %}" hx-target='#updateAttendanceOTModalBody' data-toggle='oh-modal-toggle' data-target='#updateOtModal' class="oh-btn oh-btn--light-bkg w-100" title="{% trans 'Edit' %}"><ion-icon name="create-outline"></ion-icon></a>
<a hx-get="{% url 'attendance-overtime-update' ot.id %}" hx-target='#updateAttendanceOTModalBody' data-toggle='oh-modal-toggle' data-target='#updateOtModal' class="oh-btn oh-btn--light-bkg w-50" title="{% trans 'Edit' %}"><ion-icon name="create-outline"></ion-icon></a>
{% endif %}
{% if perms.recruitment.delete_attendance %}
<form hx-post="{% url 'attendance-overtime-delete' ot.id %}?page={{accounts.number}}" hx-confirm="Are you sure want to delete this attendance?" hx-target="#ot-table" method='post'>
{% csrf_token %}
<button type='submit' class="oh-btn oh-btn--danger-outline oh-btn--light-bkg w-100" title="{% trans 'Remove' %}"><ion-icon name="trash-outline"></ion-icon></button>
</form>
{% comment %} <form action="{% url 'attendance-overtime-delete' ot.id %}?page={{accounts.number}}" method='post' class="w-50">
{% csrf_token %} {% endcomment %}
<button data-id={{ot.id}} class="oh-btn oh-btn--danger-outline oh-btn--light-bkg w-50 deletebutton" title="{% trans 'Remove' %}"><ion-icon name="trash-outline"></ion-icon></button>
{% comment %} </form> {% endcomment %}
{% endif %}
</div>
</div>
</div>
{% endfor %}
</div>
@@ -151,7 +150,29 @@ aria-hidden="true"
<script>
$(document).load('htmx:afterswap', function () {
$(document).ready( function () {
$('.deletebutton').click(function(){
var id = $(this).attr('data-id');
var url = `/attendance/attendance-overtime-delete/${id}/`;
// Create a form element
var form = $('<form></form>');
form.attr('method', 'POST');
form.attr('action', url);
// Create a hidden input field for the CSRF token
var csrf_token = $('input[name="csrfmiddlewaretoken"]').val();
var csrf_input = $('<input type="hidden" name="csrfmiddlewaretoken">');
csrf_input.val(csrf_token);
form.append(csrf_input);
// Append the form to the body and submit it
$(document.body).append(form);
form.submit();
});
$('.oh-table__sticky-collaspable-sort').click(function (e) {
e.preventDefault();

View File

@@ -37,10 +37,10 @@
<div class="oh-sticky-table__td">
<div class="oh-btn-group">
{% if perms.recruitment.change_attendanceovertime or request.user|is_reportingmanager %}
<a hx-get="{% url 'attendance-overtime-update' ot.id %}" hx-target='#updateAttendanceOTModalBody' data-toggle='oh-modal-toggle' data-target='#updateOtModal' class="oh-btn oh-btn--light-bkg w-100" title="{% trans 'Edit' %}"><ion-icon name="create-outline"></ion-icon></a>
<a hx-get="{% url 'attendance-overtime-update' ot.id %}" hx-target='#updateAttendanceOTModalBody' data-toggle='oh-modal-toggle' data-target='#updateOtModal' class="oh-btn oh-btn--light-bkg w-50" title="{% trans 'Edit' %}"><ion-icon name="create-outline"></ion-icon></a>
{% endif %}
{% if perms.recruitment.delete_attendance %}
<form action="{% url 'attendance-overtime-delete' ot.id %}" onsubmit="return confirm('{% trans "Are you sure want to delete this attendance?" %}')" hx-target="#ot-table" method='post'>
<form action="{% url 'attendance-overtime-delete' ot.id %}" onsubmit="return confirm('{% trans "Are you sure want to delete this attendance?" %}')" hx-target="#ot-table" method='post' class='w-50'>
{% csrf_token %}
<button type='submit' class="oh-btn oh-btn--danger-outline oh-btn--light-bkg w-100" title="{% trans 'Remove' %}"><ion-icon name="trash-outline"></ion-icon></button>
</form>
@@ -93,7 +93,6 @@
</ul>
</nav>
</div>
{% if perms.attendance.change_attendanceovertime or request.user|is_reportingmanager %}

View File

@@ -17,6 +17,7 @@ import json
from datetime import datetime
from django.shortcuts import render, redirect
from django.utils.translation import gettext_lazy as _
from django.utils.translation import gettext as __
from django.contrib import messages
from django.core.paginator import Paginator
from django.http import HttpResponseRedirect, HttpResponse, JsonResponse
@@ -269,8 +270,14 @@ def attendance_delete(request, obj_id):
try:
attendance.delete()
messages.success(request, _("Attendance deleted."))
except ProtectedError:
messages.error(request, _("You cannot delete this attendance"))
except ProtectedError as e:
model_verbose_names_set = set()
for obj in e.protected_objects:
model_verbose_names_set.add(__(obj._meta.verbose_name.capitalize()))
model_names_str = ", ".join(model_verbose_names_set)
messages.error(
request, _("An attendance entry for {} already exists.".format(model_names_str))
)
except Attendance.DoesNotExist:
messages.error(request, _("Attendance Does not exists.."))
return HttpResponseRedirect(request.META.get("HTTP_REFERER", "/"))
@@ -308,13 +315,13 @@ def attendance_bulk_delete(request):
attendance.delete()
messages.success(request, _("Attendance Deleted"))
except ProtectedError:
except ProtectedError as e:
model_verbose_names_set = set()
for obj in e.protected_objects:
model_verbose_names_set.add(__(obj._meta.verbose_name.capitalize()))
model_names_str = ", ".join(model_verbose_names_set)
messages.error(
request,
_(
"You cannot delete this %(attendance)s"
% {"attendance": attendance}
),
request, _("An attendance entry for {} already exists.".format(model_names_str))
)
except Attendance.DoesNotExist:
messages.error(request, _("Attendance not found."))