Skip to content

Commit

Permalink
refactor: split up database code and revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
mikonse committed Oct 31, 2023
1 parent 03ae664 commit 4377815
Show file tree
Hide file tree
Showing 48 changed files with 4,021 additions and 5,929 deletions.
10 changes: 6 additions & 4 deletions abrechnung/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from abrechnung import subcommand
from abrechnung.application.users import UserService
from abrechnung.config import Config
from abrechnung.database.database import create_db_pool
from abrechnung.framework.database import create_db_pool


class AdminCli(subcommand.SubCommand):
Expand Down Expand Up @@ -36,13 +36,15 @@ async def handle_create_user_command(self):
print("Passwords do not match!")
return

db_pool = await create_db_pool(self.config)
db_pool = await create_db_pool(self.config.database)
user_service = UserService(db_pool, self.config)
user_service.enable_registration = True
if self.args["skip_email_check"]:
user_service.valid_email_domains = None
await user_service.register_user(
username=self.args["name"], email=self.args["email"], password=password
await user_service.register_user( # pylint: disable=missing-kwoa
username=self.args["name"],
email=self.args["email"],
password=password,
)

async def run(self):
Expand Down
57 changes: 0 additions & 57 deletions abrechnung/application/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,3 @@

from abrechnung.config import Config
from abrechnung.domain.users import User


class NotFoundError(Exception):
pass


class InvalidCommand(Exception):
pass


class Application:
def __init__(self, db_pool: Pool, config: Config):
self.db_pool = db_pool
self.cfg = config


async def check_group_permissions(
conn: asyncpg.Connection,
group_id: int,
user: User,
is_owner: bool = False,
can_write: bool = False,
) -> tuple[bool, bool]:
membership = await conn.fetchrow(
"select is_owner, can_write from group_membership where group_id = $1 and user_id = $2",
group_id,
user.id,
)
if membership is None:
raise NotFoundError(f"group not found")

if can_write and not (membership["is_owner"] or membership["can_write"]):
raise PermissionError(f"write access to group denied")

if is_owner and not membership["is_owner"]:
raise PermissionError(f"owner access to group denied")

return membership["can_write"], membership["is_owner"]


async def create_group_log(
conn: asyncpg.Connection,
group_id: int,
user: User,
type: str,
message: Optional[str] = None,
affected_user_id: Optional[int] = None,
):
await conn.execute(
"insert into group_log (group_id, user_id, type, message, affected) "
"values ($1, $2, $3, $4, $5)",
group_id,
user.id,
type,
"" if message is None else message,
affected_user_id,
)
Loading

0 comments on commit 4377815

Please sign in to comment.