diff --git a/db/models.py b/db/models.py index 57dfeb1..4db5dbe 100644 --- a/db/models.py +++ b/db/models.py @@ -632,7 +632,47 @@ def del_user_prepaid(user_id: int): connection.commit() return result.rowcount -def get_last_drink(user_id: int, user_is_postpaid: bool, max_since_seconds: int = 60): +def get_last_drink(user_id: int, user_is_postpaid: bool, max_since_seconds: int = 3*30*24*60*60): + """ + Retrieve the most recent drink entry for a user within a specified time window. + + Args: + user_id (int): The ID of the user whose last drink is to be retrieved. + user_is_postpaid (bool): True if the user is postpaid, False if prepaid. + max_since_seconds (int, optional): Max seconds since last drink. Defaults to 3 months. + + Returns: + dict or None: Dict with 'id', 'timestamp', 'drink_type' if found within time window, + else None. + """ + if user_is_postpaid: + t = text("SELECT id, timestamp, drink_type FROM drinks WHERE postpaid_user_id = :user_id ORDER BY timestamp DESC LIMIT 1") + else: + t = text("SELECT id, timestamp, drink_type FROM drinks WHERE prepaid_user_id = :user_id ORDER BY timestamp DESC LIMIT 1") + + with engine.connect() as connection: + result = connection.execute(t, {"user_id": user_id}).fetchone() + if not result: + return None + drink_id, timestamp, drink_type_id = result + + if timestamp: + now = datetime.datetime.now(datetime.timezone.utc) + last_drink_time = datetime.datetime.fromisoformat(timestamp.replace("Z", "+00:00")) + # Ensure both are offset-aware + if last_drink_time.tzinfo is None: + last_drink_time = last_drink_time.replace(tzinfo=datetime.timezone.utc) + if (now - last_drink_time).total_seconds() > max_since_seconds: + return None + drink_obj = {"id": drink_id, "timestamp": timestamp, "drink_type_id": drink_type_id} + if drink_type_id: + drink_type_dict = get_drink_type(drink_type_id) + drink_obj["drink_type_name"] = drink_type_dict["drink_name"] + drink_obj["drink_type_icon"] = drink_type_dict["icon"] + return drink_obj + return None + +def get_last_recent_drink(user_id: int, user_is_postpaid: bool, max_since_seconds: int = 60): """ Retrieve the most recent drink entry for a user within a specified time window. @@ -666,9 +706,9 @@ def get_last_drink(user_id: int, user_is_postpaid: bool, max_since_seconds: int return None drink_obj = {"id": drink_id, "timestamp": timestamp, "drink_type_id": drink_type_id} if drink_type_id: - drink_type_name, drink_type_icon = get_drink_type(drink_type_id) - drink_obj["drink_type_name"] = drink_type_name - drink_obj["drink_type_icon"] = drink_type_icon + drink_type_dict = get_drink_type(drink_type_id) + drink_obj["drink_type_name"] = drink_type_dict["drink_name"] + drink_obj["drink_type_icon"] = drink_type_dict["icon"] return drink_obj return None diff --git a/main.py b/main.py index e165e85..e68395f 100644 --- a/main.py +++ b/main.py @@ -92,9 +92,12 @@ def home(request: Request): db_user = db.models.get_prepaid_user(user_db_id) # get last drink for current user, if not less than 60 seconds ago - last_drink = db.models.get_last_drink(user_db_id, user_is_postpaid, 60) + last_recent_drink = db.models.get_last_recent_drink(user_db_id, user_is_postpaid, 60) - most_used_drinks = db.models.get_most_used_drinks(user_db_id, user_is_postpaid, 3) + # get last drink for current user, if not less than 3 months ago + last_regular_drink = db.models.get_last_drink(user_db_id, user_is_postpaid) + + most_used_drinks = db.models.get_most_used_drinks(user_db_id, user_is_postpaid, 99) most_used_drinks.append({"drink_type_id": 1, "drink_type": "Sonstiges", "count": 0, "icon": "sonstiges.png"}) # ensure "Sonstiges" is in return templates.TemplateResponse("index.html", { @@ -105,7 +108,8 @@ def home(request: Request): "db_user": db_user, "db_users_prepaid": db_users_prepaid, "prepaid_users_from_curr_user": prepaid_users_from_curr_user, - "last_drink": last_drink, + "last_recent_drink": last_recent_drink, + "last_regular_drink": last_regular_drink, "avail_drink_types": most_used_drinks, "drink_types": drink_types, }) diff --git a/templates/index.html b/templates/index.html index c7236c8..a16aa5b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,4 +1,4 @@ -{% extends "base.html" %} {% block title %}Startseite{% endblock %} {% block +{% extends "base.html" %} {% block title %}Getränkeliste{% endblock %} {% block content %}
Aktueller Kontostand:
@@ -29,7 +29,55 @@ content %} Bitte begleiche deinen offenen Betrag! -{% endif %} {% if 'Getraenkeliste Postpaid' in user.groups %} {% if db_user.money > -5000 %} +{% endif %} +{% if last_recent_drink %} +Du bist Teil der Fachschaft Informatik.
{% if prepaid_users_from_curr_user %}Liste deiner Prepaid-User: