fix: default option in attributes
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import ForeignKey, UniqueConstraint
|
||||
from sqlalchemy.dialects.postgresql import JSONB
|
||||
from sqlalchemy import ForeignKey
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from models.base import BaseModel
|
||||
@ -34,7 +33,6 @@ class AttributeSelect(BaseModel, IdMixin, SoftDeleteMixin):
|
||||
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"))
|
||||
@ -42,5 +40,3 @@ class AttributeOption(BaseModel, IdMixin, SoftDeleteMixin):
|
||||
back_populates="options",
|
||||
lazy="noload",
|
||||
)
|
||||
|
||||
__table_args__ = (UniqueConstraint("value", "select_id", name="_value_select_uc"),)
|
||||
|
||||
@ -8,7 +8,7 @@ from models.base import BaseModel
|
||||
from models.mixins import IdMixin, SoftDeleteMixin
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from models import Module, Deal, AttributeSelect
|
||||
from models import Module, Deal, AttributeSelect, AttributeOption
|
||||
|
||||
module_attribute = Table(
|
||||
"module_attribute",
|
||||
@ -39,7 +39,15 @@ class Attribute(BaseModel, IdMixin, SoftDeleteMixin):
|
||||
comment="Применять ли изменения атрибута карточки ко всем карточкам в группе",
|
||||
)
|
||||
is_nullable: Mapped[bool] = mapped_column(default=False)
|
||||
|
||||
default_value: Mapped[Optional[dict[str, any]]] = mapped_column(JSONB)
|
||||
default_option_id: Mapped[Optional[int]] = mapped_column(
|
||||
ForeignKey("attribute_options.id")
|
||||
)
|
||||
default_option: Mapped[Optional["AttributeOption"]] = relationship(
|
||||
backref="attributes", lazy="joined"
|
||||
)
|
||||
|
||||
description: Mapped[str] = mapped_column(default="")
|
||||
|
||||
is_built_in: Mapped[bool] = mapped_column(default=False)
|
||||
|
||||
@ -68,13 +68,18 @@ class AttributeRepository(
|
||||
) -> None:
|
||||
attributes = await self._get_all_attributes_for_deal(project_id)
|
||||
for attribute, module_id in attributes:
|
||||
if attribute.default_value is None:
|
||||
def_val = (
|
||||
attribute.default_option_id
|
||||
if attribute.default_option_id is not None
|
||||
else attribute.default_value
|
||||
)
|
||||
if def_val is None:
|
||||
continue
|
||||
value = AttributeValue(
|
||||
attribute_id=attribute.id,
|
||||
deal_id=deal_id,
|
||||
module_id=module_id,
|
||||
value=attribute.default_value,
|
||||
value=def_val,
|
||||
)
|
||||
self.session.add(value)
|
||||
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
from typing import Any
|
||||
|
||||
from schemas.base import BaseSchema
|
||||
|
||||
|
||||
@ -15,7 +13,6 @@ class AttrSelectSchema(BaseSchema):
|
||||
class AttrOptionSchema(BaseSchema):
|
||||
id: int
|
||||
label: str
|
||||
value: Any
|
||||
|
||||
|
||||
class AttrSelectWithOptionsSchema(AttrSelectSchema):
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
from typing import Optional, Any
|
||||
|
||||
from schemas.attr_select import AttrSelectSchema, AttrOptionSchema
|
||||
from schemas.base import BaseSchema, BaseResponse
|
||||
|
||||
|
||||
@ -12,16 +13,12 @@ class AttributeTypeSchema(BaseSchema):
|
||||
name: str
|
||||
|
||||
|
||||
class AttributeSelectSchema(BaseSchema):
|
||||
id: int
|
||||
label: str
|
||||
|
||||
|
||||
class CreateAttributeSchema(BaseSchema):
|
||||
label: str
|
||||
is_applicable_to_group: bool
|
||||
is_nullable: bool
|
||||
default_value: Optional[Any]
|
||||
default_option_id: Optional[int] = None
|
||||
description: str
|
||||
type_id: int
|
||||
select_id: Optional[int] = None
|
||||
@ -31,7 +28,8 @@ class AttributeSchema(CreateAttributeSchema):
|
||||
id: int
|
||||
is_built_in: bool
|
||||
type: AttributeTypeSchema
|
||||
select: Optional[AttributeSelectSchema]
|
||||
default_option: Optional[AttrOptionSchema] = None
|
||||
select: Optional[AttrSelectSchema]
|
||||
|
||||
|
||||
class UpdateAttributeSchema(BaseSchema):
|
||||
@ -39,6 +37,7 @@ class UpdateAttributeSchema(BaseSchema):
|
||||
is_applicable_to_group: Optional[bool] = None
|
||||
is_nullable: Optional[bool] = None
|
||||
default_value: Optional[Any] = None
|
||||
default_option_id: Optional[int] = None
|
||||
description: Optional[str] = None
|
||||
|
||||
|
||||
@ -52,7 +51,7 @@ class DealModuleAttributeSchema(BaseSchema):
|
||||
original_label: str
|
||||
value: Optional[Any]
|
||||
type: AttributeTypeSchema
|
||||
select: Optional[AttributeSelectSchema]
|
||||
select: Optional[AttrSelectSchema]
|
||||
default_value: Any
|
||||
description: str
|
||||
is_applicable_to_group: bool
|
||||
|
||||
@ -40,9 +40,7 @@ class AttributeService(
|
||||
attributes = []
|
||||
for attr, attr_value, attr_label in deal_attributes:
|
||||
select_schema = (
|
||||
AttributeSelectSchema.model_validate(attr.select)
|
||||
if attr.select
|
||||
else None
|
||||
AttrSelectSchema.model_validate(attr.select) if attr.select else None
|
||||
)
|
||||
|
||||
attribute = DealModuleAttributeSchema(
|
||||
|
||||
Reference in New Issue
Block a user