informaniac
1 year ago
24 changed files with 793 additions and 549 deletions
@ -1,75 +1,90 @@
@@ -1,75 +1,90 @@
|
||||
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.core.management.base import BaseCommand |
||||
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 _ |
||||
from django.utils.translation import gettext as _ |
||||
import datetime |
||||
|
||||
|
||||
class Command(BaseCommand): |
||||
help = 'Sends out a digest of transactions to recently active users' |
||||
help = "Sends out a digest of transactions to recently active users" |
||||
|
||||
def handle(self, **options): |
||||
translation.activate('de') |
||||
translation.activate("de") |
||||
|
||||
tpl = get_template('cashonly/core/daily_digest.txt') |
||||
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) |
||||
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, |
||||
"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) |
||||
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'))} |
||||
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') |
||||
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 |
||||
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)] |
||||
rcpts = ["%s <%s>" % (name, a.user.email)] |
||||
else: |
||||
self.stdout.write(self.style.WARNING( |
||||
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')), |
||||
messages.append( |
||||
( |
||||
"%s%s" |
||||
% (settings.EMAIL_SUBJECT_PREFIX, _("Account Statement")), |
||||
tpl.render(context), |
||||
settings.DEFAULT_FROM_EMAIL, rcpts)) |
||||
settings.DEFAULT_FROM_EMAIL, |
||||
rcpts, |
||||
) |
||||
) |
||||
|
||||
send_mass_mail(tuple(messages)) |
||||
|
@ -1,33 +1,36 @@
@@ -1,33 +1,36 @@
|
||||
from django.conf.urls import url |
||||
from django.urls import re_path |
||||
from cashonly.web import views |
||||
|
||||
urlpatterns = [ |
||||
url(r'^$', views.overview, name='overview'), |
||||
|
||||
url(r'^product/(?P<pk>\d+)/$', views.ProductView.as_view(), |
||||
name='product'), |
||||
|
||||
url(r'transactions/$', views.transactions, {'detailed': False, 'page': 1}, |
||||
name='transactions'), |
||||
|
||||
url(r'transactions/(?P<page>\d+)/$', views.transactions, |
||||
{'detailed': False}, name='transactions'), |
||||
|
||||
url(r'transactions/(?P<page>\d+)/detailed/$', views.transactions, |
||||
{'detailed': True}, name='transactions_detailed'), |
||||
|
||||
url(r'products/((?P<category_id>\d+)/)?$', views.products, |
||||
name='products'), |
||||
|
||||
url(r'buy/(?P<product_id>\d+)/$', views.buy, name='buy'), |
||||
|
||||
url(r'buy/(?P<product_id>\d+)/really/$', views.buy, |
||||
{'confirm': True}, name='buy_really'), |
||||
|
||||
url(r'buy/thanks/$', views.buy_thanks, name='buy_thanks'), |
||||
|
||||
url(r'buy/error/$', views.buy_error, name='buy_error'), |
||||
|
||||
url(r'usersettings(/(?P<submit>\w+))?/$', views.usersettings, |
||||
name='usersettings'), |
||||
re_path(r"^$", views.overview, name="overview"), |
||||
re_path(r"^product/(?P<pk>\d+)/$", views.ProductView.as_view(), name="product"), |
||||
re_path( |
||||
r"transactions/$", |
||||
views.transactions, |
||||
{"detailed": False, "page": 1}, |
||||
name="transactions", |
||||
), |
||||
re_path( |
||||
r"transactions/(?P<page>\d+)/$", |
||||
views.transactions, |
||||
{"detailed": False}, |
||||
name="transactions", |
||||
), |
||||
re_path( |
||||
r"transactions/(?P<page>\d+)/detailed/$", |
||||
views.transactions, |
||||
{"detailed": True}, |
||||
name="transactions_detailed", |
||||
), |
||||
re_path(r"products/((?P<category_id>\d+)/)?$", views.products, name="products"), |
||||
re_path(r"buy/(?P<product_id>\d+)/$", views.buy, name="buy"), |
||||
re_path( |
||||
r"buy/(?P<product_id>\d+)/really/$", |
||||
views.buy, |
||||
{"confirm": True}, |
||||
name="buy_really", |
||||
), |
||||
re_path(r"buy/thanks/$", views.buy_thanks, name="buy_thanks"), |
||||
re_path(r"buy/error/$", views.buy_error, name="buy_error"), |
||||
re_path(r"usersettings(/(?P<submit>\w+))?/$", views.usersettings, name="usersettings"), |
||||
] |
||||
|
Loading…
Reference in new issue