This commit is contained in:
2023-11-04 00:34:13 +01:00
parent 6796f4b207
commit b0d16a037f
3 changed files with 33 additions and 12 deletions

View File

@ -1,19 +1,20 @@
import csv import csv
from model import * from model import *
def load_csv(filename): def load_csv(filename):
with open("data/" + filename + ".csv", newline="") as file: with open("data/" + filename + ".csv", newline="") as file:
reader = csv.reader(file, delimiter=",") reader = csv.reader(file, delimiter=",")
next(reader, None) # skip header next(reader, None) # skip header
return list(reader) return list(reader)
# @todo CSV-Writer # @todo CSV-Writer
def write_csv(filename): def write_csv(filename):
with open("data/" + filename + ".csv", newline="") as file: with open("data/" + filename + ".csv", newline="") as file:
writer = csv.writer(file, delimiter=",") writer = csv.writer(file, delimiter=",")
# @todo Complete CSV Files
# @todo Reload Static Data
# Reload static database data, this has to be called from the app context # Reload static database data, this has to be called from the app context
def reload_static_data(db): def reload_static_data(db):
print("Initializing DataBase with Static Values...") print("Initializing DataBase with Static Values...")
@ -45,6 +46,7 @@ def reload_static_data(db):
db.session.commit() db.session.commit()
# @todo Export Dynamic Data # @todo Export Dynamic Data
def export_dynamic_data(): def export_dynamic_data():
pass pass

View File

@ -5,17 +5,19 @@ from database_utils import reload_static_data, export_dynamic_data
app = Flask(__name__) app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///formula10.db"; app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///formula10.db"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False; app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app) db.init_app(app)
@app.route("/") @app.route("/")
def index(): def index():
reload_static_data(db) reload_static_data(db)
return render_template("index.jinja") return render_template("index.jinja")
# @app.route("/teams", methods=["GET", "POST"]) # @app.route("/teams", methods=["GET", "POST"])
# def teams(): # def teams():
# if request.method == "POST": # if request.method == "POST":

View File

@ -10,12 +10,14 @@ db = SQLAlchemy()
# Modeling these entities separately should make it easier to add new guess categories, for example team guesses or GP guesses # Modeling these entities separately should make it easier to add new guess categories, for example team guesses or GP guesses
def optional_date(date_string): def optional_date(date_string):
if date_string == "\\N": if date_string == "\\N":
return None return None
return dt.date.fromisoformat(date_string) return dt.date.fromisoformat(date_string)
def optional_time(time_string): def optional_time(time_string):
if time_string == "\\N": if time_string == "\\N":
return None return None
@ -26,18 +28,21 @@ def optional_time(time_string):
return dt.time.fromisoformat(time_string) return dt.time.fromisoformat(time_string)
def optional_int(int_string): def optional_int(int_string):
if int_string == "\\N": if int_string == "\\N":
return None return None
return int(int_string) return int(int_string)
def optional_str(str_string): def optional_str(str_string):
if str_string == "\\N": if str_string == "\\N":
return None return None
return str(str_string) return str(str_string)
class Circuit(db.Model): class Circuit(db.Model):
__tablename__ = "circuit" __tablename__ = "circuit"
@ -55,6 +60,7 @@ class Circuit(db.Model):
location: Mapped[str] = mapped_column(String(128)) location: Mapped[str] = mapped_column(String(128))
country: Mapped[str] = mapped_column(String(128)) country: Mapped[str] = mapped_column(String(128))
class Season(db.Model): class Season(db.Model):
__tablename__ = "season" __tablename__ = "season"
@ -62,7 +68,8 @@ class Season(db.Model):
self.year = int(row[0]) self.year = int(row[0])
return self return self
year: Mapped[int] = mapped_column(Integer, primary_key=True) # This is the year year: Mapped[int] = mapped_column(Integer, primary_key=True)
class Race(db.Model): class Race(db.Model):
__tablename__ = "race" __tablename__ = "race"
@ -73,7 +80,9 @@ class Race(db.Model):
self.round = int(row[2]) self.round = int(row[2])
self.circuitId = int(row[3]) self.circuitId = int(row[3])
self.name = str(row[4]) self.name = str(row[4])
self.date = optional_date(row[5]) # optional_date shouldn't return None for this table self.date = optional_date(
row[5]
) # optional_date shouldn't return None for this table
self.time = optional_time(row[6]) self.time = optional_time(row[6])
return self return self
@ -87,6 +96,7 @@ class Race(db.Model):
circuit: Mapped["Circuit"] = relationship("Circuit", foreign_keys=[circuitId]) circuit: Mapped["Circuit"] = relationship("Circuit", foreign_keys=[circuitId])
class Constructor(db.Model): class Constructor(db.Model):
__tablename__ = "constructor" __tablename__ = "constructor"
@ -102,6 +112,7 @@ class Constructor(db.Model):
name: Mapped[str] = mapped_column(String(64)) name: Mapped[str] = mapped_column(String(64))
nationality: Mapped[str] = mapped_column(String(64)) nationality: Mapped[str] = mapped_column(String(64))
class Driver(db.Model): class Driver(db.Model):
__tablename__ = "driver" __tablename__ = "driver"
@ -112,7 +123,9 @@ class Driver(db.Model):
self.code = str(row[3]) self.code = str(row[3])
self.forename = str(row[4]) self.forename = str(row[4])
self.surname = str(row[5]) self.surname = str(row[5])
self.dob = optional_date(row[6]) # optional_date shouldn't return None for this table self.dob = optional_date(
row[6]
) # optional_date shouldn't return None for this table
self.nationality = str(row[7]) self.nationality = str(row[7])
return self return self
@ -125,6 +138,7 @@ class Driver(db.Model):
dob: Mapped[dt.date] = mapped_column(Date) dob: Mapped[dt.date] = mapped_column(Date)
nationality: Mapped[str] = mapped_column(String(32)) nationality: Mapped[str] = mapped_column(String(32))
class Status(db.Model): class Status(db.Model):
__tablename__ = "status" __tablename__ = "status"
@ -136,6 +150,7 @@ class Status(db.Model):
statusId: Mapped[int] = mapped_column(Integer, primary_key=True) statusId: Mapped[int] = mapped_column(Integer, primary_key=True)
status: Mapped[str] = mapped_column(String(32)) status: Mapped[str] = mapped_column(String(32))
class Result(db.Model): class Result(db.Model):
__tablename__ = "result" __tablename__ = "result"
@ -181,7 +196,9 @@ class Result(db.Model):
race: Mapped["Race"] = relationship("Race", foreign_keys=[raceId]) race: Mapped["Race"] = relationship("Race", foreign_keys=[raceId])
driver: Mapped["Driver"] = relationship("Driver", foreign_keys=[driverId]) driver: Mapped["Driver"] = relationship("Driver", foreign_keys=[driverId])
constructor: Mapped["Constructor"] = relationship("Constructor", foreign_keys=[constructorId]) constructor: Mapped["Constructor"] = relationship(
"Constructor", foreign_keys=[constructorId]
)
status: Mapped["Status"] = relationship("Status", foreign_keys=[statusId]) status: Mapped["Status"] = relationship("Status", foreign_keys=[statusId])