mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-15 23:38:52 -04:00
Refactored the forms so that fields like email_address can be used in multiple forms. Refactored form validation so that a query function is passed into the form to be run, this way the form is not exposed to the dao layer and the query is more efficient. This PR still requires some frontend attention. Will work with Chris to update the templates.
71 lines
1.6 KiB
Python
71 lines
1.6 KiB
Python
from datetime import datetime
|
|
|
|
from sqlalchemy.orm import load_only
|
|
|
|
from app import db, login_manager
|
|
from app.models import User
|
|
from app.main.encryption import hashpw
|
|
|
|
|
|
@login_manager.user_loader
|
|
def load_user(user_id):
|
|
return get_user_by_id(user_id)
|
|
|
|
|
|
def insert_user(user):
|
|
user.password = hashpw(user.password)
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
|
|
|
|
# TODO Would be better to have a generic get and update for user
|
|
# something that replicates the sql functionality.
|
|
def get_user_by_id(id):
|
|
return User.query.filter_by(id=id).first()
|
|
|
|
|
|
def get_all_users():
|
|
return User.query.all()
|
|
|
|
|
|
def get_user_by_email(email_address):
|
|
return User.query.filter_by(email_address=email_address).first()
|
|
|
|
|
|
def increment_failed_login_count(id):
|
|
user = User.query.filter_by(id=id).first()
|
|
user.failed_login_count += 1
|
|
db.session.commit()
|
|
|
|
|
|
def activate_user(id):
|
|
user = get_user_by_id(id)
|
|
user.state = 'active'
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
|
|
|
|
def update_email_address(id, email_address):
|
|
user = get_user_by_id(id)
|
|
user.email_address = email_address
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
|
|
|
|
def update_mobile_number(id, mobile_number):
|
|
user = get_user_by_id(id)
|
|
user.mobile_number = mobile_number
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
|
|
|
|
def update_password(user, password):
|
|
user.password = hashpw(password)
|
|
user.password_changed_at = datetime.now()
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
|
|
|
|
def find_all_email_address():
|
|
return [x.email_address for x in User.query.options(load_only("email_address")).all()]
|