brewman/brewman/brewman/models/account.py

51 lines
1.4 KiB
Python

import uuid
from typing import TYPE_CHECKING
from sqlalchemy.orm import Mapped, relationship
from ..db.base_class import reg
from .account_base import AccountBase
if TYPE_CHECKING:
from .product import Product
@reg.mapped_as_dataclass(unsafe_hash=True)
class Account(AccountBase):
__mapper_args__ = {"polymorphic_identity": ""} # type: ignore
products: Mapped[list["Product"]] = relationship(
"Product", primaryjoin="Account.id==Product.account_id", back_populates="account"
)
def __init__(
self,
name: str,
type_id: int,
is_starred: bool,
is_active: bool,
is_reconcilable: bool,
cost_centre_id: uuid.UUID,
code: int | None = None,
id_: uuid.UUID | None = None,
is_fixture: bool = False,
) -> None:
if code is not None:
self.code = code
self.name = name
self.type_id = type_id
self.is_starred = is_starred
self.is_active = is_active
self.is_reconcilable = is_reconcilable
self.cost_centre_id = cost_centre_id
if id_ is not None:
self.id = id_
self.is_fixture = is_fixture
def can_delete(self, advanced_delete: bool) -> tuple[bool, str]:
if len(self.products) > 0:
return False, "Account has products"
return super(Account, self).can_delete(advanced_delete)