Move filtering by association to get_orders

Fixes duplication and restores old `/orders` behaviour
This commit is contained in:
Charlotte Van Petegem 2022-05-30 20:23:14 +02:00
parent 5306561ddd
commit 426357f00d
No known key found for this signature in database
GPG key ID: 019E764B7184435A
2 changed files with 10 additions and 9 deletions

View file

@ -31,11 +31,11 @@ def home() -> str:
"Generate the home view" "Generate the home view"
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.association.in_(current_user.association_list()) (Order.stoptime > prev_day) & (Order.stoptime < datetime.now())
) )
return render_template( return render_template(
"home.html", orders=get_orders( "home.html", orders=get_orders(
((datetime.now() > Order.starttime) & (Order.stoptime > datetime.now()) | (Order.stoptime == None)) & Order.association.in_(current_user.association_list()) ((datetime.now() > Order.starttime) & (Order.stoptime > datetime.now()) | (Order.stoptime == None))
), recently_closed=recently_closed ), recently_closed=recently_closed
) )

View file

@ -27,7 +27,7 @@ def orders(form: OrderForm = None) -> str:
form.location_id.default = location_id form.location_id.default = location_id
form.process() form.process()
form.populate() form.populate()
return render_template("orders.html", orders=get_orders(expression=Order.association.in_(current_user.association_list())), form=form) return render_template("orders.html", orders=get_orders(), form=form)
@order_bp.route("/create", methods=["POST"]) @order_bp.route("/create", methods=["POST"])
@ -396,16 +396,17 @@ def get_orders(expression=None) -> typing.List[Order]:
"""Give the list of all currently open and public Orders""" """Give the list of all currently open and public Orders"""
order_list: typing.List[OrderForm] = [] order_list: typing.List[OrderForm] = []
if expression is None: if expression is None:
expression = (datetime.now() > Order.starttime) & ( expression = ((datetime.now() > Order.starttime) & (
Order.stoptime Order.stoptime
> datetime.now() > datetime.now()
# pylint: disable=C0121 # pylint: disable=C0121
) | (Order.stoptime == None) ) | (Order.stoptime == None)
) & (Order.association.in_(current_user.association_list()))
if not current_user.is_anonymous(): if not current_user.is_anonymous():
order_list = Order.query.filter(expression).all() order_list = Order.query.filter(expression).all()
else: else:
order_list = Order.query.filter( order_list = Order.query.filter(
# pylint: disable=C0121 # pylint: disable=C0121
expression & (Order.public == True) expression & (Order.public == True) & (Order.association.in_(current_user.association_list()))
).all() ).all()
return order_list return order_list