-
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
from datetime import datetime
|
||||
|
||||
from pydantic import BaseModel, EmailStr, Field, field_validator
|
||||
|
||||
from app.models.enums import AdminRole
|
||||
from app.schemas.common import InputModel, OrmModel
|
||||
|
||||
|
||||
def _password_strength(value: str) -> str:
|
||||
if len(value) < 10:
|
||||
raise ValueError("Password must be at least 10 characters")
|
||||
if not any(c.isalpha() for c in value) or not any(c.isdigit() for c in value):
|
||||
raise ValueError("Password must contain letters and numbers")
|
||||
return value
|
||||
|
||||
|
||||
class LoginRequest(InputModel):
|
||||
email: EmailStr
|
||||
password: str = Field(min_length=1, max_length=256)
|
||||
|
||||
|
||||
class AdminOut(OrmModel):
|
||||
id: int
|
||||
email: EmailStr
|
||||
full_name: str
|
||||
role: AdminRole
|
||||
is_active: bool
|
||||
last_login_at: datetime | None
|
||||
created_at: datetime
|
||||
|
||||
|
||||
class TokenResponse(BaseModel):
|
||||
access_token: str
|
||||
token_type: str = "bearer"
|
||||
expires_in: int
|
||||
admin: AdminOut
|
||||
|
||||
|
||||
class ChangePasswordRequest(InputModel):
|
||||
current_password: str = Field(min_length=1, max_length=256)
|
||||
new_password: str = Field(max_length=256)
|
||||
|
||||
@field_validator("new_password")
|
||||
@classmethod
|
||||
def _pw(cls, v: str) -> str:
|
||||
return _password_strength(v)
|
||||
|
||||
|
||||
class AdminCreate(InputModel):
|
||||
email: EmailStr
|
||||
full_name: str = Field(min_length=2, max_length=150)
|
||||
password: str = Field(max_length=256)
|
||||
role: AdminRole = AdminRole.ADMIN
|
||||
|
||||
@field_validator("password")
|
||||
@classmethod
|
||||
def _pw(cls, v: str) -> str:
|
||||
return _password_strength(v)
|
||||
|
||||
|
||||
class AdminUpdate(InputModel):
|
||||
full_name: str | None = Field(default=None, min_length=2, max_length=150)
|
||||
role: AdminRole | None = None
|
||||
is_active: bool | None = None
|
||||
password: str | None = Field(default=None, max_length=256)
|
||||
|
||||
@field_validator("password")
|
||||
@classmethod
|
||||
def _pw(cls, v: str | None) -> str | None:
|
||||
return None if v is None else _password_strength(v)
|
||||
@@ -0,0 +1,41 @@
|
||||
import re
|
||||
from typing import Annotated, Generic, TypeVar
|
||||
|
||||
from pydantic import AfterValidator, BaseModel, ConfigDict
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
_PHONE_ALLOWED = re.compile(r"^\+?[0-9\s\-()]{7,20}$")
|
||||
|
||||
|
||||
def _validate_phone(value: str) -> str:
|
||||
value = value.strip()
|
||||
if not _PHONE_ALLOWED.match(value):
|
||||
raise ValueError("Enter a valid phone number")
|
||||
digits = re.sub(r"\D", "", value)
|
||||
if not 7 <= len(digits) <= 15:
|
||||
raise ValueError("Phone number must have 7 to 15 digits")
|
||||
return ("+" if value.startswith("+") else "") + digits
|
||||
|
||||
|
||||
Phone = Annotated[str, AfterValidator(_validate_phone)]
|
||||
|
||||
|
||||
class InputModel(BaseModel):
|
||||
model_config = ConfigDict(str_strip_whitespace=True, extra="forbid")
|
||||
|
||||
|
||||
class OrmModel(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
class Page(BaseModel, Generic[T]):
|
||||
items: list[T]
|
||||
total: int
|
||||
page: int
|
||||
page_size: int
|
||||
|
||||
|
||||
class MessageResponse(BaseModel):
|
||||
success: bool = True
|
||||
message: str
|
||||
@@ -0,0 +1,230 @@
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from pydantic import BaseModel, EmailStr, Field, field_validator
|
||||
|
||||
from app.schemas.common import InputModel, OrmModel, Phone
|
||||
from app.schemas.enquiry import EnquiryListItem
|
||||
from app.services.image_processing import media_url
|
||||
|
||||
# ----------------------------------------------------------------- services
|
||||
|
||||
|
||||
class ServicePublic(BaseModel):
|
||||
id: int
|
||||
title: str
|
||||
slug: str
|
||||
short_description: str | None
|
||||
description: str | None
|
||||
icon: str | None
|
||||
sort_order: int
|
||||
image_url: str | None
|
||||
thumbnail_url: str | None
|
||||
|
||||
@staticmethod
|
||||
def _fields(s) -> dict:
|
||||
return {
|
||||
"id": s.id,
|
||||
"title": s.title,
|
||||
"slug": s.slug,
|
||||
"short_description": s.short_description,
|
||||
"description": s.description,
|
||||
"icon": s.icon,
|
||||
"sort_order": s.sort_order,
|
||||
"image_url": media_url(s.image_watermarked_path),
|
||||
"thumbnail_url": media_url(s.image_thumbnail_path),
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def build(cls, s) -> "ServicePublic":
|
||||
return cls(**cls._fields(s))
|
||||
|
||||
|
||||
class ServiceAdmin(ServicePublic):
|
||||
is_published: bool
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
@classmethod
|
||||
def build(cls, s) -> "ServiceAdmin":
|
||||
return cls(
|
||||
**cls._fields(s),
|
||||
is_published=s.is_published,
|
||||
created_at=s.created_at,
|
||||
updated_at=s.updated_at,
|
||||
)
|
||||
|
||||
|
||||
class ServiceCreate(InputModel):
|
||||
title: str = Field(min_length=2, max_length=150)
|
||||
slug: str | None = Field(default=None, max_length=150, pattern=r"^[a-z0-9]+(?:-[a-z0-9]+)*$")
|
||||
short_description: str | None = Field(default=None, max_length=300)
|
||||
description: str | None = Field(default=None, max_length=10_000)
|
||||
icon: str | None = Field(default=None, max_length=60)
|
||||
sort_order: int = Field(default=0, ge=0, le=100_000)
|
||||
is_published: bool = False
|
||||
|
||||
|
||||
class ServiceUpdate(InputModel):
|
||||
title: str | None = Field(default=None, min_length=2, max_length=150)
|
||||
slug: str | None = Field(default=None, max_length=150, pattern=r"^[a-z0-9]+(?:-[a-z0-9]+)*$")
|
||||
short_description: str | None = Field(default=None, max_length=300)
|
||||
description: str | None = Field(default=None, max_length=10_000)
|
||||
icon: str | None = Field(default=None, max_length=60)
|
||||
sort_order: int | None = Field(default=None, ge=0, le=100_000)
|
||||
is_published: bool | None = None
|
||||
|
||||
|
||||
# ----------------------------------------------------------------- testimonials
|
||||
|
||||
|
||||
def _check_video_url(v: str | None) -> str | None:
|
||||
if v is None or not v.strip():
|
||||
return None
|
||||
v = v.strip()
|
||||
if not v.lower().startswith(("https://", "http://")):
|
||||
raise ValueError("Video link must start with https://")
|
||||
return v
|
||||
|
||||
|
||||
class TestimonialPublic(BaseModel):
|
||||
id: int
|
||||
customer_name: str
|
||||
designation: str | None
|
||||
location: str | None
|
||||
content: str | None
|
||||
video_url: str | None
|
||||
rating: int
|
||||
project_id: int | None
|
||||
sort_order: int
|
||||
image_url: str | None
|
||||
thumbnail_url: str | None
|
||||
|
||||
@staticmethod
|
||||
def _fields(t) -> dict:
|
||||
return {
|
||||
"id": t.id,
|
||||
"customer_name": t.customer_name,
|
||||
"designation": t.designation,
|
||||
"location": t.location,
|
||||
"content": t.content,
|
||||
"video_url": t.video_url,
|
||||
"rating": t.rating,
|
||||
"project_id": t.project_id,
|
||||
"sort_order": t.sort_order,
|
||||
"image_url": media_url(t.image_watermarked_path),
|
||||
"thumbnail_url": media_url(t.image_thumbnail_path),
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def build(cls, t) -> "TestimonialPublic":
|
||||
return cls(**cls._fields(t))
|
||||
|
||||
|
||||
class TestimonialAdmin(TestimonialPublic):
|
||||
is_published: bool
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
@classmethod
|
||||
def build(cls, t) -> "TestimonialAdmin":
|
||||
return cls(**cls._fields(t), is_published=t.is_published, created_at=t.created_at, updated_at=t.updated_at)
|
||||
|
||||
|
||||
class TestimonialCreate(InputModel):
|
||||
customer_name: str = Field(min_length=2, max_length=150)
|
||||
designation: str | None = Field(default=None, max_length=150)
|
||||
location: str | None = Field(default=None, max_length=150)
|
||||
content: str | None = Field(default=None, max_length=3000)
|
||||
video_url: str | None = Field(default=None, max_length=500)
|
||||
rating: int = Field(default=5, ge=1, le=5)
|
||||
project_id: int | None = None
|
||||
sort_order: int = Field(default=0, ge=0, le=100_000)
|
||||
is_published: bool = True
|
||||
|
||||
@field_validator("video_url")
|
||||
@classmethod
|
||||
def _video(cls, v: str | None) -> str | None:
|
||||
return _check_video_url(v)
|
||||
|
||||
|
||||
class TestimonialUpdate(InputModel):
|
||||
customer_name: str | None = Field(default=None, min_length=2, max_length=150)
|
||||
designation: str | None = Field(default=None, max_length=150)
|
||||
location: str | None = Field(default=None, max_length=150)
|
||||
content: str | None = Field(default=None, max_length=3000)
|
||||
video_url: str | None = Field(default=None, max_length=500)
|
||||
rating: int | None = Field(default=None, ge=1, le=5)
|
||||
project_id: int | None = None
|
||||
sort_order: int | None = Field(default=None, ge=0, le=100_000)
|
||||
is_published: bool | None = None
|
||||
|
||||
@field_validator("video_url")
|
||||
@classmethod
|
||||
def _video(cls, v: str | None) -> str | None:
|
||||
return _check_video_url(v)
|
||||
|
||||
|
||||
# ----------------------------------------------------------------- contact messages
|
||||
|
||||
|
||||
class ContactCreate(InputModel):
|
||||
name: str = Field(min_length=2, max_length=150)
|
||||
email: EmailStr
|
||||
phone: Phone | None = None
|
||||
subject: str | None = Field(default=None, max_length=200)
|
||||
message: str = Field(min_length=5, max_length=5000)
|
||||
website: str | None = Field(default=None, max_length=200) # honeypot
|
||||
|
||||
@field_validator("phone", "subject", "website", mode="before")
|
||||
@classmethod
|
||||
def _blank_to_none(cls, v):
|
||||
return None if isinstance(v, str) and not v.strip() else v
|
||||
|
||||
|
||||
class ContactOut(OrmModel):
|
||||
id: int
|
||||
name: str
|
||||
email: str
|
||||
phone: str | None
|
||||
subject: str | None
|
||||
message: str
|
||||
is_read: bool
|
||||
created_at: datetime
|
||||
|
||||
|
||||
class ContactUpdate(InputModel):
|
||||
is_read: bool
|
||||
|
||||
|
||||
# ----------------------------------------------------------------- site content
|
||||
|
||||
|
||||
class SiteContentOut(OrmModel):
|
||||
key: str
|
||||
value: dict[str, Any]
|
||||
updated_at: datetime
|
||||
|
||||
|
||||
class SiteContentUpsert(InputModel):
|
||||
value: dict[str, Any] = Field(description="Arbitrary JSON object for this content block")
|
||||
|
||||
|
||||
# ----------------------------------------------------------------- dashboard
|
||||
|
||||
|
||||
class DashboardStats(BaseModel):
|
||||
total_enquiries: int
|
||||
new_enquiries: int
|
||||
contacted: int
|
||||
site_visits_scheduled: int
|
||||
quotations_sent: int
|
||||
converted: int
|
||||
rejected: int
|
||||
enquiries_last_30_days: int
|
||||
completed_projects: int
|
||||
published_projects: int
|
||||
total_projects: int
|
||||
published_services: int
|
||||
unread_contact_messages: int
|
||||
recent_enquiries: list[EnquiryListItem]
|
||||
@@ -0,0 +1,67 @@
|
||||
from datetime import datetime
|
||||
|
||||
from pydantic import EmailStr, Field, field_validator
|
||||
|
||||
from app.models.enums import EnquiryStatus, PropertyType
|
||||
from app.schemas.common import InputModel, OrmModel, Phone
|
||||
|
||||
|
||||
class EnquiryCreate(InputModel):
|
||||
name: str = Field(min_length=2, max_length=150)
|
||||
phone: Phone
|
||||
email: EmailStr | None = None
|
||||
city: str | None = Field(default=None, max_length=120)
|
||||
address: str | None = Field(default=None, max_length=500)
|
||||
property_type: PropertyType | None = None
|
||||
monthly_bill: float | None = Field(default=None, ge=0, le=100_000_000)
|
||||
required_capacity_kw: float | None = Field(default=None, gt=0, le=100_000)
|
||||
message: str | None = Field(default=None, max_length=2000)
|
||||
# Honeypot: real users never see/fill this field; bots often do.
|
||||
website: str | None = Field(default=None, max_length=200)
|
||||
|
||||
@field_validator("email", "city", "address", "message", "website", mode="before")
|
||||
@classmethod
|
||||
def _blank_to_none(cls, v):
|
||||
return None if isinstance(v, str) and not v.strip() else v
|
||||
|
||||
|
||||
class EnquiryNoteOut(OrmModel):
|
||||
id: int
|
||||
content: str
|
||||
author_name: str | None = None
|
||||
created_at: datetime
|
||||
|
||||
@classmethod
|
||||
def from_note(cls, note) -> "EnquiryNoteOut":
|
||||
return cls(
|
||||
id=note.id,
|
||||
content=note.content,
|
||||
author_name=note.author.full_name if note.author else None,
|
||||
created_at=note.created_at,
|
||||
)
|
||||
|
||||
|
||||
class EnquiryListItem(OrmModel):
|
||||
id: int
|
||||
name: str
|
||||
phone: str | None
|
||||
email: str | None
|
||||
city: str | None
|
||||
property_type: PropertyType | None
|
||||
required_capacity_kw: float | None
|
||||
status: EnquiryStatus
|
||||
source: str
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
|
||||
class EnquiryDetail(EnquiryListItem):
|
||||
address: str | None
|
||||
monthly_bill: float | None
|
||||
message: str | None
|
||||
notes: list[EnquiryNoteOut] = []
|
||||
|
||||
|
||||
class EnquiryUpdate(InputModel):
|
||||
status: EnquiryStatus | None = None
|
||||
note: str | None = Field(default=None, min_length=1, max_length=5000, description="Appends an internal note")
|
||||
@@ -0,0 +1,161 @@
|
||||
from datetime import date, datetime
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.models.enums import ProjectCategory, ProjectStatus
|
||||
from app.schemas.common import InputModel
|
||||
from app.services.image_processing import media_url
|
||||
|
||||
|
||||
class ProjectImagePublic(BaseModel):
|
||||
id: int
|
||||
url: str
|
||||
thumbnail_url: str
|
||||
width: int
|
||||
height: int
|
||||
caption: str | None
|
||||
is_featured: bool
|
||||
sort_order: int
|
||||
|
||||
@classmethod
|
||||
def build(cls, img) -> "ProjectImagePublic":
|
||||
return cls(
|
||||
id=img.id,
|
||||
url=media_url(img.watermarked_path),
|
||||
thumbnail_url=media_url(img.thumbnail_path),
|
||||
width=img.width,
|
||||
height=img.height,
|
||||
caption=img.caption,
|
||||
is_featured=img.is_featured,
|
||||
sort_order=img.sort_order,
|
||||
)
|
||||
|
||||
|
||||
class ProjectImageAdmin(ProjectImagePublic):
|
||||
project_id: int
|
||||
original_filename: str
|
||||
mime_type: str
|
||||
file_size: int
|
||||
original_download_path: str
|
||||
created_at: datetime
|
||||
|
||||
@classmethod
|
||||
def build(cls, img) -> "ProjectImageAdmin":
|
||||
base = ProjectImagePublic.build(img).model_dump()
|
||||
return cls(
|
||||
**base,
|
||||
project_id=img.project_id,
|
||||
original_filename=img.original_filename,
|
||||
mime_type=img.mime_type,
|
||||
file_size=img.file_size,
|
||||
original_download_path=f"/api/admin/project-images/{img.id}/original",
|
||||
created_at=img.created_at,
|
||||
)
|
||||
|
||||
|
||||
class _ProjectBase(BaseModel):
|
||||
id: int
|
||||
title: str
|
||||
slug: str
|
||||
category: ProjectCategory
|
||||
location: str | None
|
||||
capacity_kw: float | None
|
||||
installation_date: date | None
|
||||
description: str | None
|
||||
status: ProjectStatus
|
||||
|
||||
@staticmethod
|
||||
def _fields(p) -> dict:
|
||||
return {
|
||||
"id": p.id,
|
||||
"title": p.title,
|
||||
"slug": p.slug,
|
||||
"category": p.category,
|
||||
"location": p.location,
|
||||
"capacity_kw": float(p.capacity_kw) if p.capacity_kw is not None else None,
|
||||
"installation_date": p.installation_date,
|
||||
"description": p.description,
|
||||
"status": p.status,
|
||||
}
|
||||
|
||||
|
||||
class ProjectPublicSummary(_ProjectBase):
|
||||
featured_image: ProjectImagePublic | None
|
||||
image_count: int
|
||||
|
||||
@classmethod
|
||||
def build(cls, p) -> "ProjectPublicSummary":
|
||||
fi = p.featured_image
|
||||
return cls(
|
||||
**cls._fields(p),
|
||||
featured_image=ProjectImagePublic.build(fi) if fi else None,
|
||||
image_count=len(p.images),
|
||||
)
|
||||
|
||||
|
||||
class ProjectPublicDetail(ProjectPublicSummary):
|
||||
images: list[ProjectImagePublic]
|
||||
|
||||
@classmethod
|
||||
def build(cls, p) -> "ProjectPublicDetail":
|
||||
return cls(
|
||||
**ProjectPublicSummary.build(p).model_dump(),
|
||||
images=[ProjectImagePublic.build(i) for i in p.images],
|
||||
)
|
||||
|
||||
|
||||
class ProjectAdmin(_ProjectBase):
|
||||
is_published: bool
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
featured_image: ProjectImageAdmin | None
|
||||
images: list[ProjectImageAdmin]
|
||||
|
||||
@classmethod
|
||||
def build(cls, p) -> "ProjectAdmin":
|
||||
fi = p.featured_image
|
||||
return cls(
|
||||
**cls._fields(p),
|
||||
is_published=p.is_published,
|
||||
created_at=p.created_at,
|
||||
updated_at=p.updated_at,
|
||||
featured_image=ProjectImageAdmin.build(fi) if fi else None,
|
||||
images=[ProjectImageAdmin.build(i) for i in p.images],
|
||||
)
|
||||
|
||||
|
||||
class ProjectCreate(InputModel):
|
||||
title: str = Field(min_length=2, max_length=200)
|
||||
slug: str | None = Field(default=None, max_length=200, pattern=r"^[a-z0-9]+(?:-[a-z0-9]+)*$")
|
||||
category: ProjectCategory
|
||||
location: str | None = Field(default=None, max_length=200)
|
||||
capacity_kw: float | None = Field(default=None, gt=0, le=100_000)
|
||||
installation_date: date | None = None
|
||||
description: str | None = Field(default=None, max_length=10_000)
|
||||
status: ProjectStatus = ProjectStatus.COMPLETED
|
||||
is_published: bool = False
|
||||
|
||||
|
||||
class ProjectUpdate(InputModel):
|
||||
"""PUT body. Only fields that are sent are changed."""
|
||||
|
||||
title: str | None = Field(default=None, min_length=2, max_length=200)
|
||||
slug: str | None = Field(default=None, max_length=200, pattern=r"^[a-z0-9]+(?:-[a-z0-9]+)*$")
|
||||
category: ProjectCategory | None = None
|
||||
location: str | None = Field(default=None, max_length=200)
|
||||
capacity_kw: float | None = Field(default=None, gt=0, le=100_000)
|
||||
installation_date: date | None = None
|
||||
description: str | None = Field(default=None, max_length=10_000)
|
||||
status: ProjectStatus | None = None
|
||||
is_published: bool | None = None
|
||||
|
||||
|
||||
class ProjectImageUpdate(InputModel):
|
||||
caption: str | None = Field(default=None, max_length=300)
|
||||
sort_order: int | None = Field(default=None, ge=0, le=100_000)
|
||||
is_featured: bool | None = Field(default=None, description="true makes this the project's featured image")
|
||||
|
||||
|
||||
class UploadResult(BaseModel):
|
||||
uploaded: list[ProjectImageAdmin]
|
||||
project: ProjectAdmin
|
||||
Reference in New Issue
Block a user