|
| 1 | +import base64 |
| 2 | +from os import path |
| 3 | + |
| 4 | +from docusign_esign import EnvelopesApi, RecipientViewRequest, Document, Signer, EnvelopeDefinition, SignHere, Tabs, \ |
| 5 | + Recipients |
| 6 | +from flask import session, url_for, request |
| 7 | + |
| 8 | +from ...consts import authentication_method, demo_docs_path, pattern, signer_client_id |
| 9 | +from ...docusign import create_api_client |
| 10 | +from ...ds_config import DS_CONFIG |
| 11 | + |
| 12 | + |
| 13 | +class Eg044FocusedViewController: |
| 14 | + @staticmethod |
| 15 | + def get_args(): |
| 16 | + """Get request and session arguments""" |
| 17 | + # More data validation would be a good idea here |
| 18 | + # Strip anything other than characters listed |
| 19 | + # 1. Parse request arguments |
| 20 | + signer_email = pattern.sub("", request.form.get("signer_email")) |
| 21 | + signer_name = pattern.sub("", request.form.get("signer_name")) |
| 22 | + envelope_args = { |
| 23 | + "signer_email": signer_email, |
| 24 | + "signer_name": signer_name, |
| 25 | + "signer_client_id": signer_client_id, |
| 26 | + "ds_return_url": url_for("ds.ds_return", _external=True), |
| 27 | + } |
| 28 | + args = { |
| 29 | + "account_id": session["ds_account_id"], |
| 30 | + "base_path": session["ds_base_path"], |
| 31 | + "access_token": session["ds_access_token"], |
| 32 | + "envelope_args": envelope_args |
| 33 | + } |
| 34 | + return args |
| 35 | + |
| 36 | + @classmethod |
| 37 | + def worker(cls, args): |
| 38 | + """ |
| 39 | + 1. Create the envelope request object |
| 40 | + 2. Send the envelope |
| 41 | + 3. Create the Recipient View request object |
| 42 | + 4. Obtain the recipient_view_url for the embedded signing |
| 43 | + """ |
| 44 | + #ds-snippet-start:eSign44Step3 |
| 45 | + envelope_args = args["envelope_args"] |
| 46 | + # 1. Create the envelope request object |
| 47 | + envelope_definition = cls.make_envelope(envelope_args) |
| 48 | + |
| 49 | + # 2. call Envelopes::create API method |
| 50 | + # Exceptions will be caught by the calling function |
| 51 | + api_client = create_api_client(base_path=args["base_path"], access_token=args["access_token"]) |
| 52 | + |
| 53 | + envelope_api = EnvelopesApi(api_client) |
| 54 | + results = envelope_api.create_envelope(account_id=args["account_id"], envelope_definition=envelope_definition) |
| 55 | + |
| 56 | + envelope_id = results.envelope_id |
| 57 | + #ds-snippet-end:eSign44Step3 |
| 58 | + |
| 59 | + # 3. Create the Recipient View request object |
| 60 | + #ds-snippet-start:eSign44Step4 |
| 61 | + recipient_view_request = RecipientViewRequest( |
| 62 | + authentication_method=authentication_method, |
| 63 | + client_user_id=envelope_args["signer_client_id"], |
| 64 | + recipient_id="1", |
| 65 | + return_url=envelope_args["ds_return_url"], |
| 66 | + user_name=envelope_args["signer_name"], |
| 67 | + email=envelope_args["signer_email"], |
| 68 | + frame_ancestors=["http://localhost:3000", "https://apps-d.docusign.com"], |
| 69 | + message_origins=["https://apps-d.docusign.com"] |
| 70 | + ) |
| 71 | + #ds-snippet-end:eSign44Step4 |
| 72 | + |
| 73 | + # 4. Obtain the recipient_view_url for the embedded signing |
| 74 | + # Exceptions will be caught by the calling function |
| 75 | + |
| 76 | + #ds-snippet-start:eSign44Step5 |
| 77 | + results = envelope_api.create_recipient_view( |
| 78 | + account_id=args["account_id"], |
| 79 | + envelope_id=envelope_id, |
| 80 | + recipient_view_request=recipient_view_request |
| 81 | + ) |
| 82 | + |
| 83 | + return {"envelope_id": envelope_id, "redirect_url": results.url} |
| 84 | + #ds-snippet-end:eSign44Step5 |
| 85 | + |
| 86 | + @classmethod |
| 87 | + #ds-snippet-start:eSign44Step2 |
| 88 | + def make_envelope(cls, args): |
| 89 | + """ |
| 90 | + Creates envelope |
| 91 | + args -- parameters for the envelope: |
| 92 | + signer_email, signer_name, signer_client_id |
| 93 | + returns an envelope definition |
| 94 | + """ |
| 95 | + |
| 96 | + # document 1 (pdf) has tag /sn1/ |
| 97 | + # |
| 98 | + # The envelope has one recipient. |
| 99 | + # recipient 1 - signer |
| 100 | + with open(path.join(demo_docs_path, DS_CONFIG["doc_pdf"]), "rb") as file: |
| 101 | + content_bytes = file.read() |
| 102 | + base64_file_content = base64.b64encode(content_bytes).decode("ascii") |
| 103 | + |
| 104 | + # Create the document model |
| 105 | + document = Document( # create the DocuSign document object |
| 106 | + document_base64=base64_file_content, |
| 107 | + name="Example document", # can be different from actual file name |
| 108 | + file_extension="pdf", # many different document types are accepted |
| 109 | + document_id=1 # a label used to reference the doc |
| 110 | + ) |
| 111 | + |
| 112 | + # Create the signer recipient model |
| 113 | + signer = Signer( |
| 114 | + # The signer |
| 115 | + email=args["signer_email"], |
| 116 | + name=args["signer_name"], |
| 117 | + recipient_id="1", |
| 118 | + routing_order="1", |
| 119 | + # Setting the client_user_id marks the signer as embedded |
| 120 | + client_user_id=args["signer_client_id"] |
| 121 | + ) |
| 122 | + |
| 123 | + # Create a sign_here tab (field on the document) |
| 124 | + sign_here = SignHere( |
| 125 | + # DocuSign SignHere field/tab |
| 126 | + anchor_string="/sn1/", |
| 127 | + anchor_units="pixels", |
| 128 | + anchor_y_offset="10", |
| 129 | + anchor_x_offset="20" |
| 130 | + ) |
| 131 | + |
| 132 | + # Add the tabs model (including the sign_here tab) to the signer |
| 133 | + # The Tabs object wants arrays of the different field/tab types |
| 134 | + signer.tabs = Tabs(sign_here_tabs=[sign_here]) |
| 135 | + |
| 136 | + # Next, create the top level envelope definition and populate it. |
| 137 | + envelope_definition = EnvelopeDefinition( |
| 138 | + email_subject="Please sign this document sent from the Python SDK", |
| 139 | + documents=[document], |
| 140 | + # The Recipients object wants arrays for each recipient type |
| 141 | + recipients=Recipients(signers=[signer]), |
| 142 | + status="sent" # requests that the envelope be created and sent. |
| 143 | + ) |
| 144 | + |
| 145 | + return envelope_definition |
| 146 | + #ds-snippet-end:eSign44Step2 |
0 commit comments