-
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
|
||||
from sqlalchemy import (
|
||||
Boolean,
|
||||
CheckConstraint,
|
||||
Date,
|
||||
ForeignKey,
|
||||
Index,
|
||||
Integer,
|
||||
Numeric,
|
||||
String,
|
||||
Text,
|
||||
false,
|
||||
text,
|
||||
)
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.models.base import Base, TimestampMixin
|
||||
from app.models.enums import ProjectCategory, ProjectStatus, db_enum
|
||||
|
||||
|
||||
class Project(TimestampMixin, Base):
|
||||
__tablename__ = "projects"
|
||||
__table_args__ = (
|
||||
CheckConstraint("capacity_kw IS NULL OR capacity_kw > 0", name="capacity_positive"),
|
||||
Index("ix_projects_published_category", "is_published", "category"),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
title: Mapped[str] = mapped_column(String(200))
|
||||
slug: Mapped[str] = mapped_column(String(220), unique=True, index=True)
|
||||
category: Mapped[ProjectCategory] = mapped_column(db_enum(ProjectCategory, "project_category"))
|
||||
location: Mapped[str | None] = mapped_column(String(200))
|
||||
capacity_kw: Mapped[Decimal | None] = mapped_column(Numeric(10, 2))
|
||||
installation_date: Mapped[date | None] = mapped_column(Date)
|
||||
description: Mapped[str | None] = mapped_column(Text)
|
||||
status: Mapped[ProjectStatus] = mapped_column(
|
||||
db_enum(ProjectStatus, "project_status"),
|
||||
default=ProjectStatus.COMPLETED,
|
||||
server_default=ProjectStatus.COMPLETED.value,
|
||||
)
|
||||
is_published: Mapped[bool] = mapped_column(Boolean, default=False, server_default=false())
|
||||
|
||||
images: Mapped[list["ProjectImage"]] = relationship(
|
||||
back_populates="project",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
order_by="(ProjectImage.sort_order, ProjectImage.id)",
|
||||
lazy="selectin",
|
||||
)
|
||||
|
||||
@property
|
||||
def featured_image(self) -> "ProjectImage | None":
|
||||
for img in self.images:
|
||||
if img.is_featured:
|
||||
return img
|
||||
return self.images[0] if self.images else None
|
||||
|
||||
|
||||
class ProjectImage(TimestampMixin, Base):
|
||||
__tablename__ = "project_images"
|
||||
__table_args__ = (
|
||||
# At most one featured image per project.
|
||||
Index(
|
||||
"uq_project_images_one_featured",
|
||||
"project_id",
|
||||
unique=True,
|
||||
postgresql_where=text("is_featured"),
|
||||
),
|
||||
CheckConstraint("file_size > 0", name="file_size_positive"),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
project_id: Mapped[int] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"), index=True)
|
||||
original_filename: Mapped[str] = mapped_column(String(255))
|
||||
# Paths are relative to UPLOAD_DIR, e.g. "watermarked/3f2a....jpg"
|
||||
original_path: Mapped[str] = mapped_column(String(300))
|
||||
watermarked_path: Mapped[str] = mapped_column(String(300))
|
||||
thumbnail_path: Mapped[str] = mapped_column(String(300))
|
||||
mime_type: Mapped[str] = mapped_column(String(50))
|
||||
file_size: Mapped[int] = mapped_column(Integer)
|
||||
width: Mapped[int] = mapped_column(Integer)
|
||||
height: Mapped[int] = mapped_column(Integer)
|
||||
caption: Mapped[str | None] = mapped_column(String(300))
|
||||
sort_order: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
|
||||
is_featured: Mapped[bool] = mapped_column(Boolean, default=False, server_default=false())
|
||||
|
||||
project: Mapped[Project] = relationship(back_populates="images")
|
||||
Reference in New Issue
Block a user