Skip to content

Commit 8c166ae

Browse files
committed
added admin examples
1 parent 4a8ed35 commit 8c166ae

27 files changed

Lines changed: 849 additions & 2 deletions

app/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .rooms import examples as rooms_examples
1111
from .click import examples as click_examples
1212
from .monitor import examples as monitor_examples
13+
from .admin import examples as admin_examples
1314
from .views import core
1415

1516
session_path = "/tmp/python_recipe_sessions"
@@ -20,6 +21,8 @@
2021
app = Flask(__name__, template_folder="click/templates")
2122
elif EXAMPLES_API_TYPE["Monitor"]:
2223
app = Flask(__name__, template_folder="monitor/templates")
24+
elif EXAMPLES_API_TYPE["Admin"]:
25+
app = Flask(__name__, template_folder="admin/templates")
2326
else:
2427
app = Flask(__name__)
2528
app.config.from_pyfile("config.py")
@@ -59,6 +62,12 @@
5962
app.register_blueprint(click_examples.eg003)
6063
app.register_blueprint(click_examples.eg004)
6164
app.register_blueprint(click_examples.eg005)
65+
66+
elif EXAMPLES_API_TYPE["Admin"]:
67+
app.register_blueprint(admin_examples.eg001)
68+
app.register_blueprint(admin_examples.eg002)
69+
app.register_blueprint(admin_examples.eg003)
70+
6271
else:
6372
app.register_blueprint(examples.eg001)
6473
app.register_blueprint(examples.eg002)

app/admin/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .utils import create_admin_api_client

app/admin/examples/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .eg001_create_a_new_user import eg001
2+
from .eg002_bulk_export_user_data import eg002
3+
from .eg003_add_users_via_bulk_import import eg003
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .views import eg001
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from docusign_admin.apis import UsersApi
2+
from flask import session
3+
4+
from app.admin.utils import create_admin_api_client
5+
from app.consts import pattern
6+
from app.ds_config import DS_CONFIG
7+
8+
9+
class Eg001Controller:
10+
11+
@staticmethod
12+
def get_args(request):
13+
"""
14+
Get request and session arguments
15+
"""
16+
17+
return {
18+
'first_name': pattern.sub("", request.form.get("first_name")),
19+
'last_name': pattern.sub("", request.form.get("last_name")),
20+
'user_email': pattern.sub("", request.form.get("user_email")),
21+
'profile_id': pattern.sub("", request.form.get("profile_id")),
22+
'group_id': pattern.sub("", request.form.get("group_id")),
23+
'activate_membership': bool(request.form.get("activate_membership"))
24+
}
25+
26+
@staticmethod
27+
def worker(args):
28+
"""
29+
1. Create the API client object
30+
2. Create the user API request object
31+
3. Create a request body for the create_user method
32+
4. Creates a user using a method from the user API
33+
"""
34+
35+
# 1. Create the API client object
36+
api_client = create_admin_api_client(
37+
access_token=session["ds_access_token"]
38+
)
39+
40+
# 2. Create the user API request object
41+
user_api = UsersApi(api_client=api_client)
42+
43+
# 3. Create a request body for the create_user method
44+
request_body = {
45+
"user_name": f"{args['first_name']} {args['last_name']}",
46+
"first_name": args['first_name'],
47+
"last_name": args['last_name'],
48+
"email": args['user_email'],
49+
"auto_activate_memberships": args['activate_membership'],
50+
"accounts": [
51+
{
52+
"id": session["ds_account_id"],
53+
"permission_profile": {
54+
"id": args['profile_id'],
55+
},
56+
"groups": [
57+
{
58+
"id": args['group_id'],
59+
}
60+
]
61+
}
62+
]
63+
}
64+
65+
# 4. Creates a user using a method from the user API
66+
response = user_api.create_user(
67+
DS_CONFIG["organization_id"],
68+
request_body
69+
)
70+
return response
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""Example 001: Create a new user"""
2+
3+
import json
4+
from os import path
5+
6+
from flask import Blueprint, render_template, request, 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+
13+
from .controller import Eg001Controller
14+
15+
eg = "eg001" # Reference (and URL) for this example
16+
eg001 = Blueprint(eg, __name__)
17+
18+
@eg001.route("/eg001", methods=["POST"])
19+
@authenticate(eg=eg)
20+
def get_user_data():
21+
"""
22+
1. Get required arguments
23+
2. Call the worker method
24+
3. Render the response
25+
"""
26+
27+
# 1. Get required arguments
28+
args = Eg001Controller.get_args(request)
29+
30+
# 2. Call the worker method
31+
try:
32+
results = Eg001Controller.worker(args)
33+
current_app.logger.info(f"ID of the created user: {results.id}")
34+
except ApiException as err:
35+
return process_error(err)
36+
37+
# 3. Render the response
38+
return render_template(
39+
"example_done.html",
40+
title="Create a new user",
41+
h1="Result of creating a new user",
42+
message="Results from Users:createUser method:",
43+
json=json.dumps(json.dumps(results.to_dict(), default=str))
44+
)
45+
46+
@eg001.route("/eg001", methods=["GET"])
47+
@authenticate(eg=eg)
48+
def get_view():
49+
"""
50+
Responds with the form for the example
51+
"""
52+
53+
# Render the response
54+
return render_template(
55+
"eg001_create_a_new_user.html",
56+
title="Create a new user",
57+
source_file=path.basename(path.dirname(__file__)) + "/controller.py",
58+
source_url=DS_CONFIG["admin_github_url"] + path.basename(
59+
path.dirname(__file__)) + "/controller.py",
60+
documentation=DS_CONFIG["documentation"] + eg,
61+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .views import eg002
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
from docusign_admin import ApiClient
2+
from docusign_admin.apis import BulkExportsApi
3+
from flask import session
4+
5+
from app.admin.utils import create_admin_api_client
6+
from app.ds_config import DS_CONFIG
7+
8+
9+
class Eg002Controller:
10+
11+
@staticmethod
12+
def _get_export_api():
13+
"""
14+
1. Create the API client object
15+
2. Return the bulk exports API Object
16+
"""
17+
18+
# 1. Create the API client object
19+
api_client = create_admin_api_client(
20+
access_token=session["ds_access_token"]
21+
)
22+
23+
# 2. Return the bulk exports API Object
24+
return BulkExportsApi(api_client=api_client)
25+
26+
@classmethod
27+
def worker(cls):
28+
"""
29+
1. Create the export API object
30+
2. Create a user list export request
31+
3. Save user_list_export_id in a client session
32+
4. Returns a list of pending and completed export requests
33+
"""
34+
35+
# 1. Create the export API object
36+
export_api = cls._get_export_api()
37+
38+
# 2. Create a user list export request
39+
response = export_api.create_user_list_export(
40+
DS_CONFIG["organization_id"],
41+
{
42+
"type": "organization_memberships_export"
43+
}
44+
)
45+
46+
# 3. Save user_list_export_id in a client session
47+
session['user_list_export_id'] = response.id
48+
49+
# 4. Returns a list of pending and completed export requests
50+
return response
51+
52+
@classmethod
53+
def get_csv_user_list(cls):
54+
"""
55+
Getting the csv file of the current list of users:
56+
1. Create the export API object
57+
2. Getting the user list export response
58+
3. Trying to get the user list export id
59+
4. Create the API client object
60+
5. Add headers to the API client object and the desired URL
61+
6. Getting a response containing a csv file
62+
7. Returns the csv file
63+
"""
64+
65+
# 1. Create the export API object
66+
export_api = cls._get_export_api()
67+
68+
# 2. Getting the user list export response
69+
response = export_api.get_user_list_export(
70+
DS_CONFIG["organization_id"],
71+
session['user_list_export_id']
72+
)
73+
74+
# 3. Trying to get the user list export id
75+
try:
76+
obj_id = response.results[0].id
77+
except TypeError:
78+
return None
79+
80+
# 4. Create the API client object
81+
api_client = ApiClient()
82+
83+
# 5. Add headers to the API client object and the desired URL
84+
headers = {"Authorization": "Bearer " + session["ds_access_token"]}
85+
url = (
86+
"https://demo.docusign.net/restapi/v2/organization_exports/"
87+
f"{DS_CONFIG['organization_id']}/user_list/{obj_id}"
88+
)
89+
90+
# 6. Getting a response containing a csv file
91+
response = api_client.request("GET", url, headers=headers)
92+
93+
# 7. Returns the csv file
94+
return response.data.decode("UTF8")
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .views import eg003

0 commit comments

Comments
 (0)