agency unique email contraint fail solved
This commit is contained in:
parent
2211c5f3b2
commit
227a48b285
@ -27,7 +27,8 @@ from .models import (
|
||||
Participants,
|
||||
Message,
|
||||
Person,
|
||||
Document
|
||||
Document,
|
||||
CustomUser
|
||||
)
|
||||
|
||||
# from django_summernote.widgets import SummernoteWidget
|
||||
@ -1028,17 +1029,20 @@ class HiringAgencyForm(forms.ModelForm):
|
||||
raise ValidationError("Please enter a valid email address.")
|
||||
|
||||
# Check uniqueness (optional - remove if multiple agencies can have same email)
|
||||
instance = self.instance
|
||||
if not instance.pk: # Creating new instance
|
||||
if HiringAgency.objects.filter(email=email).exists():
|
||||
raise ValidationError("An agency with this email already exists.")
|
||||
else: # Editing existing instance
|
||||
if (
|
||||
HiringAgency.objects.filter(email=email)
|
||||
.exclude(pk=instance.pk)
|
||||
.exists()
|
||||
):
|
||||
raise ValidationError("An agency with this email already exists.")
|
||||
# instance = self.instance
|
||||
email = email.lower().strip()
|
||||
if CustomUser.objects.filter(email=email).exists():
|
||||
raise ValidationError("This email is already associated with a user account.")
|
||||
# if not instance.pk: # Creating new instance
|
||||
# if HiringAgency.objects.filter(email=email).exists():
|
||||
# raise ValidationError("An agency with this email already exists.")
|
||||
# else: # Editing existing instance
|
||||
# if (
|
||||
# HiringAgency.objects.filter(email=email)
|
||||
# .exclude(pk=instance.pk)
|
||||
# .exists()
|
||||
# ):
|
||||
# raise ValidationError("An agency with this email already exists.")
|
||||
return email.lower().strip() if email else email
|
||||
|
||||
def clean_phone(self):
|
||||
|
||||
@ -3313,6 +3313,7 @@ def agency_create(request):
|
||||
if request.method == "POST":
|
||||
form = HiringAgencyForm(request.POST)
|
||||
if form.is_valid():
|
||||
|
||||
agency = form.save()
|
||||
messages.success(request, f'Agency "{agency.name}" created successfully!')
|
||||
return redirect("agency_detail", slug=agency.slug)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user