From b2c9cb1923e80dc537deeec602ee1dcfe7332f16 Mon Sep 17 00:00:00 2001 From: Horilla Date: Tue, 6 Feb 2024 18:44:14 +0530 Subject: [PATCH] [UPDT] EMPLOYEE: Leading zeroes on employee badge --- employee/forms.py | 60 ++++++++++++++++++++++++------------- employee/methods/methods.py | 6 ++++ 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/employee/forms.py b/employee/forms.py index 17c9d6e04..78c6eda17 100644 --- a/employee/forms.py +++ b/employee/forms.py @@ -28,6 +28,7 @@ from django.contrib.auth.models import User from django.forms import DateInput, ModelChoiceField, TextInput from django.utils.translation import gettext as _ from django.utils.translation import gettext_lazy as trans +from horilla.decorators import logger from employee.models import ( Actiontype, BonusPoint, @@ -158,26 +159,43 @@ class EmployeeForm(ModelForm): prefix = get_intial_prefix(None)["get_intial_prefix"] data = get_ordered_badge_ids() result = [] - for sublist in data: - for item in sublist: - if isinstance(item, str) and item.lower().startswith(prefix.lower()): - # Find the index of the item in the sublist - index = sublist.index(item) - # Check if there is a next item in the sublist - if index + 1 < len(sublist): - result = sublist[index + 1] - result = re.findall(r"[a-zA-Z]+|\d+|[^a-zA-Z\d\s]", result) + try: + for sublist in data: + for item in sublist: + if isinstance(item, str) and item.lower().startswith(prefix.lower()): + # Find the index of the item in the sublist + index = sublist.index(item) + # Check if there is a next item in the sublist + if index + 1 < len(sublist): + result = sublist[index + 1] + result = re.findall(r"[a-zA-Z]+|\d+|[^a-zA-Z\d\s]", result) - if result: - prefix = [] - incremented = False - for item in reversed(result): - if not incremented and isinstance(eval(str(item)), int): - item = eval(item) + 1 - incremented = True - prefix.insert(0, str(item)) - prefix = "".join(prefix) + if result: + prefix = [] + incremented = False + for item in reversed(result): + total_letters = len(item) + total_zero_leads = 0 + for letter in item: + if letter == "0": + total_zero_leads = total_zero_leads + 1 + continue + break + if total_zero_leads: + item = item[total_zero_leads:] + if isinstance(item,list): + item = item[-1] + if not incremented and isinstance(eval(str(item)), int): + item = int(item) + 1 + incremented = True + if isinstance(item,int): + item = "{:0{}d}".format(item,total_letters) + prefix.insert(0, str(item)) + prefix = "".join(prefix) + except Exception as e: + logger.exception(e) + prefix = get_intial_prefix(None)["get_intial_prefix"] return prefix def clean_badge_id(self): @@ -430,9 +448,9 @@ class BulkUpdateFieldForm(forms.Form): ] self.fields["update_fields"].choices = updated_choices for visible in self.visible_fields(): - visible.field.widget.attrs[ - "class" - ] = "oh-select oh-select-2 select2-hidden-accessible oh-input w-100" + visible.field.widget.attrs["class"] = ( + "oh-select oh-select-2 select2-hidden-accessible oh-input w-100" + ) class EmployeeNoteForm(ModelForm): diff --git a/employee/methods/methods.py b/employee/methods/methods.py index 9cbcc6165..86d68bba5 100644 --- a/employee/methods/methods.py +++ b/employee/methods/methods.py @@ -1,6 +1,8 @@ """ employee/methods.py """ + +import re from employee.models import Employee from itertools import groupby from base.context_processors import get_intial_prefix @@ -24,6 +26,10 @@ def get_ordered_badge_ids(): .order_by("badge_id") .values_list("badge_id", flat=True) ) + if not data.first(): + data = [ + f'{get_intial_prefix(None)["get_intial_prefix"]}0001', + ] # Separate pure number strings and convert them to integers pure_numbers = [int(item) for item in data if item.isdigit()]