1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-04-26 18:40:53 +00:00

Rewrite user notificatione sending

This commit is contained in:
zhengxie 2013-01-21 21:36:04 +08:00
parent f2ee3c614b
commit a92a39f62e
6 changed files with 69 additions and 63 deletions

View File

@ -1,8 +0,0 @@
#!/bin/sh
cd /home/gonggeng/seahub
export PYTHONPATH=thirdpart
export DJANGO_SETTINGS_MODULE=settings
./user_notification.py

View File

View File

@ -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))

View File

@ -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

View File

@ -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)