Use SQLAlchemy Session.get instead of filter_by

master
Pierre Jaury 9 years ago
parent 3eca6864c3
commit d3b13c2412

@ -13,7 +13,7 @@ from freeposte.admin import models
# Register the login components # Register the login components
login_manager.login_view = "admin.login" login_manager.login_view = "admin.login"
login_manager.user_loader(models.User.get_by_email) login_manager.user_loader(models.User.query.get)
@app.context_processor @app.context_processor
def inject_user(): def inject_user():

@ -70,10 +70,6 @@ class Email(Base):
primary_key=True, nullable=False, primary_key=True, nullable=False,
default=updater) default=updater)
@classmethod
def get_by_email(cls, email):
return cls.query.filter_by(email=email).first()
def __str__(self): def __str__(self):
return self.email return self.email
@ -134,7 +130,7 @@ class User(Email):
@classmethod @classmethod
def login(cls, email, password): def login(cls, email, password):
user = cls.get_by_email(email) user = cls.query.get(email)
return user if (user and user.check_password(password)) else None return user if (user and user.check_password(password)) else None

@ -5,7 +5,7 @@ import flask
def get_domain_admin(domain_name): def get_domain_admin(domain_name):
domain = models.Domain.query.filter_by(name=domain_name).first() domain = models.Domain.query.get(domain_name)
if not domain: if not domain:
flask.abort(404) flask.abort(404)
if not domain in flask_login.current_user.get_managed_domains(): if not domain in flask_login.current_user.get_managed_domains():
@ -22,7 +22,7 @@ def get_user(user_email, admin=False):
if user_email is None: if user_email is None:
user = flask_login.current_user user = flask_login.current_user
else: else:
user = models.User.get_by_email(user_email) user = models.User.query.get(user_email)
if not user: if not user:
flask.abort(404) flask.abort(404)
if not user.domain in flask_login.current_user.get_managed_domains(): if not user.domain in flask_login.current_user.get_managed_domains():
@ -34,7 +34,7 @@ def get_user(user_email, admin=False):
def get_alias(alias): def get_alias(alias):
alias = models.Alias.get_by_email(alias) alias = models.Alias.query.get(alias)
if not alias: if not alias:
flask.abort(404) flask.abort(404)
if not alias.domain in flask_login.current_user.get_managed_domains(): if not alias.domain in flask_login.current_user.get_managed_domains():
@ -43,7 +43,7 @@ def get_alias(alias):
def get_fetch(fetch_id): def get_fetch(fetch_id):
fetch = models.Fetch.query.filter_by(id=fetch_id).first() fetch = models.Fetch.query.get(fetch_id)
if not fetch: if not fetch:
flask.abort(404) flask.abort(404)
if not fetch.user.domain in flask_login.current_user.get_managed_domains(): if not fetch.user.domain in flask_login.current_user.get_managed_domains():

@ -19,7 +19,7 @@ def admin_list():
def admin_create(): def admin_create():
form = forms.AdminForm() form = forms.AdminForm()
if form.validate_on_submit(): if form.validate_on_submit():
user = models.User.query.filter_by(email=form.admin.data).first() user = models.User.query.get(form.admin.data)
if user: if user:
user.global_admin = True user.global_admin = True
db.session.add(user) db.session.add(user)
@ -34,7 +34,7 @@ def admin_create():
@app.route('/admin/delete/<admin>', methods=['GET']) @app.route('/admin/delete/<admin>', methods=['GET'])
@flask_login.login_required @flask_login.login_required
def admin_delete(admin): def admin_delete(admin):
user = models.User.query.filter_by(email=admin).first() user = models.User.query.get(admin)
if user: if user:
user.global_admin = False user.global_admin = False
db.session.add(user) db.session.add(user)

@ -18,7 +18,7 @@ def domain_create():
utils.require_global_admin() utils.require_global_admin()
form = forms.DomainForm() form = forms.DomainForm()
if form.validate_on_submit(): if form.validate_on_submit():
if models.Domain.query.filter_by(name=form.name.data).first(): if models.Domain.query.get(form.name.data):
flask.flash('Domain %s is already used' % form.name.data, 'error') flask.flash('Domain %s is already used' % form.name.data, 'error')
else: else:
domain = models.Domain(name=form.name.data) domain = models.Domain(name=form.name.data)

Loading…
Cancel
Save