Update views with HLDS models

Not expected to work yet, but if I don't do this generating migrations
gives errors.
This commit is contained in:
Midgard 2020-01-26 16:02:03 +01:00
parent 238c841239
commit d5bb14d3a5
Signed by: midgard
GPG key ID: 511C112F1331BBB4
5 changed files with 38 additions and 19 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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/<int:location_id>")
@general_bp.route("/location/<location_id>")
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)

View file

@ -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)