Skip to content

Commit 436c28f

Browse files
authored
Merge pull request #191 from rambo/velkoja
Add "velkoja" app for automatically sending notifications of overdue payments
2 parents da1790c + 6b27245 commit 436c28f

24 files changed

+282
-6
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ ENV LANGUAGE en_US:en
4848
# Install python requirements
4949
RUN virtualenv -p `which python3.4` ../asylum-venv
5050
COPY project/requirements /opt/asylum/requirements/
51-
RUN . ../asylum-venv/bin/activate && pip install -r requirements/local.txt
51+
RUN . ../asylum-venv/bin/activate && pip install packaging appdirs && pip install -r requirements/local.txt && chown -R asylum:asylum ../asylum-venv
5252

5353
# Configure application
5454
USER root

docker/dev.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ SCRIPTDIR=$(python -c 'import os,sys;print os.path.dirname(os.path.realpath(sys.
44
# Make sure we're in the correct place relative to Dockerfile no matter where we were called from
55
cd `dirname "$SCRIPTDIR"`
66

7-
if [ "$( docker images asylum_dev:latest )" == "" ]
7+
if [ "$( docker images -q asylum_dev:latest )" == "" ]
88
then
99
docker build -t asylum_dev .
1010
fi
@@ -15,7 +15,7 @@ if [ "$(docker ps -a -q -f name=asylum_dev)" == "" ]
1515
then
1616
echo "First run"
1717
set -x
18-
docker run --name asylum_dev -i -p 8000:8000 -p 1080:1080 -v `pwd -P`/project:/opt/asylum asylum_dev
18+
docker run --name asylum_dev -it -p 8000:8000 -p 1080:1080 -v `pwd -P`/project:/opt/asylum asylum_dev
1919
set +x
2020
else
2121
set -x

project/config/settings/common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
'rest_framework',
5353
'rest_framework.authtoken',
5454
'django_markdown',
55+
'django_jinja',
5556
)
5657

5758
# Apps specific for this project go here.
@@ -63,6 +64,7 @@
6364
'creditor',
6465
'ndaparser',
6566
'holviapp',
67+
'velkoja',
6668
)
6769

6870
# See: https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
@@ -266,6 +268,8 @@
266268
RECURRINGTRANSACTIONS_CALLBACKS_HANDLER = env('RECURRINGTRANSACTIONS_CALLBACKS_HANDLER', default=None)
267269
HOLVI_POOL = env('HOLVI_POOL', default=None)
268270
HOLVI_APIKEY = env('HOLVI_APIKEY', default=None)
271+
HOLVI_BARCODE_IBAN = env('HOLVI_BARCODE_IBAN', default=None)
272+
HOLVI_NOTIFICATION_INTERVAL_DAYS = env('HOLVI_NOTIFICATION_INTERVAL_DAYS', default=7)
269273

270274
REST_FRAMEWORK = {
271275
'DEFAULT_AUTHENTICATION_CLASSES': [

project/config/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
# Your stuff: custom urls includes go here
3838
url(r'^members/', include('members.urls')),
39+
url(r'^velkoja/', include('velkoja.urls')),
3940

4041
url(r'^api/', include(router.urls)),
4142
url(r'^api-auth/get-token/', authtoken_views.obtain_auth_token),

project/config/wsgi.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
environ.Env.read_env(str(ROOT_DIR + '.env'))
2828

2929

30-
3130
if env.bool('USE_SENTRY', False):
3231
from raven.contrib.django.raven_compat.middleware.wsgi import Sentry
3332

project/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"watch": "npm i && bower install && gulp watch",
77
"build": "npm i && bower install && gulp",
88
"build_noninteractive": "npm i && bower --config.interactive=false install && gulp",
9-
"fix": "PREFIX=\"# -*- coding: utf-8 -*-\"; find . -name '._*' | xargs rm ; for f in $(find . -name '*.py' -and -not -path '*/venv/*'); do echo \"=================\" ; echo $f; autopep8 -ri --max-line-length=10000 $f; flake8 $f; isort -rc $f; grep -qFe \"$PREFIX\" $f || echo \"$PREFIX\n$(cat $f)\" > $f ; done "
9+
"fix": "PREFIX=\"# -*- coding: utf-8 -*-\"; find . -name '._*' | xargs rm ; for f in $(find . -name '*.py' -and -not -path '*/migrations/*' -and -not -path '*/venv/*'); do echo \"=================\" ; echo $f; autopep8 -ri --max-line-length=10000 $f; flake8 $f; isort -rc $f; grep -qFe \"$PREFIX\" $f || echo \"$PREFIX\n$(cat $f)\" > $f ; done "
1010
},
1111
"repository": {
1212
"type": "git",

project/requirements/base.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ django-filter==0.11.0
3939
djangorestframework-filters==0.6.0
4040
django-markdown==0.8.4
4141
django-settings-export==1.0.6
42-
holviapi==0.1.20160123
42+
holviapi==0.2.20170129
4343
python-dateutil==2.4.2

project/velkoja/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding: utf-8 -*-

project/velkoja/admin.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# -*- coding: utf-8 -*-
2+
from django.contrib import admin
3+
4+
# Register your models here.

project/velkoja/holvichecker.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding: utf-8 -*-
2+
import logging
3+
from decimal import Decimal
4+
5+
from django.conf import settings
6+
from django.core.mail import EmailMessage
7+
from django.template import Context
8+
from django.template.loader import get_template
9+
from django.utils import timezone
10+
from holviapi.utils import barcode as bank_barcode
11+
from holviapp.utils import list_invoices
12+
13+
from .models import NotificationSent
14+
15+
logger = logger.getLogger()
16+
17+
18+
class HolviOverdueInvoicesHandler(object):
19+
def process_overdue(self, send=False):
20+
barcode_iban = settings.HOLVI_BARCODE_IBAN
21+
body_template = get_template('velkoja/notification_email_body.jinja')
22+
subject_template = get_template('velkoja/notification_email_subject.jinja')
23+
overdue = list_invoices(status='overdue')
24+
ret = []
25+
for invoice in overdue:
26+
# Quick check to make sure the invoice has not been credited
27+
if float(invoice._jsondata.get('credited_sum')) > 0:
28+
continue
29+
# If we have already sent notification recently, do not sent one just yet
30+
if NotificationSent.objects.filter(transaction_unique_id=invoice.code).count():
31+
notified = NotificationSent.objects.get(transaction_unique_id=invoice.code)
32+
if (timezone.now() - notified.stamp).days < settings.HOLVI_NOTIFICATION_INTERVAL_DAYS:
33+
continue
34+
35+
if send:
36+
invoice.send()
37+
38+
barcode = None
39+
if barcode_iban:
40+
barcode = bank_barcode(barcode_iban, invoice.rf_reference, Decimal(invoice.due_sum))
41+
42+
mail = EmailMessage()
43+
mail.subject = subject_template.render(Context({"invoice": invoice, "barcode": barcode})).strip()
44+
mail.body = body_template.render(Context({"invoice": invoice, "barcode": barcode}))
45+
mail.to = [invoice.receiver.email]
46+
if send:
47+
try:
48+
mail.send()
49+
except Exception as e:
50+
logger.exception("Sending email failed")
51+
52+
try:
53+
notified = NotificationSent.objects.get(transaction_unique_id=invoice.code)
54+
notified.notification_no += 1
55+
except NotificationSent.DoesNotExist:
56+
notified = NotificationSent()
57+
notified.transaction_unique_id = invoice.code
58+
notified.stamp = timezone.now()
59+
notified.email = invoice.receiver.email
60+
if send:
61+
notified.save()
62+
ret.append((notified, invoice))
63+
return ret

0 commit comments

Comments
 (0)