Skip to content

Commit

Permalink
Use relative redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
tlocke committed Dec 5, 2024
1 parent 0d2be01 commit dc0396b
Show file tree
Hide file tree
Showing 41 changed files with 112 additions and 180 deletions.
1 change: 0 additions & 1 deletion chellow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
8 changes: 1 addition & 7 deletions chellow/e/computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

from zish import dumps

import chellow.bank_holidays
import chellow.utils
from chellow.models import (
Bill,
BillType,
Expand Down Expand Up @@ -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
Expand Down
14 changes: 4 additions & 10 deletions chellow/e/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
flash,
g,
make_response,
redirect,
render_template as rtemplate,
request,
)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions chellow/gas/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
[],
)
Expand Down
5 changes: 3 additions & 2 deletions chellow/gas/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
flash,
g,
make_response,
redirect,
render_template as rtemplate,
request,
)
Expand Down Expand Up @@ -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):
Expand Down
5 changes: 2 additions & 3 deletions chellow/reports/report_109.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)
5 changes: 2 additions & 3 deletions chellow/reports/report_111.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 2 additions & 3 deletions chellow/reports/report_169.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +23,6 @@
to_ct,
to_utc,
)
from chellow.views import chellow_redirect


def _write_row(writer, titles, vals, total):
Expand Down Expand Up @@ -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)
5 changes: 2 additions & 3 deletions chellow/reports/report_181.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)
7 changes: 3 additions & 4 deletions chellow/reports/report_183.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,7 +22,6 @@
to_ct,
to_utc,
)
from chellow.views import chellow_redirect


def _write_row(writer, titles, vals, total):
Expand Down Expand Up @@ -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")
Expand All @@ -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)
5 changes: 2 additions & 3 deletions chellow/reports/report_187.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -20,7 +20,6 @@
req_str,
to_ct,
)
from chellow.views import chellow_redirect


def csv_str(row):
Expand Down Expand Up @@ -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)
5 changes: 2 additions & 3 deletions chellow/reports/report_219.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)
5 changes: 2 additions & 3 deletions chellow/reports/report_231.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)
5 changes: 2 additions & 3 deletions chellow/reports/report_233.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)
5 changes: 2 additions & 3 deletions chellow/reports/report_241.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from dateutil.relativedelta import relativedelta

from flask import g
from flask import g, redirect

import pytz

Expand All @@ -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(
Expand Down Expand Up @@ -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)
5 changes: 2 additions & 3 deletions chellow/reports/report_247.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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:
Expand Down
Loading

0 comments on commit dc0396b

Please sign in to comment.