Skip to content

Commit

Permalink
fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
brassy-endomorph committed Sep 17, 2024
1 parent 2e2d065 commit dd6a833
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
4 changes: 2 additions & 2 deletions dev_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
13 changes: 11 additions & 2 deletions hushline/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion hushline/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
10 changes: 6 additions & 4 deletions hushline/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -260,14 +260,14 @@ 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(
db.session.scalars(
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
Expand Down Expand Up @@ -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"))

Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions tests/auth_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit dd6a833

Please sign in to comment.