61 lines
2.4 KiB
Python
61 lines
2.4 KiB
Python
import datetime
|
|
import uuid
|
|
|
|
from typing import TYPE_CHECKING, List, Optional
|
|
|
|
from sqlalchemy import Column, Date, DateTime, ForeignKey, Unicode
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from sqlalchemy.orm import Mapped, relationship
|
|
|
|
from .meta import Base
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
# if the target of the relationship is in another module
|
|
# that cannot normally be imported at runtime
|
|
from .account_base import AccountBase
|
|
from .rate_contract_item import RateContractItem
|
|
from .user import User
|
|
|
|
|
|
class RateContract(Base):
|
|
__tablename__ = "rate_contracts"
|
|
|
|
id: uuid.UUID = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
date: datetime.date = Column("date", Date, nullable=False, index=True)
|
|
vendor_id: uuid.UUID = Column("vendor_id", UUID(as_uuid=True), ForeignKey("accounts.id"), nullable=False)
|
|
valid_from: Optional[datetime.date] = Column("valid_from", Date(), nullable=True)
|
|
valid_till: Optional[datetime.date] = Column("valid_till", Date(), nullable=True)
|
|
narration: str = Column("narration", Unicode(1000), nullable=False)
|
|
|
|
user_id: uuid.UUID = Column("user_id", UUID(as_uuid=True), ForeignKey("users.id"), nullable=False)
|
|
creation_date: datetime.datetime = Column("creation_date", DateTime(), nullable=False)
|
|
last_edit_date: datetime.datetime = Column("last_edit_date", DateTime(), nullable=False)
|
|
|
|
user: Mapped["User"] = relationship("User")
|
|
vendor: Mapped["AccountBase"] = relationship("AccountBase", back_populates="rate_contracts")
|
|
items: List["RateContractItem"] = relationship("RateContractItem", back_populates="rate_contract")
|
|
|
|
def __init__(
|
|
self,
|
|
date: datetime.date,
|
|
vendor_id: uuid.UUID,
|
|
valid_from: Optional[datetime.date],
|
|
valid_till: Optional[datetime.date],
|
|
user_id: uuid.UUID,
|
|
narration: str = "",
|
|
creation_date: Optional[datetime.datetime] = None,
|
|
last_edit_date: Optional[datetime.datetime] = None,
|
|
id_: Optional[uuid.UUID] = None,
|
|
):
|
|
self.date = date
|
|
self.vendor_id = vendor_id
|
|
self.valid_from = valid_from
|
|
self.valid_till = valid_till
|
|
self.narration = narration
|
|
self.user_id = user_id
|
|
self.creation_date = creation_date or datetime.datetime.utcnow()
|
|
self.last_edit_date = last_edit_date or datetime.datetime.utcnow()
|
|
if id_ is not None:
|
|
self.id = id_
|