-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #504 from chinapandaman/PPF-502
PPF-502: supports signature field
- Loading branch information
Showing
13 changed files
with
175 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Contains signature middleware.""" | ||
|
||
from typing import BinaryIO, Union | ||
from os.path import expanduser | ||
|
||
from .adapter import fp_or_f_obj_or_stream_to_stream | ||
from .widget import Widget | ||
|
||
|
||
class Signature(Widget): | ||
"""A class to represent a signature field widget.""" | ||
|
||
def __init__( | ||
self, | ||
name: str, | ||
value: Union[bytes, str, BinaryIO] = None, | ||
) -> None: | ||
"""Constructs all attributes for the signature field.""" | ||
|
||
super().__init__(name, value) | ||
|
||
@property | ||
def schema_definition(self) -> dict: | ||
"""Json schema definition of the signature field.""" | ||
|
||
return {"type": "string"} | ||
|
||
@property | ||
def sample_value(self) -> str: | ||
"""Sample value of the signature field.""" | ||
|
||
return expanduser("~/Downloads/sample_image.jpg") | ||
|
||
@property | ||
def stream(self) -> Union[bytes, None]: | ||
"""Converts the value of the signature field image to a stream.""" | ||
|
||
return ( | ||
fp_or_f_obj_or_stream_to_stream(self.value) | ||
if self.value is not None | ||
else None | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
|
||
from PyPDFForm import PdfWrapper | ||
|
||
|
||
def test_fill_signature(pdf_samples, image_samples, request): | ||
expected_path = os.path.join( | ||
pdf_samples, "signature", "test_fill_signature.pdf" | ||
) | ||
with open(expected_path, "rb+") as f: | ||
obj = PdfWrapper( | ||
os.path.join(pdf_samples, "signature", "sample_template_with_signature.pdf") | ||
).fill( | ||
{"signature": os.path.join(image_samples, "sample_signature.png")} | ||
) | ||
|
||
request.config.results["expected_path"] = expected_path | ||
request.config.results["stream"] = obj.read() | ||
|
||
expected = f.read() | ||
|
||
if os.name != "nt": | ||
assert len(obj.read()) == len(expected) | ||
assert obj.read() == expected | ||
|
||
|
||
def test_signature_schema(pdf_samples): | ||
obj = PdfWrapper(os.path.join(pdf_samples, "signature", "sample_template_with_signature.pdf")) | ||
|
||
assert obj.widgets["signature"].schema_definition == {"type": "string"} | ||
|
||
|
||
def test_signature_sample_value(pdf_samples): | ||
obj = PdfWrapper(os.path.join(pdf_samples, "signature", "sample_template_with_signature.pdf")) | ||
|
||
assert obj.widgets["signature"].sample_value == os.path.expanduser( | ||
"~/Downloads/sample_image.jpg") | ||
|
||
|
||
def test_fill_signature_overlap(pdf_samples, image_samples, request): | ||
expected_path = os.path.join( | ||
pdf_samples, "signature", "test_fill_signature_overlap.pdf" | ||
) | ||
with open(expected_path, "rb+") as f: | ||
obj = PdfWrapper( | ||
os.path.join(pdf_samples, "signature", "sample_template_with_signature_overlap.pdf") | ||
).fill( | ||
{"signature": os.path.join(image_samples, "sample_signature.png")} | ||
) | ||
|
||
request.config.results["expected_path"] = expected_path | ||
request.config.results["stream"] = obj.read() | ||
|
||
expected = f.read() | ||
|
||
if os.name != "nt": | ||
assert len(obj.read()) == len(expected) | ||
assert obj.read() == expected |