forked from docusign/code-examples-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
66 lines (57 loc) · 2.32 KB
/
controller.py
File metadata and controls
66 lines (57 loc) · 2.32 KB
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
from docusign_esign import EnvelopesApi
from flask import request, session
from ....consts import pattern
from ....docusign import create_api_client
class Eg007Controller:
@staticmethod
def get_args():
"""
Get session arguments
"""
# More data validation would be a good idea here
# Strip anything other than characters listed
document_id = pattern.sub("", request.form.get("document_id"))
args = {
"account_id": session["ds_account_id"],
"envelope_id": session["envelope_id"],
"base_path": session["ds_base_path"],
"access_token": session["ds_access_token"],
"document_id": document_id,
"envelope_documents": session["envelope_documents"]
}
return args
@staticmethod
def worker(args):
"""
1. Call the envelope get method
"""
# Exceptions will be caught by the calling function
api_client = create_api_client(base_path=args["base_path"], access_token=args["access_token"])
envelope_api = EnvelopesApi(api_client)
document_id = args["document_id"]
# The SDK always stores the received file as a temp file
# 1. Call the envelope get method
temp_file = envelope_api.get_document(
account_id=args["account_id"],
document_id=document_id,
envelope_id=args["envelope_id"]
)
doc_item = next(item for item in args["envelope_documents"]["documents"] if item["document_id"] == document_id)
doc_name = doc_item["name"]
has_pdf_suffix = doc_name[-4:].upper() == ".PDF"
pdf_file = has_pdf_suffix
# Add .pdf if it"s a content or summary doc and doesn"t already end in .pdf
if (doc_item["type"] == "content" or doc_item["type"] == "summary") and not has_pdf_suffix:
doc_name += ".pdf"
pdf_file = True
# Add .zip as appropriate
if doc_item["type"] == "zip":
doc_name += ".zip"
# Return the file information
if pdf_file:
mimetype = "application/pdf"
elif doc_item["type"] == "zip":
mimetype = "application/zip"
else:
mimetype = "application/octet-stream"
return {"mimetype": mimetype, "doc_name": doc_name, "data": temp_file}