|
| 1 | +import base64 |
| 2 | +from os import path |
| 3 | + |
| 4 | +from docusign_esign import EnvelopesApi, EnvelopeDefinition, Document, Signer, SignHere, Tabs, Recipients |
| 5 | +from docusign_esign.models import Workflow, WorkflowStep |
| 6 | +from flask import session, request |
| 7 | + |
| 8 | +from ....consts import demo_docs_path, pattern |
| 9 | +from ....docusign import create_api_client |
| 10 | +from ....ds_config import DS_CONFIG |
| 11 | + |
| 12 | + |
| 13 | +class Eg032Controller: |
| 14 | + @staticmethod |
| 15 | + def get_args(): |
| 16 | + """Get request and session arguments""" |
| 17 | + |
| 18 | + # Strip anything other than characters listed |
| 19 | + signer1_email = pattern.sub("", request.form.get("signer1_email")) |
| 20 | + signer1_name = pattern.sub("", request.form.get("signer1_name")) |
| 21 | + signer2_email = pattern.sub("", request.form.get("signer2_email")) |
| 22 | + signer2_name = pattern.sub("", request.form.get("signer2_name")) |
| 23 | + envelope_args = { |
| 24 | + "signer1_email": signer1_email, |
| 25 | + "signer1_name": signer1_name, |
| 26 | + "signer2_email": signer2_email, |
| 27 | + "signer2_name": signer2_name, |
| 28 | + "status": "Sent", |
| 29 | + } |
| 30 | + args = { |
| 31 | + "account_id": session["ds_account_id"], |
| 32 | + "base_path": session["ds_base_path"], |
| 33 | + "access_token": session["ds_access_token"], |
| 34 | + "envelope_args": envelope_args |
| 35 | + } |
| 36 | + return args |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def worker(cls, args): |
| 40 | + """ |
| 41 | + 1. Create the envelope request object |
| 42 | + 2. Send the envelope |
| 43 | + """ |
| 44 | + |
| 45 | + envelope_args = args["envelope_args"] |
| 46 | + # 1. Create the envelope request object |
| 47 | + envelope_definition = cls.make_envelope(envelope_args) |
| 48 | + api_client = create_api_client( |
| 49 | + base_path=args["base_path"], access_token=args["access_token"] |
| 50 | + ) |
| 51 | + # 2. call Envelopes::create API method |
| 52 | + # Exceptions will be caught by the calling function |
| 53 | + envelopes_api = EnvelopesApi(api_client) |
| 54 | + results = envelopes_api.create_envelope( |
| 55 | + account_id=args["account_id"], |
| 56 | + envelope_definition=envelope_definition |
| 57 | + ) |
| 58 | + |
| 59 | + envelope_id = results.envelope_id |
| 60 | + |
| 61 | + return {"envelope_id": envelope_id} |
| 62 | + |
| 63 | + @classmethod |
| 64 | + def make_envelope(cls, args): |
| 65 | + """ |
| 66 | + Creates envelope |
| 67 | + Document: A txt document. |
| 68 | + DocuSign will convert document to the PDF format. |
| 69 | + """ |
| 70 | + |
| 71 | + # The envelope has two recipients. |
| 72 | + # recipient 1 - signer1 |
| 73 | + # recipient 2 - signer2 |
| 74 | + # The envelope will be sent first to the signer1. |
| 75 | + # After it is signed, a signature workflow will be paused. |
| 76 | + # After resuming (unpause) the signature workflow will send to the second recipient. |
| 77 | + |
| 78 | + # create the envelope definition |
| 79 | + env = EnvelopeDefinition(email_subject="EnvelopeWorkflowTest") |
| 80 | + |
| 81 | + # read file from a local directory |
| 82 | + # The reads could raise an exception if the file is not available! |
| 83 | + with open(path.join(demo_docs_path, DS_CONFIG["doc_txt"]), "rb") as file: |
| 84 | + doc_docx_bytes = file.read() |
| 85 | + doc_b64 = base64.b64encode(doc_docx_bytes).decode("ascii") |
| 86 | + |
| 87 | + # Create the document model. |
| 88 | + document = Document( # create the DocuSign document object |
| 89 | + document_base64=doc_b64, |
| 90 | + name="Welcome", # can be different from actual file name |
| 91 | + file_extension="txt", # many different document types are accepted |
| 92 | + document_id="1" # a label used to reference the doc |
| 93 | + ) |
| 94 | + |
| 95 | + # The order in the docs array determines the order in the envelope. |
| 96 | + env.documents = [document,] |
| 97 | + |
| 98 | + # Create a workflow model |
| 99 | + workflow_step = WorkflowStep( |
| 100 | + action="pause_before", |
| 101 | + trigger_on_item="routing_order", |
| 102 | + item_id="2" |
| 103 | + ) |
| 104 | + workflow = Workflow(workflow_steps=[workflow_step,]) |
| 105 | + # Add the workflow to the envelope object |
| 106 | + env.workflow = workflow |
| 107 | + |
| 108 | + # Create the signer recipient models |
| 109 | + # routingOrder (lower means earlier) determines the order of deliveries |
| 110 | + # to the recipients. |
| 111 | + signer1 = Signer( |
| 112 | + email=args["signer1_email"], |
| 113 | + name=args["signer1_name"], |
| 114 | + recipient_id="1", |
| 115 | + routing_order="1" |
| 116 | + ) |
| 117 | + signer2 = Signer( |
| 118 | + email=args["signer2_email"], |
| 119 | + name=args["signer2_name"], |
| 120 | + recipient_id="2", |
| 121 | + routing_order="2" |
| 122 | + ) |
| 123 | + |
| 124 | + # Create signHere fields (also known as tabs) on the documents. |
| 125 | + sign_here1 = SignHere( |
| 126 | + document_id="1", |
| 127 | + page_number="1", |
| 128 | + tab_label="Sign Here", |
| 129 | + x_position="200", |
| 130 | + y_position="200" |
| 131 | + ) |
| 132 | + |
| 133 | + sign_here2 = SignHere( |
| 134 | + document_id="1", |
| 135 | + page_number="1", |
| 136 | + tab_label="Sign Here", |
| 137 | + x_position="300", |
| 138 | + y_position="200" |
| 139 | + ) |
| 140 | + |
| 141 | + # Add the tabs model (including the sign_here tabs) to the signer |
| 142 | + # The Tabs object wants arrays of the different field/tab types |
| 143 | + signer1.tabs = Tabs(sign_here_tabs=[sign_here1,]) |
| 144 | + signer2.tabs = Tabs(sign_here_tabs=[sign_here2,]) |
| 145 | + |
| 146 | + # Add the recipients to the envelope object |
| 147 | + recipients = Recipients(signers=[signer1, signer2]) |
| 148 | + env.recipients = recipients |
| 149 | + |
| 150 | + # Request that the envelope be sent by setting |status| to "sent". |
| 151 | + # To request that the envelope be created as a draft, set to "created" |
| 152 | + env.status = args["status"] |
| 153 | + return env |
0 commit comments