Skip to content

Commit

Permalink
swappable model
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelpoi committed Nov 6, 2024
1 parent 7909a9c commit cfc1bab
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 12 deletions.
2 changes: 1 addition & 1 deletion demoapp/custom_user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ class CustomEmailAddress(AbstractEmailAddress):
phone_number = models.CharField(max_length=20, blank=True, null=True)

class Meta:
pass
swappable = 'EMAIL_ADDRESS_MODEL'
4 changes: 2 additions & 2 deletions demoapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@
('email/default.html', _('Default')),
('email/placeholders.html', _('Placeholders')),
],
'EMAIL_ADDRESS_MODEL': 'custom_user.models.CustomEmailAddress',
}
WSGI_APPLICATION = "demoapp.wsgi.application"
EMAIL_ADDRESS_MODEL = 'sendmail.EmailAddress'
#EMAIL_ADDRESS_MODEL = 'custom_user.CustomEmailAddress'

# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
Expand Down Expand Up @@ -229,7 +230,6 @@
CELERY_TASK_SERIALIZER = "json"
CELERY_TASK_TRACK_STARTED = True


CKEDITOR_CONFIGS = {
'default': {
'toolbar': 'Custom',
Expand Down
1 change: 1 addition & 0 deletions demoapp/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@

]
POST_OFFICE_PLACEHOLDERS_CACHE = False
EMAIL_ADDRESS_MODEL = 'sendmail.EmailAddress'
SENDMAIL = {
'BACKENDS': {
'default': 'django.core.mail.backends.dummy.EmailBackend',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 5.1 on 2024-11-06 09:12

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('sendmail', '0008_alter_emailmergemodel_options'),
migrations.swappable_dependency(settings.EMAIL_ADDRESS_MODEL),
]

operations = [
migrations.AlterField(
model_name='emailmergemodel',
name='extra_recipients',
field=models.ManyToManyField(blank=True, help_text='extra bcc recipients', to=settings.EMAIL_ADDRESS_MODEL),
),
migrations.AlterField(
model_name='emailmodel',
name='recipients',
field=models.ManyToManyField(related_name='to_emails', through='sendmail.Recipient', to=settings.EMAIL_ADDRESS_MODEL),
),
migrations.AlterField(
model_name='recipient',
name='address',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.EMAIL_ADDRESS_MODEL),
),
]
3 changes: 2 additions & 1 deletion sendmail/models/emailaddress.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.conf import settings

from sendmail.logutils import setup_loghandlers
from sendmail.validators import validate_email_with_name
Expand Down Expand Up @@ -51,7 +52,7 @@ class Recipient(models.Model):
('bcc', _('Bcc')),
]
email = models.ForeignKey('sendmail.EmailModel', on_delete=models.CASCADE)
address = models.ForeignKey(get_email_address_model(), on_delete=models.CASCADE)
address = models.ForeignKey(settings.EMAIL_ADDRESS_MODEL, on_delete=models.CASCADE)
send_type = models.CharField(max_length=12, choices=SEND_TYPES, default='to')

def __str__(self):
Expand Down
7 changes: 2 additions & 5 deletions sendmail/models/emailmerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
from sendmail import cache
from sendmail.cache_utils import get_placeholders
from sendmail.logutils import setup_loghandlers
from sendmail.models.emailaddress import EmailAddress
from sendmail.parser import process_template
from sendmail.sanitizer import clean_html
from sendmail.settings import get_languages_list, get_template_engine, get_email_address_model
from sendmail.settings import get_template_engine, get_email_address_model
from sendmail.validators import validate_template_syntax
from django.db.models.signals import post_delete, pre_delete
from django.dispatch import receiver

logger = setup_loghandlers('INFO')

Expand All @@ -33,7 +30,7 @@ class EmailMergeModel(models.Model):
created = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
extra_recipients = models.ManyToManyField(
get_email_address_model(),
settings.EMAIL_ADDRESS_MODEL,
blank=True,
help_text='extra bcc recipients',
)
Expand Down
3 changes: 2 additions & 1 deletion sendmail/models/emailmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.utils.translation import pgettext_lazy
from django.conf import settings

from sendmail.connections import connections
from sendmail.logutils import setup_loghandlers
Expand Down Expand Up @@ -41,7 +42,7 @@ class EmailModel(models.Model):
]

from_email = models.CharField(_('Email From'), max_length=254, validators=[validate_email_with_name])
recipients = models.ManyToManyField(get_email_address_model(), related_name='to_emails', through=Recipient)
recipients = models.ManyToManyField(settings.EMAIL_ADDRESS_MODEL, related_name='to_emails', through=Recipient)
subject = models.CharField(_('Subject'), max_length=989, blank=True)
message = models.TextField(_('Message'), blank=True)
html_message = models.TextField(_('HTML Message'), blank=True)
Expand Down
5 changes: 3 additions & 2 deletions sendmail/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import warnings

from django.apps import apps
from django.utils.module_loading import import_string
from django.conf import settings
from django.core.cache import caches
Expand Down Expand Up @@ -119,8 +120,8 @@ def get_template_engine():


def get_email_address_model():
model_name = get_config().get('EMAIL_ADDRESS_MODEL', 'sendmail.models.emailaddress.EmailAddress')
return import_string(model_name)
model_name = getattr(settings, 'EMAIL_ADDRESS_MODEL') or 'sendmail.model.EmailAddress'
return apps.get_model(model_name)


# def get_override_recipients():
Expand Down

0 comments on commit cfc1bab

Please sign in to comment.