|
| 1 | +import uuid |
| 2 | +from dataclasses import dataclass, field |
| 3 | +from sqlalchemy import String, ForeignKey, Index, CheckConstraint, Column, Boolean, BigInteger |
| 4 | +from sqlalchemy.orm import relationship, foreign, remote |
| 5 | +from sqlalchemy.dialects.postgresql import JSONB, UUID |
| 6 | +from typing import TYPE_CHECKING, Optional, List, Dict, Any |
| 7 | +from .base import ORM_BASE, CommonMixin |
| 8 | +from ..utils import asUUID |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + from .space import Space |
| 12 | + |
| 13 | + |
| 14 | +# Block type configuration matching Go version |
| 15 | +BLOCK_TYPES = { |
| 16 | + "page": { |
| 17 | + "name": "page", |
| 18 | + "allow_children": True, |
| 19 | + "require_parent": False, |
| 20 | + }, |
| 21 | + "text": { |
| 22 | + "name": "text", |
| 23 | + "allow_children": True, |
| 24 | + "require_parent": True, |
| 25 | + }, |
| 26 | + "snippet": { |
| 27 | + "name": "snippet", |
| 28 | + "allow_children": True, |
| 29 | + "require_parent": True, |
| 30 | + }, |
| 31 | +} |
| 32 | + |
| 33 | +# Block type constants matching Go version |
| 34 | +BLOCK_TYPE_PAGE = "page" |
| 35 | +BLOCK_TYPE_TEXT = "text" |
| 36 | +BLOCK_TYPE_SNIPPET = "snippet" |
| 37 | + |
| 38 | + |
| 39 | +def is_valid_block_type(block_type: str) -> bool: |
| 40 | + """Check if the given type is valid""" |
| 41 | + return block_type in BLOCK_TYPES |
| 42 | + |
| 43 | + |
| 44 | +def get_block_type_config(block_type: str) -> Dict[str, Any]: |
| 45 | + """Get the configuration of a block type""" |
| 46 | + if not is_valid_block_type(block_type): |
| 47 | + raise ValueError(f"invalid block type: {block_type}") |
| 48 | + return BLOCK_TYPES[block_type] |
| 49 | + |
| 50 | + |
| 51 | +def get_all_block_types() -> Dict[str, Dict[str, Any]]: |
| 52 | + """Get all supported block types""" |
| 53 | + return BLOCK_TYPES |
| 54 | + |
| 55 | + |
| 56 | +@ORM_BASE.mapped |
| 57 | +@dataclass |
| 58 | +class Block(CommonMixin): |
| 59 | + __tablename__ = "blocks" |
| 60 | + |
| 61 | + __table_args__ = ( |
| 62 | + # Indexes matching Go version |
| 63 | + Index("idx_blocks_space", "space_id"), |
| 64 | + Index("idx_blocks_space_type", "space_id", "type"), |
| 65 | + Index("idx_blocks_space_type_archived", "space_id", "type", "is_archived"), |
| 66 | + # Unique constraint for space, parent, sort combination |
| 67 | + Index("ux_blocks_space_parent_sort", "space_id", "parent_id", "sort", unique=True), |
| 68 | + # Check constraints matching Go version |
| 69 | + CheckConstraint( |
| 70 | + "type IN ('page', 'text', 'snippet')", |
| 71 | + name="ck_block_type", |
| 72 | + ), |
| 73 | + ) |
| 74 | + |
| 75 | + space_id: asUUID = field( |
| 76 | + metadata={ |
| 77 | + "db": Column( |
| 78 | + UUID(as_uuid=True), |
| 79 | + ForeignKey("spaces.id", ondelete="CASCADE", onupdate="CASCADE"), |
| 80 | + nullable=False, |
| 81 | + ) |
| 82 | + } |
| 83 | + ) |
| 84 | + |
| 85 | + type: str = field( |
| 86 | + metadata={ |
| 87 | + "db": Column( |
| 88 | + String, |
| 89 | + nullable=False, |
| 90 | + ) |
| 91 | + } |
| 92 | + ) |
| 93 | + |
| 94 | + parent_id: Optional[asUUID] = field( |
| 95 | + default=None, |
| 96 | + metadata={ |
| 97 | + "db": Column( |
| 98 | + UUID(as_uuid=True), |
| 99 | + ForeignKey("blocks.id", ondelete="CASCADE", onupdate="CASCADE"), |
| 100 | + nullable=True, |
| 101 | + ) |
| 102 | + }, |
| 103 | + ) |
| 104 | + |
| 105 | + title: str = field( |
| 106 | + default="", |
| 107 | + metadata={ |
| 108 | + "db": Column( |
| 109 | + String, |
| 110 | + nullable=False, |
| 111 | + default="", |
| 112 | + ) |
| 113 | + }, |
| 114 | + ) |
| 115 | + |
| 116 | + props: Dict[str, Any] = field( |
| 117 | + default_factory=dict, |
| 118 | + metadata={ |
| 119 | + "db": Column( |
| 120 | + JSONB, |
| 121 | + nullable=False, |
| 122 | + default={}, |
| 123 | + ) |
| 124 | + }, |
| 125 | + ) |
| 126 | + |
| 127 | + sort: int = field( |
| 128 | + default=0, |
| 129 | + metadata={ |
| 130 | + "db": Column( |
| 131 | + BigInteger, |
| 132 | + nullable=False, |
| 133 | + default=0, |
| 134 | + ) |
| 135 | + }, |
| 136 | + ) |
| 137 | + |
| 138 | + is_archived: bool = field( |
| 139 | + default=False, |
| 140 | + metadata={ |
| 141 | + "db": Column( |
| 142 | + Boolean, |
| 143 | + nullable=False, |
| 144 | + default=False, |
| 145 | + ) |
| 146 | + }, |
| 147 | + ) |
| 148 | + |
| 149 | + # Relationships |
| 150 | + space: "Space" = field( |
| 151 | + init=False, |
| 152 | + metadata={ |
| 153 | + "db": relationship( |
| 154 | + "Space", |
| 155 | + back_populates="blocks", |
| 156 | + ) |
| 157 | + }, |
| 158 | + ) |
| 159 | + |
| 160 | + parent: Optional["Block"] = field( |
| 161 | + init=False, |
| 162 | + metadata={ |
| 163 | + "db": relationship( |
| 164 | + "Block", |
| 165 | + remote_side=lambda: Block.id, |
| 166 | + foreign_keys=lambda: Block.parent_id, |
| 167 | + back_populates="children", |
| 168 | + lazy="select", |
| 169 | + ) |
| 170 | + }, |
| 171 | + ) |
| 172 | + |
| 173 | + children: List["Block"] = field( |
| 174 | + default_factory=list, |
| 175 | + init=False, |
| 176 | + metadata={ |
| 177 | + "db": relationship( |
| 178 | + "Block", |
| 179 | + back_populates="parent", |
| 180 | + cascade="all, delete-orphan", |
| 181 | + lazy="selectin", |
| 182 | + ) |
| 183 | + }, |
| 184 | + ) |
| 185 | + |
| 186 | + def validate(self) -> None: |
| 187 | + """Validate the fields of a Block""" |
| 188 | + # Check if the type is valid |
| 189 | + if not is_valid_block_type(self.type): |
| 190 | + raise ValueError(f"invalid block type: {self.type}") |
| 191 | + |
| 192 | + config = get_block_type_config(self.type) |
| 193 | + |
| 194 | + # Check the parent-child relationship constraints |
| 195 | + if config["require_parent"] and self.parent_id is None: |
| 196 | + raise ValueError(f"block type '{self.type}' requires a parent") |
| 197 | + |
| 198 | + if not config["require_parent"] and self.type != BLOCK_TYPE_PAGE and self.parent_id is None: |
| 199 | + raise ValueError("only page type blocks can exist without a parent") |
| 200 | + |
| 201 | + def validate_for_creation(self) -> None: |
| 202 | + """Validate the constraints for creation""" |
| 203 | + self.validate() |
| 204 | + # Can add specific validation logic for creation here |
| 205 | + |
| 206 | + def can_have_children(self) -> bool: |
| 207 | + """Check if the block type can have children""" |
| 208 | + try: |
| 209 | + config = get_block_type_config(self.type) |
| 210 | + return config["allow_children"] |
| 211 | + except ValueError: |
| 212 | + return False |
0 commit comments