diff --git a/app/fatmodels.py b/app/fatmodels.py index ac3217e..2ab2f29 100644 --- a/app/fatmodels.py +++ b/app/fatmodels.py @@ -2,7 +2,9 @@ import typing from sqlalchemy.sql import desc, func -from models import Location, Order, OrderItem, Product, User +from hlds.definitions import location_definitions +from hlds.models import Location, Dish +from models import Order, OrderItem, User class FatModel: @@ -16,7 +18,13 @@ class FatModel: class FatLocation(Location, FatModel): - pass + @classmethod + def all(cls): + return location_definitions + + @classmethod + def amount(cls): + return len(location_definitions) class FatOrder(Order, FatModel): @@ -42,16 +50,16 @@ class FatOrderItem(OrderItem, FatModel): pass -class FatProduct(Product, FatModel): +class FatDish(Dish, FatModel): @classmethod def top4(cls) -> None: top4 = ( - OrderItem.query.join(Product) - .join(Location) - .group_by(Product.id) + OrderItem.query + .join(Order) + .group_by(Order.location_id, OrderItem.dish_id) .with_entities( - Product.name, Location.name, func.count( - Product.id).label("count") + Order.location_id, OrderItem.dish_id, func.count( + OrderItem.dish_id).label("count") ) .order_by(desc("count")) .limit(4) diff --git a/app/forms.py b/app/forms.py index b40a1ee..5a1520e 100644 --- a/app/forms.py +++ b/app/forms.py @@ -7,7 +7,9 @@ from flask_wtf import FlaskForm as Form from wtforms import (DateTimeField, SelectField, StringField, SubmitField, validators) -from models import Location, User +from hlds.definitions import location_definitions +from hlds.models import Location +from models import User from utils import euro_string @@ -36,7 +38,7 @@ class OrderForm(Form): (current_user.id, current_user.username), ] self.location_id.choices = [ - (l.id, l.name) for l in Location.query.order_by("name") + (l.id, l.name) for l in location_definitions ] if self.stoptime.data is None: self.stoptime.data = datetime.now() + timedelta(hours=1) diff --git a/app/hlds/definitions.py b/app/hlds/definitions.py index 45b27d2..4267ff4 100644 --- a/app/hlds/definitions.py +++ b/app/hlds/definitions.py @@ -13,3 +13,4 @@ DATA_DIR = path.join(path.dirname(__file__), "..", "..", "data") # pylint: disable=invalid-name location_definitions: List[Location] = parse_all_directory(DATA_DIR) +location_definitions.sort(key=lambda l: l.name) diff --git a/app/views/general.py b/app/views/general.py index 5abf865..fe36cf9 100644 --- a/app/views/general.py +++ b/app/views/general.py @@ -3,6 +3,7 @@ import os from datetime import datetime, timedelta import yaml +import typing from flask import Flask, render_template, make_response from flask import request, jsonify @@ -11,13 +12,22 @@ from flask import current_app as app from flask import send_from_directory, url_for from flask_login import login_required -from models import Location, Order +from hlds.definitions import location_definitions +from hlds.models import Location +from models import Order # import views from views.order import get_orders general_bp = Blueprint("general_bp", __name__) +def _first(iterable: typing.Iterable, default=None): + try: + return next(iter(iterable)) + except StopIteration: + return default + + @general_bp.route("/") def home() -> str: "Generate the home view" @@ -109,21 +119,19 @@ def css(): @general_bp.route("/map") def map_view() -> str: "Generate the map view" - locs = Location.query.order_by("name") - return render_template("maps.html", locations=locs) + return render_template("maps.html", locations=location_definitions) @general_bp.route("/location") def locations() -> str: "Generate the location view" - locs = Location.query.order_by("name") - return render_template("locations.html", locations=locs) + return render_template("locations.html", locations=location_definitions) -@general_bp.route("/location/") +@general_bp.route("/location/") def location(location_id) -> str: "Generate the location view given an id" - loc = Location.query.filter(Location.id == location_id).first() + loc = _first(filter(lambda l: l.id == location_id, location_definitions)) if loc is None: abort(404) return render_template("location.html", location=loc, title=loc.name) diff --git a/app/views/stats.py b/app/views/stats.py index 02771a2..7204db9 100644 --- a/app/views/stats.py +++ b/app/views/stats.py @@ -3,7 +3,7 @@ from flask import Blueprint from flask import current_app as app from flask import render_template -from fatmodels import FatLocation, FatOrder, FatOrderItem, FatProduct, FatUser +from fatmodels import FatLocation, FatOrder, FatOrderItem, FatDish, FatUser stats_blueprint = Blueprint("stats_blueprint", __name__) @@ -17,7 +17,7 @@ def stats() -> str: "locations": FatLocation.amount(), "users": FatUser.amount(), "orderitems": FatOrderItem.amount(), - "products": FatProduct.amount(), + "products": FatDish.amount(), } } return render_template("stats.html", data=data)