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:
parent
90720b3cec
commit
fc9d439cf9
4
app.py
4
app.py
@ -11,6 +11,7 @@ from xml.etree import ElementTree as ET
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from database import get_db, ScaleResult
|
from database import get_db, ScaleResult
|
||||||
import geoip2.database
|
import geoip2.database
|
||||||
|
from datetime import datetime, UTC
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
templates = Jinja2Templates(directory="templates")
|
templates = Jinja2Templates(directory="templates")
|
||||||
@ -133,7 +134,8 @@ async def result(request: Request, scale_id: str, db: Session = Depends(get_db))
|
|||||||
location=location,
|
location=location,
|
||||||
raw_response=dict(form_data),
|
raw_response=dict(form_data),
|
||||||
sum_response=responses,
|
sum_response=responses,
|
||||||
avg_response=average
|
avg_response=average,
|
||||||
|
created_at=datetime.now(UTC)
|
||||||
)
|
)
|
||||||
db.add(db_response)
|
db.add(db_response)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
from sqlalchemy import create_engine, Column, Integer, String, Float, DateTime, ForeignKey, JSON
|
from sqlalchemy import create_engine, Column, Integer, String, Float, DateTime, ForeignKey, JSON
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.orm import sessionmaker, relationship
|
from sqlalchemy.orm import sessionmaker, relationship
|
||||||
from datetime import datetime, UTC
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
SQLALCHEMY_DATABASE_URL = "sqlite:///./public/psychoscales.db"
|
SQLALCHEMY_DATABASE_URL = "sqlite:///./public/psychoscales.db"
|
||||||
@ -25,7 +24,7 @@ class ScaleResult(Base):
|
|||||||
raw_response = Column(JSON)
|
raw_response = Column(JSON)
|
||||||
sum_response = Column(JSON)
|
sum_response = Column(JSON)
|
||||||
avg_response = Column(JSON)
|
avg_response = Column(JSON)
|
||||||
created_at = Column(DateTime, default=datetime.now(UTC))
|
created_at = Column(DateTime)
|
||||||
|
|
||||||
# Create tables
|
# Create tables
|
||||||
Base.metadata.create_all(bind=engine)
|
Base.metadata.create_all(bind=engine)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user