This commit is contained in:
Yax 2022-04-26 20:51:42 +02:00
parent 5a286fa0d2
commit 325455439a
18 changed files with 261 additions and 459 deletions

View file

@ -12,12 +12,12 @@ logger = logging.getLogger(__name__)
class Mailer:
def __init__(
self,
smtp_host,
smtp_port,
smtp_login,
smtp_password,
site_admin_email,
self,
smtp_host,
smtp_port,
smtp_login,
smtp_password,
site_admin_email,
):
self._smtp_host = smtp_host
self._smtp_port = smtp_port
@ -35,7 +35,9 @@ class Mailer:
msg["From"] = sender
context = ssl.create_default_context()
with smtplib.SMTP_SSL(self._smtp_host, self._smtp_port, context=context) as server:
with smtplib.SMTP_SSL(
self._smtp_host, self._smtp_port, context=context
) as server:
server.login(self._smtp_login, self._smtp_password)
server.send_message(msg, sender, receivers)
return True

View file

@ -11,12 +11,12 @@ from stacosys.model.comment import Comment
class Rss:
def __init__(
self,
lang,
rss_file,
rss_proto,
site_name,
site_url,
self,
lang,
rss_file,
rss_proto,
site_name,
site_url,
):
self._lang = lang
self._rss_file = rss_file
@ -29,16 +29,16 @@ class Rss:
items = []
for row in (
Comment.select()
.where(Comment.published)
.order_by(-Comment.published)
.limit(10)
Comment.select()
.where(Comment.published)
.order_by(-Comment.published)
.limit(10)
):
item_link = "%s://%s%s" % (self._rss_proto, self._site_url, row.url)
items.append(
PyRSS2Gen.RSSItem(
title="%s - %s://%s%s"
% (self._rss_proto, row.author_name, self._site_url, row.url),
% (self._rss_proto, row.author_name, self._site_url, row.url),
link=item_link,
description=md.convert(row.content),
guid=PyRSS2Gen.Guid("%s/%d" % (item_link, row.id)),

View file

@ -34,14 +34,21 @@ def find_not_published_comments():
def find_published_comments_by_url(url):
return Comment.select(Comment).where((Comment.url == url) & (Comment.published.is_null(False))).order_by(
+Comment.published)
return (
Comment.select(Comment)
.where((Comment.url == url) & (Comment.published.is_null(False)))
.order_by(+Comment.published)
)
def count_published_comments(url):
return Comment.select(Comment).where(
(Comment.url == url) & (Comment.published.is_null(False))).count() if url else Comment.select(Comment).where(
Comment.published.is_null(False)).count()
return (
Comment.select(Comment)
.where((Comment.url == url) & (Comment.published.is_null(False)))
.count()
if url
else Comment.select(Comment).where(Comment.published.is_null(False)).count()
)
def create_comment(url, author_name, author_site, author_gravatar, message):

View file

@ -17,6 +17,7 @@ def setup(db_url):
db.connect()
from stacosys.model.comment import Comment
db.create_tables([Comment], safe=True)

View file

@ -39,7 +39,9 @@ def new_form_comment():
abort(400)
# add a row to Comment table
comment = dao.create_comment(url, author_name, author_site, author_gravatar, message)
comment = dao.create_comment(
url, author_name, author_site, author_gravatar, message
)
# send notification e-mail asynchronously
submit_new_comment(comment)

View file

@ -17,45 +17,51 @@ app.add_url_rule("/web/", endpoint="index")
@app.endpoint("index")
def index():
return redirect('/web/admin')
return redirect("/web/admin")
def is_login_ok(username, password):
hashed = hashlib.sha256(password.encode()).hexdigest().upper()
return app.config.get("WEB_USERNAME") == username and app.config.get("WEB_PASSWORD") == hashed
return (
app.config.get("WEB_USERNAME") == username
and app.config.get("WEB_PASSWORD") == hashed
)
@app.route('/web/login', methods=['POST', 'GET'])
@app.route("/web/login", methods=["POST", "GET"])
def login():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
if is_login_ok(username, password):
session['user'] = username
return redirect('/web/admin')
session["user"] = username
return redirect("/web/admin")
# TODO localization
flash("Identifiant ou mot de passe incorrect")
return redirect('/web/login')
return redirect("/web/login")
# GET
return render_template("login_" + app.config.get("LANG") + ".html")
@app.route('/web/logout', methods=["GET"])
@app.route("/web/logout", methods=["GET"])
def logout():
session.pop('user')
return redirect('/web/admin')
session.pop("user")
return redirect("/web/admin")
@app.route("/web/admin", methods=["GET"])
def admin_homepage():
if not ('user' in session and session['user'] == app.config.get("WEB_USERNAME")):
if not ("user" in session and session["user"] == app.config.get("WEB_USERNAME")):
# TODO localization
flash("Vous avez été déconnecté.")
return redirect('/web/login')
return redirect("/web/login")
comments = dao.find_not_published_comments()
return render_template("admin_" + app.config.get("LANG") + ".html", comments=comments,
baseurl=app.config.get("SITE_URL"))
return render_template(
"admin_" + app.config.get("LANG") + ".html",
comments=comments,
baseurl=app.config.get("SITE_URL"),
)
@app.route("/web/admin", methods=["POST"])
@ -73,4 +79,4 @@ def admin_action():
dao.delete_comment(comment)
# TODO localization
flash("Commentaire supprimé")
return redirect('/web/admin')
return redirect("/web/admin")