From 02b98b748be26803433c7f9177dcf5a1cd35487a Mon Sep 17 00:00:00 2001 From: Niklas Brachmann Date: Tue, 17 Sep 2013 22:48:49 +0200 Subject: [PATCH] Code cosmetics --- cash/management/commands/dailydigest.py | 45 +++++++++++++++++-------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/cash/management/commands/dailydigest.py b/cash/management/commands/dailydigest.py index 3a6eb1e..6ff383f 100644 --- a/cash/management/commands/dailydigest.py +++ b/cash/management/commands/dailydigest.py @@ -1,20 +1,21 @@ -from django.core.management.base import NoArgsCommand, CommandError from cash.models import * -from django.utils.translation import ugettext as _ -from django.template.loader import get_template +from django.conf import settings +from django.core.mail import send_mass_mail +from django.core.management.base import NoArgsCommand, CommandError from django.template import Context +from django.template.loader import get_template from django.utils import translation from django.utils.dateformat import DateFormat from django.utils.formats import get_format -from django.conf import settings -from django.core.mail import send_mass_mail +from django.utils.translation import ugettext as _ import datetime RANGE = 24 USERSETTINGS_URL = 'https://cypher/kasse/usersettings/' class Command(NoArgsCommand): - help = 'Sends out the daily digest to all users with transactions in the last %dh' % RANGE + help = 'Sends out the daily digest to all users with transactions' + \ + 'in the last %dh' % RANGE def handle_noargs(self, **options): translation.activate('de') @@ -23,29 +24,45 @@ class Command(NoArgsCommand): messages = [] for a in Account.objects.all(): - context = {'name': '%s %s' % (a.user.first_name, a.user.last_name), + name = '%s %s' % (a.user.first_name, a.user.last_name) + context = {'name': name, 'credit': a.credit, 'range': RANGE, 'url': USERSETTINGS_URL} - transactions = Transaction.objects.filter(account = a).filter(timestamp__gte = datetime.datetime.now() - datetime.timedelta(hours = RANGE)) + transactions = Transaction.objects.filter(account = a) \ + .filter(timestamp__gte=(datetime.datetime.now() + - datetime.timedelta(hours = RANGE))) if transactions.count() > 0: - lengths = {'timestamp': len(_('date')), 'description': len(_('subject')), 'amount': len(_('amount'))} + lengths = {'timestamp': len(_('date')), + 'description': len(_('subject')), + 'amount': len(_('amount'))} + sum = 0 for t in transactions: - lengths['timestamp'] = max(lengths['timestamp'], len(DateFormat(t.timestamp).format(get_format('SHORT_DATETIME_FORMAT')))) - lengths['description'] = max(lengths['description'], len(t.description)) - lengths['amount'] = max(lengths['amount'], len(str(t.amount))) + lengths['timestamp'] = \ + max(lengths['timestamp'], len(DateFormat(t.timestamp) \ + .format(get_format('SHORT_DATETIME_FORMAT')))) + lengths['description'] = \ + max(lengths['description'], len(t.description)) + lengths['amount'] = \ + max(lengths['amount'], len(str(t.amount))) t.description = t.description.split('\n') sum += t.amount - lengths['sum'] = lengths['timestamp'] + lengths['description'] + lengths['amount'] + lengths['sum'] = lengths['timestamp'] + \ + lengths['description'] + lengths['amount'] context['lengths'] = lengths context['tl'] = transactions context['sum'] = sum - messages.append(('%s%s' % (settings.EMAIL_SUBJECT_PREFIX, _('Account Statement')), tpl.render(Context(context)), settings.DEFAULT_FROM_EMAIL, ['%s %s <%s>' % (a.user.first_name, a.user.last_name, a.user.email)])) + rcpts = ['%s <%s>' % (name, a.user.email)] + + messages.append(('%s%s' % (settings.EMAIL_SUBJECT_PREFIX, + _('Account Statement')), + tpl.render(Context(context)), + settings.DEFAULT_FROM_EMAIL, rcpts)) send_mass_mail(tuple(messages))