diff --git a/brewman/.flake8 b/brewman/.flake8 index 01ee7607..cbe41b29 100644 --- a/brewman/.flake8 +++ b/brewman/.flake8 @@ -1,4 +1,4 @@ [flake8] -ignore = E203 +ignore = E203, W503 max-line-length = 120 exclude = .git,__pycache__,__init__.py,.mypy_cache,.pytest_cache diff --git a/brewman/brewman/routers/account.py b/brewman/brewman/routers/account.py index f65fa3ae..8422042a 100644 --- a/brewman/brewman/routers/account.py +++ b/brewman/brewman/routers/account.py @@ -171,7 +171,7 @@ def balance(id_: uuid.UUID, date, db: Session) -> Decimal: bal = bal.where(Voucher.date <= date) bal = bal.where(Voucher.voucher_type != VoucherType.ISSUE).where(Journal.account_id == id_) - result: Decimal = db.execute(bal).scalar() + result: Decimal = db.execute(bal).scalar_one_or_none() return Decimal(0) if result is None else result diff --git a/brewman/brewman/routers/issue_grid.py b/brewman/brewman/routers/issue_grid.py index 2b7e23c1..bc9e9ad0 100644 --- a/brewman/brewman/routers/issue_grid.py +++ b/brewman/brewman/routers/issue_grid.py @@ -1,4 +1,5 @@ -from datetime import datetime +from datetime import date, datetime +from typing import List from fastapi import APIRouter, Security from sqlalchemy import select @@ -8,29 +9,30 @@ from ..core.security import get_current_active_user as get_user from ..db.session import SessionFuture from ..models.voucher import Voucher from ..models.voucher_type import VoucherType +from ..schemas.issue_grid_item import IssueGridItem from ..schemas.user import UserToken router = APIRouter() -@router.get("/{date}") +@router.get("/{date_}", response_model=List[IssueGridItem]) def grid_date( - date: str, + date_: str, user: UserToken = Security(get_user, scopes=["issue"]), -): - date = datetime.strptime(date, "%d-%b-%Y") +) -> List[IssueGridItem]: + date_obj = datetime.strptime(date_, "%d-%b-%Y") with SessionFuture() as db: - return get_grid(date, db) + return get_grid(date_obj, db) -def get_grid(date, db: Session): - list_ = [] +def get_grid(date_: date, db: Session) -> List[IssueGridItem]: + list_: List[IssueGridItem] = [] query = ( db.execute( select(Voucher) .join(Voucher.journals) - .where(Voucher.date == date, Voucher.voucher_type == VoucherType.ISSUE) + .where(Voucher.date == date_, Voucher.voucher_type == VoucherType.ISSUE) .order_by(Voucher.creation_date) ) .unique() @@ -41,11 +43,11 @@ def get_grid(date, db: Session): for voucher in query: amount = voucher.journals[0].amount list_.append( - { - "id": str(voucher.id), - "amount": amount, - "source": [j.cost_centre.name for j in voucher.journals if j.debit == -1], - "destination": [j.cost_centre.name for j in voucher.journals if j.debit == 1], - } + IssueGridItem( + id=str(voucher.id), + amount=amount, + source=next(j.cost_centre.name for j in voucher.journals if j.debit == -1), + destination=next(j.cost_centre.name for j in voucher.journals if j.debit == 1), + ) ) return list_ diff --git a/brewman/brewman/schemas/issue_grid_item.py b/brewman/brewman/schemas/issue_grid_item.py new file mode 100644 index 00000000..d52c67fd --- /dev/null +++ b/brewman/brewman/schemas/issue_grid_item.py @@ -0,0 +1,15 @@ +import uuid + +from decimal import Decimal + +from pydantic import BaseModel + + +class IssueGridItem(BaseModel): + id_: uuid.UUID + amount: Decimal + source: str + destination: str + + class Config: + fields = {"id_": "id"}