Merge branch 'frontend' of https://gitea.7o7.cx/roberteam/lewangoalski into frontend
This commit is contained in:
@@ -105,6 +105,20 @@ def debugger_halt(r):
|
||||
breakpoint()
|
||||
return 200, "ok", []
|
||||
|
||||
def last_goal_for(sportowiec: str = "MVC8zHZD"):
|
||||
"""
|
||||
Pobierz klub, dla którego sportowiec (domyślnie Robert) oddał ostatni gol.
|
||||
|
||||
:returns: Klub, lub None
|
||||
:rtype: kluby | None
|
||||
"""
|
||||
|
||||
sportowiec = getDb().simple_select_all("sportowcy", zewnetrzne_id_zawodnika=sportowiec)
|
||||
if sportowiec != []:
|
||||
return sportowiec[0].ostatni_gol_dla
|
||||
|
||||
return None
|
||||
|
||||
def lookup(data, request):
|
||||
"""
|
||||
Obsługuje zapytania zwrócone do /api/v1/...
|
||||
|
||||
@@ -6,6 +6,7 @@ from sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase, Session, rela
|
||||
from typing import List
|
||||
import time
|
||||
import toml
|
||||
import traceback
|
||||
|
||||
global db
|
||||
|
||||
@@ -167,18 +168,40 @@ class baza():
|
||||
try:
|
||||
return_val = func(self, *args, **kwargs)
|
||||
except:
|
||||
print( "\033[91m"
|
||||
f"Wystąpił błąd podczas wykonywania zapytania SQL:"
|
||||
"\033[0m"
|
||||
"\n"
|
||||
f"{traceback.format_exc()}")
|
||||
self.session.rollback()
|
||||
self.session.close()
|
||||
self.refresh_session()
|
||||
return return_val
|
||||
return wrapper
|
||||
|
||||
def str_to_column(self, string: str):
|
||||
"""
|
||||
Zamienia tekstowy zapis "tabela.kolumna"
|
||||
na obiekt modelu bazy (ze zmiennej self.entities).
|
||||
Zwraca None jeśli takowego nie znajdzie.
|
||||
Zamiennik dla niepożądanego eval().
|
||||
|
||||
:param string: Zapis tekstowy
|
||||
:type string: str
|
||||
"""
|
||||
table_str = string[:string.find('.')]
|
||||
column_str = string[string.find('.') + 1:]
|
||||
if hasattr(self.entities[table_str], column_str):
|
||||
return getattr(self.entities[table_str], column_str)
|
||||
return None
|
||||
|
||||
@exit_gracefully
|
||||
def simple_select_all(self, entity_type, **kwargs):
|
||||
"""
|
||||
Użycie:
|
||||
simple_select_all(ldb.sportowcy, zewnetrzne_id_zawodnika="MVC8zHZD")
|
||||
simple_select_all("sportowcy", id_zawodnika=1)
|
||||
simple_select_all("kluby", ..., LIMIT=5, ORDER_BY="kluby.skrocona_nazwa")
|
||||
|
||||
https://stackoverflow.com/a/75316945
|
||||
Did they make it harder to query dynamically on purpose? ~Frank 19.11.2023
|
||||
@@ -187,18 +210,45 @@ class baza():
|
||||
if not isinstance(entity_type, str):
|
||||
entity_type = entity_type.__name__
|
||||
|
||||
# Save special arguments received with kwargs,
|
||||
# that are meant for SQL operations to special_args,
|
||||
# and delete from the rest, that will be passed
|
||||
# directly to filter_by().
|
||||
# They will not be passed as search query, but serve
|
||||
# as an additional search parameter.
|
||||
special_keywords = ("ORDER_BY", "ORDER_BY_DESC", "LIMIT")
|
||||
special_args = {}
|
||||
|
||||
for arg in special_keywords:
|
||||
if arg in kwargs:
|
||||
special_args[arg] = kwargs[arg]
|
||||
del kwargs[arg]
|
||||
|
||||
print(f"[{round(time.time())}] SELECT")
|
||||
|
||||
results = (
|
||||
self.session.
|
||||
query(self.entities[entity_type]).
|
||||
filter_by(**kwargs).
|
||||
all()
|
||||
filter_by(**kwargs)
|
||||
)
|
||||
|
||||
print(f"[{round(time.time())}] SELECT RESULTS: {results}")
|
||||
if "ORDER_BY" in special_args:
|
||||
column = self.str_to_column(special_args["ORDER_BY"])
|
||||
if column is not None:
|
||||
results = results.order_by(column)
|
||||
|
||||
return results
|
||||
if "ORDER_BY_DESC" in special_args:
|
||||
column = self.str_to_column(special_args["ORDER_BY_DESC"])
|
||||
if column is not None:
|
||||
results = results.order_by(column.desc())
|
||||
|
||||
if "LIMIT" in special_args:
|
||||
results = results.limit(special_args["LIMIT"])
|
||||
|
||||
results_objs = results.all()
|
||||
print(f"[{round(time.time())}] SELECT RESULTS: {results_objs}")
|
||||
|
||||
return results_objs
|
||||
|
||||
@exit_gracefully
|
||||
def simple_insert_one(self, entity_type, **kwargs):
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
from flask import render_template, request, make_response
|
||||
import lewy_api_v1
|
||||
import lewy_db
|
||||
import lewy_globals
|
||||
|
||||
def index():
|
||||
dark_mode = request.cookies.get('darkMode', 'disabled')
|
||||
# Przykładowe użycie endpointu last_goal_for():
|
||||
# roberts_last_goals_club = lewy_api_v1.last_goal_for()
|
||||
# print(roberts_last_goals_club.id_klubu)
|
||||
stats = {
|
||||
'goals': 38,
|
||||
'assists': 12,
|
||||
|
||||
Reference in New Issue
Block a user