diff --git a/dev_data.py b/dev_data.py index b33233c5..0ff3448f 100755 --- a/dev_data.py +++ b/dev_data.py @@ -31,13 +31,13 @@ def main() -> None: un1 = Username( user_id=user.id, - _username=data["username"], + _username=data["username"], # type: ignore is_primary=True, show_in_directory=True, ) un2 = Username( user_id=user.id, - _username=data["username"] + "-alias", + _username=data["username"] + "-alias", # type: ignore is_primary=False, show_in_directory=True, ) diff --git a/hushline/model.py b/hushline/model.py index 096ce077..63d89a2d 100644 --- a/hushline/model.py +++ b/hushline/model.py @@ -68,6 +68,16 @@ class Username(Model): extra_field_verified3: Mapped[Optional[bool]] = mapped_column(default=False) extra_field_verified4: Mapped[Optional[bool]] = mapped_column(default=False) + def __init__( + self, + _username: str, + is_primary: bool, + **kwargs: Any, + ) -> None: + super().__init__(**kwargs) + self._username = _username + self.is_primary = is_primary + @property def username(self) -> str: return self._username @@ -258,10 +268,9 @@ class Message(Model): username_id: Mapped[int] = mapped_column(db.ForeignKey("usernames.id")) username: Mapped["Username"] = relationship(uselist=False) - def __init__(self, **kwargs: Any) -> None: + def __init__(self, content: str, **kwargs: Any) -> None: if "_content" in kwargs: raise ValueError("Cannot set '_content' directly. Use 'content'") - content = kwargs.pop("content", None) super().__init__(**kwargs) self.content = content diff --git a/hushline/routes.py b/hushline/routes.py index fe760025..f2b50f01 100644 --- a/hushline/routes.py +++ b/hushline/routes.py @@ -364,7 +364,7 @@ def register() -> Response | str | tuple[Response | str, int]: db.session.add(user) db.session.flush() - username = Username(username=username, user_id=user.id, is_primary=True) + username = Username(_username=username, user_id=user.id, is_primary=True) db.session.add(username) db.session.commit() diff --git a/hushline/settings/__init__.py b/hushline/settings/__init__.py index 92eed2ae..5e383710 100644 --- a/hushline/settings/__init__.py +++ b/hushline/settings/__init__.py @@ -246,7 +246,7 @@ async def index() -> str | Response: ).all() # Additional admin-specific data initialization user_count = two_fa_count = pgp_key_count = two_fa_percentage = pgp_key_percentage = None - all_users = [] + all_users: list[User] = [] # Check if user is admin and add admin-specific data if user.is_admin: @@ -260,7 +260,6 @@ async def index() -> str | Response: .filter(User._pgp_key != "") ) ) - user_count = len(all_users) two_fa_percentage = (two_fa_count / user_count * 100) if user_count else 0 pgp_key_percentage = (pgp_key_count / user_count * 100) if user_count else 0 all_users = list( @@ -268,6 +267,7 @@ async def index() -> str | Response: db.select(User).join(Username).order_by(Username._username) ).all() ) + user_count = len(all_users) # Prepopulate form fields email_forwarding_form.forwarding_enabled.data = user.email is not None @@ -343,7 +343,9 @@ def change_password() -> str | Response: flash("New password is invalid.") return redirect(url_for("settings.index")) - if not user.check_password(change_password_form.old_password.data): + if not change_password_form.old_password.data or not user.check_password( + change_password_form.old_password.data + ): flash("Incorrect old password.", "error") return redirect(url_for("settings.index")) @@ -503,7 +505,7 @@ def update_pgp_key() -> Response | str: if form.validate_on_submit(): pgp_key = form.pgp_key.data - if pgp_key.strip() == "": + if pgp_key is None or pgp_key.strip() == "": # If the field is empty, remove the PGP key user.pgp_key = None user.email = None # remove the forwarding email if the PGP key is removed diff --git a/tests/auth_helper.py b/tests/auth_helper.py index 6f589f3b..1b5d47b5 100644 --- a/tests/auth_helper.py +++ b/tests/auth_helper.py @@ -91,10 +91,8 @@ def login_user(client: FlaskClient, username: str, password: str) -> User | None f'href="/inbox?username={username}"'.encode() in response.data ), f"Inbox link should be present for the user {username}" - if username := db.session.scalars( - db.select(Username).filter_by(_username=username) - ).one_or_none(): - return username.user + if uname := db.session.scalars(db.select(Username).filter_by(_username=username)).one_or_none(): + return uname.user return None