Skip to content

Instantly share code, notes, and snippets.

@tdfischer
Created August 2, 2018 03:46
Show Gist options
  • Save tdfischer/aaeffed1b6273df64bc2bc5e61766f37 to your computer and use it in GitHub Desktop.
Save tdfischer/aaeffed1b6273df64bc2bc5e61766f37 to your computer and use it in GitHub Desktop.
from django.utils import timezone
from django.core.management.base import BaseCommand
from django.db.models import Count
from crm.models import Turf
from django.core.mail import EmailMessage
from django.template import loader
class Command(BaseCommand):
def handle(self, *args, **options):
email_template = loader.get_template('new-neighbors-notification.eml')
for turf in Turf.objects.all():
notifications = turf.notification_targets.all()
if len(notifications) > 0:
for target in notifications:
lastSent = target.last_notified
if lastSent is None:
lastSent = timezone.now()
newbies = turf.members.filter(joined_on__gt=lastSent)
if len(newbies) > 0:
generated_email = email_template.render({
'newbies': newbies
})
email_obj = EmailMessage(
subject="New neighbors in %s"%(turf.name),
body=generated_email,
to=[target.email],
reply_to=[target.email]
)
email_obj.encoding = 'utf-8'
email_obj.send()
#print turf.name
#for newbie in newbies:
# print "\t%s <%s>"%(newbie.person.name, newbie.person.email)
else:
print "No notifications configured for", turf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment