Skip to content

Commit

Permalink
fixed get_parent() in grid.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mdipierro committed Sep 5, 2024
1 parent c2bf124 commit cffcba4
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions py4web/utils/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,6 @@ def col2key(col):

for index, col in enumerate(self.param.columns):
if isinstance(col, Column):

if not col.key:
col.key = f"column-{index}"
self.columns.append(col)
Expand Down Expand Up @@ -1365,35 +1364,37 @@ def get_parent(path, parent_field):
:return parent_id: the id of the parent record
"""
parent_id = request.query.get("parent_id")
if parent_id is not None:
return int(parent_id)

child_table = parent_field._table
fn = parent_field.name

# find the record id of the parent from the child table record
# if not found, search the record id of the parent from the child table record
if path:
parts = path.split("/")
record_id = parts[1] if len(parts) > 1 else None
if record_id:
record = child_table(record_id)
if record:
parent_id = record[fn]
if parent_id is not None:
return int(parent_id)

# not passed in, check in the form
if not parent_id:
parent_id = request.forms.get(fn)

# not found yet, check in the referer
if not parent_id:
referrer = request.query.get("_referrer")
if referrer:
kvp = base64.b16decode(referrer.encode("utf8")).decode("utf8")
if "parent_id" in kvp:
parent_id = kvp.split("parent_id=")[1]
# else, check in the form
parent_id = request.forms.get(fn)
if parent_id is not None:
return int(parent_id)

if parent_id and "&" in parent_id:
parent_id = parent_id.split("&")[0]
# else, check in the referer
referrer = request.query.get("_referrer")
if referrer:
kvp = base64.b16decode(referrer.encode("utf8")).decode("utf8")
if "parent_id" in kvp:
parent_id = kvp.split("parent_id=")[1].split("&")[0]
return int(parent_id)

return parent_id
return None


class AttributesPlugin:
Expand Down

0 comments on commit cffcba4

Please sign in to comment.