feat: add daily + global notes
This commit is contained in:
+61
-2
@@ -1,5 +1,5 @@
|
||||
from datetime import date as date_type
|
||||
from datetime import timedelta
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional
|
||||
|
||||
from django.db.models import Avg, Max, Min
|
||||
@@ -10,7 +10,14 @@ from ninja import Router, Schema
|
||||
from ninja.errors import HttpError
|
||||
|
||||
from accounts.auth import AUTH
|
||||
from tracker.models import DoseLog, Medication, PulseReading, ScheduledDose
|
||||
from tracker.models import (
|
||||
DailyNote,
|
||||
DoseLog,
|
||||
GlobalNote,
|
||||
Medication,
|
||||
PulseReading,
|
||||
ScheduledDose,
|
||||
)
|
||||
|
||||
router = Router(auth=AUTH)
|
||||
|
||||
@@ -48,10 +55,30 @@ class PulseOut(Schema):
|
||||
notes: str
|
||||
|
||||
|
||||
class DailyNoteOut(Schema):
|
||||
date: date_type
|
||||
body: str
|
||||
|
||||
|
||||
class DailyNoteIn(Schema):
|
||||
body: str
|
||||
date: Optional[date_type] = None
|
||||
|
||||
|
||||
class GlobalNoteOut(Schema):
|
||||
body: str
|
||||
updated_at: Optional[datetime] = None
|
||||
|
||||
|
||||
class GlobalNoteIn(Schema):
|
||||
body: str
|
||||
|
||||
|
||||
class DayOut(Schema):
|
||||
date: date_type
|
||||
doses: list[DoseStatusOut]
|
||||
pulse: Optional[PulseOut]
|
||||
note: Optional[DailyNoteOut]
|
||||
|
||||
|
||||
class DoseLogIn(Schema):
|
||||
@@ -116,6 +143,7 @@ def build_day(day: date_type) -> dict:
|
||||
for dose in doses
|
||||
],
|
||||
"pulse": PulseReading.objects.filter(date=day).first(),
|
||||
"note": DailyNote.objects.filter(date=day).first(),
|
||||
}
|
||||
|
||||
|
||||
@@ -190,6 +218,37 @@ def log_dose(request: HttpRequest, payload: DoseLogIn):
|
||||
return build_day(day)
|
||||
|
||||
|
||||
# --- Notes -----------------------------------------------------------------
|
||||
|
||||
|
||||
@router.post("/notes/daily", response=DayOut)
|
||||
def save_daily_note(request: HttpRequest, payload: DailyNoteIn):
|
||||
"""The day's journal entry. One per day; an empty body removes it."""
|
||||
day = payload.date or timezone.localdate()
|
||||
|
||||
if payload.body.strip():
|
||||
DailyNote.objects.update_or_create(date=day, defaults={"body": payload.body})
|
||||
else:
|
||||
DailyNote.objects.filter(date=day).delete()
|
||||
|
||||
return build_day(day)
|
||||
|
||||
|
||||
@router.get("/notes/global", response=GlobalNoteOut)
|
||||
def get_global_note(request: HttpRequest):
|
||||
"""The single shared note: standing instructions and things to do."""
|
||||
return GlobalNote.load()
|
||||
|
||||
|
||||
@router.put("/notes/global", response=GlobalNoteOut)
|
||||
def save_global_note(request: HttpRequest, payload: GlobalNoteIn):
|
||||
"""Replace the shared note's content. Last write wins."""
|
||||
note = GlobalNote.load()
|
||||
note.body = payload.body
|
||||
note.save(update_fields=["body", "updated_at"])
|
||||
return note
|
||||
|
||||
|
||||
# --- Pulse -----------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user