diff options
Diffstat (limited to 'forum/management')
-rw-r--r-- | forum/management/commands/send_email_alerts.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/forum/management/commands/send_email_alerts.py b/forum/management/commands/send_email_alerts.py new file mode 100644 index 00000000..3c37aaa3 --- /dev/null +++ b/forum/management/commands/send_email_alerts.py @@ -0,0 +1,41 @@ +from django.core.management.base import NoArgsCommand +from django.db import connection +from forum.models import * +import collections +from django.core.mail import EmailMessage +from django.utils.translation import ugettext as _ +import settings + +class Command(NoArgsCommand): + def handle_noargs(self,**options): + try: + self.send_email_alerts() + except Exception, e: + print e + finally: + connection.close() + + def send_email_alerts(self): + report_time = datetime.datetime.now() + feeds = EmailFeed.objects.all() + user_ctype = ContentType.objects.get_for_model(User) + + #lists of update messages keyed by email address + update_collection = collections.defaultdict(list) + for feed in feeds: + update_summary = feed.get_update_summary() + if update_summary != None: + email = feed.get_email() + update_collection[email].append(update_summary) + feed.reported_at = report_time + feed.save() + + for email, updates in update_collection.items(): + text = '\n'.join(updates) + subject = _('updates from website') + print 'sent %s to %s' % (updates,email) + msg = EmailMessage(subject, text, settings.DEFAULT_FROM_EMAIL, [email]) + msg.content_subtype = 'html' + msg.send() + + |