-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from adamspd/153-add-possibility-to-send-all-…
…emails-using-django-q Added possibility to send all emails with django q
- Loading branch information
Showing
10 changed files
with
170 additions
and
79 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,73 +1,90 @@ | ||
from django.conf import settings | ||
# email_sender.py | ||
# Path: appointment/email_sender/email_sender.py | ||
|
||
from django.core.mail import mail_admins, send_mail | ||
from django.template import loader | ||
from django_q.tasks import async_task | ||
|
||
from appointment.settings import APP_DEFAULT_FROM_EMAIL | ||
from appointment.settings import APP_DEFAULT_FROM_EMAIL, check_q_cluster | ||
|
||
|
||
def has_required_email_settings(): | ||
"""Check if all required email settings are configured and warn if any are missing.""" | ||
from django.conf import settings as s | ||
required_settings = [ | ||
'EMAIL_BACKEND', | ||
'EMAIL_HOST', | ||
'EMAIL_PORT', | ||
'EMAIL_HOST_USER', | ||
'EMAIL_HOST_PASSWORD', | ||
'EMAIL_USE_TLS', | ||
'EMAIL_USE_LOCALTIME', | ||
'ADMINS', | ||
'EMAIL_BACKEND', 'EMAIL_HOST', 'EMAIL_PORT', | ||
'EMAIL_HOST_USER', 'EMAIL_HOST_PASSWORD', 'EMAIL_USE_TLS', | ||
'EMAIL_USE_LOCALTIME', 'ADMINS', | ||
] | ||
missing_settings = [ | ||
setting_name for setting_name in required_settings if not hasattr(s, setting_name) | ||
] | ||
|
||
for setting_name in required_settings: | ||
if not hasattr(settings, setting_name): | ||
print(f"Warning: '{setting_name}' not found in settings. Email functionality will be disabled.") | ||
return False | ||
if missing_settings: | ||
missing_settings_str = ", ".join(missing_settings) | ||
print(f"Warning: The following settings are missing in settings.py: {missing_settings_str}. " | ||
"Email functionality will be disabled.") | ||
return False | ||
return True | ||
|
||
|
||
def render_email_template(template_url, context): | ||
if template_url: | ||
return loader.render_to_string(template_url, context) | ||
return "" | ||
|
||
|
||
def send_email(recipient_list, subject: str, template_url: str = None, context: dict = None, from_email=None, | ||
message: str = None): | ||
if not has_required_email_settings(): | ||
return | ||
|
||
if from_email is None: | ||
from_email = APP_DEFAULT_FROM_EMAIL | ||
|
||
html_message = "" | ||
|
||
if template_url: | ||
html_message = loader.render_to_string( | ||
template_name=template_url, | ||
context=context | ||
) | ||
from_email = from_email or APP_DEFAULT_FROM_EMAIL | ||
html_message = render_email_template(template_url, context) | ||
|
||
try: | ||
send_mail( | ||
subject=subject, | ||
message=message if not template_url else "", | ||
html_message=html_message if template_url else None, | ||
from_email=from_email, | ||
recipient_list=recipient_list, | ||
fail_silently=False, | ||
if get_use_django_q_for_emails() and check_q_cluster(): | ||
# Asynchronously send the email using Django-Q | ||
async_task( | ||
"appointment.tasks.send_email_task", recipient_list=recipient_list, subject=subject, | ||
message=message, html_message=html_message if template_url else None, from_email=from_email | ||
) | ||
except Exception as e: | ||
print(f"Error sending email: {e}") | ||
else: | ||
# Synchronously send the email | ||
try: | ||
send_mail( | ||
subject=subject, message=message if not template_url else "", | ||
html_message=html_message if template_url else None, from_email=from_email, | ||
recipient_list=recipient_list, fail_silently=False, | ||
) | ||
except Exception as e: | ||
print(f"Error sending email: {e}") | ||
|
||
|
||
def notify_admin(subject: str, template_url: str = None, context: dict = None, message: str = None): | ||
if not has_required_email_settings(): | ||
return | ||
|
||
html_message = "" | ||
if template_url: | ||
html_message = loader.render_to_string( | ||
template_name=template_url, | ||
context=context | ||
) | ||
html_message = render_email_template(template_url, context) | ||
|
||
if get_use_django_q_for_emails() and check_q_cluster(): | ||
# Enqueue the task to send admin email asynchronously | ||
async_task('appointment.tasks.notify_admin_task', subject=subject, message=message, html_message=html_message) | ||
else: | ||
# Synchronously send admin email | ||
try: | ||
mail_admins( | ||
subject=subject, message=message if not template_url else "", | ||
html_message=html_message if template_url else None | ||
) | ||
except Exception as e: | ||
print(f"Error sending email to admin: {e}") | ||
|
||
|
||
def get_use_django_q_for_emails(): | ||
"""Get the value of the USE_DJANGO_Q_FOR_EMAILS setting.""" | ||
try: | ||
mail_admins( | ||
subject=subject, | ||
message=message if not template_url else "", | ||
html_message=html_message if template_url else None | ||
) | ||
except Exception as e: | ||
print(f"Error sending email to admin: {e}") | ||
from django.conf import settings | ||
return getattr(settings, 'USE_DJANGO_Q_FOR_EMAILS', False) | ||
except AttributeError: | ||
print("Error accessing USE_DJANGO_Q_FOR_EMAILS. Defaulting to False.") | ||
return False |
This file contains 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 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 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 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 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
Oops, something went wrong.