From cc0b3378a9579e7608803e6cbea082ee63b740da Mon Sep 17 00:00:00 2001 From: b3yond Date: Wed, 8 Aug 2018 10:24:20 +0200 Subject: [PATCH] you can now subscribe to mail notifications! Also db bugfixes. --- db.py | 27 +++++++++++++++++++++++++++ frontend.py | 14 ++++++++++---- template/mail.tpl | 2 +- template/settings.tpl | 3 +++ user.py | 11 ++++++++++- 5 files changed, 51 insertions(+), 6 deletions(-) diff --git a/db.py b/db.py index 9409fe9..03f59a6 100644 --- a/db.py +++ b/db.py @@ -135,6 +135,14 @@ class DB(object): ''') def user_token(self, email, password): + """ + This function is called by the register confirmation process. It wants + to write an email to the email table and a passhash to the user table. + + :param email: a string with an E-Mail address. + :param password: a string with a passhash. + :return: + """ return jwt.encode({ 'email': email, 'passhash': scrypt_mcf( @@ -142,6 +150,25 @@ class DB(object): ).decode('ascii') }, self.secret).decode('ascii') + def mail_subscription_token(self, email, city): + """ + This function is called by the mail subscription process. It wants + to write an email to the mailinglist table. + + :param email: string + :param city: string + :return: a token with an encoded json dict { email: x, city: y } + """ + return jwt.encode({ + 'email': email, + 'city': city + }, self.secret).decode('ascii') + + def confirm_subscription(self, token): + json = jwt.decode(token, self.secret) + return json['email'], json['city'] + + def confirm(self, token, city): from user import User try: diff --git a/frontend.py b/frontend.py index badd203..980fae1 100755 --- a/frontend.py +++ b/frontend.py @@ -97,14 +97,20 @@ def display_mail_page(city): @post('/city/mail/submit/') def subscribe_mail(city): email = request.forms['mailaddress'] - code = generate_code(email, city) + token = db.mail_subscription_token(email, city) + confirm_link = url('city/mail/confirm/' + token) + print(confirm_link) # only for local testing # send mail with code to email + sendmail(email, "Subscribe to Ticketfrei " + city + " Mail Notifications", + body="To subscribe to the mail notifications for Ticketfrei " + city + ", click on this link: " + token) -@get('/city/mail/confirm/') +@get('/city/mail/confirm/') @view('template/city.tpl') -def confirm_subscribe(code): - email, city = parse_code(code) +def confirm_subscribe(token): + email, city = db.confirm_subscription(token) + print(email) # debug + print(city) # debug user = db.by_city(city) user.add_subscriber(email) redirect('/city/' + city) diff --git a/template/mail.tpl b/template/mail.tpl index 2b16981..66c0e86 100644 --- a/template/mail.tpl +++ b/template/mail.tpl @@ -6,7 +6,7 @@ import markdown as md html = md.markdown(markdown) %> -
+
diff --git a/template/settings.tpl b/template/settings.tpl index 1510030..371d66e 100644 --- a/template/settings.tpl +++ b/template/settings.tpl @@ -65,6 +65,9 @@ +<% +# todo: hide this part, if there is already a telegram bot connected. +%>

Connect with Telegram

diff --git a/user.py b/user.py index ae4a82c..232a754 100644 --- a/user.py +++ b/user.py @@ -74,6 +74,7 @@ slut hure jude schwuchtel +schlampe fag faggot nigger @@ -116,6 +117,7 @@ schlitz def save_seen_toot(self, toot_id): db.execute("UPDATE seen_toots SET toot_id = ? WHERE user_id = ?;", (toot_id, self.uid)) + db.commit() def get_seen_tweet(self): db.execute("SELECT tweet_id FROM seen_tweets WHERE user_id = ?;", @@ -125,6 +127,7 @@ schlitz def save_seen_tweet(self, tweet_id): db.execute("UPDATE seen_tweets SET tweet_id = ? WHERE user_id = ?;", (tweet_id, self.uid)) + db.commit() def get_seen_dm(self): db.execute("SELECT message_id FROM seen_dms WHERE user_id = ?;", @@ -134,6 +137,7 @@ schlitz def save_seen_dm(self, tweet_id): db.execute("UPDATE seen_dms SET message_id = ? WHERE user_id = ?;", (tweet_id, self.uid)) + db.commit() def get_mailinglist(self): db.execute("SELECT email FROM mailinglist WHERE user_id = ? AND active = 1;", (self.uid, )) @@ -146,10 +150,12 @@ schlitz def save_seen_mail(self, mail_date): db.execute("UPDATE seen_mail SET mail_date = ? WHERE user_id = ?;", (mail_date, self.uid)) + db.commit() def set_trigger_words(self, patterns): db.execute("UPDATE triggerpatterns SET patterns = ? WHERE user_id = ?;", (patterns, self.uid)) + db.commit() def get_trigger_words(self): db.execute("SELECT patterns FROM triggerpatterns WHERE user_id = ?;", @@ -157,11 +163,13 @@ schlitz return db.cur.fetchone()[0] def add_subscriber(self, email): - db.execute("INSERT INTO mailinglist(user_id, email, active VALUES(?, ?, ?);", (self.uid, email, 1)) + db.execute("INSERT INTO mailinglist(user_id, email, active) VALUES(?, ?, ?);", (self.uid, email, 1)) + db.commit() def set_badwords(self, words): db.execute("UPDATE badwords SET words = ? WHERE user_id = ?;", (words, self.uid)) + db.commit() def get_badwords(self): db.execute("SELECT words FROM badwords WHERE user_id = ?;", @@ -237,6 +245,7 @@ schlitz def set_markdown(self, markdown): db.execute("UPDATE cities SET markdown = ? WHERE user_id = ?;", (markdown, self.uid)) + db.commit() def get_city(self): db.execute("SELECT city FROM cities WHERE user_id == ?;", (self.uid, ))