PsychoScalesOrg/database.py
mxr612 319844e55c feat: add CSV download endpoint for scale results
- Implemented a new endpoint to download scale results as a CSV file.
- The endpoint retrieves responses based on the scale ID and formats the data, including question IDs and subscale scores, into a CSV format.
- Updated database URL to remove the public directory reference for better file access.
2025-06-16 10:19:52 +08:00

38 lines
1.1 KiB
Python

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
import json
SQLALCHEMY_DATABASE_URL = "sqlite:///./psychoscales.db"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False},
json_serializer=lambda obj: json.dumps(obj, ensure_ascii=False)
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
class ScaleResult(Base):
__tablename__ = "responses"
id = Column(Integer, primary_key=True, index=True)
scale_id = Column(String, index=True)
user_agent = Column(String)
ip_address = Column(String)
location = Column(JSON)
raw_response = Column(JSON)
sum_response = Column(JSON)
avg_response = Column(JSON)
created_at = Column(DateTime)
# Create tables
Base.metadata.create_all(bind=engine)
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()