2019-09-10 02:50:22 +02:00
|
|
|
"Script to generate the general views of Haldis"
|
2019-08-28 03:46:04 +02:00
|
|
|
import os
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2019-11-21 10:52:08 +01:00
|
|
|
from flask import Flask, render_template, make_response
|
|
|
|
from flask import request
|
2019-08-28 03:46:04 +02:00
|
|
|
from flask import Blueprint, abort
|
|
|
|
from flask import current_app as app
|
2019-12-05 19:19:36 +01:00
|
|
|
from flask import send_from_directory, url_for
|
2019-08-28 03:46:04 +02:00
|
|
|
from flask_login import login_required
|
|
|
|
|
2019-12-05 19:19:36 +01:00
|
|
|
import yaml
|
|
|
|
|
2019-08-28 03:46:04 +02:00
|
|
|
from models import Location, Order
|
|
|
|
# import views
|
|
|
|
from views.order import get_orders
|
|
|
|
|
2019-09-05 03:33:29 +02:00
|
|
|
general_bp = Blueprint("general_bp", __name__)
|
2019-08-28 03:46:04 +02:00
|
|
|
|
|
|
|
|
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"
|
2019-08-28 03:46:04 +02:00
|
|
|
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
|
|
|
|
)
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2019-12-05 19:19:36 +01:00
|
|
|
|
2019-11-21 10:52:08 +01:00
|
|
|
@general_bp.route("/css")
|
|
|
|
def css():
|
|
|
|
"Generate the css"
|
2019-12-05 19:39:35 +01:00
|
|
|
if (request.cookies.get('performance') and request.cookies.get('performance') == 'highPerformance'):
|
2019-12-06 12:32:51 +01:00
|
|
|
cssPath = 'static/css/themes/highPerformance/'
|
2019-12-05 19:39:35 +01:00
|
|
|
else:
|
2019-12-06 12:32:51 +01:00
|
|
|
cssPath = 'static/css/themes/lowPerformance/'
|
|
|
|
|
2019-11-21 10:52:08 +01:00
|
|
|
if request.cookies.get('theme'):
|
|
|
|
if request.cookies['theme'] == 'customTheme':
|
|
|
|
#TODO: The custom theme is hardcoded :(. Make the server auto select a custom team.
|
2019-11-22 12:22:25 +01:00
|
|
|
# Here seasonal themes will be returned; matching the current date.
|
|
|
|
|
|
|
|
# Open the YAML file with all the themes.
|
2019-12-06 15:34:39 +01:00
|
|
|
path = os.path.join(app.root_path, "views/themes.yml")
|
2019-12-06 12:32:51 +01:00
|
|
|
with open(path, 'r') as stream:
|
2019-11-22 12:22:25 +01:00
|
|
|
data = yaml.safe_load(stream)
|
|
|
|
# Build a dictionary from the YAML file with all the themes and there attributes.
|
|
|
|
themes = {}
|
|
|
|
for item in data:
|
|
|
|
key = list(item.keys())[0]
|
|
|
|
themes[key] = item[key]
|
2019-12-05 20:38:07 +01:00
|
|
|
|
2019-11-22 12:22:25 +01:00
|
|
|
# Get the current date.
|
|
|
|
current_day = datetime.now().day
|
|
|
|
current_month = datetime.now().month
|
|
|
|
|
|
|
|
# Check each theme in the dictionary and return the first one that is "correct"
|
|
|
|
for theme in themes.values():
|
|
|
|
if theme['type'] == 'static-date':
|
2019-12-05 20:38:07 +01:00
|
|
|
start_day, start_month = theme['start'].split('/')
|
|
|
|
start_day = int(start_day)
|
|
|
|
start_month = int(start_month)
|
|
|
|
|
|
|
|
end_day, end_month = theme['end'].split('/')
|
|
|
|
end_day = int(end_day)
|
|
|
|
end_month = int(end_month)
|
|
|
|
|
|
|
|
if end_month < start_month:
|
|
|
|
# Hacky (werkt nu maar kan beter)
|
|
|
|
end_month += 12
|
|
|
|
|
|
|
|
if theme['type'] == 'static-date':
|
|
|
|
|
|
|
|
if (((start_month == current_month) and
|
|
|
|
(start_day <= current_day)) or
|
|
|
|
(start_month <= current_month)):
|
|
|
|
|
|
|
|
if (((end_month == current_month) and
|
|
|
|
(end_day >= current_day)) or
|
|
|
|
(end_month > current_month)):
|
2019-12-06 15:34:39 +01:00
|
|
|
path = os.path.join(app.root_path, cssPath, theme['file'])
|
2019-12-05 20:38:07 +01:00
|
|
|
break
|
2019-11-21 10:52:08 +01:00
|
|
|
else:
|
2019-12-06 12:32:51 +01:00
|
|
|
if request.cookies['theme'] == 'darkmode' :
|
2019-12-06 15:34:39 +01:00
|
|
|
path = os.path.join(app.root_path, "static/css/themes/lowPerformance/darkmode.css")
|
2019-12-06 12:32:51 +01:00
|
|
|
else:
|
2019-12-06 15:34:39 +01:00
|
|
|
path = os.path.join(app.root_path, "static/css/themes/lowPerformance/lightmode.css")
|
2019-12-06 12:32:51 +01:00
|
|
|
|
|
|
|
# Tijdelijk ongebruikt tot bewezen dat het veilig is
|
|
|
|
#try:
|
|
|
|
# path = os.path.join(str(app.root_path), "static/css/themes/lowPerformance/", request.cookies['theme']+".css")
|
|
|
|
# f = open(path)
|
|
|
|
#except IOError:
|
|
|
|
# f = open(cssPath+"lightmode.css")
|
2019-11-21 10:52:08 +01:00
|
|
|
else:
|
2019-12-06 15:34:39 +01:00
|
|
|
path = os.path.join(app.root_path, "static/css/themes/lowPerformance/lightmode.css")
|
2019-12-06 12:32:51 +01:00
|
|
|
f = open(path)
|
2019-11-21 10:52:08 +01:00
|
|
|
response = make_response(f.read())
|
|
|
|
response.headers['Content-Type'] = 'text/css'
|
2019-12-05 19:19:36 +01:00
|
|
|
f.close()
|
2019-11-21 10:52:08 +01:00
|
|
|
return response
|
|
|
|
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2019-09-10 02:50:22 +02:00
|
|
|
@general_bp.route("/map")
|
|
|
|
def map_view() -> str:
|
|
|
|
"Generate the map view"
|
2019-09-05 03:33:29 +02:00
|
|
|
locs = Location.query.order_by("name")
|
|
|
|
return render_template("maps.html", locations=locs)
|
2019-08-28 03:46:04 +02:00
|
|
|
|
|
|
|
|
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"
|
2019-09-05 03:33:29 +02:00
|
|
|
locs = Location.query.order_by("name")
|
|
|
|
return render_template("locations.html", locations=locs)
|
2019-08-28 03:46:04 +02:00
|
|
|
|
|
|
|
|
2019-09-10 02:50:22 +02:00
|
|
|
@general_bp.route("/location/<int:location_id>")
|
|
|
|
def location(location_id) -> str:
|
|
|
|
"Generate the location view given an id"
|
|
|
|
loc = Location.query.filter(Location.id == location_id).first()
|
2019-08-28 03:46:04 +02:00
|
|
|
if loc is None:
|
|
|
|
abort(404)
|
2019-09-05 03:33:29 +02:00
|
|
|
return render_template("location.html", location=loc, title=loc.name)
|
2019-08-28 03:46:04 +02:00
|
|
|
|
|
|
|
|
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-08-28 03:46:04 +02:00
|
|
|
|
|
|
|
|
2019-09-05 03:33:29 +02:00
|
|
|
@general_bp.route("/profile/")
|
2019-08-28 03:46:04 +02:00
|
|
|
@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"
|
2019-09-05 03:33:29 +02:00
|
|
|
return render_template("profile.html")
|
2019-08-28 03:46:04 +02:00
|
|
|
|
|
|
|
|
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",
|
|
|
|
)
|
2019-08-28 03:46:04 +02:00
|
|
|
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",
|
|
|
|
)
|