Skip to content

Instantly share code, notes, and snippets.

@jkatz
Last active February 28, 2017 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jkatz/f1f44ae6501dd0a380e8 to your computer and use it in GitHub Desktop.
Save jkatz/f1f44ae6501dd0a380e8 to your computer and use it in GitHub Desktop.
from django.conf import settings
from django.core.mail import send_mail
from django.db import models, transaction
from django.db.models.signals import post_save
from .tasks import *
class Contact(models.Model):
"""store contact information for emailing a user in the address book"""
user = models.ForeignKey(settings.AUTH_USER_MODEL, help_text="A reference to the User for the purpose of his/her address book")
first_name = models.TextField(help_text='The first name')
last_name = models.TextField(help_text='The last name')
email = models.EmailField(max_length=255, help_text='The email address')
zipcode = models.CharField(max_length=10, help_text='The zip code')
birthdate = models.DateField(null=True, blank=True, help_text='The birthday')
@classmethod
def send_birthday_greetings(cls, start_date):
"""send a birthday greeting to people!
:param datetime.date start_date: the birthday date to send...probably ``datetime.date.today()``
"""
for contact in cls.objects.filter(birthdate=start_date).all():
send_birthday_greeting.delay(contact.id)
@transaction.atomic()
def change_name(self, first_name, last_name):
"""change the first_name and last_name of the ``Contact`` and then send
and alert to ``contract.User`` that the name has changed
:param str first_name: The new first_name for the Contact
:param str last_name: The new last_name for the Contact
"""
self.first_name = first_name
self.last_name = last_name
self.save()
send_change_of_name(self.id)
@transaction.atomic()
def change_zipcode(self, zipcode):
"""change the zipcode and send an alert to the ``Contact.user`` that it has changed
:param str zipcode: The new zipcode
"""
self.zipcode = zipcode
self.save()
send_change_of_zipcode.delay(self.id)
def send_birthday_greeting(self):
"""send a birthday greeting to this ``Contact``"""
subject = "Happy Birthday %s!" % self.first_name
message = """Happy Birthday to You, %s!!""" % self.first_name
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.email])
def send_change_of_name(self):
"""send change of name information to ``Contact.user``"""
subject = "Name Update Info"
message = """Just to let you know, %s has changed names to: %s %s""" % (self.first_name, self.first_name, self.last_name,)
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.email])
def send_change_of_zipcode(self):
"""send change of zipcode information to ``Contact.user``"""
subject = "Zipcode Update Info"
message = """Just to let you know, %s has change the zipcode to: %s""" % (self.first_name, self.zipcode,)
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.email])
from celery import task
__all__ = [
"send_birthday_greeting",
"send_change_of_name",
"send_change_of_zipcode",
]
@task
def send_birthday_greeting(contact_id):
"""send a friendly birthday greeting to a Contact!
:param int contact_id: a reference for ``Contact.id`` to look up and send the greeting
"""
from .models import Contact
try:
contact = Contact.objects.get(id=contact_id)
except Contact.DoesNotExist:
return
contact.send_birthday_greeting()
@task
def send_change_of_name(contact_id):
"""send an alert that someone's name has changed!
:param int contact_id: a reference for ``Contact.id`` to look up and send the greeting
"""
from .models import Contact
try:
contact = Contact.objects.get(id=contact_id)
except Contact.DoesNotExist:
return
contact.send_change_of_name()
@task
def send_change_of_zipcode(contact_id):
"""send an alert that someone's address changed!
:param int contact_id: a reference for ``Contact.id`` to look up and send the greeting
"""
from .models import Contact
try:
contact = Contact.objects.get(id=contact_id)
except Contact.DoesNotExist:
return
contact.send_change_of_zipcode()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment