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
def get_all_lexicons(db: DbContext) -> Sequence[Lexicon]:
def get_all(db: DbContext) -> Sequence[Lexicon]:
"""Get all lexicons."""
return db(select(Lexicon)).scalars()

View File

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

View File

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

View File

@ -39,7 +39,7 @@ def get_login_manager() -> LoginManager:
user_id = int(user_id_str)
except:
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)
@ -58,7 +58,7 @@ def login():
# POST with valid data
username: str = form.username.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):
# Bad creds
flash("Login not recognized")

View File

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