diff --git a/app/database.py b/app/database.py index b2422d3..d452465 100644 --- a/app/database.py +++ b/app/database.py @@ -7,6 +7,7 @@ from consumption import Consumption from deposit import Deposit import random as rand import datetime +from settings import settings DATABASE = 'test/database.db' @@ -56,6 +57,7 @@ def get_user(u): u.isblack=row[6] u.isbaron=row[7] u.isshown=row[8] + u.autoblack=row[9] print u return u @@ -73,6 +75,7 @@ def get_user_by_name(name): u.isblack=row[6] u.isbaron=row[7] u.isshown=row[8] + u.autoblack=row[9] print u return u @@ -108,6 +111,7 @@ def get_users(): u.isblack=row[6] u.isbaron=row[7] u.isshown=row[8] + u.autoblack=row[9] users.append(u) return users @@ -119,7 +123,7 @@ def add_user(u): def update_user(u): #query_db("UPDATE users SET (NAME, LONGNAME, EMAIL, RFID_ID, ISBLACK, ISBARON, ISSHOWN) VALUES (?, ?, ?, ?, ?, ?, ?) WHERE ID=?", (u.name, u.longname, u.email, u.rfid_id, u.isblack, u.isbaron, u.isshown, u.id)) - query_db("UPDATE users SET NAME=?, LONGNAME=?, EMAIL=?, RFID_ID=?, ISBLACK=?, ISBARON=?, ISSHOWN=? WHERE ID=?", (u.name, u.longname, u.email, u.rfid_id, u.isblack, u.isbaron, u.isshown, u.id)) + query_db("UPDATE users SET NAME=?, LONGNAME=?, EMAIL=?, RFID_ID=?, ISBLACK=?, ISBARON=?, ISSHOWN=?, AUTOBLACK=? WHERE ID=?", (u.name, u.longname, u.email, u.rfid_id, u.isblack, u.isbaron, u.isshown, u.autoblack, u.id)) get_db().commit() @@ -199,6 +203,12 @@ def add_consume(username, productid): #INSERT INTO USERS (NAME, PASSWORD, LONGNAME, EMAIL, RFID_ID) VALUES (? ,? ,?, ?, ?)", (u.name, u.password, u.longname, u.email, u.rfid_id)) query_db("INSERT INTO CONSUMED (PRODNR, CONSUMER, PRICE, TIME) VALUES (?, ?, ?, ?)", (str(product.id), str(consumerid), product.price, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) get_db().commit() + + if settings.autoBlack: + if get_debt(name=username) > settings.blockLimit: + u = get_user_by_name(username) + u.isblack = True + update_user(u) print "consumed" return @@ -213,6 +223,8 @@ def get_debt(name=None): deposits = get_deposits(get_user_by_name(name).id) for deposit in deposits: debt -= deposit.amount + + debt = round(debt, 2) return debt @@ -234,6 +246,21 @@ def get_deposits(userid = None): deposits.append(d) return deposits +def add_deposit(username, amount): + consumerid = query_db("SELECT ID FROM USERS WHERE NAME = ?", [username], one=True) + consumerid = int(consumerid[0]) + query_db("INSERT INTO DEPOSITS (USERID, AMOUNT, TIME) VALUES (?, ?, ?)", (str(consumerid), amount, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) + get_db().commit() + if settings.autoUnblack: + if get_debt(name=username) < settings.blockLimit: + u = get_user_by_name(username) + u.isblack = False + update_user(u) + print "deposit" + + return + + ##for testing only def generate_test_users(): @@ -267,4 +294,4 @@ def add_test_consume(consumerid, productid, daysago): product = get_product_by_id(productid) query_db("INSERT INTO CONSUMED (PRODNR, CONSUMER, PRICE, TIME) VALUES (?, ?, ?, ?)", (str(product.id), str(consumerid), product.price, (datetime.datetime.now()-datetime.timedelta(days=daysago)).strftime("%Y-%m-%d %H:%M:%S"))) get_db().commit() - return \ No newline at end of file + return diff --git a/app/gui.py b/app/gui.py index 0caef94..5af9a9e 100644 --- a/app/gui.py +++ b/app/gui.py @@ -274,7 +274,7 @@ class PanelUsers (wx.Panel): self.but.SetBackgroundColour((255-(i*20 % 40), (160+(i*50 % 100)), 0)) #self.but.SetForegroundColour("#006699") - self.but.SetPressColor(wx.Color(255,255,255,0)) + self.but.SetPressColor(wx.Colour(255,255,255,0)) self.but.SetFont(wx.Font(25, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "Humor Sans")) self.but.Bind(wx.EVT_LEFT_DOWN, parent.onUser, id=self.but.Id) self.but_names.append(self.but) diff --git a/app/send_email.py b/app/send_email.py new file mode 100644 index 0000000..fa1ae48 --- /dev/null +++ b/app/send_email.py @@ -0,0 +1,59 @@ +import smtplib +import database +# import email +# from email.mime.text import MIMEText +# from email.MIMEText import MIMEText +import user + +def send_email(recipient, subject, body): + gmail_user = 'bier1baroness' + gmail_pwd = 'test11test11' + FROM = 'bier1baroness@gmail.com' + SUBJECT = subject + TEXT = body + + # Prepare actual message + message = u"Content-Type: text/plain; charset=utf-8\nFrom: Baroness <%s>\nTo: %s\nSubject: %s\n\n%s" % (FROM, recipient, SUBJECT, TEXT) + message = message.encode('utf-8') + + # message = msg.as_string() + print message + try: + server = smtplib.SMTP("smtp.gmail.com:587") + #server.set_debuglevel(1) + server.starttls() + server.login(gmail_user, gmail_pwd) + server.sendmail(FROM, recipient, message) + server.quit() + print 'Mail was sent to %s' % recipient + + except: + print "Failed to send mail to %s" %recipient + +def send_emails(body, subject, users): + FROM = 'bier1baroness@gmail.com' + + for user in users: + debt = get_debt(user.name) + subject_parsed = parse_email(subject, user, debt) + body_parsed = parse_email(body, user, debt) + send_email(user.email, subject_parsed, body_parsed) + + + + +def parse_email(text, u, dept): + + text = text.replace('%%longname%%', u.longname) + text = text.replace('%%dept%%', str(dept)) + + if text.find('%%if_is_black%%', 0, text.__len__()): + start = text.find('%%if_is_black%%', 0, text.__len__()) + end = text.find('%%end_if_is_black%%') + + if not u.isblack: + text = text.replace(text[start:end], '') + text = text.replace('%%if_is_black%%', '') + text = text.replace('%%end_if_is_black%%', '') + + return text diff --git a/app/settings.py b/app/settings.py index 4200cc8..864cb2d 100644 --- a/app/settings.py +++ b/app/settings.py @@ -32,8 +32,8 @@ class Settings: ##Payment Incentives #Money Limit - self.autoBlock = False #Automatically block user if money owed > limit - self.autoUnblock = False #Automatically unblock user if money owed < limit + self.autoBlack = True #Automatically block user if money owed > limit + self.autoUnblack = True #Automatically unblock user if money owed < limit self.blockLimit = 150 #Money limit for automatic blocking (default 150) #Mail Spam self.autoAnnoy = False #Automatically send payment eMails diff --git a/app/templates/billing.html b/app/templates/billing.html index 248c1d7..4fed4f7 100644 --- a/app/templates/billing.html +++ b/app/templates/billing.html @@ -5,11 +5,13 @@

{{ success }}

{% endif %}

Abrechnung

+

An alle User Rechnungen versenden.

+ @@ -18,6 +20,7 @@ + @@ -26,5 +29,4 @@
Name GeschwärztAuto-Schwärzen Schulden Bezahlt
{{user.longname}} {% if user.isblack %} ☑ {% else %} ☐ {% endif %}{% if user.autoblack %} ☑ {% else %} ☐ {% endif %} {{debt[user.id-1]}} € Einzelne Rechnung versenden
-

An alle User Rechnungen versenden.

{% endblock %} \ No newline at end of file diff --git a/app/templates/billing_mass_mail.html b/app/templates/billing_mass_mail.html index 8605953..99dc2ed 100644 --- a/app/templates/billing_mass_mail.html +++ b/app/templates/billing_mass_mail.html @@ -1,23 +1,34 @@ {% extends "base.html"%} -{% set title = "An alle User Rechnung versenden" %} +{% set title = "An alle User Rechnungen versenden" %} {% block content %}

TODO:

{% if success %}

{{ success }}

{% endif %}

Rechnung an alle verschicken

- +du hast %%dept%%€ Schulden. +Bitte überweise diesen Betrag an das Konto: +%%if_is_black%%Weil du zu hohe Schulden hast, bist du geschwärzt%%end_if_is_black%% +
+

Warnung: Das versenden vieler emails dauert ein weilchen.

+ +

Kommandos

+

Der text den du eingibst wird von der Baroness noch personalisiert. Folgende Komandos kennt die Baroness.

+ {% endblock %} \ No newline at end of file diff --git a/app/templates/billing_personal.html b/app/templates/billing_personal.html index da741d7..1bf3a59 100644 --- a/app/templates/billing_personal.html +++ b/app/templates/billing_personal.html @@ -11,13 +11,20 @@ {% endif %}

Rechnung an {{user_to_bill.longname}} verschicken

- +du hast zurzeit {{dept}} € Schulden. +{% if user_to_bill.isblack %}Weil du zu hohe Schulden hast, bist du geschwärzt.{% endif %} +Bitte überweise den Betrag an das Konto: + Peter Schleter; + IBAN; BIC; +dein Baron, +{{user.longname}} +

An alle User Rechnungen versenden.

+ {% endblock %} \ No newline at end of file diff --git a/app/templates/manage_users.html b/app/templates/manage_users.html index ce7f793..5316382 100644 --- a/app/templates/manage_users.html +++ b/app/templates/manage_users.html @@ -12,6 +12,7 @@ Geschwärzt Baron Angezeigt + Auto-Schwärzen {% for user in users %} @@ -24,6 +25,7 @@ {% if user.isblack %} ☑ {% else %} ☐ {% endif %} {% if user.isbaron %} ☑ {% else %} ☐ {% endif %} {% if user.isshown %} ☑ {% else %} ☐ {% endif %} + {% if user.autoblack %} ☑ {% else %} ☐ {% endif %} bearbeiten {% endfor %} diff --git a/app/templates/manage_users_edit.html b/app/templates/manage_users_edit.html index 8200be1..0868f60 100644 --- a/app/templates/manage_users_edit.html +++ b/app/templates/manage_users_edit.html @@ -17,6 +17,7 @@ Geschwärzt:
Baron:
Angezeigt:
+ Auto-Schwärzen:
{% endif %} diff --git a/app/user.py b/app/user.py index f6ba854..b3080a3 100644 --- a/app/user.py +++ b/app/user.py @@ -10,6 +10,7 @@ class User: self.isblack=False self.isbaron=False self.isshown=False + self.autoblack=False def __str__(self): @@ -68,4 +69,12 @@ class User: else: s = "%s, True" % s + if self.autoblack is None: + s = "%s, None" % s + else: + if self.isshown is 0 or self. isshown is False: + s = "%s, False" % s + else: + s = "%s, True" % s + return s diff --git a/app/views.py b/app/views.py index 3a92d4c..c69e379 100644 --- a/app/views.py +++ b/app/views.py @@ -5,6 +5,7 @@ from database import * from plot import * from user import User from product import Product +from send_email import send_email, send_emails from consumption import Consumption import bcrypt import os @@ -134,6 +135,12 @@ def manage_users_edit(name=None): else: u.isshown = False + if 'autoblack' in request.form: + u.autoblack = True + else: + u.autoblack = False + + update_user(u) return redirect('/manage_users') @@ -261,8 +268,22 @@ def personal(): @requires_baron def billing(): users = get_users() + if request.method == 'POST': - return render_template('billing.html', users=users, success="Not Implemented", dept=0, user=get_user_by_name(session.get('name'))) + for user in users: + formname = "%s_payed" % user.name + if formname in request.form: + # add payment here + payment = float(request.form[formname]) + if payment != 0: + add_deposit(user.name, payment) + print "%s payed %d" % (user.name, payment) + debt = [0 for user in users] + users = get_users() # refresh users for correct viewing of autounblacking + for user in users: + debt[user.id-1] = get_debt(user.name) + + return render_template('billing.html', users=users, success="Writing to database is not implemented", debt=debt, user=get_user_by_name(session.get('name'))) if request.method == 'GET': debt = [0 for user in users] for user in users: @@ -270,22 +291,41 @@ def billing(): return render_template('billing.html', users=users, debt=debt, user=get_user_by_name(session.get('name'))) -@app.route('/billing/send_personal_bill/', methods=['GET','POST']) +@app.route('/billing/send_personal_bill/', methods=['GET', 'POST']) @requires_baron def send_personal_bill(name=None): if request.method == 'POST': - return "To be implemented" - #return redirect('/billing') + users = get_users() + u = get_user_by_name(name) + message = request.form['message'] + subject = request.form['subject'] + send_email(u.email, subject, message) + + success = "Die Rechnung wurde an %s versendet." %u.longname + return render_template('billing.html', users=users, success=success, dept=0, user=get_user_by_name(session.get('name'))) if request.method == 'GET': - return render_template('billing_personal.html', user_to_bill=get_user_by_name(name) ,user=get_user_by_name(session.get('name'))) + return render_template('billing_personal.html', user_to_bill=get_user_by_name(name), dept=get_debt(name), user=get_user_by_name(session.get('name'))) -@app.route('/billing/send_all_bills', methods=['GET','POST']) +@app.route('/billing/send_all_bills', methods=['GET', 'POST']) @requires_baron def send_mass_mail(name=None): if request.method == 'POST': - return "To be implemented" + users = get_users() + u = get_user_by_name(name) + message = request.form['message'] + subject = request.form['subject'] + + send_emails(message, subject, users) + #for user in users: + # message_parsed = parse_email(message, user, 3) # change ammount of depts here + # subject_parsed = parse_email(subject, user, 3) # change ammount of depts here! + # send_email(user.email, subject_parsed, message_parsed) + + success = "An alle user werden Rechnungen versendet." + return render_template('billing.html', users=users, success=success, dept=0, user=get_user_by_name(session.get('name'))) + if request.method == 'GET': return render_template('billing_mass_mail.html', user=get_user_by_name(session.get('name'))) diff --git a/doc/database.sql b/doc/database.sql index b4f09d2..0111873 100644 --- a/doc/database.sql +++ b/doc/database.sql @@ -39,7 +39,8 @@ CREATE TABLE Users( rfid_id TEXT, isblack BOOLEAN DEFAULT 0, isbaron BOOLEAN DEFAULT 0, - isshown BOOLEAN DEFAULT 1 + isshown BOOLEAN DEFAULT 1, + autoblack BOOLEAN DEFAULT 1 ); -- The table PRODUCTS contains information about the beverages available diff --git a/test/database.db b/test/database.db index cfbeef2..b0b2db7 100644 Binary files a/test/database.db and b/test/database.db differ