diff --git a/backend/djangoindia/db/admin.py b/backend/djangoindia/db/admin.py index 828d42c..f080685 100644 --- a/backend/djangoindia/db/admin.py +++ b/backend/djangoindia/db/admin.py @@ -221,9 +221,9 @@ class SponsorAdmin(admin.ModelAdmin): class UpdateAdmin(admin.ModelAdmin): form = UpdateForm list_display = ("email_subject", "type", "created_at", "mail_sent") - search_fields = ["email_subject", "type"] + search_fields = ["email_subject", "created_by__username", "created_by__first_name", "type"] readonly_fields = ("created_at", "updated_at") - actions = ["send_update"] + actions = ["send_update", "send_test_email"] @admin.action(description="Send selected updates to subscribers") def send_update(self, request, queryset): @@ -231,6 +231,24 @@ def send_update(self, request, queryset): update.send_bulk_emails() self.message_user(request, "Update emails sent.") + @admin.action(description="Send test email") + def send_test_email(self, request, queryset) -> None: + """ + Admin action to send test emails to the recipients specified in the queryset. + + Args: + request + queryset + + Returns: + None + + Raises: + None + """ + for update in queryset: + update.send_test_email() + self.message_user(request, "Test email sent.") @admin.register(CommunityPartner) class CommunityPartnerAdmin(admin.ModelAdmin): diff --git a/backend/djangoindia/db/migrations/0002_update_test_email_recipients.py b/backend/djangoindia/db/migrations/0002_update_test_email_recipients.py new file mode 100644 index 0000000..fc00078 --- /dev/null +++ b/backend/djangoindia/db/migrations/0002_update_test_email_recipients.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.5 on 2025-01-12 17:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('db', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='update', + name='test_email_recipients', + field=models.TextField(blank=True, help_text='Enter comma-separated email addresses for the test email.'), + ), + ] diff --git a/backend/djangoindia/db/models/update.py b/backend/djangoindia/db/models/update.py index 3d8dc8d..02feedc 100644 --- a/backend/djangoindia/db/models/update.py +++ b/backend/djangoindia/db/models/update.py @@ -1,6 +1,9 @@ from django_prose_editor.fields import ProseEditorField +from django.core.exceptions import ValidationError +from django.conf import settings from django.db import models +from django.core.mail import send_mail from djangoindia.bg_tasks.send_update import send_mass_update_email_task @@ -19,6 +22,10 @@ class UpdateType(models.TextChoices): email_body = ProseEditorField() recipients = models.ManyToManyField("Subscriber", related_name="received_updates") mail_sent = models.BooleanField(default=False) + test_email_recipients = models.TextField( + blank=True, + help_text="Enter comma-separated email addresses for the test email." + ) def __str__(self): return self.email_subject @@ -27,6 +34,33 @@ def send_bulk_emails(self): if self.id and self.recipients.count() > 0: send_mass_update_email_task.delay(self.id) + def send_test_email(self) -> None: + """ + Send a test email to the test email recipients. + + Args: + None + + Returns: + None + + Raises: + ValidationError: If there is an error sending the email. + """ + for email in self.test_email_recipients.split(","): + email = email.strip() + if email: + try: + send_mail( + subject=self.email_subject, + message=self.email_body, + from_email=settings.DEFAULT_FROM_EMAIL, + recipient_list=[email], + fail_silently=False + ) + except ValidationError as e: + raise ValidationError(f"Error sending test email: {str(e)}") + def get_formatted_type(self): words = self.type.split("_") formatted_words = [word.capitalize() for word in words]