Add seasonguessresult table

This commit is contained in:
2024-02-27 20:21:37 +01:00
parent 44549f019d
commit 262bcc8d5e
5 changed files with 74 additions and 6 deletions

View File

@ -0,0 +1 @@
user_name,hot_take_correct,overtakes_correct
1 user_name hot_take_correct overtakes_correct

View File

@ -4,7 +4,7 @@ from flask import redirect, render_template, request
from werkzeug import Response
from formula10.database.update_queries import update_race_result, update_user
from formula10.database.import_export import export_dynamic_data, reload_static_data
from formula10.database.import_export import export_dynamic_data, reload_season_guess_result_data, reload_static_data
from formula10.domain.template_model import TemplateModel
from formula10 import app
@ -28,6 +28,12 @@ def load_static() -> Response:
return redirect("/")
@app.route("/load/seasonresults")
def load_season_results() -> Response:
reload_season_guess_result_data()
return redirect("/")
# @app.route("/load/dynamic")
# def load_dynamic() -> Response:
# reload_dynamic_data()

View File

@ -8,6 +8,7 @@ from formula10.database.model.db_race import DbRace
from formula10.database.model.db_race_guess import DbRaceGuess
from formula10.database.model.db_race_result import DbRaceResult
from formula10.database.model.db_season_guess import DbSeasonGuess
from formula10.database.model.db_season_guess_result import DbSeasonGuessResult
from formula10.database.model.db_team import DbTeam
from formula10.database.model.db_user import DbUser
@ -37,8 +38,8 @@ def write_csv(filename: str, objects: List[Any]):
# Reload static database data, this has to be called from the app context
def reload_static_data():
print("Initializing Database with Static Values...")
# Create it (if it doesn't exist!)
print("Initializing database with static values...")
# Create it/update tables (if it/they doesn't exist!)
db.create_all()
# Clear static data
@ -58,8 +59,8 @@ def reload_static_data():
def reload_dynamic_data():
print("Initializing Database with Dynamic Values...")
# Create it (if it doesn't exist!)
print("Initializing database with dynamic values...")
# Create it/update tables (if it/they doesn't exist!)
db.create_all()
# Clear dynamic data
@ -81,6 +82,21 @@ def reload_dynamic_data():
db.session.commit()
def reload_season_guess_result_data():
print("Loading season guess results...")
# Create it/update tables (if it/they doesn't exist!)
db.create_all()
# Clear result data
db.session.query(DbSeasonGuessResult).delete()
# Reload result data
for row in load_csv("data/static_import/season_guess_results.csv"):
db.session.add(DbSeasonGuessResult.from_csv(row))
db.session.commit()
def export_dynamic_data():
print("Exporting Userdata...")

View File

@ -0,0 +1,44 @@
from typing import List
from sqlalchemy import Boolean, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from formula10 import db
from formula10.database.model.db_user import DbUser
class DbSeasonGuessResult(db.Model):
"""
Manually entered results for the season bonus guesses.
"""
__tablename__ = "seasonguessresult"
__csv_header__ = ["user_name", "hot_take_correct", "overtakes_correct"]
def __init__(self, *, user_name: str, hot_take_correct: bool, overtakes_correct: bool):
self.user_name = user_name # Primary key
self.hot_take_correct = hot_take_correct
self.overtakes_correct = overtakes_correct
@classmethod
def from_csv(cls, row: List[str]):
db_season_guess_result: DbSeasonGuessResult = cls(user_name=str(row[0]),
hot_take_correct=True if str(row[1])=="True" else False,
overtakes_correct=True if str(row[2])=="True" else False)
return db_season_guess_result
# This object can't be edited from the page context
# def to_csv(self) -> List[Any]:
# return [
# self.user_name,
# self.hot_take_correct,
# self.overtakes_correct
# ]
user_name: Mapped[str] = mapped_column(ForeignKey("user.name"), primary_key=True)
hot_take_correct: Mapped[bool] = mapped_column(Boolean)
overtakes_correct: Mapped[bool] = mapped_column(Boolean)
# Relationships
user: Mapped[DbUser] = relationship("DbUser", foreign_keys=[user_name])

View File

@ -19,7 +19,8 @@ class DbUser(db.Model):
@classmethod
def from_csv(cls, row: List[str]):
db_user: DbUser = cls(name=str(row[0]), enabled=True if str(row[1])=="True" else False)
db_user: DbUser = cls(name=str(row[0]),
enabled=True if str(row[1])=="True" else False)
return db_user
def to_csv(self) -> List[Any]: