diff --git a/pms/cbvs.py b/pms/cbvs.py index bc086515c..3f604dca6 100644 --- a/pms/cbvs.py +++ b/pms/cbvs.py @@ -1,18 +1,18 @@ - - from typing import Any + +from django.contrib import messages from django.http import HttpResponse from django.shortcuts import render -from horilla_views.generic.cbv import views -from pms import models from django.urls import reverse_lazy from django.utils.translation import gettext_lazy as _trans -from django.contrib import messages -from pms.filters import BonusPointSettingFilter,EmployeeBonusPointFilter -from pms.forms import BonusPointSettingForm,EmployeeBonusPointForm +from horilla_views.generic.cbv import views +from pms import models +from pms.filters import BonusPointSettingFilter, EmployeeBonusPointFilter +from pms.forms import BonusPointSettingForm, EmployeeBonusPointForm -#================Models for BonusPointSetting============== + +# ================Models for BonusPointSetting============== class BonusPointSettingSectionView(views.HorillaSectionView): """ BonusPointSetting SectionView @@ -29,7 +29,6 @@ class BonusPointSettingSectionView(views.HorillaSectionView): template_name = "bonus/bonus_point_setting_section.html" - class BonusPointSettingNavView(views.HorillaNavView): """ BonusPointSetting nav view @@ -57,26 +56,27 @@ class BonusPointSettingFormView(views.HorillaFormView): form_class = BonusPointSettingForm model = models.BonusPointSetting new_display_title = _trans("Create Bonus Point Setting") - # template_name = "bonus/bonus_form.html" + template_name = "bonus/bonus_form.html" def get_form_kwargs(self): kwargs = super().get_form_kwargs() instance = models.BonusPointSetting.objects.filter(pk=self.kwargs["pk"]).first() kwargs["instance"] = instance return kwargs - + def get_context_data(self, **kwargs): - context= super().get_context_data(**kwargs) + context = super().get_context_data(**kwargs) return context - + def form_invalid(self, form: Any) -> HttpResponse: - + if not form.is_valid(): errors = form.errors.as_data() return render( self.request, self.template_name, {"form": form, "errors": errors} ) return super().form_invalid(form) + def form_valid(self, form: BonusPointSettingForm) -> views.HttpResponse: if form.is_valid(): message = "Bonus Point Setting added" @@ -86,7 +86,7 @@ class BonusPointSettingFormView(views.HorillaFormView): messages.success(self.request, _trans(message)) return self.HttpResponse() - + return super().form_valid(form) @@ -94,6 +94,7 @@ class BonusPointSettingListView(views.HorillaListView): """ BnusPointSetting list view """ + model = models.BonusPointSetting search_url = reverse_lazy("bonus-point-setting-list-view") filter_class = BonusPointSettingFilter @@ -125,14 +126,16 @@ class BonusPointSettingListView(views.HorillaListView): columns = [ ("Model", "get_model_display"), + ("Applicable For", "get_applicable_for_display"), ("Bonus For", "get_bonus_for_display"), ("Condition", "get_condition"), - ("Points", 'points'), - ("Is Active",'is_active'), + ("Points", "points"), + ("Is Active", "is_active_toggle"), ] -#================Models for EmployeeBonusPoint============== +# ================Models for EmployeeBonusPoint============== + class EmployeeBonusPointSectionView(views.HorillaSectionView): """ @@ -150,12 +153,13 @@ class EmployeeBonusPointSectionView(views.HorillaSectionView): template_name = "bonus/employee_bonus_point_section.html" - class EmployeeBonusPointNavView(views.HorillaNavView): """ BonusPoint nav view """ + template_name = "bonus/bonus_point_nav.html" + def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) self.create_attrs = f""" @@ -168,6 +172,20 @@ class EmployeeBonusPointNavView(views.HorillaNavView): nav_title = _trans("Employee Bonus Point ") search_url = reverse_lazy("employee-bonus-point-list-view") search_swap_target = "#listContainer" + group_by_fields = [ + ("employee_id", _trans("Employee")), + ( + "employee_id__employee_work_info__reporting_manager_id", + _trans("Reporting Manager"), + ), + ("employee_id__employee_work_info__department_id", _trans("Department")), + ("employee_id__employee_work_info__job_position_id", _trans("Job Position")), + ( + "employee_id__employee_work_info__employee_type_id", + _trans("Employement Type"), + ), + ("employee_id__employee_work_info__company_id", _trans("Company")), + ] class EmployeeBonusPointFormView(views.HorillaFormView): @@ -179,29 +197,29 @@ class EmployeeBonusPointFormView(views.HorillaFormView): model = models.EmployeeBonusPoint new_display_title = _trans("Create Employee Bonus Point ") # template_name = "bonus/bonus_form.html" - + def get_context_data(self, **kwargs): - context= super().get_context_data(**kwargs) + context = super().get_context_data(**kwargs) return context - + def form_invalid(self, form: Any) -> HttpResponse: - + if not form.is_valid(): errors = form.errors.as_data() return render( self.request, self.template_name, {"form": form, "errors": errors} ) return super().form_invalid(form) + def form_valid(self, form: EmployeeBonusPointForm) -> views.HttpResponse: if form.is_valid(): message = "Bonus Point added" if form.instance.pk: message = "Bonus Point updated" form.save() - messages.success(self.request, _trans(message)) return self.HttpResponse() - + return super().form_valid(form) @@ -209,9 +227,16 @@ class EmployeeBonusPointListView(views.HorillaListView): """ BnusPoint list view """ + model = models.EmployeeBonusPoint search_url = reverse_lazy("employee-bonus-point-list-view") filter_class = EmployeeBonusPointFilter + action_method = "action_template" + bulk_update_fields = [ + "employee_id", + "bonus_point", + "based_on", + ] # actions = [ # { # "action": "Edit", @@ -240,6 +265,5 @@ class EmployeeBonusPointListView(views.HorillaListView): columns = [ ("Employee", "employee_id"), ("Bonus Point", "bonus_point"), - ("Based On",'based_on'), - + ("Based On", "based_on"), ] diff --git a/pms/forms.py b/pms/forms.py index 7b569d53a..7cf34c288 100644 --- a/pms/forms.py +++ b/pms/forms.py @@ -1079,6 +1079,14 @@ class BonusPointSettingForm(MF): BonusPointSetting form """ + model = forms.ChoiceField( + choices=BonusPointSetting.MODEL_CHOICES, + widget=forms.Select( + attrs={ + "onchange": "ModelChange($(this))", + } + ), + ) # condition_html = forms.CharField(widget=forms.HiddenInput()) # condition_querystring = forms.CharField(widget=forms.HiddenInput()) @@ -1103,7 +1111,6 @@ class BonusPointSettingForm(MF): # attrs = self.fields["mail_to"].widget.attrs # attrs["class"] = "oh-select oh-select-2 w-100" # attrs = self.fields["model"].widget.attrs - # attrs["onchange"] = "getToMail($(this))" # self.fields["mail_template"].empty_label = None # attrs = attrs.copy() @@ -1127,6 +1134,34 @@ class BonusPointSettingForm(MF): # context = {"form": self} # table_html = render_to_string("horilla_form.html", context) # return table_html + + def clean(self): + cleaned_data = super().clean() + model = cleaned_data.get("model") + + if model in ["pms.models.EmployeeObjective", "pms.models.EmployeeKeyResult"]: + if not cleaned_data.get("applicable_for") == "owner": + raise ValidationError( + _( + f"Model Doesn't have this {cleaned_data.get('applicable_for')} field" + ) + ) + if not cleaned_data["bonus_for"] == "Closed": + raise ValidationError( + _(f"This 'Bonus for' is not in the Model's status") + ) + if model in ["project.models.Task", "project.models.Project"]: + if cleaned_data.get("applicable_for") == "owner": + raise ValidationError( + _( + f"Model Doesn't have this {cleaned_data.get('applicable_for')} field" + ) + ) + if cleaned_data["points"] <= 0: + raise ValidationError(_("Bonus point must be greater than zero")) + + return cleaned_data + # def save(self, commit: bool = ...) -> Any: # self.instance: MailAutomation = self.instance # condition_querystring = self.cleaned_data["condition_querystring"] diff --git a/pms/models.py b/pms/models.py index 8c0b47340..954919d9a 100644 --- a/pms/models.py +++ b/pms/models.py @@ -742,24 +742,35 @@ class EmployeeBonusPoint(models.Model): verbose_name="Employee", ) bonus_point = models.IntegerField(default=0) + instance = models.CharField(max_length=150, null=True, blank=True) based_on = models.CharField(max_length=150) def __str__(self): return f"{self.employee_id.employee_first_name} - {self.bonus_point}" + def action_template(self): + """ + This method for get custom column for managers. + """ + return render_template( + path="bonus/bonus_point_action.html", + context={"instance": self}, + ) + class BonusPointSetting(models.Model): MODEL_CHOICES = [ - ("pms.models.EmployeeObjective", "Objective"), - ("pms.models.EmployeeKeyResult", "Key Result"), + ("pms.models.EmployeeObjective", _("Objective")), + ("pms.models.EmployeeKeyResult", _("Key Result")), ] if apps.is_installed("project"): MODEL_CHOICES += [ - ("project.models.Task", "Task"), - ("project.models.Project", "Project"), + ("project.models.Task", _("Task")), + ("project.models.Project", _("Project")), ] BONUS_FOR = [ - ("completed", "Completing"), + ("completed", _("Completing")), + ("Closed", _("Closing")), ] CONDITIONS = [ ("=", "="), @@ -769,12 +780,20 @@ class BonusPointSetting(models.Model): (">=", ">="), ] FIELD_1 = [ - ("complition_date", "Completion Date"), + ("complition_date", _("Completion Date")), ] FIELD_2 = [ - ("end_date", "End Date"), + ("end_date", _("End Date")), + ] + APPLECABLE_FOR = [ + ("owner", _("Owner")), + ("members", _("Members")), + ("managers", _("Managers")), ] model = models.CharField(max_length=100, choices=MODEL_CHOICES, null=False) + applicable_for = models.CharField( + max_length=50, choices=APPLECABLE_FOR, null=True, blank=True + ) bonus_for = models.CharField(max_length=25, choices=BONUS_FOR) field_1 = models.CharField(max_length=25, choices=FIELD_1, null=True, blank=True) conditions = models.CharField( @@ -808,6 +827,12 @@ class BonusPointSetting(models.Model): """ return dict(BonusPointSetting.FIELD_2).get(self.field_2) + def get_applicable_for_display(self): + """ + Display applicable_for + """ + return dict(BonusPointSetting.APPLECABLE_FOR).get(self.applicable_for) + def get_condition(self): """ Get the condition for bonus @@ -824,7 +849,16 @@ class BonusPointSetting(models.Model): context={"instance": self}, ) - def create_employee_bonus(self, employee, field_1, field_2): + def is_active_toggle(self): + """ + For toggle is_active field + """ + return render_template( + path="bonus/is_active_toggle.html", + context={"instance": self}, + ) + + def create_employee_bonus(self, employee, field_1, field_2, instance): """ For creating employee bonus """ @@ -836,55 +870,23 @@ class BonusPointSetting(models.Model): "<=": operator.le, ">=": operator.ge, } - if operator_mapping[self.conditions](field_1, field_2): + if ( + operator_mapping[self.conditions](field_1, field_2) + ) and not EmployeeBonusPoint.objects.filter( + employee_id=employee, + instance=instance, + based_on=(f"{self.get_bonus_for_display()} {instance}"), + ).exists(): EmployeeBonusPoint( employee_id=employee, - based_on=(f"{self.get_bonus_for_display} {self.model}"), + based_on=(f"{self.get_bonus_for_display()} {instance}"), bonus_point=self.points, + instance=instance, ).save() def save(self, *args, **kwargs): super().save(*args, **kwargs) - model_class = get_model_class(self.model) - - def create_signal_handler(name, bonus_point_setting): - def signal_handler(sender, instance, created, **kwargs): - """ - Signal handler for post-save events of the model instances. - """ - # request = getattr(_thread_locals, "request", None) - # previous_record = getattr(_thread_locals, "previous_record", None) - # previous_instance = None - # if previous_record: - # previous_instance = previous_record["instance"] - - # if BonusPointSetting.objects.filter(model='Task').exists(): - # bonus_point_settings = BonusPointSetting.objects.filter(model='Task') - # for bs in bonus_point_settings: - - field_1 = date.today() - field_2 = instance.end_date - if bonus_point_setting.bonus_for == instance.status: - - for employee in instance.task_members.all(): - bonus_point_setting.create_employee_bonus( - employee, field_1, field_2 - ) - - signal_handler.__name__ = name - signal_handler.model_class = model_class - signal_handler.bonus_point_setting = bonus_point_setting - return signal_handler - - # Create and connect the signal handler - handler_name = f"{self.id}_signal_handler" - dynamic_signal_handler = create_signal_handler(handler_name, self) - # SIGNAL_HANDLERS.append(dynamic_signal_handler) - post_save.connect( - dynamic_signal_handler, sender=dynamic_signal_handler.model_class - ) - def manipulate_existing_data(): from dateutil.relativedelta import relativedelta diff --git a/pms/signals.py b/pms/signals.py index 7078f185e..3878ebdd5 100644 --- a/pms/signals.py +++ b/pms/signals.py @@ -1,28 +1,33 @@ """ pms/signals.py """ + import copy import logging import threading import types -from django.db.models.signals import post_delete, post_save, pre_save -from django.dispatch import receiver -from horilla.horilla_middlewares import _thread_locals -from pms.models import BonusPointSetting from datetime import date +from django.db.models.signals import m2m_changed, post_delete, post_save, pre_save +from django.dispatch import receiver +from employee.methods.methods import check_relationship_with_employee_model +from horilla.horilla_middlewares import _thread_locals +from horilla.signals import pre_bulk_update +from pms.models import BonusPointSetting + +logger = logging.getLogger(__name__) SIGNAL_HANDLERS = [] INSTANCE_HANDLERS = [] + def start_automation(): """ Automation signals """ from horilla_automations.methods.methods import get_model_class, split_query_string - @receiver(post_delete, sender=BonusPointSetting) @receiver(post_save, sender=BonusPointSetting) @@ -41,53 +46,6 @@ def start_automation(): post_save.disconnect(handler, sender=handler.model_class) SIGNAL_HANDLERS.clear() - def create_post_bulk_update_handler(automation, model_class, query_strings): - def post_bulk_update_handler(sender, queryset, *args, **kwargs): - def _bulk_update_thread_handler( - queryset, previous_queryset_copy, automation - ): - request = getattr(queryset, "request", None) - - if request: - for index, instance in enumerate(queryset): - previous_instance = previous_queryset_copy[index] - send_automated_mail( - request, - False, - automation, - query_strings, - instance, - previous_instance, - ) - previous_bulk_record = getattr(_thread_locals, "previous_bulk_record", None) - previous_queryset = None - if previous_bulk_record: - previous_queryset = previous_bulk_record["queryset"] - previous_queryset_copy = previous_bulk_record["queryset_copy"] - - bulk_thread = threading.Thread( - target=_bulk_update_thread_handler, - args=(queryset, previous_queryset_copy, automation), - ) - bulk_thread.start() - - func_name = f"{automation.method_title}_post_bulk_signal_handler" - - # Dynamically create a function with a unique name - handler = types.FunctionType( - post_bulk_update_handler.__code__, - globals(), - name=func_name, - argdefs=post_bulk_update_handler.__defaults__, - closure=post_bulk_update_handler.__closure__, - ) - - # Set additional attributes on the function - handler.model_class = model_class - handler.automation = automation - - return handler - def start_connection(): """ Method to start signal connection accordingly to the automation @@ -95,90 +53,104 @@ def start_automation(): clear_connection() bonus_point_settings = BonusPointSetting.objects.filter(is_active=True) for bonus_point_setting in bonus_point_settings: - - # condition_querystring = bonus_point_setting.condition_querystring.replace( - # "automation_multiple_", "" - # ) - - # query_strings = split_query_string(condition_querystring) - # model_path should me in the form of pms.models.Objective model_path = bonus_point_setting.model model_class = get_model_class(model_path) + related_fields = check_relationship_with_employee_model(model_class) + field = None + type = None + for field_name, relation_type in related_fields: + if ( + bonus_point_setting.applicable_for == "members" + and relation_type == "ManyToManyField" + ): + field = field_name + type = relation_type + break + elif ( + bonus_point_setting.applicable_for == "managers" + or bonus_point_setting.applicable_for == "owner" + and relation_type == "ForeignKey" + ): + field = field_name + type = relation_type + break - # handler = create_post_bulk_update_handler( - # bonus_point_setting, model_class, query_strings - # ) - # SIGNAL_HANDLERS.append(handler) - # post_bulk_update.connect(handler, sender=model_class) - - def create_signal_handler(name, bonus_point_setting): - def signal_handler(sender, instance, created, **kwargs): + def create_signal_handler(name, bonus_point_setting, type=None, field=None): + def signal_handler(sender, instance, *args, **kwargs): """ Signal handler for post-save events of the model instances. """ - # request = getattr(_thread_locals, "request", None) - # previous_record = getattr(_thread_locals, "previous_record", None) - # previous_instance = None - # if previous_record: - # previous_instance = previous_record["instance"] - - # if BonusPointSetting.objects.filter(model='Task').exists(): - # bonus_point_settings = BonusPointSetting.objects.filter(model='Task') - # for bs in bonus_point_settings: + if type == "ManyToManyField": - field_1 = date.today() - field_2 = instance.end_date - - if bonus_point_setting.bonus_for == instance.status : - - for employee in instance.task_members.all(): - bonus_point_setting.create_employee_bonus(employee,field_1,field_2) + @receiver(m2m_changed, sender=model_class.members.through) + def members_changed(sender, instance, action, **kwargs): + """ + Handle m2m_changed signal for the members field in YourModel. + """ + if ( + action == "post_add" + or action == "post_remove" + or action == "post_clear" + or action == "post_save" + ): + # These actions occur after members are added, removed, or cleared + field_1 = date.today() + field_2 = instance.end_date + if bonus_point_setting.bonus_for == instance.status: + if field and type: + field_value = getattr(instance, field) + if type == "ManyToManyField": + # Now this should give the updated members + employees = field_value.all() + else: + employees = field_value + for employee in employees: + bonus_point_setting.create_employee_bonus( + employee, field_1, field_2, instance + ) + else: + logger("No type and field") + else: + logger("Not post add.") + + else: + field_1 = date.today() + field_2 = instance.end_date + if bonus_point_setting.bonus_for == instance.status: + if field and type: + field_value = getattr(instance, field) + if type == "ManyToManyField": + # Now this should give the updated members + employees = field_value.all() + for employee in employees: + bonus_point_setting.create_employee_bonus( + employee, field_1, field_2, instance + ) + else: + employee = field_value + bonus_point_setting.create_employee_bonus( + employee, field_1, field_2, instance + ) + else: + logger("No type and field") signal_handler.__name__ = name signal_handler.model_class = model_class + signal_handler.type = type + signal_handler.field = field signal_handler.bonus_point_setting = bonus_point_setting return signal_handler # Create and connect the signal handler handler_name = f"{bonus_point_setting.id}_signal_handler" dynamic_signal_handler = create_signal_handler( - handler_name, bonus_point_setting + handler_name, bonus_point_setting, type=type, field=field ) SIGNAL_HANDLERS.append(dynamic_signal_handler) post_save.connect( dynamic_signal_handler, sender=dynamic_signal_handler.model_class ) - def create_pre_bulk_update_handler(automation, model_class): - def pre_bulk_update_handler(sender, queryset, *args, **kwargs): - request = getattr(_thread_locals, "request", None) - if request: - queryset_copy = queryset.none() - if queryset.count(): - queryset_copy = QuerySet.from_list(copy.deepcopy(list(queryset))) - _thread_locals.previous_bulk_record = { - "automation": automation, - "queryset": queryset, - "queryset_copy": queryset_copy, - } - - func_name = f"{automation.method_title}_pre_bulk_signal_handler" - - # Dynamically create a function with a unique name - handler = types.FunctionType( - pre_bulk_update_handler.__code__, - globals(), - name=func_name, - argdefs=pre_bulk_update_handler.__defaults__, - closure=pre_bulk_update_handler.__closure__, - ) - - # Set additional attributes on the function - handler.model_class = model_class - handler.automation = automation - - return handler - def track_previous_instance(): """ method to add signal to track the automations model previous instances @@ -188,9 +160,7 @@ def start_automation(): """ Method to clear instance handler signals """ - for handler in INSTANCE_HANDLERS: - pre_save.disconnect(handler, sender=handler.model_class) pre_bulk_update.disconnect(handler, sender=handler.model_class) INSTANCE_HANDLERS.clear() @@ -200,10 +170,6 @@ def start_automation(): for bonus_setting in bonus_point_settings: model_class = get_model_class(bonus_setting.model) - # handler = create_pre_bulk_update_handler(bonus_setting, model_class) - # INSTANCE_HANDLERS.append(handler) - # pre_bulk_update.connect(handler, sender=model_class) - @receiver(pre_save, sender=model_class) def instance_handler(sender, instance, **kwargs): """ @@ -219,9 +185,7 @@ def start_automation(): "bonus_setting": bonus_setting, "instance": instance, } - instance_handler.__name__ = ( - f"{bonus_setting.id}_instance_handler" - ) + instance_handler.__name__ = f"{bonus_setting.id}_instance_handler" return instance_handler instance_handler.model_class = model_class diff --git a/pms/templates/bonus/bonus_form.html b/pms/templates/bonus/bonus_form.html index 001661f81..836fba1c5 100644 --- a/pms/templates/bonus/bonus_form.html +++ b/pms/templates/bonus/bonus_form.html @@ -24,21 +24,4 @@ {{form.as_p}} - - {% endcomment %} +{% endcomment %} diff --git a/pms/templates/bonus/bonus_point_action.html b/pms/templates/bonus/bonus_point_action.html new file mode 100644 index 000000000..cae8dc2b8 --- /dev/null +++ b/pms/templates/bonus/bonus_point_action.html @@ -0,0 +1,27 @@ +{% load i18n %} +
+ {% if perms.pms.change_employeebonuspoint %} + + {% endif %} + {% if perms.pms.delete_employeebonuspoint %} + + + + {% endif %} +
\ No newline at end of file diff --git a/pms/templates/bonus/bonus_point_nav.html b/pms/templates/bonus/bonus_point_nav.html new file mode 100644 index 000000000..9e04d5a74 --- /dev/null +++ b/pms/templates/bonus/bonus_point_nav.html @@ -0,0 +1,8 @@ +
+ {% include "generic/horilla_nav.html" %} +
+ \ No newline at end of file diff --git a/pms/templates/bonus/bonus_point_setting_section.html b/pms/templates/bonus/bonus_point_setting_section.html index 4c51fc9e6..c2562030e 100644 --- a/pms/templates/bonus/bonus_point_setting_section.html +++ b/pms/templates/bonus/bonus_point_setting_section.html @@ -1,5 +1,5 @@ -{% extends 'settings.html' %} -{% block settings %} +{% extends 'settings.html' %} +{% block settings %} {% load i18n %} {% load static %} @@ -22,28 +22,47 @@ > -
-
-
-
+ {% endblock settings %} diff --git a/pms/templates/bonus/bonus_seetting_action.html b/pms/templates/bonus/bonus_seetting_action.html index 7305ab332..1829f2004 100644 --- a/pms/templates/bonus/bonus_seetting_action.html +++ b/pms/templates/bonus/bonus_seetting_action.html @@ -1,6 +1,6 @@ {% load i18n %}
- {% if perms.leave.change_holiday %} + {% if perms.pms.change_bonuspointsetting %} {% endif %} - {% if perms.leave.delete_holiday %} + {% if perms.pms.delete_bonuspointsetting %} {% endif %} -
\ No newline at end of file + diff --git a/pms/templates/bonus/employee_bonus_point_section.html b/pms/templates/bonus/employee_bonus_point_section.html index 0843dc590..67419507b 100644 --- a/pms/templates/bonus/employee_bonus_point_section.html +++ b/pms/templates/bonus/employee_bonus_point_section.html @@ -1,5 +1,5 @@ -{% extends 'index.html' %} -{% block content %} +{% extends 'index.html' %} +{% block content %} {% load i18n %} {% load static %} @@ -22,24 +22,24 @@ > -
-
-
-
diff --git a/pms/templates/bonus/is_active_toggle.html b/pms/templates/bonus/is_active_toggle.html new file mode 100644 index 000000000..54816c1cb --- /dev/null +++ b/pms/templates/bonus/is_active_toggle.html @@ -0,0 +1,14 @@ + +
+ {% if perms.pms.change_bonuspointsetting %} + + {% else %} + + {% endif %} +
+ diff --git a/pms/urls.py b/pms/urls.py index 2d7f7c189..1b873a9d1 100644 --- a/pms/urls.py +++ b/pms/urls.py @@ -452,6 +452,11 @@ urlpatterns = [ cbvs.BonusPointSettingListView.as_view(), name="bonus-point-setting-list-view", ), + path( + "bonus-setting-form-values", + views.bonus_setting_form_values, + name="bonus-setting-form-values", + ), # ===========Employee bonus point============ path( "employee-bonus-point", @@ -473,4 +478,14 @@ urlpatterns = [ cbvs.EmployeeBonusPointListView.as_view(), name="employee-bonus-point-list-view", ), + path( + "update-employee-bonus-point//", + cbvs.EmployeeBonusPointFormView.as_view(), + name="update-employee-bonus-point", + ), + path( + "delete-employee-bonus-point//", + views.delete_employee_bonus_point, + name="delete-employee-bonus-point", + ), ] diff --git a/pms/views.py b/pms/views.py index 32ff9fd9d..48ca37639 100644 --- a/pms/views.py +++ b/pms/views.py @@ -10,6 +10,7 @@ import json from itertools import tee from urllib.parse import parse_qs +from django import forms from django.contrib import messages from django.contrib.auth.models import User from django.core.paginator import Paginator @@ -33,6 +34,8 @@ from horilla.decorators import ( permission_required, ) from horilla.group_by import group_by_queryset +from horilla_automations.methods.methods import generate_choices +from horilla_automations.methods.serialize import serialize_form from notifications.signals import notify from pms.filters import ( ActualKeyResultFilter, @@ -69,6 +72,7 @@ from pms.models import ( Answer, BonusPointSetting, Comment, + EmployeeBonusPoint, EmployeeKeyResult, EmployeeObjective, Feedback, @@ -3582,7 +3586,7 @@ def dashboard_feedback_answer(request): @permission_required("pms.delete_bonuspointsetting") def delete_bonus_point_setting(request, pk): """ - Automation delete view + Delete bonus point setting """ try: BonusPointSetting.objects.get(id=pk).delete() @@ -3591,3 +3595,48 @@ def delete_bonus_point_setting(request, pk): print(e) messages.error(request, "Something went wrong") return redirect(reverse("bonus-point-setting-list-view")) + + +@login_required +@permission_required("pms.delete_employeebonuspoint") +def delete_employee_bonus_point(request, pk): + """ + Automation delete view + """ + try: + bonus = EmployeeBonusPoint.objects.get(id=pk) + bonus.delete() + messages.success(request, _(f"{bonus} deleted")) + except Exception as e: + print(e) + messages.error(request, _("Something went wrong")) + return redirect(reverse("employee-bonus-point-list-view")) + + +@login_required +def bonus_setting_form_values(request): + model = request.GET["model"] + """ + This method is to render `mail to` fields + """ + model_path = request.GET["model"] + to_fields, mail_details_choice, model_class = generate_choices(model_path) + + class InstantModelForm(forms.ModelForm): + """ + InstantModelForm + """ + + class Meta: + model = model_class + fields = "__all__" + + serialized_form = serialize_form(InstantModelForm(), "automation_multiple_") + + return JsonResponse( + { + "choices": to_fields, + "mail_details_choice": mail_details_choice, + "serialized_form": serialized_form, + } + )