Simplify some backend names

This commit is contained in:
Tim Van Baak 2021-06-16 20:10:09 -07:00
parent 0e35f15a3a
commit 398b5705f1
5 changed files with 16 additions and 15 deletions

View File

@ -55,6 +55,6 @@ def create(
return new_lexicon return new_lexicon
def get_all_lexicons(db: DbContext) -> Sequence[Lexicon]: def get_all(db: DbContext) -> Sequence[Lexicon]:
"""Get all lexicons.""" """Get all lexicons."""
return db(select(Lexicon)).scalars() return db(select(Lexicon)).scalars()

View File

@ -21,7 +21,7 @@ def create(
db: DbContext, db: DbContext,
username: str, username: str,
password: str, password: str,
display_name: str, display_name: Optional[str],
email: str, email: str,
is_site_admin: bool, is_site_admin: bool,
) -> User: ) -> User:
@ -71,12 +71,7 @@ def create(
return new_user return new_user
def get_all_users(db: DbContext) -> Sequence[User]: def from_id(db: DbContext, user_id: int) -> Optional[User]:
"""Get all users."""
return db(select(User)).scalars()
def get_user_by_id(db: DbContext, user_id: int) -> Optional[User]:
""" """
Get a user by the user's id. Get a user by the user's id.
Returns None if no user was found. Returns None if no user was found.
@ -85,7 +80,7 @@ def get_user_by_id(db: DbContext, user_id: int) -> Optional[User]:
return user return user
def get_user_by_username(db: DbContext, username: str) -> Optional[User]: def from_username(db: DbContext, username: str) -> Optional[User]:
""" """
Get a user by the user's username. Get a user by the user's username.
Returns None if no user was found. Returns None if no user was found.
@ -94,6 +89,11 @@ def get_user_by_username(db: DbContext, username: str) -> Optional[User]:
return user return user
def get_all(db: DbContext) -> Sequence[User]:
"""Get all users."""
return db(select(User)).scalars()
def password_set(db: DbContext, username: str, new_password: str) -> None: def password_set(db: DbContext, username: str, new_password: str) -> None:
"""Set a user's password.""" """Set a user's password."""
password_hash = generate_password_hash(new_password) password_hash = generate_password_hash(new_password)

View File

@ -21,6 +21,7 @@ def command_create(args) -> int:
db: DbContext = args.get_db() db: DbContext = args.get_db()
userq.create(db, args.username, "password", args.username, args.email, False) userq.create(db, args.username, "password", args.username, args.email, False)
userq.password_set(db, args.username, args.password) userq.password_set(db, args.username, args.password)
LOG.info(f"Created user {args.username}")
return 0 return 0
@ -28,7 +29,7 @@ def command_create(args) -> int:
def command_promote(args) -> int: def command_promote(args) -> int:
"""Make a user a site admin.""" """Make a user a site admin."""
db: DbContext = args.get_db() db: DbContext = args.get_db()
user: Optional[User] = userq.get_user_by_username(db, args.username) user: Optional[User] = userq.from_username(db, args.username)
if user is None: if user is None:
args.parser.error("User not found") args.parser.error("User not found")
return -1 return -1
@ -45,7 +46,7 @@ def command_promote(args) -> int:
def command_demote(args): def command_demote(args):
"""Revoke a user's site admin status.""" """Revoke a user's site admin status."""
db: DbContext = args.get_db() db: DbContext = args.get_db()
user: Optional[User] = userq.get_user_by_username(db, args.username) user: Optional[User] = userq.from_username(db, args.username)
if user is None: if user is None:
args.parser.error("User not found") args.parser.error("User not found")
return -1 return -1

View File

@ -39,7 +39,7 @@ def get_login_manager() -> LoginManager:
user_id = int(user_id_str) user_id = int(user_id_str)
except: except:
return None return None
return userq.get_user_by_id(g.db, user_id) return userq.from_id(g.db, user_id)
login_manager.user_loader(load_user) login_manager.user_loader(load_user)
@ -58,7 +58,7 @@ def login():
# POST with valid data # POST with valid data
username: str = form.username.data username: str = form.username.data
password: str = form.password.data password: str = form.password.data
user: User = userq.get_user_by_username(g.db, username) user: User = userq.from_username(g.db, username)
if not user or not userq.password_check(g.db, username, password): if not user or not userq.password_check(g.db, username, password):
# Bad creds # Bad creds
flash("Login not recognized") flash("Login not recognized")

View File

@ -10,11 +10,11 @@
{% block main %} {% block main %}
<p>Users:</p> <p>Users:</p>
{% for user in userq.get_all_users(db) %} {% for user in userq.get_all(db) %}
{{ macros.dashboard_user_item(user) }} {{ macros.dashboard_user_item(user) }}
{% endfor %} {% endfor %}
<p>Lexicons:</p> <p>Lexicons:</p>
{% for lexicon in lexiq.get_all_lexicons(db) %} {% for lexicon in lexiq.get_all(db) %}
{{ macros.dashboard_lexicon_item(lexicon) }} {{ macros.dashboard_lexicon_item(lexicon) }}
{% endfor %} {% endfor %}
{% endblock %} {% endblock %}