52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
import uuid
|
|
|
|
from decimal import Decimal
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import ForeignKey, Numeric, UniqueConstraint, Uuid
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from ..db.base_class import reg
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
from .period import Period
|
|
from .product import Product
|
|
|
|
|
|
@reg.mapped_as_dataclass(unsafe_hash=True)
|
|
class Price:
|
|
__tablename__ = "prices"
|
|
__table_args__ = (UniqueConstraint("period_id", "product_id"),)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, insert_default=uuid.uuid4)
|
|
period_id: Mapped[uuid.UUID] = mapped_column(Uuid, ForeignKey("periods.id"), nullable=False)
|
|
product_id: Mapped[uuid.UUID] = mapped_column(Uuid, ForeignKey("products.id"), nullable=False)
|
|
price: Mapped[Decimal] = mapped_column(Numeric(precision=15, scale=2), nullable=False)
|
|
|
|
period: Mapped["Period"] = relationship("Period")
|
|
product: Mapped["Product"] = relationship("Product")
|
|
|
|
def __init__(
|
|
self,
|
|
price: Decimal,
|
|
period_id: uuid.UUID | None = None,
|
|
product_id: uuid.UUID | None = None,
|
|
period: "Period" | None = None,
|
|
product: "Product" | None = None,
|
|
id_: uuid.UUID | None = None,
|
|
):
|
|
self.price = price
|
|
if period_id is not None:
|
|
self.period_id = period_id
|
|
if product_id is not None:
|
|
self.product_id = product_id
|
|
if period is not None and (period.id is not None or period_id is None):
|
|
self.period = period
|
|
if product is not None and (product.id is not None or product_id is None):
|
|
self.product = product
|
|
if id_ is not None:
|
|
self.id = id_
|