|
| 1 | +from os import path |
| 2 | + |
| 3 | +from docusign_esign import ( |
| 4 | + EnvelopesApi, |
| 5 | + RecipientViewRequest, |
| 6 | + Document, |
| 7 | + Signer, |
| 8 | + CarbonCopy, |
| 9 | + EnvelopeDefinition, |
| 10 | + Recipients, |
| 11 | + DocumentHtmlDefinition |
| 12 | +) |
| 13 | +from flask import session, url_for, request |
| 14 | + |
| 15 | +from ...consts import authentication_method, demo_docs_path, pattern, signer_client_id |
| 16 | +from ...docusign import create_api_client |
| 17 | + |
| 18 | + |
| 19 | +class Eg038ResponsiveSigning: |
| 20 | + @staticmethod |
| 21 | + def get_args(): |
| 22 | + """Get request and session arguments""" |
| 23 | + # More data validation would be a good idea here |
| 24 | + # Strip anything other than characters listed |
| 25 | + # 1. Parse request arguments |
| 26 | + signer_email = pattern.sub("", request.form.get("signer_email")) |
| 27 | + signer_name = pattern.sub("", request.form.get("signer_name")) |
| 28 | + cc_email = pattern.sub("", request.form.get("cc_email")) |
| 29 | + cc_name = pattern.sub("", request.form.get("cc_name")) |
| 30 | + envelope_args = { |
| 31 | + "signer_email": signer_email, |
| 32 | + "signer_name": signer_name, |
| 33 | + "cc_email": cc_email, |
| 34 | + "cc_name": cc_name, |
| 35 | + "signer_client_id": signer_client_id, |
| 36 | + "ds_return_url": url_for("ds.ds_return", _external=True), |
| 37 | + "doc_file": path.join(demo_docs_path, "order_form.html") |
| 38 | + } |
| 39 | + args = { |
| 40 | + "account_id": session["ds_account_id"], |
| 41 | + "base_path": session["ds_base_path"], |
| 42 | + "access_token": session["ds_access_token"], |
| 43 | + "envelope_args": envelope_args |
| 44 | + } |
| 45 | + return args |
| 46 | + |
| 47 | + @classmethod |
| 48 | + def worker(cls, args): |
| 49 | + """ |
| 50 | + 1. Create the envelope request object |
| 51 | + 2. Send the envelope |
| 52 | + 3. Create the Recipient View request object |
| 53 | + 4. Obtain the recipient_view_url for the embedded signing |
| 54 | + """ |
| 55 | + envelope_args = args["envelope_args"] |
| 56 | + # 1. Create the envelope request object |
| 57 | + envelope_definition = cls.make_envelope(envelope_args) |
| 58 | + |
| 59 | + # 2. call Envelopes::create API method |
| 60 | + # Exceptions will be caught by the calling function |
| 61 | + api_client = create_api_client(base_path=args["base_path"], access_token=args["access_token"]) |
| 62 | + |
| 63 | + envelope_api = EnvelopesApi(api_client) |
| 64 | + results = envelope_api.create_envelope(account_id=args["account_id"], envelope_definition=envelope_definition) |
| 65 | + |
| 66 | + envelope_id = results.envelope_id |
| 67 | + |
| 68 | + # 3. Create the Recipient View request object |
| 69 | + recipient_view_request = RecipientViewRequest( |
| 70 | + authentication_method=authentication_method, |
| 71 | + client_user_id=envelope_args["signer_client_id"], |
| 72 | + recipient_id="1", |
| 73 | + return_url=envelope_args["ds_return_url"], |
| 74 | + user_name=envelope_args["signer_name"], |
| 75 | + email=envelope_args["signer_email"] |
| 76 | + ) |
| 77 | + # 4. Obtain the recipient_view_url for the embedded signing |
| 78 | + # Exceptions will be caught by the calling function |
| 79 | + results = envelope_api.create_recipient_view( |
| 80 | + account_id=args["account_id"], |
| 81 | + envelope_id=envelope_id, |
| 82 | + recipient_view_request=recipient_view_request |
| 83 | + ) |
| 84 | + |
| 85 | + return {"envelope_id": envelope_id, "redirect_url": results.url} |
| 86 | + |
| 87 | + @classmethod |
| 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 | + html_definition = DocumentHtmlDefinition( |
| 97 | + source=cls.get_html_content(args) |
| 98 | + ) |
| 99 | + |
| 100 | + # Create the document model |
| 101 | + document = Document( # create the DocuSign document object |
| 102 | + html_definition=html_definition, |
| 103 | + name="doc1.html", # can be different from actual file name |
| 104 | + document_id=1 # a label used to reference the doc |
| 105 | + ) |
| 106 | + |
| 107 | + # Create the signer recipient model |
| 108 | + signer = Signer( |
| 109 | + # The signer |
| 110 | + email=args["signer_email"], |
| 111 | + name=args["signer_name"], |
| 112 | + recipient_id="1", |
| 113 | + routing_order="1", |
| 114 | + # Setting the client_user_id marks the signer as embedded |
| 115 | + client_user_id=args["signer_client_id"], |
| 116 | + role_name="Signer" |
| 117 | + ) |
| 118 | + |
| 119 | + cc = CarbonCopy( |
| 120 | + email=args["cc_email"], |
| 121 | + name=args["cc_name"], |
| 122 | + recipient_id="2", |
| 123 | + routing_order="2" |
| 124 | + ) |
| 125 | + |
| 126 | + # Next, create the top level envelope definition and populate it. |
| 127 | + envelope_definition = EnvelopeDefinition( |
| 128 | + email_subject="Example Signing Document", |
| 129 | + documents=[document], |
| 130 | + # The Recipients object wants arrays for each recipient type |
| 131 | + recipients=Recipients(signers=[signer], carbon_copies=[cc]), |
| 132 | + status="sent" # requests that the envelope be created and sent. |
| 133 | + ) |
| 134 | + |
| 135 | + return envelope_definition |
| 136 | + |
| 137 | + @classmethod |
| 138 | + def get_html_content(cls, args): |
| 139 | + with open(args["doc_file"], "r") as file: |
| 140 | + doc_html = file.read() |
| 141 | + |
| 142 | + return doc_html.replace("{signer_name}", args["signer_name"]) \ |
| 143 | + .replace("{signer_email}", args["signer_email"]) \ |
| 144 | + .replace("{cc_name}", args["cc_name"]) \ |
| 145 | + .replace("{cc_email}", args["cc_email"]) \ |
| 146 | + .replace("/sn1/", "<ds-signature data-ds-role=\"Signer\"/>") \ |
| 147 | + .replace("/l1q/", "<input data-ds-type=\"number\"/>") \ |
| 148 | + .replace("/l2q/", "<input data-ds-type=\"number\"/>") |
0 commit comments