feat: add created_at timestamp to ScaleResult model and response handling

- Introduced created_at field in the ScaleResult model to track response creation time.
- Updated result handling logic in app.py to populate created_at with the current UTC time when saving responses.
This commit is contained in:
Miu Li 2025-06-16 09:08:21 +08:00
parent 90720b3cec
commit fc9d439cf9
2 changed files with 4 additions and 3 deletions

4
app.py
View File

@ -11,6 +11,7 @@ from xml.etree import ElementTree as ET
from sqlalchemy.orm import Session
from database import get_db, ScaleResult
import geoip2.database
from datetime import datetime, UTC
app = FastAPI()
templates = Jinja2Templates(directory="templates")
@ -133,7 +134,8 @@ async def result(request: Request, scale_id: str, db: Session = Depends(get_db))
location=location,
raw_response=dict(form_data),
sum_response=responses,
avg_response=average
avg_response=average,
created_at=datetime.now(UTC)
)
db.add(db_response)
db.commit()

View File

@ -1,7 +1,6 @@
from sqlalchemy import create_engine, Column, Integer, String, Float, DateTime, ForeignKey, JSON
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
from datetime import datetime, UTC
import json
SQLALCHEMY_DATABASE_URL = "sqlite:///./public/psychoscales.db"
@ -25,7 +24,7 @@ class ScaleResult(Base):
raw_response = Column(JSON)
sum_response = Column(JSON)
avg_response = Column(JSON)
created_at = Column(DateTime, default=datetime.now(UTC))
created_at = Column(DateTime)
# Create tables
Base.metadata.create_all(bind=engine)