7
7
import base64
8
8
import re
9
9
from docusign_esign import *
10
- from docusign_esign .rest import ApiException
10
+ from docusign_esign .client . api_exception import ApiException
11
11
12
12
eg = "eg001" # reference (and url) for this example
13
13
signer_client_id = 1000 # Used to indicate that the signer will use an embedded
14
14
# Signing Ceremony. Represents the signer's userId within
15
15
# your application.
16
- authentication_method = ' None' # How is this application authenticating
17
- # the signer? See the ` authenticationMethod' definition
16
+ authentication_method = " None" # How is this application authenticating
17
+ # the signer? See the ' authenticationMethod' definition
18
18
# https://developers.docusign.com/esign-rest-api/reference/Envelopes/EnvelopeViews/createRecipient
19
19
20
- demo_docs_path = path .abspath (path .join (path .dirname (path .realpath (__file__ )), ' static/demo_documents' ))
20
+ demo_docs_path = path .abspath (path .join (path .dirname (path .realpath (__file__ )), " static/demo_documents" ))
21
21
22
22
23
23
def controller ():
24
24
"""Controller router using the HTTP method"""
25
- if request .method == ' GET' :
25
+ if request .method == " GET" :
26
26
return get_controller ()
27
- elif request .method == ' POST' :
27
+ elif request .method == " POST" :
28
28
return create_controller ()
29
29
else :
30
- return render_template (' 404.html' ), 404
30
+ return render_template (" 404.html" ), 404
31
31
32
32
33
33
def create_controller ():
@@ -41,53 +41,53 @@ def create_controller():
41
41
# 2. Call the worker method
42
42
# More data validation would be a good idea here
43
43
# Strip anything other than characters listed
44
- pattern = re .compile (' ([^\w \-\@\.\,])+' )
45
- signer_email = pattern .sub ('' , request .form .get (' signer_email' ))
46
- signer_name = pattern .sub ('' , request .form .get (' signer_name' ))
44
+ pattern = re .compile (" ([^\w \-\@\.\,])+" )
45
+ signer_email = pattern .sub ("" , request .form .get (" signer_email" ))
46
+ signer_name = pattern .sub ("" , request .form .get (" signer_name" ))
47
47
envelope_args = {
48
- ' signer_email' : signer_email ,
49
- ' signer_name' : signer_name ,
50
- ' signer_client_id' : signer_client_id ,
51
- ' ds_return_url' : url_for (' ds_return' , _external = True ),
48
+ " signer_email" : signer_email ,
49
+ " signer_name" : signer_name ,
50
+ " signer_client_id" : signer_client_id ,
51
+ " ds_return_url" : url_for (" ds_return" , _external = True ),
52
52
}
53
53
args = {
54
- ' account_id' : session [' ds_account_id' ],
55
- ' base_path' : session [' ds_base_path' ],
56
- ' ds_access_token' : session [' ds_access_token' ],
57
- ' envelope_args' : envelope_args
54
+ " account_id" : session [" ds_account_id" ],
55
+ " base_path" : session [" ds_base_path" ],
56
+ " ds_access_token" : session [" ds_access_token" ],
57
+ " envelope_args" : envelope_args
58
58
}
59
59
60
60
try :
61
61
results = worker (args )
62
62
except ApiException as err :
63
- error_body_json = err and hasattr (err , ' body' ) and err .body
63
+ error_body_json = err and hasattr (err , " body" ) and err .body
64
64
# we can pull the DocuSign error code and message from the response body
65
65
error_body = json .loads (error_body_json )
66
- error_code = error_body and ' errorCode' in error_body and error_body [' errorCode' ]
67
- error_message = error_body and ' message' in error_body and error_body [' message' ]
66
+ error_code = error_body and " errorCode" in error_body and error_body [" errorCode" ]
67
+ error_message = error_body and " message" in error_body and error_body [" message" ]
68
68
# In production, may want to provide customized error messages and
69
69
# remediation advice to the user.
70
- return render_template (' error.html' ,
70
+ return render_template (" error.html" ,
71
71
err = err ,
72
72
error_code = error_code ,
73
73
error_message = error_message
74
74
)
75
75
if results :
76
76
# Redirect the user to the Signing Ceremony
77
- # Don' t use an iFrame!
77
+ # Don" t use an iFrame!
78
78
# State can be stored/recovered using the framework's session or a
79
79
# query parameter on the returnUrl (see the makeRecipientViewRequest method)
80
80
return redirect (results ["redirect_url" ])
81
81
82
82
else :
83
- flash (' Sorry, you need to re-authenticate.' )
83
+ flash (" Sorry, you need to re-authenticate." )
84
84
# We could store the parameters of the requested operation
85
85
# so it could be restarted automatically.
86
86
# But since it should be rare to have a token issue here,
87
87
# we'll make the user re-enter the form data after
88
88
# authentication.
89
- session ['eg' ] = url_for (eg )
90
- return redirect (url_for (' ds_must_authenticate' ))
89
+ session ["eg" ] = url_for (eg )
90
+ return redirect (url_for (" ds_must_authenticate" ))
91
91
92
92
93
93
# ***DS.snippet.0.start
@@ -105,29 +105,29 @@ def worker(args):
105
105
# 2. call Envelopes::create API method
106
106
# Exceptions will be caught by the calling function
107
107
api_client = ApiClient ()
108
- api_client .host = args [' base_path' ]
109
- api_client .set_default_header ("Authorization" , "Bearer " + args [' ds_access_token' ])
108
+ api_client .host = args [" base_path" ]
109
+ api_client .set_default_header ("Authorization" , "Bearer " + args [" ds_access_token" ])
110
110
111
111
envelope_api = EnvelopesApi (api_client )
112
- results = envelope_api .create_envelope (args [' account_id' ], envelope_definition = envelope_definition )
112
+ results = envelope_api .create_envelope (args [" account_id" ], envelope_definition = envelope_definition )
113
113
114
114
envelope_id = results .envelope_id
115
- app .logger .info (f' Envelope was created. EnvelopeId { envelope_id } ' )
115
+ app .logger .info (f" Envelope was created. EnvelopeId { envelope_id } " )
116
116
117
117
# 3. Create the Recipient View request object
118
118
recipient_view_request = RecipientViewRequest (
119
119
authentication_method = authentication_method ,
120
- client_user_id = envelope_args [' signer_client_id' ],
121
- recipient_id = '1' ,
122
- return_url = envelope_args [' ds_return_url' ],
123
- user_name = envelope_args [' signer_name' ], email = envelope_args [' signer_email' ]
120
+ client_user_id = envelope_args [" signer_client_id" ],
121
+ recipient_id = "1" ,
122
+ return_url = envelope_args [" ds_return_url" ],
123
+ user_name = envelope_args [" signer_name" ], email = envelope_args [" signer_email" ]
124
124
)
125
125
# 4. Obtain the recipient_view_url for the signing ceremony
126
126
# Exceptions will be caught by the calling function
127
- results = envelope_api .create_recipient_view (args [' account_id' ], envelope_id ,
127
+ results = envelope_api .create_recipient_view (args [" account_id" ], envelope_id ,
128
128
recipient_view_request = recipient_view_request )
129
129
130
- return {' envelope_id' : envelope_id , ' redirect_url' : results .url }
130
+ return {" envelope_id" : envelope_id , " redirect_url" : results .url }
131
131
132
132
133
133
def make_envelope (args ):
@@ -142,30 +142,30 @@ def make_envelope(args):
142
142
#
143
143
# The envelope has one recipient.
144
144
# recipient 1 - signer
145
- with open (path .join (demo_docs_path , ds_config .DS_CONFIG [' doc_pdf' ]), "rb" ) as file :
145
+ with open (path .join (demo_docs_path , ds_config .DS_CONFIG [" doc_pdf" ]), "rb" ) as file :
146
146
content_bytes = file .read ()
147
- base64_file_content = base64 .b64encode (content_bytes ).decode (' ascii' )
147
+ base64_file_content = base64 .b64encode (content_bytes ).decode (" ascii" )
148
148
149
149
# Create the document model
150
150
document = Document ( # create the DocuSign document object
151
151
document_base64 = base64_file_content ,
152
- name = ' Example document' , # can be different from actual file name
153
- file_extension = ' pdf' , # many different document types are accepted
152
+ name = " Example document" , # can be different from actual file name
153
+ file_extension = " pdf" , # many different document types are accepted
154
154
document_id = 1 # a label used to reference the doc
155
155
)
156
156
157
157
# Create the signer recipient model
158
158
signer = Signer ( # The signer
159
- email = args [' signer_email' ], name = args [' signer_name' ],
159
+ email = args [" signer_email" ], name = args [" signer_name" ],
160
160
recipient_id = "1" , routing_order = "1" ,
161
161
# Setting the client_user_id marks the signer as embedded
162
- client_user_id = args [' signer_client_id' ]
162
+ client_user_id = args [" signer_client_id" ]
163
163
)
164
164
165
165
# Create a sign_here tab (field on the document)
166
166
sign_here = SignHere ( # DocuSign SignHere field/tab
167
- anchor_string = ' /sn1/' , anchor_units = ' pixels' ,
168
- anchor_y_offset = '10' , anchor_x_offset = '20'
167
+ anchor_string = " /sn1/" , anchor_units = " pixels" ,
168
+ anchor_y_offset = "10" , anchor_x_offset = "20"
169
169
)
170
170
171
171
# Add the tabs model (including the sign_here tab) to the signer
@@ -192,14 +192,14 @@ def get_controller():
192
192
return render_template ("eg001_embedded_signing.html" ,
193
193
title = "Embedded Signing Ceremony" ,
194
194
source_file = path .basename (__file__ ),
195
- source_url = ds_config .DS_CONFIG [' github_example_url' ] + path .basename (__file__ ),
196
- documentation = ds_config .DS_CONFIG [' documentation' ] + eg ,
197
- show_doc = ds_config .DS_CONFIG [' documentation' ],
198
- signer_name = ds_config .DS_CONFIG [' signer_name' ],
199
- signer_email = ds_config .DS_CONFIG [' signer_email' ]
195
+ source_url = ds_config .DS_CONFIG [" github_example_url" ] + path .basename (__file__ ),
196
+ documentation = ds_config .DS_CONFIG [" documentation" ] + eg ,
197
+ show_doc = ds_config .DS_CONFIG [" documentation" ],
198
+ signer_name = ds_config .DS_CONFIG [" signer_name" ],
199
+ signer_email = ds_config .DS_CONFIG [" signer_email" ]
200
200
)
201
201
else :
202
202
# Save the current operation so it will be resumed after authentication
203
- session ['eg' ] = url_for (eg )
204
- return redirect (url_for (' ds_must_authenticate' ))
203
+ session ["eg" ] = url_for (eg )
204
+ return redirect (url_for (" ds_must_authenticate" ))
205
205
0 commit comments