haldis/app/views/general.py

198 lines
5.8 KiB
Python
Raw Normal View History

2019-09-10 02:50:22 +02:00
"Script to generate the general views of Haldis"
import os
from datetime import datetime, timedelta
2019-12-06 16:12:05 +01:00
import yaml
2020-03-04 21:04:16 +01:00
from typing import Optional
2019-12-06 16:12:05 +01:00
from flask import Flask, render_template, make_response
from flask import request, jsonify
from flask import Blueprint, abort
from flask import current_app as app
from flask import send_from_directory, url_for
from flask_login import login_required
from utils import first
from hlds.definitions import location_definitions
from hlds.models import Location
from models import Order
2020-07-17 11:40:15 +02:00
# import views
from views.order import get_orders
2020-03-04 21:04:16 +01:00
import json
from flask import jsonify
2019-09-05 03:33:29 +02:00
general_bp = Blueprint("general_bp", __name__)
2019-09-05 03:33:29 +02:00
@general_bp.route("/")
2019-09-08 01:34:16 +02:00
def home() -> str:
2019-09-10 02:50:22 +02:00
"Generate the home view"
prev_day = datetime.now() - timedelta(days=1)
recently_closed = get_orders(
2019-09-05 03:33:29 +02:00
((Order.stoptime > prev_day) & (Order.stoptime < datetime.now()))
)
return render_template(
"home.html", orders=get_orders(), recently_closed=recently_closed
)
def get_css_dict(css_path):
"Generate the dictionary with all the currently available themes and their names"
themes_dict = dict()
# Open the YAML file with all the themes.
path = os.path.join(app.root_path, "views/themes.yml")
2020-07-17 11:40:15 +02:00
with open(path, "r") as stream:
data = yaml.safe_load(stream)
# Build a dictionary from the YAML file with all the themes and their attributes.
themes = {}
for item in data:
key = list(item.keys())[0]
themes[key] = item[key]
# Get the current date.
current_date = datetime.now()
current_year = current_date.year
# Check each theme in the dictionary and return the first one that is "correct"
for key, theme in themes.items():
2020-07-17 11:40:15 +02:00
if theme["type"] == "static-date":
start_day, start_month = theme["start"].split("/")
start_date = datetime(year=current_year, day=int(start_day), month=int(start_month))
2020-07-17 11:40:15 +02:00
end_day, end_month = theme["end"].split("/")
if int(start_month) > int(end_month):
current_year += 1
2020-07-17 11:40:15 +02:00
end_date = datetime(year=current_year, day=int(end_day), month=int(end_month))
if start_date <= current_date <= end_date:
2020-07-17 11:40:15 +02:00
path = os.path.join(app.root_path, css_path, theme["file"])
themes_dict[key] = path
2020-07-17 11:40:15 +02:00
themes_dict["darkmode"] = os.path.join(
app.root_path, "static/css/themes/lowPerformance/darkmode.css"
)
themes_dict["lightmode"] = os.path.join(
app.root_path, "static/css/themes/lowPerformance/lightmode.css"
)
return themes_dict
def css_list():
"Generate the list of names of all the currently available themes"
2020-07-17 11:40:15 +02:00
if request.cookies.get("performance", "") == "highPerformance":
css_path = "static/css/themes/highPerformance/"
else:
2020-07-17 11:40:15 +02:00
css_path = "static/css/themes/lowPerformance/"
return list(get_css_dict(css_path).keys())
@general_bp.route("/css")
def css():
"Generate the css"
2020-07-17 11:40:15 +02:00
if request.cookies.get("performance", "") == "highPerformance":
css_path = "static/css/themes/highPerformance/"
2019-12-05 19:39:35 +01:00
else:
2020-07-17 11:40:15 +02:00
css_path = "static/css/themes/lowPerformance/"
2020-07-17 11:40:15 +02:00
cookie_theme = request.cookies.get("theme", "")
themes_dict = get_css_dict(css_path)
# TODO: Fix to work with default cookie value [customTheme]
if cookie_theme == "customTheme":
path = f"{css_path}ligtmode.css"
else:
path = themes_dict[cookie_theme]
2019-12-06 16:12:05 +01:00
f = open(path)
response = make_response(f.read())
2020-07-17 11:40:15 +02:00
response.headers["Content-Type"] = "text/css"
f.close()
return response
2019-09-10 02:50:22 +02:00
@general_bp.route("/map")
def map_view() -> str:
"Generate the map view"
return render_template("maps.html", locations=location_definitions)
2019-09-05 03:33:29 +02:00
@general_bp.route("/location")
2019-09-08 01:34:16 +02:00
def locations() -> str:
2019-09-10 02:50:22 +02:00
"Generate the location view"
return render_template("locations.html", locations=location_definitions)
@general_bp.route("/location/<location_id>")
2019-09-10 02:50:22 +02:00
def location(location_id) -> str:
"Generate the location view given an id"
loc = first(filter(lambda l: l.id == location_id, location_definitions))
if loc is None:
abort(404)
2019-09-05 03:33:29 +02:00
return render_template("location.html", location=loc, title=loc.name)
2020-03-04 21:04:16 +01:00
@general_bp.route("/location/<location_id>/<dish_id>")
def location_dish(location_id, dish_id) -> str:
2020-07-17 11:40:15 +02:00
loc: Optional[Location] = first(
filter(lambda l: l.id == location_id, location_definitions)
)
2020-03-04 21:04:16 +01:00
if loc is None:
abort(404)
dish = loc.dish_by_id(dish_id)
if dish is None:
abort(404)
return jsonify([
{
"type": c[0],
"id": c[1].id,
"name": c[1].name,
"description": c[1].description,
"options": [
{
"id": o.id,
"name": o.name,
"description": o.description,
"price": o.price,
2020-07-17 11:40:15 +02:00
"tags": o.tags,
2020-03-04 21:04:16 +01:00
}
for o in c[1].options
2020-07-17 11:40:15 +02:00
],
2020-03-04 21:04:16 +01:00
}
for c in dish.choices
])
2019-09-05 03:33:29 +02:00
@general_bp.route("/about/")
2019-09-08 01:34:16 +02:00
def about() -> str:
2019-09-10 02:50:22 +02:00
"Generate the about view"
2019-09-05 03:33:29 +02:00
return render_template("about.html")
2019-09-05 03:33:29 +02:00
@general_bp.route("/profile/")
@login_required
2019-09-08 01:34:16 +02:00
def profile() -> str:
2019-09-10 02:50:22 +02:00
"Generate the profile view"
return render_template("profile.html", themes_list=css_list())
2019-09-05 03:33:29 +02:00
@general_bp.route("/favicon.ico")
2019-09-08 01:34:16 +02:00
def favicon() -> str:
2019-09-10 02:50:22 +02:00
"Generate the favicon"
# pylint: disable=R1705
if not get_orders((Order.stoptime > datetime.now())):
2019-09-05 03:33:29 +02:00
return send_from_directory(
2019-12-06 15:34:39 +01:00
os.path.join(app.root_path, "static"),
2019-09-05 03:33:29 +02:00
"favicon.ico",
mimetype="image/x-icon",
)
else:
2019-09-05 03:33:29 +02:00
return send_from_directory(
2019-12-06 15:34:39 +01:00
os.path.join(app.root_path, "static"),
2019-09-05 03:33:29 +02:00
"favicon_orange.ico",
mimetype="image/x-icon",
)