-
Notifications
You must be signed in to change notification settings - Fork 9
React ts - Mailer models #212
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
Open
Bhavishya-Chaturvedi
wants to merge
8
commits into
Spoken-tutorial:react-ts
Choose a base branch
from
Bhavishya-Chaturvedi:react-ts
base: react-ts
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
babd248
Mailer models
Bhavishya-Chaturvedi 73eff93
Mailer models
Bhavishya-Chaturvedi d2eb758
Merge branch 'Spoken-tutorial:react-ts' into react-ts
Bhavishya-Chaturvedi 730bee1
Suggested changes implemented
Bhavishya-Chaturvedi 53d7240
Merge branch 'react-ts' of https://github.com/Bhavishya-Chaturvedi/Em…
Bhavishya-Chaturvedi d38d4ba
Suggested Changes implemented
Bhavishya-Chaturvedi 7d50bc8
Mailer Modules
Bhavishya-Chaturvedi 5e8c7ec
Settings.py & celery.py
Bhavishya-Chaturvedi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from .celery import app as celery_app | ||
|
|
||
| __all__ = ('celery_app',) |
8 changes: 8 additions & 0 deletions
8
employer_recommendation_system/employer_recommendation_system/celery.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import os | ||
| from celery import Celery | ||
|
|
||
| os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'employer_recommendation_system.settings') | ||
|
|
||
| app = Celery('employer_recommendation_system') | ||
| app.config_from_object('django.conf:settings', namespace='CELERY') | ||
| app.autodiscover_tasks() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,16 @@ | ||
| from django.contrib import admin | ||
|
|
||
| # Register your models here. | ||
| from .models import EmailRecord, EmailContent | ||
|
|
||
| @admin.register(EmailRecord) | ||
| class EmailRecordAdmin(admin.ModelAdmin): | ||
| list_display = ('email_address', 'status', 'sent_at', 'error_message') | ||
| list_filter = ('status',) | ||
| search_fields = ('email_address',) | ||
|
|
||
|
|
||
| @admin.register(EmailContent) | ||
| class EmailContentAdmin(admin.ModelAdmin): | ||
| list_display = ('subject', 'created_at', 'user_id') | ||
| filter_horizontal = ('email_records',) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,52 @@ | ||
| from django.db import models | ||
| from django.contrib.auth.models import User | ||
|
|
||
| # Create your models here. | ||
|
|
||
|
|
||
| class EmailRecord(models.Model): | ||
| """ | ||
| Stores individual email addresses and their sending status. | ||
| Each row corresponds to one email address from the uploaded CSV file. | ||
| """ | ||
| email_address = models.EmailField() | ||
| created_at = models.DateTimeField(auto_now_add=True) | ||
| status = models.CharField( | ||
| max_length=20, | ||
| choices=[ | ||
| ('pending', 'Pending'), | ||
| ('sent', 'Sent'), | ||
| ('failed', 'Failed'), | ||
| ], | ||
| default='pending' | ||
| ) | ||
| sent_at = models.DateTimeField(null=True, blank=True) | ||
| error_message = models.TextField(null=True, blank=True) | ||
|
|
||
| class Meta: | ||
| indexes = [models.Index(fields=['email_address'])] | ||
|
|
||
| def __str__(self): | ||
| return f"{self.email_address} - {self.status}" | ||
|
|
||
|
|
||
| class EmailContent(models.Model): | ||
| """ | ||
| Stores the common mail subject and body for a group of email addresses. | ||
| One EmailContent can be linked to multiple EmailRecords. | ||
| """ | ||
| subject = models.CharField(max_length=255) | ||
| mail_body = models.TextField() | ||
| email_records = models.ManyToManyField( | ||
| EmailRecord, | ||
| related_name='email_contents' | ||
| ) | ||
| created_at = models.DateTimeField(auto_now_add=True) | ||
| user = models.ForeignKey( | ||
| User, | ||
| on_delete=models.CASCADE, | ||
| related_name='email_contents' | ||
| ) | ||
|
|
||
| def __str__(self): | ||
| return f"EmailContent (Subject: {self.subject})" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from celery import shared_task | ||
| from django.utils import timezone | ||
| from django.core.mail import send_mail | ||
| from .models import EmailRecord, EmailContent | ||
|
|
||
|
|
||
| @shared_task(bind=True, max_retries=3, default_retry_delay=60) | ||
| def send_bulk_emails(self, email_content_id): | ||
| """ | ||
| Celery task to send bulk emails based on EmailContent and linked EmailRecords. | ||
| """ | ||
| try: | ||
| content = EmailContent.objects.get(pk=email_content_id) | ||
| records = content.email_records.all() | ||
|
|
||
| for record in records: | ||
| try: | ||
| send_mail( | ||
| subject=content.subject, | ||
| message=content.mail_body, | ||
| from_email=None, | ||
| recipient_list=[record.email_address], | ||
| fail_silently=False, | ||
| ) | ||
| record.status = 'sent' | ||
| record.sent_at = timezone.now() | ||
| record.save() | ||
| except Exception as e: | ||
| record.status = 'failed' | ||
| record.error_message = str(e) | ||
| record.save() | ||
|
|
||
| return f"Bulk email task completed for EmailContent ID {email_content_id}" | ||
|
|
||
| except Exception as exc: | ||
| raise self.retry(exc=exc) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import csv | ||
| from .models import EmailRecord | ||
|
|
||
| def import_emails_from_csv(csv_file): | ||
| """ | ||
| Reads a CSV containing email addresses and returns a list of EmailRecord objects. | ||
| CSV must have a column named 'email'. | ||
| """ | ||
| records = [] | ||
| reader = csv.DictReader(csv_file.read().decode('utf-8').splitlines()) | ||
| for row in reader: | ||
| email = row.get('email') | ||
| if email: | ||
| record, _ = EmailRecord.objects.get_or_create(email_address=email) | ||
| records.append(record) | ||
| return records |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,16 @@ | ||
| from django.shortcuts import render | ||
|
|
||
| # Create your views here. | ||
| from django.shortcuts import redirect | ||
| from django.contrib import messages | ||
| from .models import EmailContent | ||
| from .tasks import send_bulk_emails | ||
|
|
||
| def trigger_bulk_mail(request, content_id): | ||
| """ | ||
| Trigger Celery task to send emails asynchronously for a given EmailContent. | ||
| """ | ||
| content = EmailContent.objects.get(pk=content_id) | ||
| send_bulk_emails.delay(content.id) | ||
| messages.success(request, f"Email sending started for '{content.subject}'") | ||
| return redirect('admin:mailer_emailcontent_changelist') |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Define choices in tuple, and then use it in choices.
STATUS_CHOICES = [
('pending', 'Pending'),
('sent', 'Sent'),
('failed', 'Failed'),
]