Merge branch 'multi-deployment' of github:b3yond/ticketfrei into multi-deployment

This commit is contained in:
b3yond 2018-09-01 14:01:18 +02:00
commit ad4e65e0fa
6 changed files with 71 additions and 5 deletions

36
db.py
View file

@ -135,6 +135,14 @@ class DB(object):
''') ''')
def user_token(self, email, password): 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({ return jwt.encode({
'email': email, 'email': email,
'passhash': scrypt_mcf( 'passhash': scrypt_mcf(
@ -142,6 +150,25 @@ class DB(object):
).decode('ascii') ).decode('ascii')
}, self.secret).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): def confirm(self, token, city):
from user import User from user import User
try: try:
@ -196,6 +223,15 @@ u\d\d?
return None return None
return User(uid) return User(uid)
def by_city(self, city):
from user import User
self.execute("SELECT user_id FROM cities WHERE city=?", (city, ))
try:
uid, = self.cur.fetchone()
except TypeError:
return None
return User(uid)
def user_facing_properties(self, city): def user_facing_properties(self, city):
self.execute("""SELECT city, markdown, masto_link, twit_link self.execute("""SELECT city, markdown, masto_link, twit_link
FROM cities FROM cities

View file

@ -89,14 +89,29 @@ def city_page(city):
@get('/city/mail/<city>') @get('/city/mail/<city>')
@view('template/mail.tpl') @view('template/mail.tpl')
def display_mail_page(city, user): def display_mail_page(city):
user = db.by_city(city)
return user.state() return user.state()
@post('/city/mail/submit/<city>') @post('/city/mail/submit/<city>')
def subscribe_mail(user, city): def subscribe_mail(city):
email = request.forms['mailaddress'] email = request.forms['mailaddress']
# add confirmation mail workflow 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/<token>')
@view('template/city.tpl')
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) user.add_subscriber(email)
redirect('/city/' + city) redirect('/city/' + city)

View file

@ -6,7 +6,7 @@ import markdown as md
html = md.markdown(markdown) 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 type="text" name="mailaddress" placeholder="E-Mail address" id="mailaddress">
<input name='confirm' value='Subscribe to E-Mail notifications' type='submit'/> <input name='confirm' value='Subscribe to E-Mail notifications' type='submit'/>
</form> </form>

View file

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

View file

@ -1,3 +1,5 @@
<!DOCTYPE html>
<html lang="en">
<head> <head>
<title>Ticketfrei - {{get('title', 'A bot against control society!')}}</title> <title>Ticketfrei - {{get('title', 'A bot against control society!')}}</title>
<meta name='og:title' content='Ticketfrei'/> <meta name='og:title' content='Ticketfrei'/>
@ -25,3 +27,4 @@
<p>Contribute on <a href="https://github.com/b3yond/ticketfrei">GitHub!</a></p> <p>Contribute on <a href="https://github.com/b3yond/ticketfrei">GitHub!</a></p>
</div> </div>
</body> </body>
</html>

11
user.py
View file

@ -74,6 +74,7 @@ slut
hure hure
jude jude
schwuchtel schwuchtel
schlampe
fag fag
faggot faggot
nigger nigger
@ -116,6 +117,7 @@ schlitz
def save_seen_toot(self, toot_id): def save_seen_toot(self, toot_id):
db.execute("UPDATE seen_toots SET toot_id = ? WHERE user_id = ?;", db.execute("UPDATE seen_toots SET toot_id = ? WHERE user_id = ?;",
(toot_id, self.uid)) (toot_id, self.uid))
db.commit()
def get_seen_tweet(self): def get_seen_tweet(self):
db.execute("SELECT tweet_id FROM seen_tweets WHERE user_id = ?;", db.execute("SELECT tweet_id FROM seen_tweets WHERE user_id = ?;",
@ -125,6 +127,7 @@ schlitz
def save_seen_tweet(self, tweet_id): def save_seen_tweet(self, tweet_id):
db.execute("UPDATE seen_tweets SET tweet_id = ? WHERE user_id = ?;", db.execute("UPDATE seen_tweets SET tweet_id = ? WHERE user_id = ?;",
(tweet_id, self.uid)) (tweet_id, self.uid))
db.commit()
def get_seen_dm(self): def get_seen_dm(self):
db.execute("SELECT message_id FROM seen_dms WHERE user_id = ?;", db.execute("SELECT message_id FROM seen_dms WHERE user_id = ?;",
@ -134,6 +137,7 @@ schlitz
def save_seen_dm(self, tweet_id): def save_seen_dm(self, tweet_id):
db.execute("UPDATE seen_dms SET message_id = ? WHERE user_id = ?;", db.execute("UPDATE seen_dms SET message_id = ? WHERE user_id = ?;",
(tweet_id, self.uid)) (tweet_id, self.uid))
db.commit()
def get_mailinglist(self): def get_mailinglist(self):
db.execute("SELECT email FROM mailinglist WHERE user_id = ? AND active = 1;", (self.uid, )) 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): def save_seen_mail(self, mail_date):
db.execute("UPDATE seen_mail SET mail_date = ? WHERE user_id = ?;", db.execute("UPDATE seen_mail SET mail_date = ? WHERE user_id = ?;",
(mail_date, self.uid)) (mail_date, self.uid))
db.commit()
def set_trigger_words(self, patterns): def set_trigger_words(self, patterns):
db.execute("UPDATE triggerpatterns SET patterns = ? WHERE user_id = ?;", db.execute("UPDATE triggerpatterns SET patterns = ? WHERE user_id = ?;",
(patterns, self.uid)) (patterns, self.uid))
db.commit()
def get_trigger_words(self): def get_trigger_words(self):
db.execute("SELECT patterns FROM triggerpatterns WHERE user_id = ?;", db.execute("SELECT patterns FROM triggerpatterns WHERE user_id = ?;",
@ -157,11 +163,13 @@ schlitz
return db.cur.fetchone()[0] return db.cur.fetchone()[0]
def add_subscriber(self, email): 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): def set_badwords(self, words):
db.execute("UPDATE badwords SET words = ? WHERE user_id = ?;", db.execute("UPDATE badwords SET words = ? WHERE user_id = ?;",
(words, self.uid)) (words, self.uid))
db.commit()
def get_badwords(self): def get_badwords(self):
db.execute("SELECT words FROM badwords WHERE user_id = ?;", db.execute("SELECT words FROM badwords WHERE user_id = ?;",
@ -237,6 +245,7 @@ schlitz
def set_markdown(self, markdown): def set_markdown(self, markdown):
db.execute("UPDATE cities SET markdown = ? WHERE user_id = ?;", db.execute("UPDATE cities SET markdown = ? WHERE user_id = ?;",
(markdown, self.uid)) (markdown, self.uid))
db.commit()
def get_city(self): def get_city(self):
db.execute("SELECT city FROM cities WHERE user_id == ?;", (self.uid, )) db.execute("SELECT city FROM cities WHERE user_id == ?;", (self.uid, ))