Skip to content

Commit

Permalink
PPF-773: also resets keys to update list
Browse files Browse the repository at this point in the history
  • Loading branch information
chinapandaman committed Dec 17, 2024
1 parent 97a5331 commit a0f6d57
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion PyPDFForm/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,19 @@ def __init__(
def _init_helper(self, key_to_refresh: str = None) -> None:
"""Updates all attributes when the state of the PDF stream changes."""

refresh_not_needed = {}
new_widgets = build_widgets(self.read()) if self.read() else {}
for k, v in self.widgets.items():
if k in new_widgets:
new_widgets[k] = v
refresh_not_needed[k] = True
self.widgets = new_widgets

for key, value in self.widgets.items():
if (key_to_refresh and key == key_to_refresh) or (
key_to_refresh is None and isinstance(value, Text)
key_to_refresh is None
and isinstance(value, Text)
and not refresh_not_needed.get(key)
):
value.font = self.global_font
value.font_size = self.global_font_size
Expand Down Expand Up @@ -272,6 +276,7 @@ def commit_widget_key_updates(self) -> PdfWrapper:
self.read(), self.widgets, old_keys, new_keys, indices
)
self._init_helper()
self._keys_to_update = []

return self

Expand Down
24 changes: 24 additions & 0 deletions tests/scenario/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,30 @@ def test_update_key(issue_pdf_directory, request):
assert obj.preview == expected


def test_update_key_persist_properties(issue_pdf_directory, request):
obj = PdfWrapper(os.path.join(issue_pdf_directory, "733.pdf"))
obj.widgets["SchwabAccountNumber[0]"].font_size = 20

for i in range(1, 10):
obj.update_widget_key("Description[0]", f"Description[{i}]", 1)
obj.update_widget_key("symbol[0]", f"symbol[{i}]", 1)
obj.update_widget_key("tradedate[0]", f"tradedate[{i}]", 1)
obj.update_widget_key("settlementdate[0]", f"settlementdate[{i}]", 1)
obj.update_widget_key("quantity[0]", f"quantity[{i}]", 1)
obj.update_widget_key("costperunit[0]", f"costperunit[{i}]", 1)
obj.update_widget_key("costabasis[0]", f"costabasis[{i}]", 1)

assert obj.widgets["SchwabAccountNumber[0]"].font_size == 20

expected_path = os.path.join(issue_pdf_directory, "733_expected.pdf")
request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.preview
with open(expected_path, "rb+") as f:
expected = f.read()
assert len(obj.preview) == len(expected)
assert obj.preview == expected


def test_bulk_update_key(issue_pdf_directory, request):
obj = PdfWrapper(os.path.join(issue_pdf_directory, "733.pdf"))

Expand Down

0 comments on commit a0f6d57

Please sign in to comment.