From a92a39f62e68548fd2a4ade0bafbc344ac2b1089 Mon Sep 17 00:00:00 2001 From: zhengxie Date: Mon, 21 Jan 2013 21:36:04 +0800 Subject: [PATCH] Rewrite user notificatione sending --- notification_email.sh.template | 8 --- notifications/management/__init__.py | 0 notifications/management/commands/__init__.py | 0 .../commands/send_user_notifications.py | 60 +++++++++++++++++++ send_user_notifications.sh.template | 9 +++ user_notification.py | 55 ----------------- 6 files changed, 69 insertions(+), 63 deletions(-) delete mode 100755 notification_email.sh.template create mode 100644 notifications/management/__init__.py create mode 100644 notifications/management/commands/__init__.py create mode 100644 notifications/management/commands/send_user_notifications.py create mode 100755 send_user_notifications.sh.template delete mode 100755 user_notification.py diff --git a/notification_email.sh.template b/notification_email.sh.template deleted file mode 100755 index 6f083f1f4a..0000000000 --- a/notification_email.sh.template +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh - -cd /home/gonggeng/seahub - -export PYTHONPATH=thirdpart -export DJANGO_SETTINGS_MODULE=settings - -./user_notification.py diff --git a/notifications/management/__init__.py b/notifications/management/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/notifications/management/commands/__init__.py b/notifications/management/commands/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/notifications/management/commands/send_user_notifications.py b/notifications/management/commands/send_user_notifications.py new file mode 100644 index 0000000000..681f55d93c --- /dev/null +++ b/notifications/management/commands/send_user_notifications.py @@ -0,0 +1,60 @@ +# encoding: utf-8 +import logging +import string +from datetime import datetime +from django.core.mail import send_mail +from django.core.management.base import BaseCommand, CommandError + +from notifications.models import UserNotification +import settings + +# Get an instance of a logger +logger = logging.getLogger(__name__) + +email_template = u'''${username}您好: + +您有${cnt}条新消息,请点击下面的链接查看: +${msg_url} + +感谢使用我们的网站! + +${site_name}团队 +''' + +site_name = settings.SITE_NAME +subject = u'%s:新消息' % site_name +url = settings.SITE_BASE.rstrip('/') + settings.SITE_ROOT + 'home/my/' + +class Command(BaseCommand): + help = 'Send Email notifications to user if he/she has a new group message.' + + def handle(self, *args, **options): + logger.info('Start sending group notification...') + self.do_action() + logger.info('Finish sending group notification.\n') + + def do_action(self): + today = datetime.now() + notifications = UserNotification.objects.all() + + d = {} + for e in notifications: + if today.year != e.timestamp.year or today.month != e.timestamp.month or \ + today.day != e.timestamp.day: # Only send today's notifications + continue + if d.has_key(e.to_user): + d[e.to_user] += 1 + else: + d[e.to_user] = 1 + + for user,cnt in d.items(): + template = string.Template(email_template) + content = template.substitute(username=user, cnt=cnt, msg_url=url, \ + site_name=site_name) + try: + send_mail(subject, content, settings.DEFAULT_FROM_EMAIL, [user], \ + fail_silently=False) + logger.info('Succesfuuly sended email to %s' % user) + except Exception, e: + logger.error('Failed to send email to %s, error detail: %s' % (user, e)) + diff --git a/send_user_notifications.sh.template b/send_user_notifications.sh.template new file mode 100755 index 0000000000..b5b93b7f79 --- /dev/null +++ b/send_user_notifications.sh.template @@ -0,0 +1,9 @@ +#!/bin/sh + +cd /Users/xiez/seahub + +export CCNET_CONF_DIR=/Users/xiez/basic/conf2 +export SEAFILE_CONF_DIR=/Users/xiez/basic/conf2/seafile-data +export PYTHONPATH=/Library/Python/2.7/site-packages/Django-1.3.1:thirdpart:/usr/local/lib/python2.7/site-packages/:$PYTHONPATH + +python manage.py send_user_notifications diff --git a/user_notification.py b/user_notification.py deleted file mode 100755 index 91f3c3eed5..0000000000 --- a/user_notification.py +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/python -# encoding: utf-8 - -from datetime import datetime -import string -from django.core.mail import send_mail - -from notifications.models import UserNotification -import settings - -email_template = u'''${username}您好: - -您有${cnt}条新消息,请点击下面的链接查看: -${msg_url} - -感谢使用我们的网站! - -${site_name}团队 -''' - -today = datetime.now() -site_name = settings.SITE_NAME -subject = u'%s:新消息' % site_name - -site_base = settings.SITE_BASE -if site_base[-1] == '/': - site_base = site_base[:-1] -site_root = settings.SITE_ROOT -if site_root[-1] != '/': - site_root += '/' -url = site_base + site_root + 'home/my/' - -notifications = UserNotification.objects.all() - -d = {} -for e in notifications: - if today.year != e.timestamp.year or today.month != e.timestamp.month or \ - today.day != e.timestamp.day: - continue - if d.has_key(e.to_user): - d[e.to_user] += 1 - else: - d[e.to_user] = 1 - -for k in d.keys(): - to_user = k - cnt = d[k] - - template = string.Template(email_template) - content = template.substitute(username=to_user, cnt=cnt, msg_url=url, \ - site_name=site_name) - send_mail(subject, content, settings.DEFAULT_FROM_EMAIL, [to_user], \ - fail_silently=False) - -