mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 08:51:09 -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:
@@ -7,9 +7,20 @@ from .models import EmailConfiguration
|
||||
import json
|
||||
import base64
|
||||
|
||||
|
||||
class EmailService:
|
||||
@staticmethod
|
||||
def send_email(*, to: str, subject: str, text: str, from_email: str = None, html: str = None, reply_to: str = None, request = None, site = None):
|
||||
def send_email(
|
||||
*,
|
||||
to: str,
|
||||
subject: str,
|
||||
text: str,
|
||||
from_email: str = None,
|
||||
html: str = None,
|
||||
reply_to: str = None,
|
||||
request=None,
|
||||
site=None,
|
||||
):
|
||||
# Get the site configuration
|
||||
if site is None and request is not None:
|
||||
site = get_current_site(request)
|
||||
@@ -20,23 +31,28 @@ class EmailService:
|
||||
# Fetch the email configuration for the current site
|
||||
email_config = EmailConfiguration.objects.get(site=site)
|
||||
api_key = email_config.api_key
|
||||
|
||||
|
||||
# Use provided from_email or construct from config
|
||||
if not from_email:
|
||||
from_email = f"{email_config.from_name} <{email_config.from_email}>"
|
||||
elif '<' not in from_email:
|
||||
# If from_email is provided but doesn't include a name, add the configured name
|
||||
from_email = f"{
|
||||
email_config.from_name} <{
|
||||
email_config.from_email}>"
|
||||
elif "<" not in from_email:
|
||||
# If from_email is provided but doesn't include a name, add the
|
||||
# configured name
|
||||
from_email = f"{email_config.from_name} <{from_email}>"
|
||||
|
||||
|
||||
# Use provided reply_to or fall back to config
|
||||
if not reply_to:
|
||||
reply_to = email_config.reply_to
|
||||
|
||||
except EmailConfiguration.DoesNotExist:
|
||||
raise ImproperlyConfigured(f"Email configuration is missing for site: {site.domain}")
|
||||
raise ImproperlyConfigured(
|
||||
f"Email configuration is missing for site: {site.domain}"
|
||||
)
|
||||
|
||||
# Ensure the reply_to address is clean
|
||||
reply_to = sanitize_address(reply_to, 'utf-8')
|
||||
reply_to = sanitize_address(reply_to, "utf-8")
|
||||
|
||||
# Format data for the API
|
||||
data = {
|
||||
@@ -74,7 +90,8 @@ class EmailService:
|
||||
f"{settings.FORWARD_EMAIL_BASE_URL}/v1/emails",
|
||||
json=data,
|
||||
headers=headers,
|
||||
timeout=60)
|
||||
timeout=60,
|
||||
)
|
||||
|
||||
# Debug output
|
||||
print(f"Response Status: {response.status_code}")
|
||||
@@ -83,7 +100,10 @@ class EmailService:
|
||||
|
||||
if response.status_code != 200:
|
||||
error_message = response.text if response.text else "Unknown error"
|
||||
raise Exception(f"Failed to send email (Status {response.status_code}): {error_message}")
|
||||
raise Exception(
|
||||
f"Failed to send email (Status {
|
||||
response.status_code}): {error_message}"
|
||||
)
|
||||
|
||||
return response.json()
|
||||
except requests.RequestException as e:
|
||||
|
||||
Reference in New Issue
Block a user