wrote unsubscribe function, but BE & FE have different secrets
parent
da7ead65fa
commit
882d086a83
|
@ -7,7 +7,8 @@ import mailbox
|
||||||
import email
|
import email
|
||||||
import report
|
import report
|
||||||
from bot import Bot
|
from bot import Bot
|
||||||
|
from config import config
|
||||||
|
from db import db
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -28,8 +29,9 @@ class Mailbot(Bot):
|
||||||
recipients = user.get_mailinglist()
|
recipients = user.get_mailinglist()
|
||||||
for rec in recipients:
|
for rec in recipients:
|
||||||
rec = rec[0]
|
rec = rec[0]
|
||||||
unsubscribe_link = "" # todo: generate unsubscribe link
|
unsubscribe_text = "\n_______\nYou don't want to receive those messages? Unsubscribe with this link: "
|
||||||
body = report.text + unsubscribe_link
|
body = report.text + unsubscribe_text + config['web']['host'] + "/city/mail/unsubscribe/" \
|
||||||
|
+ db.mail_subscription_token(rec, user.get_city())
|
||||||
print(body)
|
print(body)
|
||||||
if report.author != rec:
|
if report.author != rec:
|
||||||
try:
|
try:
|
||||||
|
|
9
db.py
9
db.py
|
@ -165,12 +165,19 @@ class DB(object):
|
||||||
:param city: string
|
:param city: string
|
||||||
:return: a token with an encoded json dict { email: x, city: y }
|
:return: a token with an encoded json dict { email: x, city: y }
|
||||||
"""
|
"""
|
||||||
return jwt.encode({
|
token = jwt.encode({
|
||||||
'email': email,
|
'email': email,
|
||||||
'city': city
|
'city': city
|
||||||
}, self.secret).decode('ascii')
|
}, self.secret).decode('ascii')
|
||||||
|
print("mail_subscription_token")
|
||||||
|
print(token)
|
||||||
|
print(self.secret)
|
||||||
|
return token
|
||||||
|
|
||||||
def confirm_subscription(self, token):
|
def confirm_subscription(self, token):
|
||||||
|
print("confirm_subscription")
|
||||||
|
print(token)
|
||||||
|
print(self.secret)
|
||||||
json = jwt.decode(token, self.secret)
|
json = jwt.decode(token, self.secret)
|
||||||
return json['email'], json['city']
|
return json['email'], json['city']
|
||||||
|
|
||||||
|
|
11
frontend.py
11
frontend.py
|
@ -109,13 +109,20 @@ def subscribe_mail(city):
|
||||||
@view('template/city.tpl')
|
@view('template/city.tpl')
|
||||||
def confirm_subscribe(token):
|
def confirm_subscribe(token):
|
||||||
email, city = db.confirm_subscription(token)
|
email, city = db.confirm_subscription(token)
|
||||||
print(email) # debug
|
|
||||||
print(city) # debug
|
|
||||||
user = db.by_city(city)
|
user = db.by_city(city)
|
||||||
user.add_subscriber(email)
|
user.add_subscriber(email)
|
||||||
redirect('/city/' + city)
|
redirect('/city/' + city)
|
||||||
|
|
||||||
|
|
||||||
|
@get('/city/mail/unsubscribe/<token>')
|
||||||
|
@view('template/mail.tpl')
|
||||||
|
def unsubscribe(token):
|
||||||
|
email, city = db.confirm_subscription(token)
|
||||||
|
user = db.by_city(city)
|
||||||
|
user.remove_subscriber(email)
|
||||||
|
redirect('/city/' + city)
|
||||||
|
|
||||||
|
|
||||||
@get('/settings')
|
@get('/settings')
|
||||||
@view('template/settings.tpl')
|
@view('template/settings.tpl')
|
||||||
def settings(user):
|
def settings(user):
|
||||||
|
|
4
user.py
4
user.py
|
@ -166,6 +166,10 @@ schlitz
|
||||||
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()
|
db.commit()
|
||||||
|
|
||||||
|
def remove_subscriber(self, email):
|
||||||
|
db.execute("UPDATE mailinglist SET active = 0 WHERE email = ? AND user_id = ?;", (email, self.uid))
|
||||||
|
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))
|
||||||
|
|
Loading…
Reference in New Issue