Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added feature to send test update from admin panel #265

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions backend/djangoindia/db/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,34 @@ 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):
for update in 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 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.'),
),
]
34 changes: 34 additions & 0 deletions backend/djangoindia/db/models/update.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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]
Expand Down
Loading