From 3408e1ddd3f99f627686d52bfee6130dbcf7bb7a Mon Sep 17 00:00:00 2001 From: Tony Locke Date: Mon, 4 Sep 2023 10:46:42 +0100 Subject: [PATCH] i --- chellow/e/views.py | 58 +++++++++++++++---- .../templates/e/site_add_e_supply_form.html | 6 +- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/chellow/e/views.py b/chellow/e/views.py index 0bab87f86..d7902c96b 100644 --- a/chellow/e/views.py +++ b/chellow/e/views.py @@ -21,7 +21,7 @@ ) from sqlalchemy import Float, case, cast, false, func, null, or_, select, text, true -from sqlalchemy.orm import joinedload +from sqlalchemy.orm import aliased, joinedload from werkzeug.exceptions import BadRequest, NotFound @@ -3825,22 +3825,60 @@ def site_add_e_supply_form_get(site_id): .filter(SiteEra.site == site) .order_by(Era.start_date.desc()) ) - mop_contracts = ( - g.sess.query(Contract) + RateScriptAliasStart = aliased(RateScript) + RateScriptAliasFinish = aliased(RateScript) + mop_contracts = g.sess.scalars( + select(Contract) .join(MarketRole) - .filter(MarketRole.code == "M") + .join( + RateScriptAliasStart, + Contract.start_rate_script_id == RateScriptAliasStart.id, + ) + .join( + RateScriptAliasFinish, + Contract.finish_rate_script_id == RateScriptAliasFinish.id, + ) + .where( + MarketRole.code == "M", + start_date >= RateScriptAliasStart.start_date, + RateScriptAliasFinish.finish_date == null(), + ) .order_by(Contract.name) ) - dc_contracts = ( - g.sess.query(Contract) + dc_contracts = g.sess.scalars( + select(Contract) .join(MarketRole) - .filter(MarketRole.code.in_(("C", "D"))) + .join( + RateScriptAliasStart, + Contract.start_rate_script_id == RateScriptAliasStart.id, + ) + .join( + RateScriptAliasFinish, + Contract.finish_rate_script_id == RateScriptAliasFinish.id, + ) + .where( + MarketRole.code.in_(("C", "D")), + start_date >= RateScriptAliasStart.start_date, + RateScriptAliasFinish.finish_date == null(), + ) .order_by(Contract.name) ) - supplier_contracts = ( - g.sess.query(Contract) + supplier_contracts = g.sess.scalars( + select(Contract) .join(MarketRole) - .filter(MarketRole.code == "X") + .join( + RateScriptAliasStart, + Contract.start_rate_script_id == RateScriptAliasStart.id, + ) + .join( + RateScriptAliasFinish, + Contract.finish_rate_script_id == RateScriptAliasFinish.id, + ) + .where( + MarketRole.code == "X", + start_date >= RateScriptAliasStart.start_date, + RateScriptAliasFinish.finish_date == null(), + ) .order_by(Contract.name) ) pcs = g.sess.query(Pc).order_by(Pc.code) diff --git a/chellow/templates/e/site_add_e_supply_form.html b/chellow/templates/e/site_add_e_supply_form.html index d8a2e2ee6..60bc08cda 100644 --- a/chellow/templates/e/site_add_e_supply_form.html +++ b/chellow/templates/e/site_add_e_supply_form.html @@ -112,11 +112,11 @@ {% endfor %} - {{input_textarea('properties', '{}', 20, 80)}} + {{input_textarea('properties', '{}', 10, 80)}} {{input_checkbox('has_imp_mpan')}} - {% if 'has_imp_mpan' in request.values %} + {% if request.values.has_imp_mpan == 'true' %}
Import @@ -143,7 +143,7 @@ {{input_checkbox('has_exp_mpan')}} - {% if 'has_exp_mpan' in request.values %} + {% if request.values.has_exp_mpan == 'true' %}
Export