From 8b2920f88678d9d376fc1b4daedb2450d4c17303 Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Sat, 9 Mar 2024 19:48:07 +0100 Subject: [PATCH] Add dummy values to race result These columns are not marked nullable, so ignoring them prevents entering of race results --- flake.nix | 3 --- formula10/__init__.py | 8 ++++++-- formula10/database/model/db_race_result.py | 8 +++++++- formula10/database/update_queries.py | 11 ++++++++++ formula10/domain/model/race_result.py | 24 ++++++++++++++++++++++ 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/flake.nix b/flake.nix index 96a6698..cd7077c 100644 --- a/flake.nix +++ b/flake.nix @@ -37,9 +37,6 @@ nodePackages.sass nodePackages.postcss-cli nodePackages.autoprefixer - - sqlitebrowser # To modify tables - dbeaver # To import/export data + diagrams ]; # Use $1 for positional args diff --git a/formula10/__init__.py b/formula10/__init__.py index 6ec0860..bdd751b 100644 --- a/formula10/__init__.py +++ b/formula10/__init__.py @@ -33,16 +33,20 @@ import formula10.controller.error_controller # TODO # Large DB Update -# - Don't use names for frontend post requests, either use IDs or post the whole object (if its possible)... +# - Don't use names for frontend post requests, use IDs # - For season guess calc there is missing: Fastest laps + sprint points + sprint DNFs (in race result) # - Mask to allow changing usernames (easy if name is not used as ID) # - Maybe even masks for races + drivers + teams? # - DB fields for links to F1 site - NO: just hardcode them in with a dictionary -# - DB fields for qualifying dates # Leaderboards/Points # - Auto calculate season points (display season points in table + season guess card title?) +# Optimizations +# - Optimize PointsModel + TemplateModel. In case something is calculated often, cache it. +# - NEVER do manual DB queries, except in the DomainModel! + # General +# - Adapt diagram colors to team colors # - Add links to the official F1 stats page (for quali/result), probably best to store entire link in DB (because they are not entirely regular)? # - Unit testing (as much as possible, but especially points calculation) \ No newline at end of file diff --git a/formula10/database/model/db_race_result.py b/formula10/database/model/db_race_result.py index 39bcd64..c324dce 100644 --- a/formula10/database/model/db_race_result.py +++ b/formula10/database/model/db_race_result.py @@ -1,5 +1,6 @@ from sqlalchemy import ForeignKey, String from sqlalchemy.orm import Mapped, mapped_column, relationship +from formula10.database.model.db_driver import DbDriver from formula10.database.model.db_race import DbRace from formula10 import db @@ -20,5 +21,10 @@ class DbRaceResult(db.Model): dnf_driver_ids_json: Mapped[str] = mapped_column(String(1024), nullable=False) excluded_driver_ids_json: Mapped[str] = mapped_column(String(1024), nullable=False) + fastest_lap_id: Mapped[int] = mapped_column(ForeignKey("driver.id"), nullable=False) + sprint_dnf_driver_ids_json: Mapped[str] = mapped_column(String(1024), nullable=False) + sprint_points_json: Mapped[str] = mapped_column(String(1024), nullable=False) + # Relationships - race: Mapped[DbRace] = relationship("DbRace", foreign_keys=[race_id]) \ No newline at end of file + race: Mapped[DbRace] = relationship("DbRace", foreign_keys=[race_id]) + fastest_lap_driver: Mapped[DbDriver] = relationship("DbDriver", foreign_keys=[fastest_lap_id]) \ No newline at end of file diff --git a/formula10/database/update_queries.py b/formula10/database/update_queries.py index 2dbfb13..626e2ac 100644 --- a/formula10/database/update_queries.py +++ b/formula10/database/update_queries.py @@ -13,6 +13,7 @@ from formula10.database.model.db_season_guess import DbSeasonGuess from formula10.database.model.db_user import DbUser from formula10.database.validation import any_is_none, positions_are_contiguous, race_has_started from formula10 import ENABLE_TIMING, db +from formula10.domain.model.driver import NONE_DRIVER def find_or_create_race_guess(user_id: int, race_id: int) -> DbRaceGuess: @@ -126,6 +127,11 @@ def find_or_create_race_result(race_id: int) -> DbRaceResult: race_result.first_dnf_driver_ids_json = json.dumps(["9999"]) race_result.dnf_driver_ids_json = json.dumps(["9999"]) race_result.excluded_driver_ids_json = json.dumps(["9999"]) + + race_result.fastest_lap_id = 9999 + race_result.sprint_dnf_driver_ids_json = json.dumps(["9999"]) + race_result.sprint_points_json = json.dumps({"9999": "9999"}) + db.session.add(race_result) db.session.commit() @@ -169,6 +175,11 @@ def update_race_result(race_id: int, pxx_driver_ids_list: List[str], first_dnf_d race_result.dnf_driver_ids_json = json.dumps(dnf_driver_ids_list) race_result.excluded_driver_ids_json = json.dumps(excluded_driver_ids_list) + # @todo Dummy values + race_result.fastest_lap_id = NONE_DRIVER.id + race_result.sprint_dnf_driver_ids_json = json.dumps([NONE_DRIVER.id]) + race_result.sprint_points_json = json.dumps({NONE_DRIVER.id: 0}) + db.session.commit() race: DbRace | None = db.session.query(DbRace).filter_by(id=race_id).first() diff --git a/formula10/domain/model/race_result.py b/formula10/domain/model/race_result.py index 94f5ace..03835a0 100644 --- a/formula10/domain/model/race_result.py +++ b/formula10/domain/model/race_result.py @@ -12,12 +12,15 @@ class RaceResult: def from_db_race_result(cls, db_race_result: DbRaceResult): race_result: RaceResult = cls() race_result.race = Race.from_db_race(db_race_result.race) + race_result.fastest_lap_driver = Driver.from_db_driver(db_race_result.fastest_lap_driver) # Deserialize from json standing: Dict[str, str] = json.loads(db_race_result.pxx_driver_ids_json) initial_dnf: List[str] = json.loads(db_race_result.first_dnf_driver_ids_json) all_dnfs: List[str] = json.loads(db_race_result.dnf_driver_ids_json) standing_exclusions: List[str] = json.loads(db_race_result.excluded_driver_ids_json) + sprint_dnfs: List[str] = json.loads(db_race_result.sprint_dnf_driver_ids_json) + sprint_standing: Dict[str, str] = json.loads(db_race_result.sprint_points_json) # Populate relationships race_result.standing = { @@ -36,6 +39,14 @@ class RaceResult: Driver.from_db_driver(find_single_driver_strict(int(driver_id))) for driver_id in standing_exclusions ] + race_result.sprint_dnfs = [ + Driver.from_db_driver(find_single_driver_strict(int(driver_id))) + for driver_id in sprint_dnfs + ] + race_result.sprint_standing = { + position: Driver.from_db_driver(find_single_driver_strict(int(driver_id))) + for position, driver_id in sprint_standing.items() + } return race_result @@ -53,6 +64,12 @@ class RaceResult: standing_exclusions: List[str] = [ str(driver.id) for driver in self.standing_exclusions if driver ] + sprint_dnfs: List[str] = [ + str(driver.id) for driver in self.sprint_dnfs if driver + ] + sprint_standing: Dict[str, str] = { + position: driver.name for position, driver in self.sprint_standing.items() + } # Serialize to json db_race_result: DbRaceResult = DbRaceResult(race_id=self.race.id) @@ -60,6 +77,9 @@ class RaceResult: db_race_result.first_dnf_driver_ids_json = json.dumps(initial_dnf) db_race_result.dnf_driver_ids_json = json.dumps(all_dnfs) db_race_result.excluded_driver_ids_json = json.dumps(standing_exclusions) + db_race_result.fastest_lap_id = self.fastest_lap_driver.id + db_race_result.sprint_dnf_driver_ids_json = json.dumps(sprint_dnfs) + db_race_result.sprint_points_json = json.dumps(sprint_standing) return db_race_result @@ -78,6 +98,10 @@ class RaceResult: all_dnfs: List[Driver] standing_exclusions: List[Driver] + fastest_lap_driver: Driver + sprint_dnfs: List[Driver] + sprint_standing: Dict[str, Driver] + def offset_from_place_to_guess(self, offset: int, respect_nc:bool = True) -> Driver: position: str = str(self.race.place_to_guess + offset)