Files
sproutly/backend/schemas.py
derekc 84e7b13575 Add last login tracking, batch date auto-fill, and bug fixes
- Track last_login_at on User model, updated on every successful login
- Show last login date in admin panel user table
- Fix admin/garden date display (datetime strings already contain T separator)
- Fix My Garden Internal Server Error (MySQL does not support NULLS LAST syntax)
- Fix Log Batch infinite loop when user has zero varieties
- Auto-fill batch dates from today when creating a new batch, calculated
  from selected variety's week offsets (germination, greenhouse, garden)
- Update README with new features and batch date auto-fill formula table

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-09 00:48:04 -07:00

167 lines
3.7 KiB
Python

from __future__ import annotations
from datetime import date, datetime
from typing import Optional, List
from pydantic import BaseModel, EmailStr
from models import Category, SunRequirement, WaterNeeds, BatchStatus
# --- Auth ---
class UserCreate(BaseModel):
email: EmailStr
password: str
class UserLogin(BaseModel):
email: str
password: str
class UserOut(BaseModel):
id: int
email: str
is_admin: bool = False
is_disabled: bool = False
created_at: Optional[datetime] = None
model_config = {"from_attributes": True}
class AdminUserOut(BaseModel):
id: int
email: str
is_admin: bool
is_disabled: bool
created_at: Optional[datetime]
last_login_at: Optional[datetime]
variety_count: int
batch_count: int
class AdminResetPassword(BaseModel):
new_password: str
class Token(BaseModel):
access_token: str
token_type: str
# --- Variety ---
class VarietyBase(BaseModel):
name: str
variety_name: Optional[str] = None
category: Category = Category.vegetable
weeks_to_start: Optional[int] = None
weeks_to_greenhouse: Optional[int] = None
weeks_to_garden: Optional[int] = None
days_to_germinate: int = 7
direct_sow_ok: bool = False
frost_tolerant: bool = False
sun_requirement: SunRequirement = SunRequirement.full_sun
water_needs: WaterNeeds = WaterNeeds.medium
color: str = "#52b788"
notes: Optional[str] = None
class VarietyCreate(VarietyBase):
pass
class VarietyUpdate(VarietyBase):
pass
class VarietyOut(VarietyBase):
id: int
created_at: Optional[datetime] = None
model_config = {"from_attributes": True}
# --- Batch ---
class BatchBase(BaseModel):
variety_id: int
label: Optional[str] = None
quantity: int = 1
sow_date: Optional[date] = None
germination_date: Optional[date] = None
greenhouse_date: Optional[date] = None
garden_date: Optional[date] = None
status: BatchStatus = BatchStatus.planned
notes: Optional[str] = None
class BatchCreate(BatchBase):
pass
class BatchUpdate(BatchBase):
pass
class BatchOut(BatchBase):
id: int
created_at: Optional[datetime] = None
variety: Optional[VarietyOut] = None
model_config = {"from_attributes": True}
# --- Settings ---
class SettingsUpdate(BaseModel):
last_frost_date: Optional[date] = None
first_frost_fall_date: Optional[date] = None
ntfy_topic: Optional[str] = None
ntfy_server: Optional[str] = "https://ntfy.sh"
notification_time: Optional[str] = "07:00"
timezone: Optional[str] = "UTC"
location_name: Optional[str] = None
ntfy_username: Optional[str] = None
ntfy_password: Optional[str] = None
ntfy_api_key: Optional[str] = None
class SettingsOut(SettingsUpdate):
model_config = {"from_attributes": True}
# --- Dashboard ---
class Task(BaseModel):
type: str # "start_seeds", "pot_up", "transplant", "check_batch"
title: str
detail: str
due_date: date
days_away: int # negative = overdue
urgency: str # "overdue", "today", "week", "month"
variety_color: str
batch_id: Optional[int] = None
variety_id: Optional[int] = None
class TimelineEntry(BaseModel):
variety_id: int
name: str
full_name: str
color: str
start_day: Optional[int] = None # day of year
greenhouse_day: Optional[int] = None
garden_day: Optional[int] = None
end_day: Optional[int] = None # approx harvest / end of season
class DashboardOut(BaseModel):
tasks_overdue: List[Task]
tasks_today: List[Task]
tasks_week: List[Task]
tasks_month: List[Task]
active_batches: List[BatchOut]
timeline: List[TimelineEntry]
stats: dict
last_frost_date: Optional[date]
location_name: Optional[str]