Development
This commit is contained in:
parent
916d238602
commit
6c3abca58c
4 changed files with 208 additions and 303 deletions
|
|
@ -248,6 +248,25 @@ def passes(req, level):
|
|||
|
||||
# Snapshot helpers ====================================================
|
||||
|
||||
def _flatten_json(val, prefix):
|
||||
"""Recursively flatten a parsed JSON value into [(path, leaf_str)] pairs."""
|
||||
if isinstance(val, dict):
|
||||
out = []
|
||||
for k, v in val.items():
|
||||
out.extend(_flatten_json(v, f'{prefix}.{k}'))
|
||||
return out
|
||||
if isinstance(val, list):
|
||||
out = []
|
||||
for i, v in enumerate(val):
|
||||
out.extend(_flatten_json(v, f'{prefix}[{i}]'))
|
||||
return out
|
||||
if val is None:
|
||||
return [(prefix, None)]
|
||||
if isinstance(val, bool):
|
||||
return [(prefix, 'true' if val else 'false')]
|
||||
return [(prefix, str(val))]
|
||||
|
||||
|
||||
def build_snap_val(changes):
|
||||
"""Return a brief summary of changed field names for the history table cell."""
|
||||
if not changes:
|
||||
|
|
@ -264,11 +283,42 @@ def snap_expand_row(changes, colspan):
|
|||
return ''
|
||||
rows = ''
|
||||
for c in changes:
|
||||
bval = c['before'] if c['before'] is not None else ''
|
||||
aval = c['after'] if c['after'] is not None else ''
|
||||
field = c['field']
|
||||
before_text = c['before']
|
||||
after_text = c['after']
|
||||
vtype = c.get('value_type', 'str')
|
||||
|
||||
if vtype == 'json':
|
||||
try:
|
||||
bval = json.loads(before_text) if before_text is not None else None
|
||||
aval = json.loads(after_text) if after_text is not None else None
|
||||
if isinstance(bval, (dict, list)) or isinstance(aval, (dict, list)):
|
||||
bflat = dict(_flatten_json(bval, field)) if isinstance(bval, (dict, list)) else {}
|
||||
aflat = dict(_flatten_json(aval, field)) if isinstance(aval, (dict, list)) else {}
|
||||
if bflat or aflat:
|
||||
seen = set()
|
||||
for k in list(aflat) + list(bflat):
|
||||
if k in seen:
|
||||
continue
|
||||
seen.add(k)
|
||||
bv = bflat.get(k)
|
||||
av = aflat.get(k)
|
||||
rows += (
|
||||
'<tr>'
|
||||
f'<td class="snap-expand-field">{e(k)}</td>'
|
||||
f'<td class="snap-expand-val">{e(bv) if bv is not None else "<em>(none)</em>"}</td>'
|
||||
f'<td class="snap-expand-val">{e(av) if av is not None else "<em>(none)</em>"}</td>'
|
||||
'</tr>'
|
||||
)
|
||||
continue
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
bval = before_text if before_text is not None else ''
|
||||
aval = after_text if after_text is not None else ''
|
||||
rows += (
|
||||
'<tr>'
|
||||
f'<td class="snap-expand-field">{e(c["field"])}</td>'
|
||||
f'<td class="snap-expand-field">{e(field)}</td>'
|
||||
f'<td class="snap-expand-val">{e(bval) if bval else "<em>(none)</em>"}</td>'
|
||||
f'<td class="snap-expand-val">{e(aval) if aval else "<em>(none)</em>"}</td>'
|
||||
'</tr>'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue