Add typing to view files

This commit is contained in:
Jan-Pieter Baert 2019-09-08 01:34:16 +02:00
parent 27cb10f745
commit b7fa92a22a
No known key found for this signature in database
GPG key ID: B19186932178234A
4 changed files with 53 additions and 36 deletions

View file

@ -8,7 +8,7 @@ debug_bp = Blueprint("debug_bp", __name__)
@debug_bp.route("/routes") @debug_bp.route("/routes")
@login_required @login_required
def list_routes(): def list_routes() -> str:
import urllib import urllib
output = [] output = []

View file

@ -15,7 +15,7 @@ general_bp = Blueprint("general_bp", __name__)
@general_bp.route("/") @general_bp.route("/")
def home(): def home() -> str:
prev_day = datetime.now() - timedelta(days=1) prev_day = datetime.now() - timedelta(days=1)
recently_closed = get_orders( recently_closed = get_orders(
((Order.stoptime > prev_day) & (Order.stoptime < datetime.now())) ((Order.stoptime > prev_day) & (Order.stoptime < datetime.now()))
@ -27,19 +27,19 @@ def home():
@general_bp.route("/map", defaults={"id": None}) @general_bp.route("/map", defaults={"id": None})
@general_bp.route("/map/<int:id>") @general_bp.route("/map/<int:id>")
def map(id): def map(id) -> str:
locs = Location.query.order_by("name") locs = Location.query.order_by("name")
return render_template("maps.html", locations=locs) return render_template("maps.html", locations=locs)
@general_bp.route("/location") @general_bp.route("/location")
def locations(): def locations() -> str:
locs = Location.query.order_by("name") locs = Location.query.order_by("name")
return render_template("locations.html", locations=locs) return render_template("locations.html", locations=locs)
@general_bp.route("/location/<int:id>") @general_bp.route("/location/<int:id>")
def location(id): def location(id) -> str:
loc = Location.query.filter(Location.id == id).first() loc = Location.query.filter(Location.id == id).first()
if loc is None: if loc is None:
abort(404) abort(404)
@ -47,27 +47,27 @@ def location(id):
@general_bp.route("/about/") @general_bp.route("/about/")
def about(): def about() -> str:
return render_template("about.html") return render_template("about.html")
@general_bp.route("/profile/") @general_bp.route("/profile/")
@login_required @login_required
def profile(): def profile() -> str:
return render_template("profile.html") return render_template("profile.html")
@general_bp.route("/favicon.ico") @general_bp.route("/favicon.ico")
def favicon(): def favicon() -> str:
if len(get_orders((Order.stoptime > datetime.now()))) == 0: if len(get_orders((Order.stoptime > datetime.now()))) == 0:
return send_from_directory( return send_from_directory(
os.path.join(app.root_path, "static"), os.path.join(str(app.root_path), "static"),
"favicon.ico", "favicon.ico",
mimetype="image/x-icon", mimetype="image/x-icon",
) )
else: else:
return send_from_directory( return send_from_directory(
os.path.join(app.root_path, "static"), os.path.join(str(app.root_path), "static"),
"favicon_orange.ico", "favicon_orange.ico",
mimetype="image/x-icon", mimetype="image/x-icon",
) )

View file

@ -1,6 +1,7 @@
from werkzeug.wrappers import Response
import random import random
from datetime import datetime from datetime import datetime
import typing
# from flask import current_app as app # from flask import current_app as app
from flask import ( from flask import (
Blueprint, Blueprint,
@ -11,7 +12,9 @@ from flask import (
request, request,
session, session,
url_for, url_for,
wrappers,
) )
import werkzeug
from flask_login import current_user, login_required from flask_login import current_user, login_required
from forms import AnonOrderItemForm, OrderForm, OrderItemForm from forms import AnonOrderItemForm, OrderForm, OrderItemForm
@ -22,7 +25,7 @@ order_bp = Blueprint("order_bp", "order")
@order_bp.route("/") @order_bp.route("/")
def orders(form=None): def orders(form: OrderForm = None) -> str:
if form is None and not current_user.is_anonymous(): if form is None and not current_user.is_anonymous():
form = OrderForm() form = OrderForm()
location_id = request.args.get("location_id") location_id = request.args.get("location_id")
@ -34,7 +37,7 @@ def orders(form=None):
@order_bp.route("/create", methods=["POST"]) @order_bp.route("/create", methods=["POST"])
@login_required @login_required
def order_create(): def order_create() -> typing.Union[str, Response]:
orderForm = OrderForm() orderForm = OrderForm()
orderForm.populate() orderForm.populate()
if orderForm.validate_on_submit(): if orderForm.validate_on_submit():
@ -48,7 +51,7 @@ def order_create():
@order_bp.route("/<id>") @order_bp.route("/<id>")
def order(id, form=None): def order(id: int, form: OrderForm = None) -> str:
order = Order.query.filter(Order.id == id).first() order = Order.query.filter(Order.id == id).first()
if order is None: if order is None:
abort(404) abort(404)
@ -56,19 +59,19 @@ def order(id, form=None):
flash("Please login to see this order.", "info") flash("Please login to see this order.", "info")
abort(401) abort(401)
if form is None: if form is None:
form = AnonOrderItemForm() if current_user.is_anonymous() else OrderItemForm() form = AnonOrderItemForm() if current_user.is_anonymous() \
else OrderItemForm()
form.populate(order.location) form.populate(order.location)
if order.stoptime and order.stoptime < datetime.now(): if order.stoptime and order.stoptime < datetime.now():
form = None form = None
total_price = sum([o.product.price for o in order.items]) total_price = sum([o.product.price for o in order.items])
debts = sum([o.product.price for o in order.items if not o.paid]) debts = sum([o.product.price for o in order.items if not o.paid])
return render_template( return render_template("order.html", order=order, form=form,
"order.html", order=order, form=form, total_price=total_price, debts=debts total_price=total_price, debts=debts)
)
@order_bp.route("/<id>/items") @order_bp.route("/<id>/items")
def items_showcase(id, form=None): def items_showcase(id: int, form: OrderForm = None) -> str:
order = Order.query.filter(Order.id == id).first() order = Order.query.filter(Order.id == id).first()
if order is None: if order is None:
abort(404) abort(404)
@ -80,9 +83,10 @@ def items_showcase(id, form=None):
@order_bp.route("/<id>/edit", methods=["GET", "POST"]) @order_bp.route("/<id>/edit", methods=["GET", "POST"])
@login_required @login_required
def order_edit(id): def order_edit(id: int) -> typing.Union[str, Response]:
order = Order.query.filter(Order.id == id).first() order = Order.query.filter(Order.id == id).first()
if current_user.id is not order.courrier_id and not current_user.is_admin(): if current_user.id is not order.courrier_id and \
not current_user.is_admin():
abort(401) abort(401)
if order is None: if order is None:
abort(404) abort(404)
@ -96,7 +100,9 @@ def order_edit(id):
@order_bp.route("/<id>/create", methods=["POST"]) @order_bp.route("/<id>/create", methods=["POST"])
def order_item_create(id): def order_item_create(id: int) -> typing.Any:
# type is 'typing.Union[str, Response]', but this errors due to
# https://github.com/python/mypy/issues/7187
current_order = Order.query.filter(Order.id == id).first() current_order = Order.query.filter(Order.id == id).first()
if current_order is None: if current_order is None:
abort(404) abort(404)
@ -105,7 +111,8 @@ def order_item_create(id):
if current_user.is_anonymous() and not current_order.public: if current_user.is_anonymous() and not current_order.public:
flash("Please login to see this order.", "info") flash("Please login to see this order.", "info")
abort(401) abort(401)
form = AnonOrderItemForm() if current_user.is_anonymous() else OrderItemForm() form = AnonOrderItemForm() if current_user.is_anonymous() \
else OrderItemForm()
form.populate(current_order.location) form.populate(current_order.location)
if form.validate_on_submit(): if form.validate_on_submit():
item = OrderItem() item = OrderItem()
@ -124,22 +131,24 @@ def order_item_create(id):
@order_bp.route("/<order_id>/<item_id>/paid") @order_bp.route("/<order_id>/<item_id>/paid")
@login_required @login_required
def item_paid(order_id, item_id): def item_paid(order_id: int, item_id: int) -> typing.Optional[Response]:
item = OrderItem.query.filter(OrderItem.id == item_id).first() item = OrderItem.query.filter(OrderItem.id == item_id).first()
id = current_user.id id = current_user.id
if item.order.courrier_id == id or current_user.admin: if item.order.courrier_id == id or current_user.admin:
item.paid = True item.paid = True
db.session.commit() db.session.commit()
flash("Paid %s by %s" % (item.product.name, item.get_name()), "success") flash("Paid %s by %s" % (item.product.name, item.get_name()),
"success")
return redirect(url_for("order_bp.order", id=order_id)) return redirect(url_for("order_bp.order", id=order_id))
abort(404) abort(404)
@order_bp.route("/<order_id>/<user_name>/user_paid") @order_bp.route("/<order_id>/<user_name>/user_paid")
@login_required @login_required
def items_user_paid(order_id, user_name): def items_user_paid(order_id: int,
user_name: str) -> typing.Optional[Response]:
user = User.query.filter(User.username == user_name).first() user = User.query.filter(User.username == user_name).first()
items = [] items: typing.List[OrderItem] = []
if user: if user:
items = OrderItem.query.filter( items = OrderItem.query.filter(
(OrderItem.user_id == user.id) & (OrderItem.order_id == order_id) (OrderItem.user_id == user.id) & (OrderItem.order_id == order_id)
@ -155,13 +164,16 @@ def items_user_paid(order_id, user_name):
for item in items: for item in items:
item.paid = True item.paid = True
db.session.commit() db.session.commit()
flash("Paid %d items for %s" % (items.count(), item.get_name()), "success") flash("Paid %d items for %s" %
(len(items), item.get_name()), "success")
return redirect(url_for("order_bp.order", id=order_id)) return redirect(url_for("order_bp.order", id=order_id))
abort(404) abort(404)
@order_bp.route("/<order_id>/<item_id>/delete") @order_bp.route("/<order_id>/<item_id>/delete")
def delete_item(order_id, item_id): def delete_item(order_id: int, item_id: int) -> typing.Any:
# type is 'typing.Optional[Response]', but this errors due to
# https://github.com/python/mypy/issues/7187
item = OrderItem.query.filter(OrderItem.id == item_id).first() item = OrderItem.query.filter(OrderItem.id == item_id).first()
id = None id = None
if not current_user.is_anonymous(): if not current_user.is_anonymous():
@ -178,7 +190,7 @@ def delete_item(order_id, item_id):
@order_bp.route("/<id>/volunteer") @order_bp.route("/<id>/volunteer")
@login_required @login_required
def volunteer(id): def volunteer(id: int) -> Response:
order = Order.query.filter(Order.id == id).first() order = Order.query.filter(Order.id == id).first()
if order is None: if order is None:
abort(404) abort(404)
@ -193,7 +205,7 @@ def volunteer(id):
@order_bp.route("/<id>/close") @order_bp.route("/<id>/close")
@login_required @login_required
def close_order(id): def close_order(id: int) -> typing.Optional[Response]:
order = Order.query.filter(Order.id == id).first() order = Order.query.filter(Order.id == id).first()
if order is None: if order is None:
abort(404) abort(404)
@ -208,9 +220,13 @@ def close_order(id):
order.courrier_id = courrier.id order.courrier_id = courrier.id
db.session.commit() db.session.commit()
return redirect(url_for("order_bp.order", id=id)) return redirect(url_for("order_bp.order", id=id))
# The line below is to make sure mypy doesn't say
# "Missing return statement"
# https://github.com/python/mypy/issues/4223
return None
def select_user(items): def select_user(items) -> typing.Optional[User]:
user = None user = None
# remove non users # remove non users
items = [i for i in items if i.user_id] items = [i for i in items if i.user_id]
@ -228,8 +244,8 @@ def select_user(items):
return user return user
def get_orders(expression=None): def get_orders(expression=None) -> typing.List[Order]:
orders = [] orders: typing.List[OrderForm] = []
if expression is None: if expression is None:
expression = (datetime.now() > Order.starttime) & ( expression = (datetime.now() > Order.starttime) & (
Order.stoptime > datetime.now() Order.stoptime > datetime.now()
@ -237,5 +253,6 @@ def get_orders(expression=None):
if not current_user.is_anonymous(): if not current_user.is_anonymous():
orders = Order.query.filter(expression).all() orders = Order.query.filter(expression).all()
else: else:
orders = Order.query.filter((expression & (Order.public == True))).all() orders = Order.query.filter(
(expression & (Order.public == True))).all()
return orders return orders

View file

@ -8,7 +8,7 @@ stats_blueprint = Blueprint("stats_blueprint", __name__)
@stats_blueprint.route("/") @stats_blueprint.route("/")
def stats(): def stats() -> str:
data = { data = {
"amount": { "amount": {
"orders": FatOrder.amount(), "orders": FatOrder.amount(),