Skip to content

Commit

Permalink
added feature to send test update from admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Yadavanurag13 committed Dec 28, 2024
1 parent f9243c3 commit 8003a12
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
20 changes: 19 additions & 1 deletion backend/djangoindia/db/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class UpdateAdmin(admin.ModelAdmin):
list_display = ("email_subject", "type", "created_by", "created_at", "mail_sent")
search_fields = ["email_subject", "created_by__username", "created_by__first_name", "type"]
readonly_fields = ("created_by", "created_at", "updated_at")
actions = ["send_update"]
actions = ["send_update", "send_test_email"]

def save_model(self, request, obj, form, change):
obj.created_by = request.user
Expand All @@ -234,6 +234,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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.5 on 2024-12-24 20:01

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('db', '0013_alter_event_end_date_and_more'),
]

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.'),
),
]
35 changes: 34 additions & 1 deletion backend/djangoindia/db/models/update.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django_prose_editor.fields import ProseEditorField

from django.core.exceptions import ValidationError
from django.conf import settings
from django.contrib.auth.models import User
from django.db import models
from django.core.mail import send_mail

from djangoindia.bg_tasks.send_update import send_mass_update_email_task

Expand All @@ -21,6 +23,10 @@ class UpdateType(models.TextChoices):
created_by = models.ForeignKey(User, on_delete=models.DO_NOTHING, editable=False)
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
Expand All @@ -29,6 +35,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]
Expand Down

0 comments on commit 8003a12

Please sign in to comment.