[UPDT] BASE: Implement history field settings feature
This commit is contained in:
@@ -24,10 +24,11 @@ class HistoryForm(forms.Form):
|
||||
required=False,
|
||||
label=_("Updation description"),
|
||||
)
|
||||
history_highlight = forms.BooleanField(required=False,label=_("Updation highlight"))
|
||||
history_highlight = forms.BooleanField(
|
||||
required=False, label=_("Updation highlight")
|
||||
)
|
||||
history_tags = forms.ModelMultipleChoiceField(
|
||||
queryset=AuditTag.objects.all(), required=False,
|
||||
label = _("Updation tag")
|
||||
queryset=AuditTag.objects.all(), required=False, label=_("Updation tag")
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs) -> None:
|
||||
@@ -50,3 +51,33 @@ class HistoryForm(forms.Form):
|
||||
context = {"form": self}
|
||||
table_html = render_to_string("horilla_audit/horilla_audit_log.html", context)
|
||||
return table_html
|
||||
|
||||
|
||||
class HistoryTrackingFieldsForm(forms.Form):
|
||||
excluded_fields = [
|
||||
"id",
|
||||
"employee_id",
|
||||
"objects",
|
||||
"mobile",
|
||||
"contract_end_date",
|
||||
"additional_info",
|
||||
"experience",
|
||||
]
|
||||
def __init__(self, *args, **kwargs):
|
||||
from employee.models import EmployeeWorkInformation as model
|
||||
super(HistoryTrackingFieldsForm, self).__init__(*args, **kwargs)
|
||||
field_choices = [
|
||||
(field.name, field.verbose_name)
|
||||
for field in model._meta.get_fields()
|
||||
if hasattr(field, "verbose_name") and field.name not in self.excluded_fields
|
||||
]
|
||||
self.fields["tracking_fields"] = forms.MultipleChoiceField(
|
||||
choices=field_choices,
|
||||
required=False,
|
||||
widget=forms.SelectMultiple(
|
||||
attrs={
|
||||
"class": "oh-select oh-select-2 select2-hidden-accessible",
|
||||
"style": "height:270px;",
|
||||
}
|
||||
),
|
||||
)
|
||||
@@ -6,7 +6,6 @@ This module is used to write methods related to the history
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
|
||||
class Bot:
|
||||
def __init__(self) -> None:
|
||||
self.__str__()
|
||||
@@ -54,6 +53,21 @@ def get_field_label(model_class, field_name):
|
||||
return None
|
||||
|
||||
|
||||
def filter_history(histories,track_fields):
|
||||
filtered_histories = []
|
||||
for history in histories:
|
||||
changes = history.get("changes", [])
|
||||
filtered_changes = [
|
||||
change
|
||||
for change in changes
|
||||
if change.get("field_name", "") in track_fields
|
||||
]
|
||||
if filtered_changes:
|
||||
history["changes"] = filtered_changes
|
||||
filtered_histories.append(history)
|
||||
histories = filtered_histories
|
||||
return histories
|
||||
|
||||
def get_diff(instance):
|
||||
"""
|
||||
This method is used to find the differences in the history
|
||||
@@ -117,4 +131,10 @@ def get_diff(instance):
|
||||
"updated_by": updated_by,
|
||||
}
|
||||
)
|
||||
from .models import HistoryTrackingFields
|
||||
history_tracking_instance = HistoryTrackingFields.objects.first()
|
||||
if history_tracking_instance:
|
||||
track_fields = history_tracking_instance.tracking_fields["tracking_fields"]
|
||||
if track_fields:
|
||||
delta_changes = filter_history(delta_changes,track_fields)
|
||||
return delta_changes
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
models.py
|
||||
"""
|
||||
|
||||
from collections.abc import Iterable
|
||||
from django.db import models
|
||||
from django.dispatch import receiver
|
||||
@@ -119,6 +120,10 @@ def post_create_horilla_audit_log(sender, instance, *_args, **kwargs):
|
||||
pass
|
||||
|
||||
|
||||
class HistoryTrackingFields(models.Model):
|
||||
tracking_fields = models.JSONField(null=True, blank=True, editable=False)
|
||||
|
||||
|
||||
# class HistoryComment(models.Model):
|
||||
# """
|
||||
# HistoryComment model
|
||||
|
||||
Reference in New Issue
Block a user