From 5c58988e16e37cf61b6241ce324d48b316bb9556 Mon Sep 17 00:00:00 2001 From: QA2A <150867722+QA2A@users.noreply.github.com> Date: Thu, 12 Sep 2024 15:06:19 -0400 Subject: [PATCH] refactor: Update email query in update_email view to handle new_email 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. --- website/models.py | 28 ++++++++++++++++++---------- website/views.py | 25 +++++++++++++------------ 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/website/models.py b/website/models.py index daeb1f6..5cf700c 100644 --- a/website/models.py +++ b/website/models.py @@ -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) @@ -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}" diff --git a/website/views.py b/website/views.py index bdf857c..c676f39 100644 --- a/website/views.py +++ b/website/views.py @@ -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 @@ -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: