79 lines
2.8 KiB
Python
79 lines
2.8 KiB
Python
from sqlalchemy import Column, Integer, String, DateTime, Boolean, Text, ForeignKey, Index
|
||
from sqlalchemy.orm import relationship
|
||
from sqlalchemy.dialects.postgresql import JSONB, UUID
|
||
from datetime import datetime
|
||
import uuid
|
||
from app.models.base import BaseModel
|
||
|
||
|
||
class Form(BaseModel):
|
||
__tablename__ = "forms"
|
||
|
||
uuid = Column(UUID(as_uuid=True), default=uuid.uuid4, unique=True, nullable=False, index=True)
|
||
name = Column(String(200), nullable=False)
|
||
description = Column(Text)
|
||
version = Column(Integer, default=1)
|
||
is_active = Column(Boolean, default=True)
|
||
is_published = Column(Boolean, default=False)
|
||
form_settings = Column(JSONB, default={}) # переименовано с settings
|
||
created_by = Column(Integer, nullable=True)
|
||
|
||
# Relationships
|
||
fields = relationship("FormField", back_populates="form", cascade="all, delete-orphan")
|
||
submissions = relationship("Submission", back_populates="form", cascade="all, delete-orphan")
|
||
|
||
__table_args__ = (
|
||
Index('ix_forms_name_active', 'name', 'is_active'),
|
||
Index('ix_forms_created_at', 'created_at'),
|
||
)
|
||
|
||
@property
|
||
def fields_count(self) -> int:
|
||
return len(self.fields)
|
||
|
||
@property
|
||
def submissions_count(self) -> int:
|
||
return len(self.submissions)
|
||
|
||
|
||
class FormField(BaseModel):
|
||
__tablename__ = "form_fields"
|
||
|
||
form_id = Column(Integer, ForeignKey("forms.id", ondelete="CASCADE"))
|
||
field_id = Column(Integer, ForeignKey("fields.id", ondelete="CASCADE"))
|
||
order = Column(Integer, default=0)
|
||
is_required = Column(Boolean, default=False)
|
||
default_value = Column(JSONB, nullable=True)
|
||
visibility_conditions = Column(JSONB, default={})
|
||
validation_rules_override = Column(JSONB, nullable=True)
|
||
|
||
# Relationships
|
||
form = relationship("Form", back_populates="fields")
|
||
field = relationship("Field", back_populates="form_fields")
|
||
|
||
__table_args__ = (
|
||
Index('ix_form_fields_order', 'form_id', 'order'),
|
||
Index('ix_form_fields_unique', 'form_id', 'field_id', unique=True),
|
||
)
|
||
|
||
|
||
class Field(BaseModel):
|
||
__tablename__ = "fields"
|
||
|
||
name = Column(String(100), unique=True, nullable=False, index=True)
|
||
label = Column(String(200), nullable=False)
|
||
field_type = Column(String(50), nullable=False)
|
||
placeholder = Column(String(200))
|
||
help_text = Column(Text)
|
||
field_options = Column(JSONB, default={}) # переименовано с options
|
||
validation_rules = Column(JSONB, default={})
|
||
field_metadata = Column(JSONB, default={}) # переименовано с metadata
|
||
created_at = Column(DateTime, default=datetime.utcnow)
|
||
|
||
# Relationships
|
||
form_fields = relationship("FormField", back_populates="field")
|
||
|
||
__table_args__ = (
|
||
Index('ix_fields_type', 'field_type'),
|
||
Index('ix_fields_name_type', 'name', 'field_type'),
|
||
) |