diff --git a/website/models.py b/website/models.py index 09caef2..e676c8c 100644 --- a/website/models.py +++ b/website/models.py @@ -126,12 +126,7 @@ class Company(BaseModel): careers_url_status = models.IntegerField(null=True, blank=True) careers_url_status_updated = models.DateTimeField(null=True, blank=True) email = models.EmailField(blank=True, null=True) - screenshot = models.ImageField( - upload_to="company_screenshots", blank=True, null=True - ) - greenhouse_icon = '' # Replace with your actual SVG code - wellfound_icon = "..." # Replace with your actual SVG code - lever_icon = "..." # Replace with your actual SVG code + screenshot = models.ImageField(upload_to="company_screenshots", blank=True, null=True) phone = models.CharField(max_length=255, blank=True, null=True) class Meta: @@ -140,14 +135,50 @@ class Meta: def __str__(self): return self.name + def send_slack_notification(self, changes): + """Send a Slack notification with the changes""" + webhook_url = settings.SLACK_WEBHOOK_URL # Slack Webhook URL from settings + if not webhook_url: + return # Exit if no webhook URL is configured + + message = f"Company '{self.name}' has been updated.\nChanges:\n" + for field, (old, new) in changes.items(): + message += f"- {field}: '{old}' -> '{new}'\n" + + payload = { + "text": message, + "username": "Company Update Bot", + "icon_emoji": ":office:", + } + + try: + # Send POST request to Slack Webhook + response = requests.post(webhook_url, json=payload) + response.raise_for_status() + except requests.RequestException as e: + print(f"Error sending Slack notification: {e}") + def save(self, *args, **kwargs): + # Detect changes before saving + if self.pk: + old_company = Company.objects.get(pk=self.pk) + changes = {} + for field in self._meta.fields: + field_name = field.name + old_value = getattr(old_company, field_name) + new_value = getattr(self, field_name) + if old_value != new_value: + changes[field_name] = (old_value, new_value) + else: + changes = {"created": (None, self.name)} + # Ensure the slug is generated if it doesn't already exist if not self.slug: 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 + # Ensure slug uniqueness original_slug = self.slug queryset = Company.objects.all().exclude(pk=self.pk) counter = 1 @@ -155,38 +186,18 @@ def save(self, *args, **kwargs): self.slug = f"{original_slug}-{counter}" counter += 1 - # # Check if the email should be updated or left as is - # if self.email: - # print("Email provided checking if it exists in the BouncedEmail table" + self.email) - # from website.models import \ - # BouncedEmail # Import your BouncedEmail model - # bounced_email_exists = BouncedEmail.objects.filter(email=self.email).exists() - # print("pringing all bounced") - # for email in BouncedEmail.objects.all(): - # print(email.email) - # print(f"Email exists in BouncedEmail table: {bounced_email_exists}") - # # If the email exists in the BouncedEmail table, clear it - # if bounced_email_exists: - # self.email = None - # else: - # print('this is the case where there is no email') - # # 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 + # Save the company 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 + # Send a Slack notification if changes were detected + if changes: + self.send_slack_notification(changes) + def delete(self, *args, **kwargs): # Cache keys cache_key = f"cache_company_{self.slug}" @@ -202,6 +213,7 @@ def delete(self, *args, **kwargs): super().delete(*args, **kwargs) + class RequestLog(BaseModel): profile = models.ForeignKey(Profile, on_delete=models.CASCADE) company = models.ForeignKey(Company, on_delete=models.CASCADE) @@ -523,3 +535,4 @@ def save(self, *args, **kwargs): # start_date = models.DateField() # end_date = models.DateField() # skills = models.ManyToManyField('Skill') +# skills = models.ManyToManyField('Skill')