Skip to content

Commit

Permalink
refactor: Update email query in update_email view to handle new_email…
Browse files Browse the repository at this point in the history
… validation

This commit updates the email query in the update_email view of views.py. It adds a validation check for the new_email field to ensure that it is a valid email address before updating the company's email. This change improves the data integrity and prevents invalid email addresses from being saved.
  • Loading branch information
QA2A committed Sep 12, 2024
1 parent 53d3f68 commit 5c58988
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
28 changes: 18 additions & 10 deletions website/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def save(self, *args, **kwargs):
if not self.name:
raise ValueError("A company name is required to generate a slug.")
self.slug = slugify(self.name[:50])

# Check for uniqueness of the slug
original_slug = self.slug
queryset = Company.objects.all().exclude(pk=self.pk)
Expand All @@ -155,25 +155,33 @@ def save(self, *args, **kwargs):
self.slug = f"{original_slug}-{counter}"
counter += 1

# Cache keys
cache_key = f"cache_company_{self.slug}"
# Check if the email should be updated or left as is
if self.email:
from website.models import \
BouncedEmail # Import your BouncedEmail model
bounced_email_exists = BouncedEmail.objects.filter(email=self.email).exists()

# If the email exists in the BouncedEmail table, clear it
if bounced_email_exists:
self.email = None
else:
# If there's already an email set, keep it unless a new email is provided
existing_company = Company.objects.filter(pk=self.pk).first()
if existing_company and existing_company.email:
self.email = existing_company.email

# Save the object
super().save(*args, **kwargs)

# Cache keys
cache_key = f"cache_company_{self.slug}"

# Invalidate the cache
cache.delete(cache_key)

# Re-cache the company object
cache.set(cache_key, self, timeout=60 * 60 * 24) # Cache for 24 hours

# # Invalidate and re-cache the company list
# cache.delete(company_list_cache_key)
# companies_queryset = Company.objects.prefetch_related("application_set")
# cache.set(
# company_list_cache_key, companies_queryset, 60 * 60 * 24
# ) # Cache for 24 hours

def delete(self, *args, **kwargs):
# Cache keys
cache_key = f"cache_company_{self.slug}"
Expand Down
25 changes: 13 additions & 12 deletions website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,8 @@ def update_company(request, company_id):
if email and not re.match(r"[^@]+@[^@]+\.[^@]+", email):
return JsonResponse({"success": False, "error": "Invalid email address"})

if not company.email:
company.email = email or company.email
if email:
company.email = email

if not company.twitter_url:
company.twitter_url = request.POST.get("twitter_url") or company.twitter_url
Expand Down Expand Up @@ -602,16 +602,17 @@ def update_email(request):
return JsonResponse({"error": "Invalid email address"}, status=400)

if application.user == request.user and new_email:
application.job.company.email = new_email
application.job.company.save()
application.job.company.refresh_from_db()
return render(
request,
"partials/email.html",
{
"application": application,
},
)
if new_email and re.match(r"[^@]+@[^@]+\.[^@]+", new_email):
application.job.company.email = new_email
application.job.company.save()
application.job.company.refresh_from_db()
return render(
request,
"partials/email.html",
{
"application": application,
},
)
new_role = request.POST.get("role", None)

if new_role:
Expand Down

0 comments on commit 5c58988

Please sign in to comment.