You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

75 lines
3.0 KiB

from cashonly.core.models import *
from django.conf import settings
from django.core.mail import send_mass_mail
from django.core.management.base import BaseCommand, 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.utils.translation import ugettext as _
import datetime
class Command(BaseCommand):
help = 'Sends out a digest of transactions to recently active users'
def handle(self, **options):
translation.activate('de')
tpl = get_template('cashonly/core/daily_digest.txt')
messages = []
for a in Account.objects.filter(daily_digest=True):
name = '%s %s' % (a.user.first_name, a.user.last_name)
context = {
'name': name,
'credit': a.credit,
'range': settings.CASHONLY_DAILY_DIGEST_RANGE_HOURS,
'url': settings.CASHONLY_USERSETTINGS_URL,
}
min_ts = datetime.datetime.now() - datetime.timedelta(
hours=settings.CASHONLY_DAILY_DIGEST_RANGE_HOURS
)
transactions = Transaction.objects.filter(account=a) \
.filter(timestamp__gte=min_ts)
if transactions.count() > 0:
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)))
t.description = t.description.split('\n')
sum += t.amount
lengths['sum'] = lengths['timestamp'] + \
lengths['description'] + lengths['amount']
context['lengths'] = lengths
context['tl'] = transactions
context['sum'] = sum
if a.user.email is not None and len(a.user.email) > 0:
rcpts = ['%s <%s>' % (name, a.user.email)]
else:
self.stdout.write(self.style.WARNING(
'User "%s" has no Email address.' % (a.user.username)
))
continue
messages.append(('%s%s' % (settings.EMAIL_SUBJECT_PREFIX,
_('Account Statement')),
tpl.render(context),
settings.DEFAULT_FROM_EMAIL, rcpts))
send_mass_mail(tuple(messages))