you can now subscribe to mail notifications! Also db bugfixes.

This commit is contained in:
b3yond 2018-08-08 10:24:20 +02:00
parent d002969377
commit cc0b3378a9
5 changed files with 51 additions and 6 deletions

27
db.py
View file

@ -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:

View file

@ -97,14 +97,20 @@ def display_mail_page(city):
@post('/city/mail/submit/<city>')
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/<code>')
@get('/city/mail/confirm/<token>')
@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)

View file

@ -6,7 +6,7 @@ import markdown as md
html = md.markdown(markdown)
%>
<form action="/city/mail/submit/<% print(city) %>" method="post">
<form action="/city/mail/submit/{{!city}}" method="post">
<input type="text" name="mailaddress" placeholder="E-Mail address" id="mailaddress">
<input name='confirm' value='Subscribe to E-Mail notifications' type='submit'/>
</form>

View file

@ -65,6 +65,9 @@
</form>
</section>
<%
# todo: hide this part, if there is already a telegram bot connected.
%>
<div>
<h2>Connect with Telegram</h2>
<p>

11
user.py
View file

@ -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, ))