Skip to content

Commit

Permalink
Merge pull request #260 from SFTtech/milo/misc-fixes
Browse files Browse the repository at this point in the history
misc fixes
  • Loading branch information
mikonse authored Jan 31, 2025
2 parents 5551560 + 65b6f7f commit 7d4c3ab
Show file tree
Hide file tree
Showing 26 changed files with 254 additions and 162 deletions.
20 changes: 11 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,32 @@ test:

.PHONY: format
format:
isort .
black .
ruff format
cd frontend && npx prettier --write . && cd ..

.PHONY: check-format
check-format:
isort --check-only .
black --check .
ruff format --check

.PHONY: check-format-frontend
check-format-frontend:
cd frontend && npx prettier --check . && cd ..

.PHONY: lint
lint: pylint mypy
lint: pylint mypy ruff

.PHONY: pylint
pylint:
pylint ./**/*.py

.PHONY: ruff
ruff:
ruff check

.PHONY: ruff-fix
ruff-fix:
ruff check --fix

.PHONY: eslint
eslint:
cd frontend && npx nx lint && cd ..
Expand All @@ -32,10 +38,6 @@ eslint:
mypy:
mypy .

.PHONY: package
package:
flit build

.PHONY: docs
docs:
$(MAKE) -C docs html
Expand Down
13 changes: 11 additions & 2 deletions abrechnung/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
logger = logging.getLogger(__name__)


async def create_user(config: Config, name: str, email: str, skip_email_check: bool, no_email_confirmation: bool):
async def create_user(
config: Config,
name: str,
email: str,
skip_email_check: bool,
no_email_confirmation: bool,
):
logger.info(f"Creating user with email: {email} and username: {name}")
password = getpass(prompt="Input initial password for user:")
repeat_password = getpass(prompt="Repeat password:")
Expand All @@ -23,5 +29,8 @@ async def create_user(config: Config, name: str, email: str, skip_email_check: b
if skip_email_check:
user_service.valid_email_domains = None
await user_service.register_user( # pylint: disable=missing-kwoa
username=name, email=email, password=password, requires_email_confirmation=not no_email_confirmation
username=name,
email=email,
password=password,
requires_email_confirmation=not no_email_confirmation,
)
7 changes: 0 additions & 7 deletions abrechnung/application/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
from typing import Optional

import asyncpg
from asyncpg.pool import Pool

from abrechnung.config import Config
from abrechnung.domain.users import User
25 changes: 12 additions & 13 deletions abrechnung/application/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ async def _check_account_permissions(
account_id,
)
if not result:
raise InvalidArgument(f"account not found")
raise InvalidArgument("account not found")

if can_write and not (result["can_write"] or result["is_owner"]):
raise PermissionError(f"user does not have write permissions")
raise PermissionError("user does not have write permissions")

if account_type:
type_check = [account_type] if isinstance(account_type, str) else account_type
Expand Down Expand Up @@ -120,8 +120,7 @@ async def _get_or_create_pending_account_change(self, conn: asyncpg.Connection,
@staticmethod
async def _check_account_exists(conn: asyncpg.Connection, group_id: int, account_id: int) -> int:
acc = await conn.fetchval(
"select account_id from account_state_valid_at() "
"where group_id = $1 and account_id = $2 and not deleted",
"select account_id from account_state_valid_at() where group_id = $1 and account_id = $2 and not deleted",
group_id,
account_id,
)
Expand Down Expand Up @@ -218,7 +217,7 @@ async def create_account(

if account.owning_user_id is not None:
if not group_membership.is_owner and account.owning_user_id != user.id:
raise PermissionError(f"only group owners can associate others with accounts")
raise PermissionError("only group owners can associate others with accounts")

account_id = await conn.fetchval(
"insert into account (group_id, type) values ($1, $2) returning id",
Expand Down Expand Up @@ -292,13 +291,13 @@ async def update_account(

if account.owning_user_id is not None:
if not membership.is_owner and account.owning_user_id != user.id:
raise PermissionError(f"only group owners can associate others with accounts")
raise PermissionError("only group owners can associate others with accounts")
elif (
committed_account["owning_user_id"] is not None
and committed_account["owning_user_id"] != user.id
and not membership.is_owner
):
raise PermissionError(f"only group owners can remove other users as account owners")
raise PermissionError("only group owners can remove other users as account owners")

await conn.execute(
"insert into account_history (id, revision_id, name, description, owning_user_id, date_info) "
Expand Down Expand Up @@ -352,7 +351,7 @@ async def delete_account(
account_id,
)
if row is None:
raise InvalidArgument(f"Account does not exist")
raise InvalidArgument("Account does not exist")

# TODO: FIXME move this check into the database

Expand All @@ -376,28 +375,28 @@ async def delete_account(
)

if has_shares or has_usages:
raise InvalidArgument(f"Cannot delete an account that is references by a transaction")
raise InvalidArgument("Cannot delete an account that is references by a transaction")

if has_clearing_shares:
raise InvalidArgument(f"Cannot delete an account that is references by a clearing account")
raise InvalidArgument("Cannot delete an account that is references by a clearing account")

row = await conn.fetchrow(
"select name, revision_id, deleted from account_state_valid_at() where account_id = $1",
account_id,
)
if row is None:
raise InvalidArgument(f"Cannot delete an account without any committed changes")
raise InvalidArgument("Cannot delete an account without any committed changes")

if row["deleted"]:
raise InvalidArgument(f"Cannot delete an already deleted account")
raise InvalidArgument("Cannot delete an already deleted account")

has_clearing_shares = await conn.fetchval(
"select exists (select from account_state_valid_at() p where not p.deleted and $1 = any(p.involved_accounts))",
account_id,
)

if has_clearing_shares:
raise InvalidArgument(f"Cannot delete an account that is references by another clearing account")
raise InvalidArgument("Cannot delete an account that is references by another clearing account")

revision_id = await conn.fetchval(
"insert into account_revision (user_id, account_id) values ($1, $2) returning id",
Expand Down
27 changes: 13 additions & 14 deletions abrechnung/application/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async def create_group(
terms: str,
) -> int:
if user.is_guest_user:
raise PermissionError(f"guest users are not allowed to create group new groups")
raise PermissionError("guest users are not allowed to create group new groups")

group_id = await conn.fetchval(
"insert into grp (name, description, currency_symbol, terms, add_user_account_on_join, created_by) "
Expand Down Expand Up @@ -86,7 +86,7 @@ async def create_invite(
valid_until: datetime,
) -> int:
if user.is_guest_user:
raise PermissionError(f"guest users are not allowed to create group invites")
raise PermissionError("guest users are not allowed to create group invites")

await create_group_log(conn=conn, group_id=group_id, user=user, type="invite-created")
return await conn.fetchval(
Expand Down Expand Up @@ -116,7 +116,7 @@ async def delete_invite(
group_id,
)
if not deleted_id:
raise InvalidArgument(f"No invite with the given id exists")
raise InvalidArgument("No invite with the given id exists")
await create_group_log(conn=conn, group_id=group_id, user=user, type="invite-deleted")

async def _create_user_account(self, conn: asyncpg.Connection, group_id: int, user: User) -> int:
Expand Down Expand Up @@ -150,22 +150,22 @@ async def join_group(self, *, conn: Connection, user: User, invite_token: str) -
invite_token,
)
if not invite:
raise PermissionError(f"Invalid invite token")
raise PermissionError("Invalid invite token")

group = await conn.fetchrow(
"select id, add_user_account_on_join from grp " "where grp.id = $1",
"select id, add_user_account_on_join from grp where grp.id = $1",
invite["group_id"],
)
if not group:
raise PermissionError(f"Invalid invite token")
raise PermissionError("Invalid invite token")

user_is_already_member = await conn.fetchval(
"select exists (select user_id from group_membership where user_id = $1 and group_id = $2)",
user.id,
invite["group_id"],
)
if user_is_already_member:
raise InvalidArgument(f"User is already a member of this group")
raise InvalidArgument("User is already a member of this group")

await conn.execute(
"insert into group_membership (user_id, group_id, invited_by, can_write, is_owner) "
Expand Down Expand Up @@ -252,7 +252,7 @@ async def update_member_permissions(
is_owner: bool,
):
if user.id == member_id:
raise InvalidArgument(f"group members cannot modify their own privileges")
raise InvalidArgument("group members cannot modify their own privileges")

# not possible to have an owner without can_write
can_write = can_write if not is_owner else True
Expand All @@ -269,10 +269,10 @@ async def update_member_permissions(
return

if is_owner and not group_membership.is_owner:
raise PermissionError(f"group members cannot promote others to owner without being an owner")
raise PermissionError("group members cannot promote others to owner without being an owner")

if membership["is_owner"]:
raise PermissionError(f"group owners cannot be demoted by other group members")
raise PermissionError("group owners cannot be demoted by other group members")

if is_owner:
await create_group_log(
Expand Down Expand Up @@ -317,8 +317,7 @@ async def update_member_permissions(
)

await conn.execute(
"update group_membership gm set can_write = $3, is_owner = $4 "
"where gm.user_id = $1 and gm.group_id = $2",
"update group_membership gm set can_write = $3, is_owner = $4 where gm.user_id = $1 and gm.group_id = $2",
member_id,
group_id,
can_write,
Expand All @@ -333,7 +332,7 @@ async def delete_group(self, *, conn: Connection, user: User, group_id: int):
group_id,
)
if n_members != 1:
raise InvalidArgument(f"Can only delete a group when you are the last member")
raise InvalidArgument("Can only delete a group when you are the last member")

await conn.execute("delete from grp where id = $1", group_id)

Expand Down Expand Up @@ -367,7 +366,7 @@ async def preview_group(self, *, conn: Connection, invite_token: str) -> GroupPr
invite_token,
)
if not group:
raise PermissionError(f"invalid invite token to preview group")
raise PermissionError("invalid invite token to preview group")

return group

Expand Down
Loading

0 comments on commit 7d4c3ab

Please sign in to comment.