Created
April 10, 2021 02:15
-
-
Save chrisjakuta/b0bb42348373dda98cdd7fc57b192674 to your computer and use it in GitHub Desktop.
A class that will store all of the necessary data to send a user an email. Prototype stage. The model included is a proxy model designed to save the email information about to be sent.
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
from django.core.mail import send_mail, send_mass_mail, EmailMultiAlternatives | |
from emailapp.models import SendIntroSignUpEmailModel, SendEmailModel | |
from django.template.loader import render_to_string, get_template | |
class SendEmailMessage: | |
def __init__(self, **kwargs): | |
if not kwargs['email']: | |
raise ValueError('Please include an object instance of SendEmailModel named email.') | |
if not isinstance(kwargs['email'], SendIntroSignUpEmailModel): | |
raise TypeError('Instance needs to be on instance of emailapp.models.SendEmailModel') | |
for key, value in kwargs.items(): | |
setattr(self, key, value) | |
self.html = render_to_string('base_email.html', {'code': self.email.code}) | |
self.target = self.email.target | |
self.to_email = [self.email.target.email,] | |
self.from_email = '[email protected]' | |
self.subject = self.email.subject | |
self.message = self.email.message | |
def send(self): | |
return send_mail( | |
self.subject, | |
self.message, | |
self.from_email, | |
self.to_email, | |
html_message=self.html | |
) |
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
from django.db import models | |
import uuid | |
from django.core.mail import send_mail, EmailMessage | |
from django.utils.translation import gettext_lazy as _ | |
from nerdrich.settings import AUTH_USER_MODEL | |
import random | |
from django.db.models.query import QuerySet | |
class SendEmailModel(models.Model): | |
''' | |
A catch all email model that can be used for | |
sending introductory emails, or promo emails | |
from the admin interface. | |
''' | |
class Meta: | |
verbose_name = 'Send Email' | |
verbose_name_plural = 'Send Emails' | |
class Types(models.TextChoices): | |
SENDSIGNUPCONFIRMATIONEMAIL = 'SENDSIGNUPCONFIRMATIONEMAIL', 'Send A Sign Up Confirmation Email', | |
SENDEMAILTOONECUSTOMERMODEL = 'SENDEMAILTOONECUSTOMERMODEL', 'Send Email To One Customer', | |
SENDEMAILTOMULTIPLECUSTOMERSMODEL = 'SENDEMAILTOMULTIPLECUSTOMERSMODEL', 'Send Emails To Many Customers', | |
base_type = Types.SENDSIGNUPCONFIRMATIONEMAIL | |
type = models.CharField( | |
_('Type'), | |
max_length=50, | |
choices=Types.choices, | |
default=base_type, | |
) | |
subject = models.CharField( | |
max_length=255, | |
default='Confirm Your Email' | |
) | |
name = models.CharField( | |
max_length=255, | |
default='send-sign-up-confirmation-email' | |
) | |
message = models.CharField( | |
max_length=255, | |
default='You are almost there.\nUse this code to verify your account, and login.' | |
) | |
promo = models.BooleanField( | |
default=False, | |
) | |
promo_id = models.UUIDField( | |
default = uuid.uuid4, | |
editable = False | |
) | |
promo_name = models.CharField( | |
max_length = 255, | |
) | |
url = models.URLField( | |
max_length = 255, | |
help_text=_( | |
'Can be used for promotional, and security purposes.' | |
) | |
) | |
code = models.PositiveIntegerField( | |
default=0, | |
) | |
target = models.ForeignKey( | |
AUTH_USER_MODEL, | |
on_delete=models.CASCADE, | |
related_name='emails', | |
null=True, | |
) | |
targets = models.ManyToManyField( | |
AUTH_USER_MODEL, | |
related_name='bulk_emails' | |
) | |
def __unicode__(self): | |
return f'This email was generated for %s' % self.target | |
class SendIntroSignUpEmailModelManager(models.Manager): | |
def create_sendemailmodel(self, target, **extra_fields): | |
if not target: | |
raise ValueError('Must include an AUTH_USER_MODEL instance') | |
code = random.randrange(100000, 1000000) | |
sendemailmodel = self.model( | |
target=target, | |
code=code, | |
) | |
sendemailmodel.save(using=self._db) | |
return sendemailmodel | |
def get_queryset(self, *args, **kwargs): | |
return super().get_queryset(*args, **kwargs).filter(type=SendEmailModel.Types.SENDSIGNUPCONFIRMATIONEMAIL) | |
class SendIntroSignUpEmailModel(SendEmailModel): | |
class Meta: | |
verbose_name_plural = 'Send Intro Sign Up Email Models' | |
proxy = True | |
base_type = SendEmailModel.Types.SENDSIGNUPCONFIRMATIONEMAIL | |
objects = SendIntroSignUpEmailModelManager() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
SendEmailModel
is a proxy model, that all of the other models will inherit from. This model is intended to save info that will be sent to the user, and can later be used for Data Science or auditing. The proceeding model and manager will generate a six digit code whenever a new model is created.