Skip to content

Commit

Permalink
fix issue with object_checkboxes and object_multiselect being used as…
Browse files Browse the repository at this point in the history
… elements in a list or dict; upgrade pikepdf
  • Loading branch information
jhpyle committed Oct 1, 2024
1 parent 2f2c0a7 commit 3a8a42d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Change Log

## [1.5.12] - 2024-09-30

### Changed
- Upgraded pikepdf.

### Fixed
- Issue with `object_checkboxes` and `object_multiselect` when used as
elements in a `DAList` or `DADict`.

## [1.5.11] - 2024-09-26

### Changed
Expand Down
4 changes: 2 additions & 2 deletions docassemble_base/docassemble/base/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -10320,9 +10320,9 @@ def ensure_object_exists(saveas, datatype, the_user_dict, commands=None):
elif method == 'index':
index_name = parse_result['final_parts'][1][1:-1]
if datatype in ('multiselect', 'checkboxes'):
commands.append(parse_result['final_parts'][0] + ".initializeObject(" + repr(index_name) + ", DADict, auto_gather=False)")
commands.append(parse_result['final_parts'][0] + ".initializeObject(" + index_name + ", DADict, auto_gather=False)")
elif datatype in ('object_multiselect', 'object_checkboxes'):
commands.append(parse_result['final_parts'][0] + ".initializeObject(" + repr(index_name) + ", DAList, auto_gather=False)")
commands.append(parse_result['final_parts'][0] + ".initializeObject(" + index_name + ", DAList, auto_gather=False)")
else:
if datatype in ('multiselect', 'checkboxes'):
commands.append(saveas + ' = DADict(' + repr(saveas) + ', auto_gather=False)')
Expand Down
33 changes: 20 additions & 13 deletions docassemble_base/docassemble/base/pdftk.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,49 +278,56 @@ def fill_template(template, data_strings=None, data_names=None, hidden=None, rea
if not hasattr(page, 'Annots'):
continue
for the_annot in page.Annots:
annot = the_annot
annot_kid = None
while not (hasattr(annot, "FT") and hasattr(annot, "T")) and hasattr(annot, 'Parent'):
annot_kid = annot
annot = annot.Parent
if not (hasattr(annot, "T") and hasattr(annot, "FT")):
continue
for field, value in data_dict.items():
annot = the_annot
annot_kid = None
while not (hasattr(annot, "FT") and hasattr(annot, "T")) and hasattr(annot, 'Parent'):
annot_kid = annot
annot = annot.Parent
if not (hasattr(annot, "T") and hasattr(annot, "FT")):
continue
if field != str(annot.T):
continue
field_type = str(annot.FT)
if field_type == "/Tx":
the_string = pikepdf.String(value)
annot.V = the_string
annot.DV = the_string
elif field_type == "/Btn":
if hasattr(annot, "A"):
continue
the_name = pikepdf.Name('/' + value)
annot.V = the_name
annot.DV = the_name
# Could be radio button: if it is, set the appearance stream of the
# correct child annot
if (annot_kid is not None and hasattr(annot_kid, "AP")
and hasattr(annot_kid.AP, "N")):
annot.AS = the_name
if the_name in annot_kid.AP.N.keys():
annot_kid.AS = the_name
annot.V = the_name
else:
for off in ["/Off", "/off"]:
if off in annot_kid.AP.N.keys():
annot_kid.AS = off
annot_kid.AS = pikepdf.Name(off)
break
elif (hasattr(annot, "AP") and hasattr(annot.AP, "N")):
if the_name in annot.AP.N.keys():
annot.AS = the_name
annot.V = the_name
elif hasattr(annot.AP, "D"):
for off in ["/Off", "/off"]:
if off in annot.AP.D:
annot.AS = pikepdf.Name(off)
annot.V = pikepdf.Name(off)
break
else:
annot.AS = pikepdf.Name("/Off")
annot.V = pikepdf.Name("/Off")
elif field_type == "/Ch":
opt_list = [str(item) for item in annot.Opt]
if value not in opt_list:
opt_list.append(value)
annot.Opt = pikepdf.Array(opt_list)
the_string = pikepdf.String(value)
annot.V = the_string
annot.DV = the_string
pdf.Root.AcroForm.NeedAppearances = True
pdf.generate_appearance_streams()
pdf.Root.AcroForm.NeedAppearances = True
Expand Down
2 changes: 1 addition & 1 deletion docassemble_base/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def find_package_data(where='.', package='', exclude=standard_exclude, exclude_d
"passlib==1.7.4",
"pdfminer.six==20240706",
"phonenumbers==8.13.43",
"pikepdf==8.10.1",
"pikepdf==9.2.1",
"pillow==10.4.0",
"pkginfo==1.10.0",
"pluggy==1.5.0",
Expand Down
13 changes: 7 additions & 6 deletions docassemble_webapp/docassemble/webapp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7437,12 +7437,13 @@ def index(action_argument=None, refer=None):
try:
eval(objname, user_dict)
except:
objname_tr = sub_indices(objname, user_dict)
safe_objname = safeid(objname)
if safe_objname in known_datatypes:
if known_datatypes[safe_objname] in ('object_multiselect', 'object_checkboxes'):
docassemble.base.parse.ensure_object_exists(objname, 'object_checkboxes', user_dict)
docassemble.base.parse.ensure_object_exists(objname_tr, 'object_checkboxes', user_dict)
elif known_datatypes[safe_objname] in ('multiselect', 'checkboxes'):
docassemble.base.parse.ensure_object_exists(objname, known_datatypes[safe_objname], user_dict)
docassemble.base.parse.ensure_object_exists(objname_tr, known_datatypes[safe_objname], user_dict)
field_error = {}
validated = True
pre_user_dict = user_dict
Expand Down Expand Up @@ -7993,7 +7994,7 @@ def index(action_argument=None, refer=None):
logmessage("Received illegal variable name " + str(key))
continue
if empty_fields[orig_key] in ('object_multiselect', 'object_checkboxes'):
docassemble.base.parse.ensure_object_exists(key, empty_fields[orig_key], user_dict)
docassemble.base.parse.ensure_object_exists(sub_indices(key, user_dict), empty_fields[orig_key], user_dict)
exec(key + '.clear()', user_dict)
exec(key + '.gathered = True', user_dict)
elif empty_fields[orig_key] in ('object', 'object_radio'):
Expand Down Expand Up @@ -25775,7 +25776,7 @@ def do_sms(form, base_url, url_root, config='default', save=True):
user_dict['_internal']['command_cache'] = {}
if field.number not in user_dict['_internal']['command_cache']:
user_dict['_internal']['command_cache'][field.number] = []
docassemble.base.parse.ensure_object_exists(saveas, field.datatype, user_dict, commands=user_dict['_internal']['command_cache'][field.number])
docassemble.base.parse.ensure_object_exists(sub_indices(saveas, user_dict), field.datatype, user_dict, commands=user_dict['_internal']['command_cache'][field.number])
saveas = saveas + '.gathered'
data = 'True'
if (user_entered_skip or (inp_lower == word('none') and hasattr(field, 'datatype') and field.datatype in ('multiselect', 'object_multiselect', 'checkboxes', 'object_checkboxes'))) and ((hasattr(field, 'disableothers') and field.disableothers) or (hasattr(field, 'datatype') and field.datatype in ('multiselect', 'object_multiselect', 'checkboxes', 'object_checkboxes')) or not (interview_status.extras['required'][field.number] or (question.question_type == 'multiple_choice' and hasattr(field, 'saveas')))):
Expand Down Expand Up @@ -25977,7 +25978,7 @@ def do_sms(form, base_url, url_root, config='default', save=True):
user_dict['_internal']['command_cache'][the_field.number] = []
if hasattr(the_field, 'datatype'):
if the_field.datatype in ('object_multiselect', 'object_checkboxes'):
docassemble.base.parse.ensure_object_exists(the_saveas, the_field.datatype, user_dict, commands=user_dict['_internal']['command_cache'][the_field.number])
docassemble.base.parse.ensure_object_exists(sub_indices(the_saveas, user_dict), the_field.datatype, user_dict, commands=user_dict['_internal']['command_cache'][the_field.number])
user_dict['_internal']['command_cache'][the_field.number].append(the_saveas + '.clear()')
user_dict['_internal']['command_cache'][the_field.number].append(the_saveas + '.gathered = True')
elif the_field.datatype in ('object', 'object_radio'):
Expand All @@ -25986,7 +25987,7 @@ def do_sms(form, base_url, url_root, config='default', save=True):
except:
user_dict['_internal']['command_cache'][the_field.number].append(the_saveas + ' = None')
elif the_field.datatype in ('multiselect', 'checkboxes'):
docassemble.base.parse.ensure_object_exists(the_saveas, the_field.datatype, user_dict, commands=user_dict['_internal']['command_cache'][the_field.number])
docassemble.base.parse.ensure_object_exists(sub_indices(the_saveas, user_dict), the_field.datatype, user_dict, commands=user_dict['_internal']['command_cache'][the_field.number])
user_dict['_internal']['command_cache'][the_field.number].append(the_saveas + '.gathered = True')
else:
user_dict['_internal']['command_cache'][the_field.number].append(the_saveas + ' = None')
Expand Down
2 changes: 1 addition & 1 deletion docassemble_webapp/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def read(fname):
"passlib==1.7.4",
"pdfminer.six==20240706",
"phonenumbers==8.13.43",
"pikepdf==8.10.1",
"pikepdf==9.2.1",
"pillow==10.4.0",
"pkginfo==1.10.0",
"pluggy==1.5.0",
Expand Down

0 comments on commit 3a8a42d

Please sign in to comment.