forked from docusign/code-examples-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
197 lines (169 loc) · 6.76 KB
/
controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import base64
from os import path
from docusign_esign import EnvelopesApi, Document, Signer, EnvelopeDefinition, Recipients, \
BulkEnvelopesApi, TextCustomField, CustomFields
from docusign_esign.models import BulkSendingCopy, BulkSendingList, BulkSendingCopyRecipient, BulkSendRequest
from flask import request, session
from ....consts import demo_docs_path, pattern
from ....docusign import create_api_client
from ....ds_config import DS_CONFIG
class Eg031Controller:
@staticmethod
def get_args():
# More data validation would be a good idea here
# Strip anything other than the characters listed
signer_email_1 = pattern.sub("", request.form.get("signer_email_1"))
signer_name_1 = pattern.sub("", request.form.get("signer_name_1"))
cc_email_1 = pattern.sub("", request.form.get("cc_email_1"))
cc_name_1 = pattern.sub("", request.form.get("cc_name_1"))
signer_email_2 = pattern.sub("", request.form.get("signer_email_2"))
signer_name_2 = pattern.sub("", request.form.get("signer_name_2"))
cc_email_2 = pattern.sub("", request.form.get("cc_email_2"))
cc_name_2 = pattern.sub("", request.form.get("cc_name_2"))
args = {
"account_id": session["ds_account_id"], # Represents your {ACCOUNT_ID}
"base_path": session["ds_base_path"],
"access_token": session["ds_access_token"], # Represents your {ACCESS_TOKEN}
"signers": [{
"signer_name": signer_name_1,
"signer_email": signer_email_1,
"cc_email": cc_email_1,
"cc_name": cc_name_1
},
{
"signer_name": signer_name_2,
"signer_email": signer_email_2,
"cc_email": cc_email_2,
"cc_name": cc_name_2
}
]
}
return args
@classmethod
def worker(cls, args):
"""
1. Create an api client and construct API clients
2. Create and submit a bulk sending list
3. Create a draft envelope
4. Add custom fields to the envelope
5. Add recipients to the envelope
6. Initiate bulk envelope sending
7. Confirm sending success
"""
# Step 2. Construct your API headers
api_client = create_api_client(base_path=args["base_path"], access_token=args["access_token"])
# Step 3. Submit a bulk list
bulk_envelopes_api = BulkEnvelopesApi(api_client)
bulk_sending_list = cls.create_bulk_sending_list(args["signers"])
bulk_list = bulk_envelopes_api.create_bulk_send_list(
account_id=args["account_id"],
bulk_sending_list=bulk_sending_list
)
bulk_list_id = bulk_list.list_id
# Step 4. Create an envelope
envelope_api = EnvelopesApi(api_client)
envelope_definition = cls.make_draft_envelope()
envelope = envelope_api.create_envelope(account_id=args["account_id"], envelope_definition=envelope_definition)
envelope_id = envelope.envelope_id
# Step 5. Attach your bulk list id to the envelope
text_custom_fields = TextCustomField(name="mailingListId", required="false", show="false", value=bulk_list_id)
custom_fields = CustomFields(list_custom_fields=[], text_custom_fields=[text_custom_fields])
envelope_api.create_custom_fields(
account_id=args["account_id"],
envelope_id=envelope_id,
custom_fields=custom_fields
)
# Step 6. Add placeholder recipients
cc = Signer(
name="Multi Bulk Recipient::cc",
email="[email protected]",
role_name="cc",
note="",
routing_order="1",
status="created",
delivery_method="email",
recipient_id="12",
recipient_type="signer"
)
signer = Signer(
name="Multi Bulk Recipient::signer",
email="[email protected]",
role_name="signer",
note="",
routing_order="1",
status="created",
delivery_method="email",
recipient_id="13",
recipient_type="signer"
)
envelope_api.create_recipient(
account_id=args["account_id"],
envelope_id=envelope_id,
recipients=Recipients(signers=[signer, cc])
)
# Step 7. Initiate bulk send
bulk_send_request = BulkSendRequest(envelope_or_template_id=envelope_id)
batch = bulk_envelopes_api.create_bulk_send_request(
account_id=args["account_id"],
bulk_send_list_id=bulk_list_id,
bulk_send_request=bulk_send_request
)
batch_id = batch.batch_id
# Step 8. Confirm successful batch send
response = bulk_envelopes_api.get(account_id=args["account_id"], batch_id=batch_id)
return response
@classmethod
def create_bulk_sending_list(cls, args):
"""
1. Create recipient objects with signers
2. Create recipient objects with ccs
3. Create bulk copies objects
4. Create the bulk sending list object
"""
bulk_copies = []
for signer in args:
recipient_1 = BulkSendingCopyRecipient(
role_name="signer",
tabs=[],
name=signer["signer_name"],
email=signer["signer_email"]
)
recipient_2 = BulkSendingCopyRecipient(
role_name="cc",
tabs=[],
name=signer["cc_name"],
email=signer["cc_email"]
)
bulk_copy = BulkSendingCopy(
recipients=[recipient_1, recipient_2],
custom_fields=[]
)
bulk_copies.append(bulk_copy)
bulk_sending_list = BulkSendingList(
name="sample",
bulk_copies=bulk_copies
)
return bulk_sending_list
@classmethod
def make_draft_envelope(cls):
"""
Creates the envelope
"""
# Open the example file
with open(path.join(demo_docs_path, DS_CONFIG["doc_pdf"]), "rb") as file:
content_bytes = file.read()
base64_file_content = base64.b64encode(content_bytes).decode("ascii")
document = Document(
document_base64=base64_file_content,
name="lorem",
file_extension="pdf",
document_id=2
)
envelope_definition = EnvelopeDefinition(
email_subject="Please Sign",
documents=[document],
status="created",
envelope_id_stamping="true",
recipients={},
)
return envelope_definition