Skip to content

Commit

Permalink
[UPDT] EMPLOYEE: Optimize email duplication check by excluding curren…
Browse files Browse the repository at this point in the history
…t instance during validation
  • Loading branch information
horilla-opensource committed Mar 6, 2025
1 parent d7b1701 commit 88a4cdd
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions employee/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,28 @@ def as_p(self, *args, **kwargs):
def clean(self):
super().clean()
email = self.cleaned_data["email"]
all_employees = Employee.objects.entire()
exit_employee = all_employees.filter(email=email).first()
query = Employee.objects.entire().filter(email=email)
if self.instance and self.instance.id:
query = query.exclude(id=self.instance.id)

existing_employee = query.first()

if existing_employee:
company_id = None
if (
hasattr(existing_employee, "employee_work_info")
and existing_employee.employee_work_info
):
company_id = existing_employee.employee_work_info.company_id

if exit_employee:
company_id = getattr(
getattr(exit_employee, "employee_work_info", None), "company_id", None
)
if company_id:
error_message = _(
"Employee with this Email already exists in company {}"
).format(company_id)
"An Employee with this Email already exists in company {}".format(
company_id
)
)
else:
error_message = _(f"Employee with this Email already exists")
error_message = _("An Employee with this Email already exists")

raise forms.ValidationError({"email": error_message})

Expand Down

0 comments on commit 88a4cdd

Please sign in to comment.