[FIX] HORILLA API: Employee api permission fix
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import re
|
||||
|
||||
from django.http import QueryDict
|
||||
from responses import logger
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
|
||||
from base.models import *
|
||||
@@ -10,26 +13,47 @@ def get_next_badge_id():
|
||||
"""
|
||||
This method is used to generate badge id
|
||||
"""
|
||||
try:
|
||||
highest_badge_id = (
|
||||
Employee.objects.filter(badge_id__isnull=False)
|
||||
.order_by("-badge_id")
|
||||
.first()
|
||||
.badge_id
|
||||
)
|
||||
except AttributeError:
|
||||
highest_badge_id = None
|
||||
from base.context_processors import get_initial_prefix
|
||||
from employee.methods.methods import get_ordered_badge_ids
|
||||
|
||||
# Increment the badge_id if it exists, otherwise start from '1'
|
||||
if highest_badge_id:
|
||||
if "#" in highest_badge_id:
|
||||
prefix, number = highest_badge_id.split("#") # Split prefix and number
|
||||
# Increment the number
|
||||
new_number = str(int(number) + 1).zfill(len(number))
|
||||
new_badge_id = f"{prefix}#{new_number}"
|
||||
else:
|
||||
# Add number to existing prefix
|
||||
new_badge_id = f"{highest_badge_id}#001"
|
||||
else:
|
||||
new_badge_id = "EMP#001" # Default start badge ID if no employees exist
|
||||
return new_badge_id
|
||||
prefix = get_initial_prefix(None)["get_initial_prefix"]
|
||||
data = get_ordered_badge_ids()
|
||||
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):
|
||||
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_initial_prefix(None)["get_initial_prefix"]
|
||||
return prefix
|
||||
|
||||
Reference in New Issue
Block a user