This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added email support to Tikker: sending an email with transaction over…
…view, dinner overview and high debt if necessary. Moved secret passwords and keys to environment variables Updated flask to newest version
- Loading branch information
Showing
16 changed files
with
381 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import socket | ||
from datetime import datetime, timedelta | ||
|
||
from flask import render_template, flash | ||
from flask_mail import Message | ||
from sqlalchemy import and_ | ||
|
||
from app import app, mail, dbhandler, db | ||
from app.models import User, Purchase, Transaction, Product, Upgrade | ||
|
||
enabled = True | ||
if app.config['MAIL_PASSWORD'] is '': | ||
enabled = False | ||
disable_reason = "Er is geen wachtwoord van de mailserver bekend" | ||
|
||
|
||
def set_default_lastoverview(): | ||
for u in User.query.all(): | ||
if u.lastoverview is None: | ||
u.lastoverview = datetime.strptime("2019-07-01", "%Y-%m-%d") | ||
db.session.commit() | ||
|
||
|
||
def test_debt_email(): | ||
emails = create_debt_emails([User.query.get(1)]) | ||
send_emails(emails) | ||
|
||
|
||
def test_dinner_overview_email(): | ||
emails = create_overview_dinner_emails([User.query.get(1)]) | ||
send_emails(emails) | ||
|
||
|
||
def test_overview_email(): | ||
emails = create_overview_emails([User.query.get(1)]) | ||
send_emails(emails) | ||
|
||
|
||
def send_debt_emails(): | ||
users = User.query.all() | ||
emails = create_debt_emails(users) | ||
try: | ||
send_emails(emails) | ||
flash("Emails succesvol verstuurd!", "success") | ||
dbhandler.update_settings('last_debt_email', datetime.now().strftime("%Y-%m-%d")) | ||
except socket.gaierror: | ||
flash("Kon geen verbinding maken met de KVLS Server. Weet je zeker dat de computer internet heeft?", "danger") | ||
|
||
|
||
def send_overview_emails(): | ||
users = User.query.all() | ||
begindate = datetime.strptime(dbhandler.settings['last_overview_email'], "%Y-%m-%d") | ||
# enddate = datetime.now().replace(day=1) | ||
enddate = datetime(year=2020, month=1, day=1) | ||
if enddate.weekday() >= 4: | ||
enddate += timedelta(days=7 - enddate.weekday()) | ||
|
||
emails = create_overview_dinner_emails(users, begindate, enddate) + create_overview_emails(users, begindate, enddate) + create_debt_emails(users) | ||
|
||
try: | ||
send_emails(emails) | ||
flash("Emails succesvol verstuurd!", "success") | ||
dbhandler.update_settings('last_overview_email', enddate.strftime("%Y-%m-%d")) | ||
dbhandler.update_settings('last_debt_email', enddate.strftime("%Y-%m-%d")) | ||
except socket.gaierror: | ||
flash("Kon geen verbinding maken met de KVLS Server. Weet je zeker dat de computer internet heeft?", "danger") | ||
|
||
|
||
def monthlist_fast(dates): | ||
# start, end = [datetime.strptime(_, "%Y-%m-%d") for _ in dates] | ||
start, end = dates[0], dates[1] | ||
total_months = lambda dt: dt.month + 12 * dt.year | ||
mlist = [] | ||
for tot_m in range(total_months(start) - 1, total_months(end)): | ||
y, m = divmod(tot_m, 12) | ||
mlist.append(datetime(y, m + 1, 1).strftime("%B")) | ||
del mlist[-1] | ||
|
||
months = "" | ||
for i in range(0, len(mlist)): | ||
if i == len(mlist) - 1: | ||
months += mlist[i] | ||
elif i == len(mlist) - 2: | ||
months += mlist[i] + " en " | ||
else: | ||
months += mlist[i] + ", " | ||
|
||
return months | ||
|
||
|
||
def create_debt_emails(users): | ||
emails = [] | ||
|
||
for u in users: | ||
if u.balance < app.config['DEBT_MAXIMUM']: | ||
result = {'html': render_template('email/debt.html', user=u), | ||
'body': render_template('email/debt.txt', user=u), | ||
'recipients': [u.email], 'subject': "Je hebt een te hoge schuld!"} | ||
emails.append(result) | ||
|
||
return emails | ||
|
||
|
||
def create_overview_dinner_emails(users, begindate, enddate): | ||
emails = [] | ||
|
||
for u in users: | ||
purchases = Purchase.query.filter( | ||
and_(Purchase.product_id == dbhandler.settings['dinner_product_id'], Purchase.user_id == u.id, | ||
Purchase.timestamp > begindate, Purchase.timestamp < enddate)).all() | ||
if len(purchases) > 0: | ||
|
||
months = monthlist_fast([begindate, enddate]) | ||
total = 0 | ||
for p in purchases: | ||
total += p.amount * p.price | ||
|
||
result = {'html': render_template('email/overview_dinner.html', user=u, purchases=purchases, total=total, | ||
months=months), | ||
'body': render_template('email/overview_dinner.txt', user=u, purchases=purchases, total=total, | ||
months=months), | ||
'recipients': [u.email], 'subject': "Maandelijks overzicht Stam Opkomstdiner {}".format(months)} | ||
emails.append(result) | ||
|
||
return emails | ||
|
||
|
||
def create_overview_emails(users, begindate, enddate): | ||
emails = [] | ||
|
||
for u in users: | ||
transactions = Transaction.query.filter(and_(Transaction.user_id == u.id, Transaction.timestamp > begindate, Transaction.timestamp < enddate)).all() | ||
if len(transactions) > 0: | ||
months = monthlist_fast([begindate, enddate]) | ||
|
||
result = {'html': render_template('email/overview.html', user=u, transactions=transactions, Product=Product, | ||
Purchase=Purchase, Upgrade=Upgrade, months=months), | ||
'body': render_template('email/overview.txt', user=u, transactions=transactions, Product=Product, | ||
Purchase=Purchase, Upgrade=Upgrade, months=months), | ||
'recipients': [u.email], 'subject': "Maandelijks overzicht transacties {}".format(months)} | ||
emails.append(result) | ||
|
||
return emails | ||
|
||
|
||
def send_emails(emails): | ||
with mail.connect() as conn: | ||
for e in emails: | ||
msg = Message(recipients=e['recipients'], html=e['html'], body=e['body'], subject=e['subject']) | ||
conn.send(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<p>Beste {{ user.name }},<br></p> | ||
<p>Volgens onze administratie heb je nog een grote openstaande schuld. | ||
Het gaat hierbij om een bedrag van:</p> | ||
<p><span style="color: red; font-weight: bold; font-size: 20px">€ {{ '%.2f' % user.balance }}</span></p> | ||
|
||
<p>Zou je deze schuld zo snel mogelijk willen betalen? | ||
Extra overgemaakt geld wordt als saldo op je Tikker account gezet. | ||
Betalen kan door over te maken naar rekeningnummer NL03RABO0333194322 t.n.v. Scouting Kornet van Limburg Stirum o.v.v. Opwaardering met je naam. | ||
Je kunt ook Hans vragen een Tikkie te sturen.</p> | ||
|
||
<p><br> | ||
<i>Met vriendelijke Scoutsgroet,</i><br> | ||
<b>Tikker</b><br><br> | ||
|
||
<b>Scouting Kornet van Limburg Stirum</b><br> | ||
Hooiberglaan 7 | 8121 RA | Olst <br> | ||
<a href="http://www.kvls.nl/" target="_blank">www.kvls.nl</a><br> | ||
<img border=0 width=83 height=96 src="https://www.kvls.nl/images/images/KVLS-Logo-modern-kleur.png"> | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Beste {{ user.name }}, | ||
|
||
Volgens onze administratie heb je nog een grote openstaande schuld. | ||
Het gaat hierbij om een bedrag van | ||
|
||
€ {{ '%.2f' % user.balance }} | ||
|
||
Zou je deze schuld zo snel mogelijk willen betalen? | ||
Extra overgemaakt geld wordt als saldo op je Tikker account gezet. | ||
Betalen kan door over te maken naar rekeningnummer NL03RABO0333194322 t.n.v. Scouting Kornet van Limburg Stirum o.v.v. Opwaardering met je naam. | ||
Je kunt ook Hans vragen een Tikkie te sturen. | ||
|
||
Met vriendelijke Scoutsgroet, | ||
Tikker |
Oops, something went wrong.