wanderhome-quiz/server/app/models/entry.py

26 lines
731 B
Python
Raw Normal View History

2022-08-24 15:55:53 +01:00
from ..extensions import db
2022-08-31 01:13:49 +01:00
from ..tools.models import JsonString
2022-08-24 15:55:53 +01:00
from sqlalchemy_json import MutableJson
from datetime import datetime
from uuid import uuid4
2022-08-31 01:13:49 +01:00
class Entry(db.Model):
2022-08-24 15:55:53 +01:00
id = db.Column(db.String(36), primary_key=True)
timestamp = db.Column(db.DateTime, nullable=False)
2022-08-31 01:13:49 +01:00
answers = db.Column(JsonString, nullable=False)
results = db.Column(MutableJson, nullable=False)
2022-08-24 15:55:53 +01:00
def __repr__(self) -> str: return f'Entry with <id {self.id}>.'
2022-08-31 01:13:49 +01:00
def __init__(self, answers:list, results:dict):
self.id = uuid4().hex
self.timestamp = datetime.utcnow()
self.answers = answers
self.results = results
2022-08-24 15:55:53 +01:00
2022-08-31 01:13:49 +01:00
def add(self):
db.session.add(self)
db.session.commit()