mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 09:11:08 -05:00
Refactor test utilities and enhance ASGI settings
- Cleaned up and standardized assertions in ApiTestMixin for API response validation. - Updated ASGI settings to use os.environ for setting the DJANGO_SETTINGS_MODULE. - Removed unused imports and improved formatting in settings.py. - Refactored URL patterns in urls.py for better readability and organization. - Enhanced view functions in views.py for consistency and clarity. - Added .flake8 configuration for linting and style enforcement. - Introduced type stubs for django-environ to improve type checking with Pylance.
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
from django.core.mail.backends.base import BaseEmailBackend
|
||||
from django.contrib.sites.shortcuts import get_current_site
|
||||
from django.core.mail.message import sanitize_address
|
||||
from .services import EmailService
|
||||
from .models import EmailConfiguration
|
||||
|
||||
|
||||
class ForwardEmailBackend(BaseEmailBackend):
|
||||
def __init__(self, fail_silently=False, **kwargs):
|
||||
super().__init__(fail_silently=fail_silently)
|
||||
self.site = kwargs.get('site', None)
|
||||
self.site = kwargs.get("site", None)
|
||||
|
||||
def send_messages(self, email_messages):
|
||||
"""
|
||||
@@ -23,7 +23,7 @@ class ForwardEmailBackend(BaseEmailBackend):
|
||||
sent = self._send(message)
|
||||
if sent:
|
||||
num_sent += 1
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
if not self.fail_silently:
|
||||
raise
|
||||
return num_sent
|
||||
@@ -33,11 +33,14 @@ class ForwardEmailBackend(BaseEmailBackend):
|
||||
if not email_message.recipients():
|
||||
return False
|
||||
|
||||
# Get the first recipient (ForwardEmail API sends to one recipient at a time)
|
||||
# Get the first recipient (ForwardEmail API sends to one recipient at a
|
||||
# time)
|
||||
to_email = email_message.to[0]
|
||||
|
||||
# Get site from connection or instance
|
||||
if hasattr(email_message, 'connection') and hasattr(email_message.connection, 'site'):
|
||||
if hasattr(email_message, "connection") and hasattr(
|
||||
email_message.connection, "site"
|
||||
):
|
||||
site = email_message.connection.site
|
||||
else:
|
||||
site = self.site
|
||||
@@ -49,11 +52,16 @@ class ForwardEmailBackend(BaseEmailBackend):
|
||||
try:
|
||||
config = EmailConfiguration.objects.get(site=site)
|
||||
except EmailConfiguration.DoesNotExist:
|
||||
raise ValueError(f"Email configuration not found for site: {site.domain}")
|
||||
raise ValueError(
|
||||
f"Email configuration not found for site: {
|
||||
site.domain}"
|
||||
)
|
||||
|
||||
# Get the from email, falling back to site's default if not provided
|
||||
if email_message.from_email:
|
||||
from_email = sanitize_address(email_message.from_email, email_message.encoding)
|
||||
from_email = sanitize_address(
|
||||
email_message.from_email, email_message.encoding
|
||||
)
|
||||
else:
|
||||
from_email = config.default_from_email
|
||||
|
||||
@@ -62,13 +70,16 @@ class ForwardEmailBackend(BaseEmailBackend):
|
||||
|
||||
# Get reply-to from message headers or use default
|
||||
reply_to = None
|
||||
if hasattr(email_message, 'reply_to') and email_message.reply_to:
|
||||
if hasattr(email_message, "reply_to") and email_message.reply_to:
|
||||
reply_to = email_message.reply_to[0]
|
||||
elif hasattr(email_message, 'extra_headers') and 'Reply-To' in email_message.extra_headers:
|
||||
reply_to = email_message.extra_headers['Reply-To']
|
||||
elif (
|
||||
hasattr(email_message, "extra_headers")
|
||||
and "Reply-To" in email_message.extra_headers
|
||||
):
|
||||
reply_to = email_message.extra_headers["Reply-To"]
|
||||
|
||||
# Get message content
|
||||
if email_message.content_subtype == 'html':
|
||||
if email_message.content_subtype == "html":
|
||||
# If it's HTML content, we'll send it as text for now
|
||||
# You could extend this to support HTML emails if needed
|
||||
text = email_message.body
|
||||
@@ -82,10 +93,10 @@ class ForwardEmailBackend(BaseEmailBackend):
|
||||
text=text,
|
||||
from_email=from_email,
|
||||
reply_to=reply_to,
|
||||
site=site
|
||||
site=site,
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
if not self.fail_silently:
|
||||
raise
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user