diff --git a/employee/forms.py b/employee/forms.py index 4b0f7dae6..bad1fa455 100644 --- a/employee/forms.py +++ b/employee/forms.py @@ -154,6 +154,9 @@ class EmployeeForm(ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + self.fields['email'].widget.attrs['autocomplete'] = 'email' + self.fields['phone'].widget.attrs['autocomplete'] = 'phone' + self.fields['address'].widget.attrs['autocomplete'] = 'address' if instance := kwargs.get("instance"): # ---- # django forms not showing value inside the date, time html element. @@ -255,6 +258,7 @@ class EmployeeWorkInformationForm(ModelForm): def __init__(self, *args, disable=False, **kwargs): super().__init__(*args, **kwargs) + self.fields['email'].widget.attrs['autocomplete'] = 'email' for field in self.fields: self.fields[field].widget.attrs["placeholder"] = self.fields[field].label if disable: @@ -349,6 +353,7 @@ class EmployeeBankDetailsForm(ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + self.fields['address'].widget.attrs['autocomplete'] = 'address' for visible in self.visible_fields(): visible.field.widget.attrs["class"] = "oh-input w-100" @@ -606,3 +611,12 @@ class ActiontypeForm(ModelForm): class Meta: model = Actiontype fields = "__all__" + + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields["action_type"].widget.attrs.update( + { + "onchange": "actionChange($(this))", + } + ) diff --git a/employee/models.py b/employee/models.py index af08a0217..14b1b4090 100644 --- a/employee/models.py +++ b/employee/models.py @@ -766,6 +766,8 @@ class Actiontype(models.Model): title = models.CharField(max_length=50) action_type = models.CharField(max_length=30, choices=choice_actions) + block_option = models.BooleanField(default = False, verbose_name=_("Enable login block :"), + help_text="If is enabled, employees log in will be blocked based on period of suspension or dismissal.") def __str__(self) -> str: return f"{self.title}" diff --git a/employee/policies.py b/employee/policies.py index 4419f9151..02757c0e5 100644 --- a/employee/policies.py +++ b/employee/policies.py @@ -26,6 +26,7 @@ from django.contrib.auth.models import User from django.utils.translation import gettext_lazy as _ from notifications.signals import notify from base.methods import ( + filtersubordinates, get_key_instances, ) @@ -165,10 +166,17 @@ def disciplinary_actions(request): """ This method is used to view all Disciplinaryaction """ - form = DisciplinaryActionFilter() - queryset = DisciplinaryAction.objects.all() + employee = Employee.objects.filter(employee_user_id=request.user).first() + dis_actions = filtersubordinates( + request, DisciplinaryAction.objects.all(), "base.add_disciplinaryaction" + ).distinct() + dis_actions = dis_actions | DisciplinaryAction.objects.filter( + employee_id=employee + ).distinct() + + form = DisciplinaryActionFilter(request.GET, queryset=dis_actions) page_number = request.GET.get("page") - page_obj = paginator_qry(queryset, page_number) + page_obj = paginator_qry(form.qs, page_number) previous_data = request.GET.urlencode() return render( @@ -198,27 +206,6 @@ def get_action_type_delete(action_id): return action.action_type -def employee_account_block_unblock(emp_id): - employee = get_object_or_404(Employee, id=emp_id) - if not employee: - # messages.info(request, _("Employee not found")) - return redirect(disciplinary_actions) - user = get_object_or_404(User, id=employee.employee_user_id.id) - if not user: - # messages.info(request, _("Employee not found")) - return redirect(disciplinary_actions) - user.is_active = not user.is_active - action_message = _("blocked") if not user.is_active else _("unblocked") - user.save() - # messages.success( - # request, - # _("{employee}'s account {action_message} successfully!").format( - # employee=employee, action_message=action_message - # ), - # ) - return HttpResponse("") - - def get_action_type(action_id): """ This function is used to get the action type by the selection of title in the form. @@ -235,24 +222,16 @@ def get_action_type_delete(action_id): return action.action_type -def employee_account_block_unblock(emp_id): +def employee_account_block_unblock(emp_id, result): + employee = get_object_or_404(Employee, id=emp_id) if not employee: - # messages.info(request, _("Employee not found")) return redirect(disciplinary_actions) user = get_object_or_404(User, id=employee.employee_user_id.id) if not user: - # messages.info(request, _("Employee not found")) return redirect(disciplinary_actions) - user.is_active = not user.is_active - action_message = _("blocked") if not user.is_active else _("unblocked") + user.is_active = result user.save() - # messages.success( - # request, - # _("{employee}'s account {action_message} successfully!").format( - # employee=employee, action_message=action_message - # ), - # ) return HttpResponse("") @@ -274,34 +253,11 @@ 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"] - 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": - - end_date = start_date + timedelta(days=day) - - 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." - ) - - 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." - ) - employees.append(user) + form.save() messages.success(request, _("Disciplinary action taken.")) notify.send( @@ -335,35 +291,10 @@ def update_actions(request, action_id): form = DisciplinaryActionForm(request.POST, request.FILES, instance=action) if form.is_valid(): - emp = form.cleaned_data["employee_id"] - - 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": - - end_date = start_date + timedelta(days=day) - - 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." - ) - - 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_ids = form.cleaned_data["employee_id"] + for employee in employee_ids: + name = employee.employee_user_id employees.append(name) form.save() @@ -394,16 +325,16 @@ def delete_actions(request, action_id): action_type = get_action_type_delete(dis.action) - for i in dis.employee_id.all(): + for dis_emp in dis.employee_id.all(): if action_type == "dismissal" or action_type == "suspension": - employee = get_object_or_404(Employee, id=i.id) + employee = get_object_or_404(Employee, id=dis_emp.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." + request, "Employees login credentials will be unblocked." ) user.is_active = True user.save() @@ -424,6 +355,14 @@ def action_type_details(request): return JsonResponse({"action_type": action_type}) +@login_required +def action_type_name(request): + """ + This method is used to get the action type name by the selection of type in the form. + """ + action_type = request.POST["action_type"] + return JsonResponse({"action_type": action_type}) + @login_required def disciplinary_filter_view(request): """ diff --git a/employee/scheduler.py b/employee/scheduler.py index eeba186a4..cc4fc8f93 100644 --- a/employee/scheduler.py +++ b/employee/scheduler.py @@ -1,9 +1,7 @@ -# import datetime +import datetime +from datetime import timedelta from apscheduler.schedulers.background import BackgroundScheduler -# from employee.models import DisciplinaryAction -# from employee.policies import employee_account_block_unblock - def update_experience(): from employee.models import EmployeeWorkInformation @@ -19,24 +17,92 @@ def update_experience(): return -# def block_unblock_disciplinary(): -# from employee.models import Employee -# """ -# This scheduled task to trigger the experience calculator -# to update the employee work experience -# """ -# dis_action = DisciplinaryAction.objects.all() -# for i in dis_action: -# if i.action.action_type == 'suspension': -# if datetime.date.today() >= i.start_date or datetime.date.today() >= i.end_date: -# for emp in i.employee_id: -# employee_account_block_unblock(emp_id=emp.id) +def block_unblock_disciplinary(): + """ + This scheduled task to trigger the Disciplinary action and take the suspens + """ + from employee.models import DisciplinaryAction + from base.models import EmployeeShiftSchedule + from employee.policies import employee_account_block_unblock + dis_action = DisciplinaryAction.objects.all() + for dis in dis_action: + if dis.action.block_option: + if dis.action.action_type == 'suspension': + if dis.days: + day = dis.days + end_date = dis.start_date + timedelta(days=day) + if datetime.date.today() >= dis.start_date or datetime.date.today() >= end_date: + if datetime.date.today() >= dis.start_date: + r = False + if datetime.date.today() >= end_date: + r = True - -# return + employees = dis.employee_id.all() + for emp in employees: + employee_account_block_unblock(emp_id=emp.id, result= r) + + if dis.hours: + hour_str = dis.hours + ':00' + if hour_str > '00:00:00': + + # Checking the date of action date. + if datetime.date.today() >= dis.start_date: + + employees = dis.employee_id.all() + for emp in employees: + + # Taking the shift of employee for taking the work start time + shift = emp.employee_work_info.shift_id + shift_detail = EmployeeShiftSchedule.objects.filter(shift_id = shift) + for shi in shift_detail: + today = datetime.datetime.today() + day_of_week = today.weekday() + + # List of weekday names + weekday_names = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'] + if weekday_names[day_of_week] == shi.day.day: + + st_time = shi.start_time + + hour_time = datetime.datetime.strptime(hour_str, "%H:%M:%S").time() + + time1 = st_time + time2 = hour_time + + # Convert them to datetime objects + datetime1 = datetime.datetime.combine(datetime.date.today(), time1) + datetime2 = datetime.datetime.combine(datetime.date.today(), time2) + + # Add the datetime objects + result_datetime = datetime1 + datetime.timedelta(hours=datetime2.hour, minutes=datetime2.minute, seconds=datetime2.second) + + # Extract the time component from the result + result_time = result_datetime.time() + + # Get the current time + current_time = datetime.datetime.now().time() + + # Check if the current time matches st_time + if current_time >= st_time: + r = False + if current_time >= result_time: + r = True + + employee_account_block_unblock(emp_id=emp.id, result= r) + + if dis.action.action_type == "dismissal": + if datetime.date.today() >= dis.start_date: + if datetime.date.today() >= dis.start_date: + r = False + employees = dis.employee_id.all() + for emp in employees: + employee_account_block_unblock(emp_id=emp.id, result= r ) + + return scheduler = BackgroundScheduler() scheduler.add_job(update_experience, "interval", days=1) +scheduler.add_job(block_unblock_disciplinary, "interval", seconds=10) scheduler.start() \ No newline at end of file diff --git a/employee/templates/disciplinary_actions/disciplinary_nav.html b/employee/templates/disciplinary_actions/disciplinary_nav.html index 2199f39c6..14e9b0934 100644 --- a/employee/templates/disciplinary_actions/disciplinary_nav.html +++ b/employee/templates/disciplinary_actions/disciplinary_nav.html @@ -214,6 +214,31 @@ + {% endblock %} diff --git a/employee/templates/disciplinary_actions/disciplinary_records.html b/employee/templates/disciplinary_actions/disciplinary_records.html index 0152c4c7b..523688a7b 100644 --- a/employee/templates/disciplinary_actions/disciplinary_records.html +++ b/employee/templates/disciplinary_actions/disciplinary_records.html @@ -40,6 +40,7 @@ {% trans "Employee" %}
{% trans "Action Taken" %}
+
{% trans "Login Block" %}
{% trans "Action Date" %}
{% trans "Attachments" %}
{% trans "Description" %}
@@ -76,12 +77,11 @@ /> {{emp.employee_first_name|truncatechars:8}} + >{{emp.get_full_name}} - + {% comment %} - + {% endcomment %} {% endfor %} @@ -98,6 +98,10 @@ {% else %}
{{ i.action }}
{% endif %} +
+ {% if i.action.block_option %} {% trans "Yes" %} + {% else %}{% trans "No" %}{% endif %} +
{% if i.action.action_type == 'suspension' and i.unit_in == "days" %}
{{ i.start_date }} diff --git a/employee/urls.py b/employee/urls.py index d6c4cbe7c..1e38efa18 100644 --- a/employee/urls.py +++ b/employee/urls.py @@ -356,6 +356,11 @@ urlpatterns = [ policies.action_type_details, name="action-type-details", ), + path( + "action-type-name", + policies.action_type_name, + name="action-type-name", + ), path( "disciplinary-filter-view", policies.disciplinary_filter_view,