From dc0396be26febfdf547465675fce7c92aaf65b48 Mon Sep 17 00:00:00 2001 From: Tony Locke Date: Thu, 5 Dec 2024 11:07:04 +0000 Subject: [PATCH] Use relative redirects --- chellow/__init__.py | 1 - chellow/e/computer.py | 8 +- chellow/e/views.py | 14 +--- chellow/gas/engine.py | 4 +- chellow/gas/views.py | 5 +- chellow/reports/report_109.py | 5 +- chellow/reports/report_111.py | 5 +- chellow/reports/report_169.py | 5 +- chellow/reports/report_181.py | 5 +- chellow/reports/report_183.py | 7 +- chellow/reports/report_187.py | 5 +- chellow/reports/report_219.py | 5 +- chellow/reports/report_231.py | 5 +- chellow/reports/report_233.py | 5 +- chellow/reports/report_241.py | 5 +- chellow/reports/report_247.py | 5 +- chellow/reports/report_29.py | 5 +- chellow/reports/report_291.py | 5 +- chellow/reports/report_33.py | 5 +- chellow/reports/report_387.py | 5 +- chellow/reports/report_41.py | 5 +- chellow/reports/report_429.py | 5 +- chellow/reports/report_59.py | 5 +- chellow/reports/report_81.py | 5 +- chellow/reports/report_87.py | 10 +-- chellow/reports/report_asset_comparison.py | 5 +- chellow/reports/report_batches.py | 5 +- chellow/reports/report_bills.py | 5 +- chellow/reports/report_csv_llfcs.py | 5 +- chellow/reports/report_csv_site_hh_data.py | 5 +- chellow/reports/report_csv_site_snags.py | 5 +- chellow/reports/report_ecoes_comparison.py | 5 +- chellow/reports/report_g_monthly_duration.py | 5 +- chellow/reports/report_g_supplies_snapshot.py | 5 +- .../reports/report_g_supply_virtual_bill.py | 5 +- chellow/reports/report_g_virtual_bills.py | 5 +- chellow/reports/report_g_virtual_bills_hh.py | 5 +- chellow/reports/report_sscs.py | 5 +- chellow/reports/report_supply_contacts.py | 5 +- chellow/utils.py | 2 - chellow/views.py | 81 ++++++++----------- 41 files changed, 112 insertions(+), 180 deletions(-) diff --git a/chellow/__init__.py b/chellow/__init__.py index 53cd6e2c2..e7adfdda3 100644 --- a/chellow/__init__.py +++ b/chellow/__init__.py @@ -89,7 +89,6 @@ def create_app(testing=False, instance_path=None): props = configuration.make_properties() api_props = props.get("api", {}) api.description = api_props.get("description", "Access Chellow data") - chellow.utils.url_root = props.get("url_root", "") chellow.dloads.startup(Path(app.instance_path), run_deleter=(not testing)) diff --git a/chellow/e/computer.py b/chellow/e/computer.py index cb21eac97..8fc74e216 100644 --- a/chellow/e/computer.py +++ b/chellow/e/computer.py @@ -16,8 +16,6 @@ from zish import dumps -import chellow.bank_holidays -import chellow.utils from chellow.models import ( Bill, BillType, @@ -218,11 +216,7 @@ def hh_rate(sess, caches, contract_id_or_name, date, market_role_code=None): f"The market role code {market_role_code} isn't recognized." ) - vals = PropDict( - f"the rate script {chellow.utils.url_root}{seg}{rs.id} ", - loads(rs.script), - [], - ) + vals = PropDict(f"the rate script {seg}{rs.id} ", loads(rs.script), []) for dt in hh_range(caches, cstart, cfinish): if dt not in cont_cache: cont_cache[dt] = vals diff --git a/chellow/e/views.py b/chellow/e/views.py index 09ca1e2b8..ff2910816 100644 --- a/chellow/e/views.py +++ b/chellow/e/views.py @@ -16,6 +16,7 @@ flash, g, make_response, + redirect, render_template as rtemplate, request, ) @@ -105,14 +106,13 @@ utc_datetime_now, ) from chellow.views import ( - chellow_redirect as credirect, hx_redirect as chx_redirect, requires_editor, ) def chellow_redirect(path, code=None): - return credirect(f"/e{path}", code) + return redirect(f"/e{path}", code) def hx_redirect(path, code=None): @@ -1421,9 +1421,7 @@ def dno_rate_script_edit_delete(dno_rate_script_id): dno = Party.get_dno_by_code(g.sess, contract.name, rate_script.start_date) contract.delete_rate_script(g.sess, rate_script) g.sess.commit() - res = make_response() - res.headers["HX-Redirect"] = f"{chellow.utils.url_root}/e/dnos/{dno.id}" - return res + return hx_redirect(f"/dnos/{dno.id}") except BadRequest as e: flash(e.description) return render_template( @@ -6020,11 +6018,7 @@ def supply_note_edit_delete(supply_id, index): del supply_note["notes"][index] supply.note = dumps(supply_note) g.sess.commit() - res = make_response() - res.headers["HX-Redirect"] = ( - f"{chellow.utils.url_root}/e/supplies/{supply_id}/notes" - ) - return res + return hx_redirect(f"/supplies/{supply_id}/notes") except BadRequest as e: flash(e.description) supply_note = loads(supply.note) diff --git a/chellow/gas/engine.py b/chellow/gas/engine.py index f68624e36..a25bfd29f 100644 --- a/chellow/gas/engine.py +++ b/chellow/gas/engine.py @@ -13,7 +13,6 @@ from zish import dumps, loads -import chellow.bank_holidays from chellow.e.computer import hh_rate from chellow.models import ( BillType, @@ -323,8 +322,7 @@ def g_rates(sess, caches, g_contract_id_or_name, date, is_industry): prefix = "industry" if g_contract.is_industry else "supplier" vals = PropDict( - f"the rate script {chellow.utils.url_root}/g/{prefix}_rate_scripts/" - f"{rs.id} ", + f"the rate script /g/{prefix}_rate_scripts/{rs.id} ", loads(rs.script), [], ) diff --git a/chellow/gas/views.py b/chellow/gas/views.py index 89397fbd2..98335f30d 100644 --- a/chellow/gas/views.py +++ b/chellow/gas/views.py @@ -13,6 +13,7 @@ flash, g, make_response, + redirect, render_template as rtemplate, request, ) @@ -60,11 +61,11 @@ utc_datetime, utc_datetime_now, ) -from chellow.views import chellow_redirect as credirect, hx_redirect as chx_redirect +from chellow.views import hx_redirect as chx_redirect def chellow_redirect(path, code=None): - return credirect(f"/g{path}", code) + return redirect(f"/g{path}", code) def hx_redirect(path, code=None): diff --git a/chellow/reports/report_109.py b/chellow/reports/report_109.py index 1f3440599..b9b707128 100644 --- a/chellow/reports/report_109.py +++ b/chellow/reports/report_109.py @@ -4,7 +4,7 @@ import traceback from datetime import datetime as Datetime -from flask import g +from flask import g, redirect from sqlalchemy import or_, text from sqlalchemy.sql.expression import null @@ -13,7 +13,6 @@ from chellow.e.computer import SiteSource, contract_func, displaced_era, forecast_date from chellow.models import Contract, Era, Session, Site, SiteEra, Source, Supply from chellow.utils import c_months_u, hh_format, hh_range, req_int -from chellow.views import chellow_redirect def to_val(v): @@ -274,4 +273,4 @@ def do_get(sess): contract_id = req_int("supplier_contract_id") args = contract_id, end_year, end_month, months, g.user threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_111.py b/chellow/reports/report_111.py index fac9072aa..fa1e8f0c1 100644 --- a/chellow/reports/report_111.py +++ b/chellow/reports/report_111.py @@ -9,7 +9,7 @@ from dateutil.relativedelta import relativedelta -from flask import g, request +from flask import g, redirect, request from sqlalchemy import or_, select from sqlalchemy.orm import joinedload, subqueryload @@ -49,7 +49,6 @@ req_str, to_utc, ) -from chellow.views import chellow_redirect def add_gap(caches, gaps, elem, start_date, finish_date, is_virtual, gbp): @@ -298,7 +297,7 @@ def do_post(sess): report_run.id, ) threading.Thread(target=content, args=args).start() - return chellow_redirect(f"/report_runs/{report_run.id}", 303) + return redirect(f"/report_runs/{report_run.id}", 303) def _process_supply( diff --git a/chellow/reports/report_169.py b/chellow/reports/report_169.py index 86489971f..969e2bc09 100644 --- a/chellow/reports/report_169.py +++ b/chellow/reports/report_169.py @@ -3,7 +3,7 @@ import traceback from io import StringIO -from flask import g, request +from flask import g, redirect, request from sqlalchemy import or_, select from sqlalchemy.sql.expression import null @@ -23,7 +23,6 @@ to_ct, to_utc, ) -from chellow.views import chellow_redirect def _write_row(writer, titles, vals, total): @@ -266,4 +265,4 @@ def handle_request(mpan_cores=None): user.id, ) threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_181.py b/chellow/reports/report_181.py index 09ff7de4c..84bc6f48f 100644 --- a/chellow/reports/report_181.py +++ b/chellow/reports/report_181.py @@ -2,7 +2,7 @@ import threading import traceback -from flask import g, request +from flask import g, redirect, request from sqlalchemy import or_ from sqlalchemy.sql.expression import null @@ -23,7 +23,6 @@ req_int, to_utc, ) -from chellow.views import chellow_redirect def _make_sites(sess, year_start, year_finish, site_id, source_codes): @@ -169,4 +168,4 @@ def do_get(sess): site_id = req_int("site_id") if "site_id" in request.values else None year = req_int("year") threading.Thread(target=content, args=(year, site_id, g.user.id)).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_183.py b/chellow/reports/report_183.py index 633dda89d..b397925a1 100644 --- a/chellow/reports/report_183.py +++ b/chellow/reports/report_183.py @@ -4,7 +4,7 @@ import traceback from io import StringIO -from flask import g, request +from flask import g, redirect, request from sqlalchemy import or_, select from sqlalchemy.orm import joinedload @@ -22,7 +22,6 @@ to_ct, to_utc, ) -from chellow.views import chellow_redirect def _write_row(writer, titles, vals, total): @@ -241,7 +240,7 @@ def do_post(sess): file_name = f"sites_hh_data_{site_id}_{finish_date_str}.csv" args = site_id, start_date, finish_date, g.user.id, file_name threading.Thread(target=site_content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) else: typ = req_str("type") site_codes_str = req_str("site_codes") @@ -252,4 +251,4 @@ def do_post(sess): file_name = f"sites_hh_data_{finish_date_str}_filter.zip" args = site_codes, typ, start_date, finish_date, g.user.id, file_name threading.Thread(target=none_content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_187.py b/chellow/reports/report_187.py index 524283221..35da613c1 100644 --- a/chellow/reports/report_187.py +++ b/chellow/reports/report_187.py @@ -2,7 +2,7 @@ import traceback from datetime import datetime as Datetime -from flask import g, request +from flask import g, redirect, request from sqlalchemy import null, or_, select, text, true @@ -20,7 +20,6 @@ req_str, to_ct, ) -from chellow.views import chellow_redirect def csv_str(row): @@ -267,4 +266,4 @@ def do_post(sess): args = start_date, finish_date, supply_id, mpan_cores, is_zipped, g.user.id threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_219.py b/chellow/reports/report_219.py index 2dba97ed1..03b8f68d6 100644 --- a/chellow/reports/report_219.py +++ b/chellow/reports/report_219.py @@ -2,7 +2,7 @@ import threading import traceback -from flask import g, request +from flask import g, redirect, request from sqlalchemy import and_, or_, select from sqlalchemy.orm import joinedload @@ -21,7 +21,6 @@ User, ) from chellow.utils import c_months_u, csv_make_val, req_int -from chellow.views import chellow_redirect def content(year, month, months, supply_id, user_id): @@ -189,4 +188,4 @@ def do_get(sess): supply_id = req_int("supply_id") if "supply_id" in request.values else None args = year, month, months, supply_id, g.user.id threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_231.py b/chellow/reports/report_231.py index 0ee256545..6553203bc 100644 --- a/chellow/reports/report_231.py +++ b/chellow/reports/report_231.py @@ -3,7 +3,7 @@ import threading import traceback -from flask import g +from flask import g, redirect from sqlalchemy import or_ from sqlalchemy.sql.expression import null @@ -14,7 +14,6 @@ from chellow.e.computer import SupplySource, contract_func, forecast_date from chellow.models import Contract, Era, Session, User from chellow.utils import csv_make_val, hh_format, hh_max, hh_min, req_date, req_int -from chellow.views import chellow_redirect def content(user_id, start_date, finish_date, contract_id): @@ -134,4 +133,4 @@ def do_get(sess): args = g.user.id, start_date, finish_date, contract_id threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_233.py b/chellow/reports/report_233.py index 247083cef..4540634c2 100644 --- a/chellow/reports/report_233.py +++ b/chellow/reports/report_233.py @@ -5,7 +5,7 @@ from dateutil.relativedelta import relativedelta -from flask import g +from flask import g, redirect from sqlalchemy.orm import joinedload from sqlalchemy.sql.expression import false, select, true @@ -30,7 +30,6 @@ req_int_none, utc_datetime_now, ) -from chellow.views import chellow_redirect def content(contract_id, days_hidden, is_ignored, user_id): @@ -148,4 +147,4 @@ def do_get(sess): args = contract_id, days_hidden, is_ignored, g.user.id threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_241.py b/chellow/reports/report_241.py index 063f5568a..d5fab6691 100644 --- a/chellow/reports/report_241.py +++ b/chellow/reports/report_241.py @@ -5,7 +5,7 @@ from dateutil.relativedelta import relativedelta -from flask import g +from flask import g, redirect import pytz @@ -17,7 +17,6 @@ from chellow.e.computer import SupplySource, contract_func, forecast_date from chellow.models import Era, Session, Site, SiteEra, Supply, User from chellow.utils import HH, csv_make_val, hh_format, hh_max, hh_min, req_bool, req_int -from chellow.views import chellow_redirect def content( @@ -164,4 +163,4 @@ def do_get(sess): g.user.id, ) threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_247.py b/chellow/reports/report_247.py index e83bca94d..e219a715f 100644 --- a/chellow/reports/report_247.py +++ b/chellow/reports/report_247.py @@ -6,7 +6,7 @@ from dateutil.relativedelta import relativedelta -from flask import flash, g, make_response, render_template, request +from flask import flash, g, make_response, redirect, render_template, request import odio @@ -48,7 +48,6 @@ to_utc, utc_datetime_now, ) -from chellow.views import chellow_redirect CATEGORY_ORDER = {None: 0, "unmetered": 1, "nhh": 2, "amr": 3, "hh": 4} @@ -1129,7 +1128,7 @@ def do_post(sess): args = (scenario_props, base_name, user.id, compression, now) threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) except BadRequest as e: flash(e.description) if "scenario_id" in request.values: diff --git a/chellow/reports/report_29.py b/chellow/reports/report_29.py index b832946c3..259dc5807 100644 --- a/chellow/reports/report_29.py +++ b/chellow/reports/report_29.py @@ -3,12 +3,11 @@ import threading import traceback -from flask import g +from flask import g, redirect from chellow.dloads import open_file from chellow.models import Session, Site, User from chellow.utils import c_months_u, csv_make_val, req_int, req_str, to_ct -from chellow.views import chellow_redirect def _write_row(writer, total, values, titles): @@ -78,4 +77,4 @@ def do_get(sess): site_id = req_int("site_id") args = start_date, finish_date, site_id, typ, g.user.id threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_291.py b/chellow/reports/report_291.py index f605451eb..a74284030 100644 --- a/chellow/reports/report_291.py +++ b/chellow/reports/report_291.py @@ -2,7 +2,7 @@ import threading import traceback -from flask import g +from flask import g, redirect from sqlalchemy import or_ from sqlalchemy.sql.expression import null, true @@ -23,7 +23,6 @@ User, ) from chellow.utils import csv_make_val, hh_format, hh_max, hh_min, req_date, req_int -from chellow.views import chellow_redirect def content(supply_id, start_date, finish_date, user_id): @@ -209,4 +208,4 @@ def do_get(sess): args = supply_id, start_date, finish_date, g.user.id threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_33.py b/chellow/reports/report_33.py index 39f681a41..d736446f2 100644 --- a/chellow/reports/report_33.py +++ b/chellow/reports/report_33.py @@ -5,7 +5,7 @@ from dateutil.relativedelta import relativedelta -from flask import g, request +from flask import g, redirect, request from sqlalchemy import or_, text from sqlalchemy.orm import joinedload @@ -42,7 +42,6 @@ req_int, req_str, ) -from chellow.views import chellow_redirect def content(user_id, date, supply_id, mpan_cores): @@ -528,4 +527,4 @@ def do_get(session): args = user.id, date, supply_id, mpan_cores threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_387.py b/chellow/reports/report_387.py index 51e829286..facce95a7 100644 --- a/chellow/reports/report_387.py +++ b/chellow/reports/report_387.py @@ -3,7 +3,7 @@ import threading import traceback -from flask import g +from flask import g, redirect from sqlalchemy import or_, select from sqlalchemy.sql.expression import null, true @@ -18,7 +18,6 @@ req_date, req_int, ) -from chellow.views import chellow_redirect def content(supply_id, start_date, finish_date, user): @@ -162,4 +161,4 @@ def do_get(sess): args = supply_id, start_date, finish_date, g.user threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_41.py b/chellow/reports/report_41.py index b809154e1..608006ef5 100644 --- a/chellow/reports/report_41.py +++ b/chellow/reports/report_41.py @@ -4,7 +4,7 @@ import traceback from datetime import datetime as Datetime -from flask import g, request +from flask import g, redirect, request from sqlalchemy import or_ from sqlalchemy.sql.expression import null, true @@ -23,7 +23,6 @@ req_int, to_utc, ) -from chellow.views import chellow_redirect def _make_eras(sess, nov_start, year_finish, supply_id): @@ -211,4 +210,4 @@ def do_get(sess): year = req_int("year") supply_id = req_int("supply_id") if "supply_id" in request.values else None threading.Thread(target=content, args=(year, supply_id, g.user.id)).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_429.py b/chellow/reports/report_429.py index 89d613d62..e95fb22ce 100644 --- a/chellow/reports/report_429.py +++ b/chellow/reports/report_429.py @@ -7,7 +7,7 @@ from decimal import Decimal from itertools import combinations -from flask import g, request +from flask import g, redirect, request from sqlalchemy import null, or_, select, true from sqlalchemy.orm import joinedload @@ -18,7 +18,6 @@ from chellow.dloads import open_file from chellow.models import GBatch, GBill, GContract, GEra, Session, Site, SiteGEra, User from chellow.utils import csv_make_val, hh_max, hh_min, req_date, req_int, to_utc -from chellow.views import chellow_redirect def content(g_batch_id, g_bill_id, g_contract_id, start_date, finish_date, user_id): @@ -361,4 +360,4 @@ def do_get(sess): args = g_batch_id, g_bill_id, g_contract_id, start_date, finish_date, g.user.id threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_59.py b/chellow/reports/report_59.py index 6b873b2ab..ead914e27 100644 --- a/chellow/reports/report_59.py +++ b/chellow/reports/report_59.py @@ -5,7 +5,7 @@ from dateutil.relativedelta import relativedelta -from flask import flash, g, make_response, render_template, request +from flask import flash, g, make_response, redirect, render_template, request import odio @@ -50,7 +50,6 @@ to_utc, utc_datetime_now, ) -from chellow.views import chellow_redirect CATEGORY_ORDER = {None: 0, "unmetered": 1, "nhh": 2, "amr": 3, "hh": 4} @@ -1072,7 +1071,7 @@ def do_post(sess): sess.rollback() thread = threading.Thread(target=content, args=args) thread.start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) except BadRequest as e: flash(e.description) if "scenario_id" in request.values: diff --git a/chellow/reports/report_81.py b/chellow/reports/report_81.py index 187905cec..22ff3ca33 100644 --- a/chellow/reports/report_81.py +++ b/chellow/reports/report_81.py @@ -2,7 +2,7 @@ import threading import traceback -from flask import g +from flask import g, redirect from sqlalchemy import or_ from sqlalchemy.orm import joinedload @@ -14,7 +14,6 @@ from chellow.e.computer import SupplySource, contract_func, forecast_date from chellow.models import Contract, Era, RSession, User from chellow.utils import c_months_u, csv_make_val, hh_format, hh_max, hh_min, req_int -from chellow.views import chellow_redirect def content(user_id, contract_id, end_year, end_month, months): @@ -147,4 +146,4 @@ def do_get(sess): args = g.user.id, contract_id, end_year, end_month, months threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_87.py b/chellow/reports/report_87.py index 305759e02..78b95dae5 100644 --- a/chellow/reports/report_87.py +++ b/chellow/reports/report_87.py @@ -2,7 +2,7 @@ import threading import traceback -from flask import g +from flask import g, redirect from sqlalchemy import or_, select from sqlalchemy.sql.expression import null, true @@ -32,7 +32,6 @@ req_int, to_ct, ) -from chellow.views import chellow_redirect def _process_era( @@ -202,10 +201,7 @@ def create_csv(f, sess, start_date, finish_date, contract_id): ) writer.writerow(csv_make_val(vals.get(t)) for t in titles) except BadRequest as e: - raise BadRequest( - f"Problem with {chellow.utils.url_root}eras/{era.id}/edit " - f"{e.description}" - ) + raise BadRequest(f"Problem with /e/eras/{era.id}/edit {e.description}") sess.rollback() # Avoid long-running transaction @@ -243,4 +239,4 @@ def do_get(sess): threading.Thread( target=content, args=(start_date, finish_date, contract_id, g.user.id) ).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_asset_comparison.py b/chellow/reports/report_asset_comparison.py index f3a27433e..98c94fb34 100644 --- a/chellow/reports/report_asset_comparison.py +++ b/chellow/reports/report_asset_comparison.py @@ -4,7 +4,7 @@ import traceback from io import StringIO -from flask import g, request +from flask import g, redirect, request from sqlalchemy import select from sqlalchemy.orm import joinedload @@ -14,7 +14,6 @@ from chellow.dloads import open_file from chellow.models import Contract, Era, ReportRun, Session, Site, SiteEra, User -from chellow.views import chellow_redirect STATUSES_ACTIVE = ("IN USE / IN SERVICE", "STORED SPARE") STATUSES_INACTIVE = ("DEMOLISHED", "SOLD", "ABANDONED") @@ -194,4 +193,4 @@ def do_post(sess): sess.commit() args = user.id, StringIO(file_item.read().decode("utf8")), report_run.id threading.Thread(target=content, args=args).start() - return chellow_redirect(f"/report_runs/{report_run.id}", 303) + return redirect(f"/report_runs/{report_run.id}", 303) diff --git a/chellow/reports/report_batches.py b/chellow/reports/report_batches.py index b6ef6a4c8..e212e69c3 100644 --- a/chellow/reports/report_batches.py +++ b/chellow/reports/report_batches.py @@ -2,7 +2,7 @@ import threading import traceback -from flask import g +from flask import g, redirect from sqlalchemy.sql import func, select @@ -16,7 +16,6 @@ User, ) from chellow.utils import csv_make_val -from chellow.views import chellow_redirect def content(user_id): @@ -142,4 +141,4 @@ def content(user_id): def do_get(sess): threading.Thread(target=content, args=(g.user.id,)).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_bills.py b/chellow/reports/report_bills.py index 62e4b7848..574256ede 100644 --- a/chellow/reports/report_bills.py +++ b/chellow/reports/report_bills.py @@ -3,7 +3,7 @@ import traceback from collections import defaultdict -from flask import g +from flask import g, redirect from sqlalchemy import select from sqlalchemy.orm import joinedload @@ -11,7 +11,6 @@ from chellow.dloads import open_file from chellow.models import Batch, Bill, Session, User from chellow.utils import csv_make_val, req_int -from chellow.views import chellow_redirect def _content(sess, writer, batch_id): @@ -111,4 +110,4 @@ def do_get(sess): batch_id = req_int("batch_id") args = g.user.id, batch_id threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_csv_llfcs.py b/chellow/reports/report_csv_llfcs.py index 456f58b12..33e10e256 100644 --- a/chellow/reports/report_csv_llfcs.py +++ b/chellow/reports/report_csv_llfcs.py @@ -2,14 +2,13 @@ import threading import traceback -from flask import g +from flask import g, redirect from sqlalchemy.orm import joinedload from chellow.dloads import open_file from chellow.models import Llfc, Session, User from chellow.utils import hh_format -from chellow.views import chellow_redirect def content(user_id): @@ -64,4 +63,4 @@ def content(user_id): def do_get(sess): args = (g.user.id,) threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_csv_site_hh_data.py b/chellow/reports/report_csv_site_hh_data.py index 5d39f5f2b..83a9c02c9 100644 --- a/chellow/reports/report_csv_site_hh_data.py +++ b/chellow/reports/report_csv_site_hh_data.py @@ -3,7 +3,7 @@ import threading import traceback -from flask import g, request +from flask import g, redirect, request from sqlalchemy import null, or_, select, true from sqlalchemy.orm import joinedload @@ -11,7 +11,6 @@ from chellow.dloads import open_file from chellow.models import Era, Session, Site, SiteEra, Source, Supply, User from chellow.utils import req_date, req_int, write_row -from chellow.views import chellow_redirect TYPE_ORDER = {"hh": 0, "amr": 1, "nhh": 2, "unmetered": 3} @@ -122,4 +121,4 @@ def do_get(sess): args = start_date, finish_date, site_id, g.user.id threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_csv_site_snags.py b/chellow/reports/report_csv_site_snags.py index da024816b..2a929ae71 100644 --- a/chellow/reports/report_csv_site_snags.py +++ b/chellow/reports/report_csv_site_snags.py @@ -3,7 +3,7 @@ import traceback from datetime import datetime as Datetime, timedelta as Timedelta -from flask import g +from flask import g, redirect import pytz @@ -13,7 +13,6 @@ from chellow.dloads import open_file from chellow.models import Session, Site, Snag, User from chellow.utils import hh_format -from chellow.views import chellow_redirect def content(user_id): @@ -81,4 +80,4 @@ def content(user_id): def do_get(sess): args = (g.user.id,) threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_ecoes_comparison.py b/chellow/reports/report_ecoes_comparison.py index b9454be05..7cce6da50 100644 --- a/chellow/reports/report_ecoes_comparison.py +++ b/chellow/reports/report_ecoes_comparison.py @@ -3,7 +3,7 @@ import threading import traceback -from flask import g +from flask import g, redirect import requests @@ -26,7 +26,6 @@ User, ) from chellow.utils import csv_make_val, req_bool -from chellow.views import chellow_redirect FNAME = "ecoes_comparison" @@ -615,4 +614,4 @@ def do_get(sess): sess.commit() args = g.user.id, show_ignored, report_run.id threading.Thread(target=content, args=args).start() - return chellow_redirect(f"/report_runs/{report_run.id}", 303) + return redirect(f"/report_runs/{report_run.id}", 303) diff --git a/chellow/reports/report_g_monthly_duration.py b/chellow/reports/report_g_monthly_duration.py index 4aa4ab5ac..e1c502631 100644 --- a/chellow/reports/report_g_monthly_duration.py +++ b/chellow/reports/report_g_monthly_duration.py @@ -1,7 +1,7 @@ import threading import traceback -from flask import g, request +from flask import g, redirect, request import odio @@ -42,7 +42,6 @@ to_utc, utc_datetime_now, ) -from chellow.views import chellow_redirect def write_spreadsheet(fl, compressed, site_rows, era_rows): @@ -535,4 +534,4 @@ def do_get(sess): args = scenario_props, g.user.id, compression, now, base_name threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_g_supplies_snapshot.py b/chellow/reports/report_g_supplies_snapshot.py index e9ac5aa09..131075ddf 100644 --- a/chellow/reports/report_g_supplies_snapshot.py +++ b/chellow/reports/report_g_supplies_snapshot.py @@ -3,7 +3,7 @@ import threading import traceback -from flask import g, request +from flask import g, redirect, request from sqlalchemy import or_ from sqlalchemy.orm import joinedload @@ -16,7 +16,6 @@ from chellow.gas.engine import GDataSource, forecast_date from chellow.models import GEra, GSupply, Session from chellow.utils import csv_make_val, req_date, req_int -from chellow.views import chellow_redirect def content(date, g_supply_id, user): @@ -136,4 +135,4 @@ def do_get(session): args = (date, g_supply_id, user) threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_g_supply_virtual_bill.py b/chellow/reports/report_g_supply_virtual_bill.py index 06d401f17..170378ea6 100644 --- a/chellow/reports/report_g_supply_virtual_bill.py +++ b/chellow/reports/report_g_supply_virtual_bill.py @@ -2,7 +2,7 @@ import threading import traceback -from flask import g +from flask import g, redirect from sqlalchemy import or_ from sqlalchemy.sql.expression import null, true @@ -14,7 +14,6 @@ from chellow.gas.engine import GDataSource, g_contract_func from chellow.models import GEra, GSupply, Session, Site, SiteGEra from chellow.utils import csv_make_val, hh_format, hh_max, hh_min, req_date, req_int -from chellow.views import chellow_redirect def content(g_supply_id, file_name, start_date, finish_date, user): @@ -107,4 +106,4 @@ def do_get(sess): args = (g_supply_id, file_name, start_date, finish_date, g.user) threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_g_virtual_bills.py b/chellow/reports/report_g_virtual_bills.py index f1230334f..54d8fd08e 100644 --- a/chellow/reports/report_g_virtual_bills.py +++ b/chellow/reports/report_g_virtual_bills.py @@ -4,7 +4,7 @@ import traceback -from flask import g +from flask import g, redirect from sqlalchemy import or_ from sqlalchemy.sql.expression import null, true @@ -25,7 +25,6 @@ req_int, to_ct, ) -from chellow.views import chellow_redirect def content(start_date, finish_date, g_contract_id, user_id): @@ -137,4 +136,4 @@ def do_get(sess): args = start_date, finish_date, g_contract_id, g.user.id threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_g_virtual_bills_hh.py b/chellow/reports/report_g_virtual_bills_hh.py index ac849f953..c7fd15820 100644 --- a/chellow/reports/report_g_virtual_bills_hh.py +++ b/chellow/reports/report_g_virtual_bills_hh.py @@ -3,7 +3,7 @@ import threading import traceback -from flask import g +from flask import g, redirect from sqlalchemy import or_ from sqlalchemy.sql.expression import null, true @@ -14,7 +14,6 @@ from chellow.gas.engine import GDataSource from chellow.models import GEra, GSupply, Session, Site, SiteGEra from chellow.utils import csv_make_val, hh_format, hh_range, req_date, req_int -from chellow.views import chellow_redirect def content(g_supply_id, start_date, finish_date, user): @@ -105,4 +104,4 @@ def do_get(sess): args = g_supply_id, start_date, finish_date, g.user threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_sscs.py b/chellow/reports/report_sscs.py index 86f8628cf..84fc477eb 100644 --- a/chellow/reports/report_sscs.py +++ b/chellow/reports/report_sscs.py @@ -3,7 +3,7 @@ import traceback -from flask import g, request +from flask import g, redirect, request import odio @@ -15,7 +15,6 @@ from chellow.dloads import open_file from chellow.models import MeasurementRequirement, Session, Ssc, Tpr, User from chellow.utils import req_bool -from chellow.views import chellow_redirect def write_spreadsheet( @@ -114,4 +113,4 @@ def do_get(sess): args = (user.id, compression) threading.Thread(target=content, args=args).start() - return chellow_redirect("/downloads", 303) + return redirect("/downloads", 303) diff --git a/chellow/reports/report_supply_contacts.py b/chellow/reports/report_supply_contacts.py index 6a13d1c10..91782dd8d 100644 --- a/chellow/reports/report_supply_contacts.py +++ b/chellow/reports/report_supply_contacts.py @@ -3,7 +3,7 @@ import threading import traceback -from flask import g +from flask import g, redirect from sqlalchemy import select from sqlalchemy.orm import joinedload @@ -21,7 +21,6 @@ Supply, User, ) -from chellow.views import chellow_redirect FNAME = "supply_contacts" @@ -127,4 +126,4 @@ def do_get(sess): report_run = ReportRun.insert(sess, FNAME, g.user, FNAME, {}) sess.commit() threading.Thread(target=content, args=(g.user.id, report_run.id)).start() - return chellow_redirect(f"/report_runs/{report_run.id}", 303) + return redirect(f"/report_runs/{report_run.id}", 303) diff --git a/chellow/utils.py b/chellow/utils.py index d519911c1..89c8a7545 100644 --- a/chellow/utils.py +++ b/chellow/utils.py @@ -17,8 +17,6 @@ from zish import ZishException, loads -url_root = "" - HH = relativedelta(minutes=30) MONTH = relativedelta(months=1) YEAR = relativedelta(years=1) diff --git a/chellow/views.py b/chellow/views.py index 11bf6c7b7..9551daaaa 100644 --- a/chellow/views.py +++ b/chellow/views.py @@ -135,19 +135,9 @@ home = Blueprint("home", __name__, url_prefix="", template_folder="templates") -def chellow_redirect(path, code=None): - location = chellow.utils.url_root + path - if code is None: - res = redirect(location) - else: - res = redirect(location, code) - - return res - - def hx_redirect(path, status=None): res = Response(status=status) - res.headers["HX-Redirect"] = chellow.utils.url_root + path + res.headers["HX-Redirect"] = path return res @@ -165,7 +155,7 @@ def decorated_function(*args, **kwargs): @home.route("/configuration", methods=["GET"]) def configuration(): config = Contract.get_non_core_by_name(g.sess, "configuration") - return chellow_redirect(f"/non_core_contracts/{config.id}") + return redirect(f"/non_core_contracts/{config.id}") @home.route("/health") @@ -215,7 +205,7 @@ def local_reports_post(): return Response("There's already a report with that name.", status=400) else: raise - return chellow_redirect(f"/local_reports/{report.id}", 303) + return redirect(f"/local_reports/{report.id}", 303) @home.route("/local_reports/") @@ -229,7 +219,7 @@ def local_report_post(report_id): report = Report.get_by_id(g.sess, report_id) if "delete" in request.values: g.sess.delete(report) - return chellow_redirect("/local_reports", 303) + return redirect("/local_reports", 303) else: name = req_str("name") @@ -243,7 +233,7 @@ def local_report_post(report_id): return Response("There's already a report with that name.", status=400) else: raise - return chellow_redirect(f"/local_reports/{report.id}", 303) + return redirect(f"/local_reports/{report.id}", 303) @home.route("/scenarios") @@ -259,7 +249,7 @@ def scenario_add_post(): properties = req_zish("properties") scenario = Scenario.insert(g.sess, name, properties) g.sess.commit() - return chellow_redirect(f"/scenarios/{scenario.id}", 303) + return redirect(f"/scenarios/{scenario.id}", 303) except BadRequest as e: g.sess.rollback() flash(e.description) @@ -352,16 +342,13 @@ def scenario_edit_post(scenario_id): properties = req_zish("properties") scenario.update(name, properties) g.sess.commit() - return chellow_redirect(f"/scenarios/{scenario.id}", 303) + return redirect(f"/scenarios/{scenario.id}", 303) except BadRequest as e: g.sess.rollback() description = e.description flash(description) return make_response( - render_template( - "scenario_edit.html", - scenario=scenario, - ), + render_template("scenario_edit.html", scenario=scenario), 400, ) @@ -372,9 +359,7 @@ def scenario_edit_delete(scenario_id): scenario = Scenario.get_by_id(g.sess, scenario_id) scenario.delete(g.sess) g.sess.commit() - res = make_response() - res.headers["HX-Redirect"] = f"{chellow.utils.url_root}/e/scenarios" - return res + return hx_redirect("/scenarios") except BadRequest as e: g.sess.rollback() flash(e.description) @@ -633,7 +618,7 @@ def users_post(): party = g.sess.query(Party).get(party_id) user = User.insert(g.sess, email_address, password, role, party) g.sess.commit() - return chellow_redirect("/users/" + str(user.id), 303) + return redirect("/users/" + str(user.id), 303) except BadRequest as e: g.sess.rollback() flash(e.description) @@ -669,11 +654,11 @@ def user_post(user_id): raise BadRequest("The password must be at least 6 characters long.") user.set_password(new_password) g.sess.commit() - return chellow_redirect("/users/" + str(user.id), 303) + return redirect("/users/" + str(user.id), 303) elif "delete" in request.values: g.sess.delete(user) g.sess.commit() - return chellow_redirect("/users", 303) + return redirect("/users", 303) else: email_address = req_str("email_address") user_role_code = req_str("user_role_code") @@ -684,7 +669,7 @@ def user_post(user_id): party = Party.get_by_id(g.sess, party_id) user.update(email_address, user_role, party) g.sess.commit() - return chellow_redirect("/users/" + str(user.id), 303) + return redirect("/users/" + str(user.id), 303) except BadRequest as e: flash(e.description) parties = ( @@ -743,7 +728,7 @@ def general_imports_post(): ) f.seek(0) proc_id = chellow.general_import.start_process(f) - return chellow_redirect("/general_imports/" + str(proc_id), 303) + return redirect(f"/general_imports/{proc_id}", 303) except BadRequest as e: flash(e.description) return render_template( @@ -921,7 +906,7 @@ def site_edit_post(site_id): site.delete(g.sess) g.sess.commit() flash("Site deleted successfully.") - return chellow_redirect("/sites", 303) + return redirect("/sites", 303) elif "update" in request.form: code = req_str("code") @@ -929,7 +914,7 @@ def site_edit_post(site_id): site.update(code, name) g.sess.commit() flash("Site updated successfully.") - return chellow_redirect(f"/sites/{site.id}", 303) + return redirect(f"/sites/{site.id}", 303) elif "insert_gas" in request.form: name = req_str("name") @@ -967,7 +952,7 @@ def site_edit_post(site_id): soq, ) g.sess.commit() - return chellow_redirect(f"/g/supplies/{g_supply.id}", 303) + return redirect(f"/g/supplies/{g_supply.id}", 303) else: raise BadRequest( "The request must contain one of the following parameter names: " @@ -1049,7 +1034,7 @@ def site_add_post(): name = req_str("name") site = Site.insert(g.sess, code, name) g.sess.commit() - return chellow_redirect("/sites/" + str(site.id), 303) + return redirect(f"/sites/{site.id}", 303) except BadRequest as e: flash(e.description) return render_template("site_add.html") @@ -1079,7 +1064,7 @@ def sites_get(): ) if len(sites) == 1: - return chellow_redirect(f"/sites/{sites[0].id}") + return redirect(f"/sites/{sites[0].id}") else: return render_template("sites.html", sites=sites, limit=LIMIT) else: @@ -1147,9 +1132,9 @@ def supplies_get(): ) if len(e_eras) == 1 and len(g_eras) == 0: - return chellow_redirect(f"/e/supplies/{e_eras[0].supply.id}", 307) + return redirect(f"/e/supplies/{e_eras[0].supply.id}", 307) elif len(e_eras) == 0 and len(g_eras) == 1: - return chellow_redirect(f"/g/supplies/{g_eras[0].g_supply.id}", 307) + return redirect(f"/g/supplies/{g_eras[0].g_supply.id}", 307) else: return render_template( "supplies.html", e_eras=e_eras, g_eras=g_eras, max_results=max_results @@ -1595,7 +1580,7 @@ def report_run_post(run_id): run = g.sess.query(ReportRun).filter(ReportRun.id == run_id).one() run.delete(g.sess) g.sess.commit() - return chellow_redirect("/report_runs", 303) + return redirect("/report_runs", 303) @home.route("/report_runs//spreadsheet") @@ -1705,7 +1690,7 @@ def report_run_row_post(row_id): g.sess.commit() flash("Update successful") - return chellow_redirect(f"/report_run_rows/{row_id}", 303) + return redirect(f"/report_run_rows/{row_id}", 303) @home.route("/non_core_contracts") @@ -1905,17 +1890,17 @@ def non_core_contract_edit_post(contract_id): if "delete" in request.values: contract.delete(g.sess) g.sess.commit() - return chellow_redirect("/non_core_contracts", 303) + return redirect("/non_core_contracts", 303) if "update_state" in request.values: state = req_zish("state") contract.update_state(state) g.sess.commit() - return chellow_redirect(f"/non_core_contracts/{contract.id}", 303) + return redirect(f"/non_core_contracts/{contract.id}", 303) else: properties = req_zish("properties") contract.update_properties(properties) g.sess.commit() - return chellow_redirect(f"/non_core_contracts/{contract.id}", 303) + return redirect(f"/non_core_contracts/{contract.id}", 303) except BadRequest as e: flash(e.description) return make_response( @@ -2014,7 +1999,7 @@ def national_grid_get(): def national_grid_post(): importer = chellow.national_grid.importer importer.go() - return chellow_redirect("/national_grid", 303) + return redirect("/national_grid", 303) @home.route("/non_core_contracts//add_rate_script") @@ -2037,7 +2022,7 @@ def non_core_rate_script_add_post(contract_id): start_date = req_date("start") rate_script = contract.insert_rate_script(g.sess, start_date, {}) g.sess.commit() - return chellow_redirect("/non_core_rate_scripts/" + str(rate_script.id), 303) + return redirect(f"/non_core_rate_scripts/{rate_script.id}", 303) except BadRequest as e: flash(e.description) now = utc_datetime_now() @@ -2109,7 +2094,7 @@ def non_core_rate_script_edit_post(rs_id): if "delete" in request.values: contract.delete_rate_script(g.sess, rate_script) g.sess.commit() - return chellow_redirect(f"/non_core_contracts/{contract.id}", 303) + return redirect(f"/non_core_contracts/{contract.id}", 303) else: script = req_zish("script") start_date = req_hh_date("start") @@ -2121,7 +2106,7 @@ def non_core_rate_script_edit_post(rs_id): g.sess, rate_script, start_date, finish_date, script ) g.sess.commit() - return chellow_redirect(f"/non_core_rate_scripts/{rate_script.id}", 303) + return redirect(f"/non_core_rate_scripts/{rate_script.id}", 303) except BadRequest as e: g.sess.rollback() flash(e.description) @@ -2152,7 +2137,7 @@ def non_core_auto_importer_post(contract_id): except ModuleNotFoundError: importer = import_module(f"chellow.e.{contract.name}").get_importer() importer.go() - return chellow_redirect(f"/non_core_contracts/{contract.id}/auto_importer", 303) + return redirect(f"/non_core_contracts/{contract.id}/auto_importer", 303) except BadRequest as e: g.sess.rollback() flash(e.description) @@ -2262,7 +2247,7 @@ def rate_server_get(): def rate_server_post(): importer = chellow.rate_server.importer importer.go() - return chellow_redirect("/rate_server", 303) + return redirect("/rate_server", 303) @home.route("/tester") @@ -2275,7 +2260,7 @@ def tester_get(): def tester_post(): tester = chellow.testing.tester tester.go() - return chellow_redirect("/tester", 303) + return redirect("/tester", 303) @home.route("/user_roles")