From 684b6a2971a49d2da80395a08eb2cfaa4250310c Mon Sep 17 00:00:00 2001 From: Horilla Date: Tue, 3 Dec 2024 17:21:00 +0530 Subject: [PATCH] [UPDT] ATTENDANCE: Updated get_diff_dict function for improved performance and readability by reducing redundant operations --- attendance/methods/utils.py | 94 ++-- .../attendance/dashboard/dashboard.html | 453 +++++++----------- 2 files changed, 237 insertions(+), 310 deletions(-) diff --git a/attendance/methods/utils.py b/attendance/methods/utils.py index 2ce81fe86..82dbd3356 100644 --- a/attendance/methods/utils.py +++ b/attendance/methods/utils.py @@ -98,53 +98,63 @@ def get_diff_dict(first_dict, other_dict, model=None): Args: first_dict: The first dictionary to compare. other_dict: The second dictionary to compare. - model: The model class + model: The model class (optional, for verbose names and type-specific formatting) Returns: A dictionary of differing keys with their old and new values. """ - # model is passed as argument if any need of verbose name instead of field name difference = {} - if model is None: - for key in first_dict: - if first_dict[key] != other_dict[key]: - # get the verbose name of the field - difference[key] = (first_dict[key], other_dict[key]) - return difference - for key in first_dict: - if first_dict[key] != other_dict[key]: - # get the verbose name of the field - field = model._meta.get_field(key) - verb_key = field.verbose_name - value = first_dict[key] - other_value = other_dict[key] - if isinstance(field, models.DateField): - if value is not None and value != "None": - value = datetime.strptime(value, "%Y-%m-%d").strftime("%d %b %Y") - if other_value is not None and other_value != "None": - other_value = datetime.strptime(other_value, "%Y-%m-%d").strftime( - "%d %b %Y" - ) - elif isinstance(field, models.TimeField): - if value is not None and value != "None": - if len(value.split(":")) == 2: - value = value + ":00" - value = datetime.strptime(value, "%H:%M:%S").strftime("%I:%M %p") - if other_value is not None and value != "None": - if len(other_value.split(":")) == 2: - other_value = other_value + ":00" - if other_value != "None": - other_value = datetime.strptime( - other_value, "%H:%M:%S" - ).strftime("%I:%M %p") - else: - other_value = "None" - elif isinstance(field, models.ForeignKey): - if value is not None and len(str(value)): - value = field.related_model.objects.get(id=value) - if other_value is not None and len(str(other_value)): - other_value = field.related_model.objects.get(id=other_value) - difference[verb_key] = (value, other_value) + + for key, value in first_dict.items(): + other_value = other_dict.get(key) + if value == other_value: + continue # Skip if values are the same + + if not model: + difference[key] = (value, other_value) + continue + + # Fetch the model field metadata + field = model._meta.get_field(key) + verbose_key = field.verbose_name + + # Handle specific field types + if isinstance(field, models.DateField): + value = ( + datetime.strptime(value, "%Y-%m-%d").strftime("%d %b %Y") + if value and value != "None" + else value + ) + other_value = ( + datetime.strptime(other_value, "%Y-%m-%d").strftime("%d %b %Y") + if other_value and other_value != "None" + else other_value + ) + elif isinstance(field, models.TimeField): + + def format_time(val): + if val and val != "None": + val += ":00" if len(val.split(":")) == 2 else "" + return datetime.strptime(val, "%H:%M:%S").strftime("%I:%M %p") + return val + + value = format_time(value) + other_value = format_time(other_value) + elif isinstance(field, models.ForeignKey): + value = ( + field.related_model.objects.get(id=value) + if value and str(value).isdigit() + else value + ) + other_value = ( + field.related_model.objects.get(id=other_value) + if other_value and str(other_value).isdigit() + else other_value + ) + + # Add the difference + difference[verbose_key] = (value, other_value) + return difference diff --git a/attendance/templates/attendance/dashboard/dashboard.html b/attendance/templates/attendance/dashboard/dashboard.html index 8112d8579..a34370e99 100644 --- a/attendance/templates/attendance/dashboard/dashboard.html +++ b/attendance/templates/attendance/dashboard/dashboard.html @@ -5,7 +5,7 @@ position: sticky; right: 0; background-color: #fff; - } + }
@@ -14,89 +14,191 @@
- {% trans "Today's Attendances" %} + {% trans "Today's Attendances" %}
- {{marked_attendances_ratio}}% + {{marked_attendances_ratio}}%
- {% comment %} {{marked_attendances}}/{{expected_attendances}} {% endcomment %} + {% comment %} + {{marked_attendances}}/{{expected_attendances}} + {% endcomment %}
-
+
- {% trans "On Time" %} + {% trans "On Time" %}
- {{on_time}} + {{on_time}}
- {% comment %} {{on_time_ratio}}% {% endcomment %} + {% comment %} + {{on_time_ratio}}% + {% endcomment %}
- {% if late_come_early_out_tracking %} -
-
-
- {% trans "Late Come" %} -
-
-
- {{late_come}} + {% if late_come_early_out_tracking %} +
+
+
+ {% trans "Late Come" %} +
+
+
+ {{late_come}}
- {% endif %} + {% endif %} +
-
-
-
-
-
-
-
- {% trans "Attendance Analytic" %} -
-
- + "> + + + + + + + + +
+
+ +
+
+
+
+
+
+
+ {% trans 'Offline Employees' %} + +
+
+
+
+
+
+ +
+
+
+
+
+ {% trans "Hours Chart" %} +
+
+ +
+
+
+ +
+
+
+
+
+
+ +
+
+
+
+ {% trans "On Break" %} +
+ +
+ {% if on_break %} +
    + {% for emp in on_break %} +
  • +
    +
    + Amy Winehouse +
    + {{emp.employee_id}} +
    +
  • + {% endfor %} +
+ {% else %} +
+
+ +

{% trans "No employees are currently taking a break." %}

+
+
+ {% endif %} +
+
+
+
+
+
+ {% trans "Overtime To Validate" %} +
+
+ {% include "attendance/dashboard/overtime_table.html" %} +
+
+
+
+
+ +
+ {% include "attendance/dashboard/to_validate_table.html" %} +
+
+
+
+
+ {% trans "Department Overtime Chart" %} +
+
+ - - + +
-
- +
+
-
- {% include "dashboard/not_in_yet.html" %} -
- -
-
-
-
-
- {% trans "Hours Chart" %} -
-
- -
-
-
- -
-
-
-
-
-
- -
-
-
-
- {% trans "On Break" %} -
- -
- {% if on_break %} -
    - {% for emp in on_break %} -
  • -
    -
    - Amy Winehouse -
    - {{emp.employee_id}} -
    -
  • - {% endfor %} -
- {% else %} -
-
- -

{% trans "No employees on break...." %}

-
-
- {% endif %} -
-
-
-
-
-
- {% trans "Overtime To Validate" %} -
-
- {% include "attendance/dashboard/overtime_table.html" %} -
-
-
-
-
- -
- {% include "attendance/dashboard/to_validate_table.html" %}
-
-
-
-
- {% trans "Department Overtime Chart" %} -
-
- - - - -
-
- -
-
-
-
-
+
-
- - + + - + -{% endblock content %} + {% endblock content %}