[UPDT] EMPLOYEE: Error handling in policies deletion methods

This commit is contained in:
Horilla
2024-02-29 18:36:48 +05:30
parent 276cf4c3f8
commit 26fef5d0e7

View File

@@ -14,7 +14,13 @@ from base.views import paginator_qry
from urllib.parse import parse_qs
from employee.filters import DisciplinaryActionFilter, PolicyFilter
from employee.forms import DisciplinaryActionForm, PolicyForm
from employee.models import Actiontype, DisciplinaryAction, Employee, Policy, PolicyMultipleFile
from employee.models import (
Actiontype,
DisciplinaryAction,
Employee,
Policy,
PolicyMultipleFile,
)
from horilla.decorators import permission_required, login_required
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _
@@ -99,9 +105,15 @@ def delete_policies(request):
"""
This method is to delete policy
"""
ids = request.GET.getlist("ids")
Policy.objects.filter(id__in=ids).delete()
messages.success(request, "Policies deleted")
try:
ids = request.GET.getlist("ids")
count, dict = Policy.objects.filter(id__in=ids).delete()
if count == 0:
messages.error(request, _("Policies Not Found"))
else:
messages.success(request, "Policies deleted")
except ValueError:
messages.error(request, _("Policies Not Found"))
return redirect(view_policies)
@@ -174,9 +186,10 @@ def get_action_type(action_id):
"""
This function is used to get the action type by the selection of title in the form.
"""
action = Actiontype.objects.get(title=action_id['action'])
action = Actiontype.objects.get(title=action_id["action"])
return action.action_type
def get_action_type_delete(action_id):
"""
This function is used to get the action type by the selection of title in the form.
@@ -210,9 +223,10 @@ def get_action_type(action_id):
"""
This function is used to get the action type by the selection of title in the form.
"""
action = Actiontype.objects.get(title=action_id['action'])
action = Actiontype.objects.get(title=action_id["action"])
return action.action_type
def get_action_type_delete(action_id):
"""
This function is used to get the action type by the selection of title in the form.
@@ -255,26 +269,32 @@ def create_actions(request):
form = DisciplinaryActionForm(request.POST, request.FILES)
if form.is_valid():
employee_ids = form.cleaned_data["employee_id"]
start_date = form.cleaned_data['start_date']
day = form.cleaned_data['days']
start_date = form.cleaned_data["start_date"]
day = form.cleaned_data["days"]
action_id = form.cleaned_data
action_type = get_action_type(action_id)
for employee in employee_ids:
user = employee.employee_user_id
if action_type == 'suspension':
if action_type == "suspension":
end_date = start_date + timedelta(days=day)
if datetime.date.today() >= start_date or datetime.date.today() >= end_date:
if (
datetime.date.today() >= start_date
or datetime.date.today() >= end_date
):
employee_account_block_unblock(emp_id=employee.id)
messages.warning(request, "Employees login credentials will be bloked.")
messages.warning(
request, "Employees login credentials will be bloked."
)
if action_type == 'dismissal':
if action_type == "dismissal":
if datetime.date.today() >= start_date:
employee_account_block_unblock(emp_id = employee.id)
messages.warning(request, "Employees login credentials will be bloked.")
employee_account_block_unblock(emp_id=employee.id)
messages.warning(
request, "Employees login credentials will be bloked."
)
employees.append(user)
form.save()
@@ -302,7 +322,7 @@ def update_actions(request, action_id):
"""
action = DisciplinaryAction.objects.get(id=action_id)
form = DisciplinaryActionForm(instance = action)
form = DisciplinaryActionForm(instance=action)
employees = []
if request.method == "POST":
form = DisciplinaryActionForm(request.POST, request.FILES, instance=action)
@@ -310,26 +330,32 @@ def update_actions(request, action_id):
if form.is_valid():
emp = form.cleaned_data["employee_id"]
start_date = form.cleaned_data['start_date']
day = form.cleaned_data['days']
start_date = form.cleaned_data["start_date"]
day = form.cleaned_data["days"]
action_id = form.cleaned_data
action_type = get_action_type(action_id)
for i in emp:
name = i.employee_user_id
if action_type == 'suspension':
if action_type == "suspension":
end_date = start_date + timedelta(days=day)
if datetime.date.today() >= start_date or datetime.date.today() >= end_date:
if (
datetime.date.today() >= start_date
or datetime.date.today() >= end_date
):
employee_account_block_unblock(emp_id=i.id)
messages.warning(request, "Employees login credentials will be bloked.")
messages.warning(
request, "Employees login credentials will be bloked."
)
if action_type == 'dismissal':
if action_type == "dismissal":
if datetime.date.today() >= start_date:
employee_account_block_unblock(emp_id = i.id)
messages.warning(request, "Employees login credentials will be bloked.")
employee_account_block_unblock(emp_id=i.id)
messages.warning(
request, "Employees login credentials will be bloked."
)
employees.append(name)
@@ -357,22 +383,24 @@ def delete_actions(request, action_id):
This method is used to delete Disciplinary action
"""
dis = DisciplinaryAction.objects.get(id = action_id)
dis = DisciplinaryAction.objects.get(id=action_id)
action_type = get_action_type_delete(dis.action)
for i in dis.employee_id.all():
if action_type == 'dismissal' or action_type == 'suspension':
if action_type == "dismissal" or action_type == "suspension":
employee = get_object_or_404(Employee, id=i.id)
user = get_object_or_404(User, id=employee.employee_user_id.id)
if user.is_active:
pass
else:
messages.warning(request, "Employees login credentials will be unbloked.")
messages.warning(
request, "Employees login credentials will be unbloked."
)
user.is_active = True
user.save()
dis.delete()
messages.success(request, _("Disciplinary action deleted."))
return redirect(disciplinary_filter_view)