Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: split up database code and revisions #174

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/ci_testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.9
- name: Set up Python 3.10
uses: actions/setup-python@v2
with:
python-version: 3.9
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip flit
Expand All @@ -24,10 +24,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.9
- name: Set up Python 3.10
uses: actions/setup-python@v2
with:
python-version: 3.9
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip flit
Expand All @@ -40,8 +40,8 @@ jobs:
strategy:
matrix:
python-version:
- "3.9"
- "3.10"
- "3.11"
os: [ubuntu-latest]
services:
postgres:
Expand Down Expand Up @@ -79,10 +79,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.9
- name: Set up Python 3.10
uses: actions/setup-python@v2
with:
python-version: 3.9
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip flit
Expand Down
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
Loading