fix: default option in attributes

This commit is contained in:
2025-11-03 10:43:10 +04:00
parent be878717e5
commit a7bda3d9f6
6 changed files with 24 additions and 21 deletions

View File

@ -1,7 +1,6 @@
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from sqlalchemy import ForeignKey, UniqueConstraint from sqlalchemy import ForeignKey
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column, relationship from sqlalchemy.orm import Mapped, mapped_column, relationship
from models.base import BaseModel from models.base import BaseModel
@ -34,7 +33,6 @@ class AttributeSelect(BaseModel, IdMixin, SoftDeleteMixin):
class AttributeOption(BaseModel, IdMixin, SoftDeleteMixin): class AttributeOption(BaseModel, IdMixin, SoftDeleteMixin):
__tablename__ = "attribute_options" __tablename__ = "attribute_options"
value: Mapped[dict[str, any]] = mapped_column(JSONB)
label: Mapped[str] = mapped_column() label: Mapped[str] = mapped_column()
select_id: Mapped[int] = mapped_column(ForeignKey("attribute_selects.id")) select_id: Mapped[int] = mapped_column(ForeignKey("attribute_selects.id"))
@ -42,5 +40,3 @@ class AttributeOption(BaseModel, IdMixin, SoftDeleteMixin):
back_populates="options", back_populates="options",
lazy="noload", lazy="noload",
) )
__table_args__ = (UniqueConstraint("value", "select_id", name="_value_select_uc"),)

View File

@ -8,7 +8,7 @@ from models.base import BaseModel
from models.mixins import IdMixin, SoftDeleteMixin from models.mixins import IdMixin, SoftDeleteMixin
if TYPE_CHECKING: if TYPE_CHECKING:
from models import Module, Deal, AttributeSelect from models import Module, Deal, AttributeSelect, AttributeOption
module_attribute = Table( module_attribute = Table(
"module_attribute", "module_attribute",
@ -39,7 +39,15 @@ class Attribute(BaseModel, IdMixin, SoftDeleteMixin):
comment="Применять ли изменения атрибута карточки ко всем карточкам в группе", comment="Применять ли изменения атрибута карточки ко всем карточкам в группе",
) )
is_nullable: Mapped[bool] = mapped_column(default=False) is_nullable: Mapped[bool] = mapped_column(default=False)
default_value: Mapped[Optional[dict[str, any]]] = mapped_column(JSONB) 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="") description: Mapped[str] = mapped_column(default="")
is_built_in: Mapped[bool] = mapped_column(default=False) is_built_in: Mapped[bool] = mapped_column(default=False)

View File

@ -68,13 +68,18 @@ class AttributeRepository(
) -> None: ) -> None:
attributes = await self._get_all_attributes_for_deal(project_id) attributes = await self._get_all_attributes_for_deal(project_id)
for attribute, module_id in attributes: 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 continue
value = AttributeValue( value = AttributeValue(
attribute_id=attribute.id, attribute_id=attribute.id,
deal_id=deal_id, deal_id=deal_id,
module_id=module_id, module_id=module_id,
value=attribute.default_value, value=def_val,
) )
self.session.add(value) self.session.add(value)

View File

@ -1,5 +1,3 @@
from typing import Any
from schemas.base import BaseSchema from schemas.base import BaseSchema
@ -15,7 +13,6 @@ class AttrSelectSchema(BaseSchema):
class AttrOptionSchema(BaseSchema): class AttrOptionSchema(BaseSchema):
id: int id: int
label: str label: str
value: Any
class AttrSelectWithOptionsSchema(AttrSelectSchema): class AttrSelectWithOptionsSchema(AttrSelectSchema):

View File

@ -1,5 +1,6 @@
from typing import Optional, Any from typing import Optional, Any
from schemas.attr_select import AttrSelectSchema, AttrOptionSchema
from schemas.base import BaseSchema, BaseResponse from schemas.base import BaseSchema, BaseResponse
@ -12,16 +13,12 @@ class AttributeTypeSchema(BaseSchema):
name: str name: str
class AttributeSelectSchema(BaseSchema):
id: int
label: str
class CreateAttributeSchema(BaseSchema): class CreateAttributeSchema(BaseSchema):
label: str label: str
is_applicable_to_group: bool is_applicable_to_group: bool
is_nullable: bool is_nullable: bool
default_value: Optional[Any] default_value: Optional[Any]
default_option_id: Optional[int] = None
description: str description: str
type_id: int type_id: int
select_id: Optional[int] = None select_id: Optional[int] = None
@ -31,7 +28,8 @@ class AttributeSchema(CreateAttributeSchema):
id: int id: int
is_built_in: bool is_built_in: bool
type: AttributeTypeSchema type: AttributeTypeSchema
select: Optional[AttributeSelectSchema] default_option: Optional[AttrOptionSchema] = None
select: Optional[AttrSelectSchema]
class UpdateAttributeSchema(BaseSchema): class UpdateAttributeSchema(BaseSchema):
@ -39,6 +37,7 @@ class UpdateAttributeSchema(BaseSchema):
is_applicable_to_group: Optional[bool] = None is_applicable_to_group: Optional[bool] = None
is_nullable: Optional[bool] = None is_nullable: Optional[bool] = None
default_value: Optional[Any] = None default_value: Optional[Any] = None
default_option_id: Optional[int] = None
description: Optional[str] = None description: Optional[str] = None
@ -52,7 +51,7 @@ class DealModuleAttributeSchema(BaseSchema):
original_label: str original_label: str
value: Optional[Any] value: Optional[Any]
type: AttributeTypeSchema type: AttributeTypeSchema
select: Optional[AttributeSelectSchema] select: Optional[AttrSelectSchema]
default_value: Any default_value: Any
description: str description: str
is_applicable_to_group: bool is_applicable_to_group: bool

View File

@ -40,9 +40,7 @@ class AttributeService(
attributes = [] attributes = []
for attr, attr_value, attr_label in deal_attributes: for attr, attr_value, attr_label in deal_attributes:
select_schema = ( select_schema = (
AttributeSelectSchema.model_validate(attr.select) AttrSelectSchema.model_validate(attr.select) if attr.select else None
if attr.select
else None
) )
attribute = DealModuleAttributeSchema( attribute = DealModuleAttributeSchema(