mirror of
https://github.com/avatao-content/test-tutorial-framework
synced 2024-11-14 16:17:17 +00:00
Improve db Session logic
This commit is contained in:
parent
ccf348e5c5
commit
d4f4d5b288
@ -14,16 +14,17 @@ session_factory = sessionmaker(
|
|||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def Session():
|
def Session(factory=session_factory):
|
||||||
session = session_factory()
|
session = factory()
|
||||||
try:
|
try:
|
||||||
yield session
|
yield session
|
||||||
session.commit()
|
session.commit()
|
||||||
except:
|
except:
|
||||||
session.rollback()
|
session.rollback()
|
||||||
raise
|
raise
|
||||||
finally:
|
# session is closed by flask
|
||||||
session.close()
|
# finally:
|
||||||
|
# session.close()
|
||||||
|
|
||||||
|
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
from os import urandom, getenv
|
from os import urandom, getenv
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
from flask import Flask, render_template, request, session, url_for, g
|
from flask import Flask, render_template, request, session, url_for, g
|
||||||
|
|
||||||
from model import init_db, session_factory
|
from model import init_db, session_factory, Session
|
||||||
from user_ops import UserOps
|
from user_ops import UserOps
|
||||||
from errors import InvalidCredentialsError, UserExistsError
|
from errors import InvalidCredentialsError, UserExistsError
|
||||||
|
|
||||||
@ -20,6 +21,8 @@ def get_db_session():
|
|||||||
g.db_session = session_factory()
|
g.db_session = session_factory()
|
||||||
return g.db_session
|
return g.db_session
|
||||||
|
|
||||||
|
Session = partial(Session, get_db_session)
|
||||||
|
|
||||||
|
|
||||||
@app.teardown_appcontext
|
@app.teardown_appcontext
|
||||||
def close_db_session(err): # pylint: disable=unused-argument
|
def close_db_session(err): # pylint: disable=unused-argument
|
||||||
@ -31,9 +34,12 @@ def close_db_session(err): # pylint: disable=unused-argument
|
|||||||
def index():
|
def index():
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
try:
|
try:
|
||||||
UserOps(request.form.get('username'),
|
with Session() as db_session:
|
||||||
|
UserOps(
|
||||||
|
request.form.get('username'),
|
||||||
request.form.get('password'),
|
request.form.get('password'),
|
||||||
get_db_session()).authenticate()
|
db_session
|
||||||
|
).authenticate()
|
||||||
except InvalidCredentialsError:
|
except InvalidCredentialsError:
|
||||||
return render_template('login.html', alert='Invalid credentials!')
|
return render_template('login.html', alert='Invalid credentials!')
|
||||||
|
|
||||||
@ -61,10 +67,11 @@ def register():
|
|||||||
return render_template('register.html', alert='Passwords do not match! Please try again.')
|
return render_template('register.html', alert='Passwords do not match! Please try again.')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
with Session() as db_session:
|
||||||
UserOps(
|
UserOps(
|
||||||
request.form.get('username'),
|
request.form.get('username'),
|
||||||
request.form.get('password'),
|
request.form.get('password'),
|
||||||
get_db_session()
|
db_session
|
||||||
).register()
|
).register()
|
||||||
except UserExistsError:
|
except UserExistsError:
|
||||||
return render_template('register.html', alert='Username already in use.')
|
return render_template('register.html', alert='Username already in use.')
|
||||||
|
@ -62,6 +62,5 @@ class UserOps:
|
|||||||
passwordhash=PasswordHasher.hash(self.password)
|
passwordhash=PasswordHasher.hash(self.password)
|
||||||
)
|
)
|
||||||
self.db_session.add(user)
|
self.db_session.add(user)
|
||||||
self.db_session.commit()
|
|
||||||
|
|
||||||
self.log(f'User "{self.username}" registered!')
|
self.log(f'User "{self.username}" registered!')
|
||||||
|
Loading…
Reference in New Issue
Block a user