1+ """Example 002: Bulk export user data"""
2+
3+ import json
4+ from os import path
5+
6+ from flask import Blueprint , render_template , Response , current_app
7+ from docusign_admin .client .api_exception import ApiException
8+
9+ from app .error_handlers import process_error
10+ from app .docusign import authenticate
11+ from app .ds_config import DS_CONFIG
12+ from .controller import Eg002Controller
13+
14+
15+ eg = "eg002" # Reference (and URL) for this example
16+ eg002 = Blueprint (eg , __name__ )
17+
18+ @eg002 .route ("/eg002" , methods = ["POST" ])
19+ @authenticate (eg = eg )
20+ def get_user_list_data ():
21+ """
22+ 1. Call the worker method
23+ 2. Render the response
24+ """
25+
26+ # 1. Call the worker method
27+ try :
28+ results = Eg002Controller .worker ()
29+ current_app .logger .info (f"User list export ID: { results .id } " )
30+ except ApiException as err :
31+ return process_error (err )
32+
33+ # 2. Render the response
34+ return render_template (
35+ "example_done.html" ,
36+ get_csv = True ,
37+ title = "Bulk export user data" ,
38+ h1 = "Result of creating a bulk export user data" ,
39+ message = "Results from UserExport:createUserListExport:" ,
40+ json = json .dumps (json .dumps (results .to_dict (), default = str ))
41+ )
42+
43+ @eg002 .route ("/eg002" , methods = ["GET" ])
44+ @authenticate (eg = eg )
45+ def get_view ():
46+ """
47+ Responds with the form for the example
48+ """
49+
50+ # Render the response
51+ return render_template (
52+ "eg002_bulk_export_user_data.html" ,
53+ title = "Bulk export user data" ,
54+ source_file = path .basename (path .dirname (__file__ )) + "/controller.py" ,
55+ source_url = (
56+ DS_CONFIG ["admin_github_url" ] +
57+ path .basename (path .dirname (__file__ )) + "/controller.py"
58+ ),
59+ documentation = DS_CONFIG ["documentation" ] + eg ,
60+ )
61+
62+ @eg002 .route ("/eg002check" , methods = ["GET" ])
63+ @authenticate (eg = eg )
64+ def check_if_csv_ready ():
65+ """
66+ 1. Checking if a CSV file exists
67+ 2. Render the response
68+ """
69+
70+ # 1. Checking if a CSV file exists
71+ try :
72+ csv_file = Eg002Controller .get_csv_user_list ()
73+ except ApiException as err :
74+ return process_error (err )
75+
76+ # 2. Render the response
77+ return render_template (
78+ "eg002_file_state.html" ,
79+ get_csv = bool (csv_file )
80+ )
81+
82+ @eg002 .route ("/eg002csv" , methods = ["GET" ])
83+ @authenticate (eg = eg )
84+ def get_csv ():
85+ """
86+ 1. Getting an existing CSV file
87+ 2. Returns the finished csv file to the user
88+ """
89+
90+ # 1. Getting an existing CSV file
91+ try :
92+ csv_file = Eg002Controller .get_csv_user_list ()
93+ except ApiException as err :
94+ return process_error (err )
95+
96+ # 2. Returns the finished csv file to the user
97+ return Response (
98+ csv_file ,
99+ mimetype = "text/csv" ,
100+ headers = {
101+ "Content-disposition" :"attachment; filename=user_list.csv"
102+ }
103+ )
104+
0 commit comments