Features: - Categories: Global and team-specific tree categorization (admin-managed) - Tags: Flexible tree tagging with autocomplete (author + admin) - User folders: Personal tree collections with subfolder support - Hierarchical structure (max 3 levels deep) - Right-click context menu for folder management - Cascade delete for subfolders - Filter trees by category, tags, and folder in library view Backend: - New models: Category, Tag, UserFolder with relationships - New API endpoints for categories, tags, and folders - Tree organization migrations (005, 006) Frontend: - FolderSidebar with hierarchical folder tree - FolderEditModal for create/edit with color picker - AddToFolderMenu for quick tree organization - TagInput with autocomplete and TagBadges display - Updated TreeMetadataForm and TreeLibraryPage Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
94 lines
3.4 KiB
Python
94 lines
3.4 KiB
Python
import uuid
|
|
from datetime import datetime, timezone
|
|
from typing import TYPE_CHECKING, Optional
|
|
from sqlalchemy import String, DateTime, ForeignKey, Integer, UniqueConstraint, Table, Column
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from app.core.database import Base
|
|
|
|
if TYPE_CHECKING:
|
|
from app.models.tree import Tree
|
|
from app.models.user import User
|
|
|
|
|
|
# Junction table for folder-tree many-to-many relationship
|
|
user_folder_trees = Table(
|
|
'user_folder_trees',
|
|
Base.metadata,
|
|
Column('folder_id', UUID(as_uuid=True), ForeignKey('user_folders.id', ondelete='CASCADE'), primary_key=True),
|
|
Column('tree_id', UUID(as_uuid=True), ForeignKey('trees.id', ondelete='CASCADE'), primary_key=True),
|
|
Column('added_at', DateTime(timezone=True), nullable=False, default=lambda: datetime.now(timezone.utc)),
|
|
Column('display_order', Integer, nullable=False, default=0)
|
|
)
|
|
|
|
|
|
class UserFolder(Base):
|
|
"""User-specific folders for organizing trees.
|
|
|
|
- Each folder belongs to a single user
|
|
- Trees can be in multiple folders (like labels/tags)
|
|
- Folders are purely organizational - they don't affect tree visibility or permissions
|
|
"""
|
|
__tablename__ = "user_folders"
|
|
__table_args__ = (
|
|
# Allow same name under different parents
|
|
UniqueConstraint('user_id', 'name', 'parent_id', name='uq_user_folders_user_name_parent'),
|
|
)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
primary_key=True,
|
|
default=uuid.uuid4
|
|
)
|
|
user_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("users.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True
|
|
)
|
|
name: Mapped[str] = mapped_column(String(100), nullable=False)
|
|
color: Mapped[str] = mapped_column(String(7), nullable=False, default="#6366f1")
|
|
icon: Mapped[str] = mapped_column(String(50), nullable=False, default="folder")
|
|
display_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
parent_id: Mapped[Optional[uuid.UUID]] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("user_folders.id", ondelete="CASCADE"),
|
|
nullable=True,
|
|
index=True
|
|
)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
default=lambda: datetime.now(timezone.utc)
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
default=lambda: datetime.now(timezone.utc),
|
|
onupdate=lambda: datetime.now(timezone.utc)
|
|
)
|
|
|
|
# Relationships
|
|
user: Mapped["User"] = relationship("User", back_populates="folders")
|
|
trees: Mapped[list["Tree"]] = relationship(
|
|
"Tree",
|
|
secondary=user_folder_trees,
|
|
back_populates="folders"
|
|
)
|
|
# Self-referential relationships for folder hierarchy
|
|
parent: Mapped[Optional["UserFolder"]] = relationship(
|
|
"UserFolder",
|
|
remote_side="UserFolder.id",
|
|
back_populates="children",
|
|
foreign_keys=[parent_id]
|
|
)
|
|
children: Mapped[list["UserFolder"]] = relationship(
|
|
"UserFolder",
|
|
back_populates="parent",
|
|
cascade="all, delete-orphan",
|
|
foreign_keys="UserFolder.parent_id"
|
|
)
|
|
|
|
@property
|
|
def tree_count(self) -> int:
|
|
"""Returns the number of trees in this folder."""
|
|
return len(self.trees) if self.trees else 0
|