Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add use_appname parameter to the Form, Grid, and Auth classes #911

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions py4web/utils/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ def abort_or_redirect(self, page, message=""):

if "text/html" not in request.headers.get("accept", ""):
raise HTTP(403, body={"message": message})
redirect_next = request.fullpath
if self.auth.param.use_appname_in_redirects == False:
redirect_next = request.fullpath.replace(f"/{request.app_name}", "")
else:
redirect_next = request.fullpath
if request.query_string:
redirect_next = redirect_next + "?{}".format(request.query_string)
self.auth.flash.set(message)
Expand All @@ -91,7 +94,10 @@ def goto_login(self, message=""):
request.headers.get("json-redirects", "") != "on"
):
raise HTTP(403)
redirect_next = request.fullpath
if self.auth.param.use_appname_in_redirects == False:
redirect_next = request.fullpath.replace(f"/{request.app_name}", "")
else:
redirect_next = request.fullpath
if request.query_string:
redirect_next = redirect_next + "?{}".format(request.query_string)
redirect(
Expand Down Expand Up @@ -1482,7 +1488,10 @@ def register(self, model=False):
self.auth.get_or_delete_existing_unverified_account(email)
extra_form_fields = self.auth.extra_form_fields.get("register", [])
fields += extra_form_fields
form = Form(fields, submit_value=button_name, formstyle=self.formstyle)
form = Form(fields,
submit_value=button_name,
formstyle=self.formstyle,
use_appname=self.auth.param.use_appname_in_redirects)
user = None
if form.accepted:
# notice that here the form is alrealdy validated
Expand Down Expand Up @@ -1620,6 +1629,7 @@ def login(self, model=False):
fields,
submit_value=button_name,
formstyle=self.formstyle,
use_appname=self.auth.param.use_appname_in_redirects
)
user = None
next_url = prevent_open_redirect(request.query.get("next"))
Expand Down Expand Up @@ -1682,7 +1692,7 @@ def two_factor(self):
next_url = self.auth.session.get("auth.2fa_next_url")

if not user_id:
redirect(URL("index"))
redirect(URL("index", use_appname=self.auth.param.use_appname_in_redirects))

user = self.auth.db.auth_user(user_id)
code = self.auth.session.get("auth.2fa_code")
Expand Down Expand Up @@ -1710,6 +1720,7 @@ def two_factor(self):
form_name="auth_2fa",
keep_values=True,
hidden=dict(next_url=next_url),
use_appname=self.auth.param.use_appname_in_redirects,
)

if form.accepted:
Expand Down Expand Up @@ -1783,6 +1794,7 @@ def request_reset_password(self, model=False):
fields,
submit_value=button_name,
formstyle=self.formstyle,
use_appname=self.auth.param.use_appname_in_redirects
)
if form.accepted:
email = form.vars.get("email", "")
Expand Down Expand Up @@ -1851,6 +1863,7 @@ def reset_password(self, model=False):
fields,
formstyle=self.formstyle,
submit_value=button_name,
use_appname=self.auth.param.use_appname_in_redirects
)
self._process_change_password_form(form, user, False)
if form.accepted:
Expand Down Expand Up @@ -1896,6 +1909,7 @@ def change_password(self, model=False):
fields,
formstyle=self.formstyle,
submit_value=button_name,
use_appname=self.auth.param.use_appname_in_redirects
)
user = self.auth.db.auth_user(self.auth.user_id)
self._process_change_password_form(form, user, True)
Expand Down Expand Up @@ -1959,6 +1973,7 @@ def profile(self, model=False):
formstyle=self.formstyle,
deletable=deletable,
submit_value=button_name,
use_appname=self.auth.param.use_appname_in_redirects
)
if form.accepted:
self._set_flash("profile-saved")
Expand Down Expand Up @@ -1996,4 +2011,4 @@ def _postprocessing(self, action, form=None, user=None):
if action in self.auth.on_accept:
self.auth.on_accept[action](form, user)
if not form or form.accepted:
redirect(self.auth.session.get(f"_next_{action}") or URL("index"))
redirect(self.auth.session.get(f"_next_{action}") or URL("index", use_appname=self.auth.param.use_appname_in_redirects))
6 changes: 6 additions & 0 deletions py4web/utils/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,16 @@ def __call__(
deletable,
noncreate,
show_id,
use_appname,
kwargs=None,
):
kwargs = kwargs if kwargs else {}

kwargs["_accept-charset"] = "utf8"
form_method = "POST"
form_action = request.url.split(":", 1)[1]
if use_appname == False:
form_action = form_action.replace(f"/{request.app_name}", "")
form_enctype = "multipart/form-data"

if "_method" in kwargs:
Expand Down Expand Up @@ -696,6 +699,7 @@ def __init__(
signing_info=None,
submit_value="Submit",
show_id=False,
use_appname=None,
**kwargs,
):
self.param = Param(
Expand Down Expand Up @@ -732,6 +736,7 @@ def __init__(
self.lifespan = lifespan
self.csrf_protection = csrf_protection
self.show_id = show_id
self.use_appname = use_appname
# initialized and can change
self.vars = {}
self.errors = {}
Expand Down Expand Up @@ -927,6 +932,7 @@ def helper(self):
self.deletable,
self.noncreate,
show_id=self.show_id,
use_appname=self.use_appname,
kwargs=self.kwargs,
)
for item in self.param.sidecar:
Expand Down
14 changes: 11 additions & 3 deletions py4web/utils/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ def __init__(
grid_class_style=GridClassStyle,
T=lambda text: text,
groupby=None,
use_appname=None,
# deprecated
fields=None,
form_maker=Form,
Expand Down Expand Up @@ -351,6 +352,8 @@ def __init__(
fullpath = request.fullpath.rstrip("/")
if path == "index":
fullpath = fullpath[:-6]
if use_appname == False:
fullpath = fullpath.replace(f"/{request.app_name}", "")
redirect(
f"{fullpath}/select"
+ (f"?{request.query_string}" if request.query_string else "")
Expand Down Expand Up @@ -403,12 +406,16 @@ def __init__(
header_elements=None,
footer_elements=None,
required_fields=required_fields or [],
use_appname=use_appname
)

# instance variables that will be computed
self.action = None
self.current_page_number = None
self.endpoint = request.fullpath[: -len(self.path)].rstrip("/")
if use_appname == False:
self.endpoint = self.endpoint.replace(f"/{request.app_name}", "")
print(f"endpoint = {self.endpoint}")
self.hidden_fields = None
self.form = None
self.number_of_pages = None
Expand Down Expand Up @@ -606,6 +613,7 @@ def compute(row, name=str(col)):
formstyle=self.param.formstyle,
validation=self.param.validation,
show_id=self.param.show_id,
use_appname=self.param.use_appname,
**attrs,
)
if self.action == "new" and self.param.create:
Expand Down Expand Up @@ -940,12 +948,12 @@ def _make_col_header(self, col, index, sort_order):
if orderby:
if orderby == sort_order:
sort_query_parms["orderby"] = "~" + orderby
url = URL(self.endpoint, "select", vars=sort_query_parms)
url = URL(self.endpoint, "select", vars=sort_query_parms, use_appname=self.param.use_appname)
attrs = self.attributes_plugin.link(url=url)
col_header = A(heading, up, **attrs)
else:
sort_query_parms["orderby"] = orderby
url = URL(self.endpoint, "select", vars=sort_query_parms)
url = URL(self.endpoint, "select", vars=sort_query_parms, use_appname=self.param.use_appname)
attrs = self.attributes_plugin.link(url=url)
col_header = A(
heading, dw if "~" + orderby == sort_order else "", **attrs
Expand Down Expand Up @@ -1130,7 +1138,7 @@ def _make_table_pager(self):
else "grid-pagination-button"
)
attrs = self.attributes_plugin.link(
url=URL(self.endpoint, "select", vars=pager_query_parms)
url=URL(self.endpoint, "select", vars=pager_query_parms, use_appname=self.param.use_appname)
)
pager.append(
A(
Expand Down