1+ import base64
2+ from os import path
3+
4+ from docusign_esign import EnvelopesApi , Envelope , EnvelopeDefinition , Document , Signer , SignHere , \
5+ Tabs , Recipients , Workflow , DelayedRoutingApiModel , EnvelopeDelayRuleApiModel , WorkflowStep
6+ from ...consts import demo_docs_path , pattern , signer_client_id
7+ from ...docusign import create_api_client
8+ from ...ds_config import DS_CONFIG
9+
10+ class Eg036DelayedRoutingController :
11+
12+ @classmethod
13+ def worker (cls , args ):
14+
15+ envelope_args = args ["envelope_args" ]
16+ envelope_definition = cls .make_envelope (envelope_args , DS_CONFIG ["doc_docx" ], DS_CONFIG ["doc_pdf" ])
17+
18+ api_client = create_api_client (base_path = args ["base_path" ], access_token = args ["access_token" ])
19+ envelopes_api = EnvelopesApi (api_client )
20+ results = envelopes_api .create_envelope (account_id = args ["account_id" ], envelope_definition = envelope_definition )
21+
22+ envelope_id = results .envelope_id
23+
24+ return {"envelope_id" : envelope_id }
25+
26+
27+ @classmethod
28+ def make_envelope (cls , args , doc_docx_path , doc_pdf_path ):
29+ """
30+ Creates envelope
31+ Document 1: A PDF document.
32+ The recipients' field tags are placed using <b>anchor</b> strings.
33+ """
34+
35+ # document 1 (PDF) has sign here anchor tag /sn1/
36+ #
37+ # The envelope has two recipients.
38+ # recipient 1 - signer
39+ # recipient 2 - second signer
40+ # The envelope will be sent first to the signer.
41+ # After it is signed, there will be a delay before it is sent to the second signer.
42+
43+ # create the envelope definition
44+ env = EnvelopeDefinition (
45+ email_subject = "Please sign this document"
46+ )
47+
48+ with open (path .join (demo_docs_path , doc_pdf_path ), "rb" ) as file :
49+ doc1_pdf_bytes = file .read ()
50+ doc1_b64 = base64 .b64encode (doc1_pdf_bytes ).decode ("ascii" )
51+
52+ # Create the document models
53+ document1 = Document ( # create the DocuSign document object
54+ document_base64 = doc1_b64 ,
55+ name = "Lorem Ipsum" , # can be different from actual file name
56+ file_extension = "pdf" , # many different document types are accepted
57+ document_id = "1" # a label used to reference the doc
58+ )
59+ # The order in the docs array determines the order in the envelope
60+ env .documents = [document1 ]
61+
62+ # Create the signer recipient model
63+ signer1 = Signer (
64+ email = args ["signer_email" ],
65+ name = args ["signer_name" ],
66+ recipient_id = "1" ,
67+ routing_order = "1"
68+ )
69+ # routingOrder (lower means earlier) determines the order of deliveries
70+ # to the recipients.
71+
72+ # create a second recipient
73+ signer2 = Signer (
74+ email = args ["signer_email2" ],
75+ name = args ["signer_name2" ],
76+ recipient_id = "2" ,
77+ routing_order = "2"
78+ )
79+
80+ # Create signHere fields (also known as tabs) on the documents,
81+ # We"re using anchor (autoPlace) positioning
82+ #
83+ # The DocuSign platform searches throughout your envelope"s
84+ # documents for matching anchor strings.
85+
86+ sign_here1 = SignHere (
87+ anchor_string = "/sn1/" ,
88+ anchor_units = "pixels" ,
89+ anchor_y_offset = "10" ,
90+ anchor_x_offset = "20"
91+ )
92+
93+ sign_here2 = SignHere (
94+ x_position = "320" ,
95+ y_position = "175" ,
96+ page_number = "1" ,
97+ document_id = "1"
98+ )
99+
100+ # Add the tabs model (including the sign_here tabs) to the signer
101+ # The Tabs object wants arrays of the different field/tab types
102+ signer1 .tabs = Tabs (sign_here_tabs = [sign_here1 ])
103+ signer2 .tabs = Tabs (sign_here_tabs = [sign_here2 ])
104+
105+ # Add the recipients to the envelope object
106+ recipients = Recipients (signers = [signer1 , signer2 ])
107+ env .recipients = recipients
108+
109+ delay = "0." + args ["delay" ] + ":00:00"
110+
111+ workflow = Workflow ()
112+
113+ workflow_step = WorkflowStep ()
114+ workflow_step .action = "pause_before"
115+ workflow_step .trigger_on_item = "routing_order"
116+ workflow_step .item_id = "2"
117+ workflow_step .status = "pending"
118+ delayed_routing = DelayedRoutingApiModel (rules = [EnvelopeDelayRuleApiModel (delay = delay )])
119+ workflow_step .delayed_routing = delayed_routing
120+ workflow .workflow_steps = [workflow_step ]
121+ env .workflow = workflow
122+
123+ # Request that the envelope be sent by setting |status| to "sent".
124+ # To request that the envelope be created as a draft, set to "created"
125+ env .status = args ["status" ]
126+
127+ return env
0 commit comments