Commit 088c0fe5 by benjaoming

Merge pull request #115 from jdcaballero/master

django-notify email notifications
parents e448e2b7 fbd75ae0
import os
import sys
import time
from datetime import datetime
from optparse import make_option
from django.contrib.sites.models import Site
from django.core import mail
from django.core.management.base import BaseCommand, CommandError
from django.template.loader import render_to_string
from django.utils.translation import ugettext as _
from django_notify import models
from django_notify.settings import INTERVALS, DEFAULT_EMAIL,NOTIFY_SLEEP_TIME,EMAIL_SUBJECT
class Command(BaseCommand):
help = 'Sends Notification emails to subscribed users taking into account the subscription interval'
option_list = BaseCommand.option_list + (
make_option('--daemon','-d',
action='store_true',
dest='daemon',
default=True,
help='Go to daemon mode and exit'),
)
def _send_user_notifications(self,context,connection):
subject = _(EMAIL_SUBJECT)
message = render_to_string('emails/notification_email_message.txt',
context)
email = mail.EmailMessage(subject, message, DEFAULT_EMAIL,
[context['user'].email], connection=connection)
email.send()
def handle(self, *args, **options):
daemon = options['daemon']
# Run as daemon, ie. fork the process
if daemon:
try:
fpid = os.fork()
if fpid > 0:
# Running as daemon now. PID is fpid
pid_file = file('/tmp/daemon-example.pid', "w")
pid_file.write(str(fpid))
pid_file.close()
sys.exit(0)
except OSError, e:
sys.stderr.write("fork failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
# This could be /improved by looking up the last notified person
last_sent = None
context = {'user': None,
'notifications': None,
'digest': None,
'site': Site.objects.get_current()}
#create a connection to smtp server for reuse
connection = mail.get_connection()
while True:
connection.open()
started_sending_at = datetime.now()
if last_sent:
settings = models.Settings.objects.filter(
interval__lte=((started_sending_at-last_sent).seconds // 60) // 60
).order_by('user')
else:
settings = models.Settings.objects.all().order_by('user')
for setting in settings:
context['user'] = setting.user
context['notifications']= []
#get the index of the tuple corresponding to the interval and get the string name
context['digest'] = INTERVALS[[y[0] for y in INTERVALS].index(setting.interval)][1]
for subscription in setting.subscription_set.filter(send_emails=True,latest__is_emailed=False):
context['notifications'].append(subscription.latest)
subscription.latest.is_emailed=True
subscription.latest.save()
if len(context['notifications']) > 0:
self._send_user_notifications(context,connection)
connection.close()
last_sent = datetime.now()
elapsed_minutes = ((last_sent - started_sending_at).seconds) // 60
time.sleep(max((min(INTERVALS)[0]*60-elapsed_minutes)*60,NOTIFY_SLEEP_TIME))
...@@ -18,6 +18,12 @@ ENABLE_ADMIN = getattr(django_settings, "NOTIFY_ENABLE_ADMIN", False) ...@@ -18,6 +18,12 @@ ENABLE_ADMIN = getattr(django_settings, "NOTIFY_ENABLE_ADMIN", False)
# to a proper SMTP server, just leave it off... # to a proper SMTP server, just leave it off...
SEND_EMAILS = getattr(django_settings, "NOTIFY_SEND_EMAILS", False) SEND_EMAILS = getattr(django_settings, "NOTIFY_SEND_EMAILS", False)
EMAIL_SUBJECT = "Notifications"
DEFAULT_EMAIL = 'notifications@example.com'
#NOTIFY_SLEEP_TIME must be greater than 0 to allow for Garbage Collection
NOTIFY_SLEEP_TIME = 10
# You can always make up more numbers... they simply identify which notifications # You can always make up more numbers... they simply identify which notifications
# to send when invoking the script, and the number indicates how many hours # to send when invoking the script, and the number indicates how many hours
# to minimum pass between each notification. # to minimum pass between each notification.
...@@ -27,12 +33,11 @@ DAILY = 24-1 # Subtract 1, because the job finishes less than 24h before the nex ...@@ -27,12 +33,11 @@ DAILY = 24-1 # Subtract 1, because the job finishes less than 24h before the nex
WEEKLY = 7*24-1 WEEKLY = 7*24-1
INTERVALS = getattr(django_settings, "NOTIFY_INTERVALS", INTERVALS = getattr(django_settings, "NOTIFY_INTERVALS",
[(INSTANTLY, _(u'instantly')), [(INSTANTLY, _(u'instant')),
(DAILY, _(u'daily')), (DAILY, _(u'daily')),
(WEEKLY, _(u'weekly'))]) (WEEKLY, _(u'weekly'))])
INTERVALS_DEFAULT = INSTANTLY INTERVALS_DEFAULT = INSTANTLY
# Minimum logging and digital garbage! Don't save too much crap! # Minimum logging and digital garbage! Don't save too much crap!
# After how many days should viewed notifications be deleted? # After how many days should viewed notifications be deleted?
......
{% load i18n %}{% autoescape off %}{% load url from future %}
{% blocktrans with user.username as username %}Dear {{ username }},{% endblocktrans %}
{% blocktrans with site.name as site %} These are the {{ digest }} notifications from {{ site }}.{% endblocktrans %}
{% for n in notifications %}
http://{{ site.domain }}{{n.url}} - {{ n.message }}
{% endfor %}
{% trans "Thanks for using our site!" %}
{% trans "Sincerely" %},
{{ site.name }}
{% endautoescape %}
{% load i18n %}
{% blocktrans with site.name as site %} {{ digest }} Notifications {{ site }}.{% endblocktrans %}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment