Rollback on error
This commit is contained in:
parent
55514664f0
commit
1074cf0e0a
|
@ -1,7 +1,8 @@
|
|||
from flask import Blueprint, render_template, g, session, redirect, request, url_for, flash
|
||||
from flask import Blueprint, render_template, g, session, redirect, request, url_for, flash, current_app, abort
|
||||
import secrets
|
||||
from datetime import datetime
|
||||
import re
|
||||
from sqlalchemy import func
|
||||
|
||||
from ..models import *
|
||||
from ..markdown import *
|
||||
|
@ -12,18 +13,20 @@ blacklisted_titles = ["new", "by-id"]
|
|||
allowed_titles = re.compile(r"^[a-zA-Z0-9 \(\)\.\,:;]+$")
|
||||
|
||||
def urlify(title):
|
||||
return title[0].upper() + title[1:].replace(" ", "_")
|
||||
return title.replace(" ", "_")
|
||||
|
||||
|
||||
def unurlify(title):
|
||||
return title.replace("_", " ").lower()
|
||||
return title.replace("_", " ")
|
||||
|
||||
|
||||
@article_provider.route("/<title>")
|
||||
def show(title):
|
||||
real_title = unurlify(title)
|
||||
print(real_title)
|
||||
article = Article.query.filter_by(title=real_title).first_or_404()
|
||||
article = Article.query.filter(
|
||||
func.lower(Article.title) == real_title.lower()).first_or_404()
|
||||
if article.title != real_title:
|
||||
return redirect(url_for(".show", title=urlify(article.title)), code=302)
|
||||
revision = ArticleRevision.query.filter_by(article_id=article.id).order_by(
|
||||
ArticleRevision.date.desc()).first()
|
||||
toc, content = md_render(revision.content)
|
||||
|
@ -44,7 +47,7 @@ def new():
|
|||
if request.method == "GET":
|
||||
return render_template("article_new.html", csrf=csrf, article_title="", article_content="")
|
||||
else:
|
||||
article_title = request.form["article_title"].strip().lower()
|
||||
article_title = request.form["article_title"].strip()
|
||||
article_content = request.form["article_content"]
|
||||
|
||||
if request.form["csrf"] != old_csrf:
|
||||
|
@ -71,7 +74,12 @@ def new():
|
|||
user=g.wl_user,
|
||||
changelog="Created article " + article_title,
|
||||
content=article_content))
|
||||
db.session.add(article)
|
||||
db.session.commit()
|
||||
try:
|
||||
db.session.add(article)
|
||||
db.session.commit()
|
||||
except:
|
||||
db.session.rollback()
|
||||
current_app.logger.exception("Failed to commit article")
|
||||
abort(503)
|
||||
session.pop("csrf", None)
|
||||
return redirect(url_for(".show", title=urlify(article_title)), code=302)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from flask import Blueprint, session, request, render_template, redirect, g, flash
|
||||
from flask import Blueprint, session, request, render_template, redirect, g, flash, current_app, abort
|
||||
import secrets
|
||||
|
||||
from ..models import User, db
|
||||
|
@ -39,9 +39,14 @@ def login():
|
|||
return render_template("login_form.html", csrf=csrf)
|
||||
user = User.query.filter_by(username=request.form["username"]).first()
|
||||
if not user:
|
||||
db.session.add(User(username=request.form["username"],
|
||||
email=secrets.token_hex(8) + "@example.com"))
|
||||
db.session.commit()
|
||||
try:
|
||||
db.session.add(User(username=request.form["username"],
|
||||
email=secrets.token_hex(8) + "@example.com"))
|
||||
db.session.commit()
|
||||
except:
|
||||
db.session.rollback()
|
||||
current_app.logger.exception("Failed to commit user")
|
||||
abort(503)
|
||||
session["username"] = request.form["username"]
|
||||
session.pop("csrf", None)
|
||||
session.pop("redirect", None)
|
||||
|
|
Loading…
Reference in New Issue