feat: deal attributes with select and options
This commit is contained in:
@ -1,6 +1,10 @@
|
||||
from sqlalchemy.orm import configure_mappers
|
||||
|
||||
from modules.fulfillment_base.models import * # noqa: F401
|
||||
from .attr_select import (
|
||||
AttributeOption as AttributeOption,
|
||||
AttributeSelect as AttributeSelect,
|
||||
)
|
||||
from .attribute import (
|
||||
AttributeType as AttributeType,
|
||||
Attribute as Attribute,
|
||||
|
||||
46
models/attr_select.py
Normal file
46
models/attr_select.py
Normal file
@ -0,0 +1,46 @@
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import ForeignKey, UniqueConstraint
|
||||
from sqlalchemy.dialects.postgresql import JSONB
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from models.base import BaseModel
|
||||
from models.mixins import IdMixin, SoftDeleteMixin
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from models import Attribute
|
||||
|
||||
|
||||
class AttributeSelect(BaseModel, IdMixin, SoftDeleteMixin):
|
||||
__tablename__ = "attribute_selects"
|
||||
|
||||
label: Mapped[str] = mapped_column()
|
||||
is_built_in: Mapped[bool] = mapped_column(
|
||||
default=False,
|
||||
comment="Если встроенный select, то запрещено редактировать пользователю",
|
||||
)
|
||||
|
||||
options: Mapped[list["AttributeOption"]] = relationship(
|
||||
back_populates="select",
|
||||
lazy="noload",
|
||||
)
|
||||
|
||||
attributes: Mapped[list["Attribute"]] = relationship(
|
||||
back_populates="select",
|
||||
lazy="noload",
|
||||
)
|
||||
|
||||
|
||||
class AttributeOption(BaseModel, IdMixin, SoftDeleteMixin):
|
||||
__tablename__ = "attribute_options"
|
||||
|
||||
value: Mapped[dict[str, any]] = mapped_column(JSONB)
|
||||
label: Mapped[str] = mapped_column()
|
||||
|
||||
select_id: Mapped[int] = mapped_column(ForeignKey("attribute_selects.id"))
|
||||
select: Mapped[AttributeSelect] = relationship(
|
||||
back_populates="options",
|
||||
lazy="noload",
|
||||
)
|
||||
|
||||
__table_args__ = (UniqueConstraint("value", "select_id", name="_value_select_uc"),)
|
||||
@ -8,8 +8,7 @@ from models.base import BaseModel
|
||||
from models.mixins import IdMixin, SoftDeleteMixin
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from models import Module, Deal
|
||||
|
||||
from models import Module, Deal, AttributeSelect
|
||||
|
||||
module_attribute = Table(
|
||||
"module_attribute",
|
||||
@ -51,6 +50,12 @@ class Attribute(BaseModel, IdMixin, SoftDeleteMixin):
|
||||
lazy="joined",
|
||||
)
|
||||
|
||||
select_id: Mapped[Optional[int]] = mapped_column(ForeignKey("attribute_selects.id"))
|
||||
select: Mapped[Optional["AttributeSelect"]] = relationship(
|
||||
back_populates="attributes",
|
||||
lazy="joined",
|
||||
)
|
||||
|
||||
modules: Mapped[list["Module"]] = relationship(
|
||||
secondary=module_attribute,
|
||||
back_populates="attributes",
|
||||
|
||||
Reference in New Issue
Block a user