|
| 1 | +""" Example 043: Share access to a DocuSign envelope inbox """ |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +from docusign_esign.client.api_exception import ApiException |
| 6 | +from flask import render_template, session, Blueprint, redirect, current_app, url_for, request |
| 7 | + |
| 8 | +from ..examples.eg043_shared_access import Eg043SharedAccessController |
| 9 | +from ...docusign import authenticate, ensure_manifest, get_example_by_number, get_user_info |
| 10 | +from ...docusign.utils import ds_logout_internal, authenticate_agent |
| 11 | +from ...ds_config import DS_CONFIG, DS_JWT |
| 12 | +from ...error_handlers import process_error |
| 13 | +from ...consts import pattern, API_TYPE |
| 14 | + |
| 15 | +example_number = 43 |
| 16 | +api = API_TYPE["ESIGNATURE"] |
| 17 | +eg = f"eg0{example_number}" # reference (and url) for this example |
| 18 | +eg043 = Blueprint(eg, __name__) |
| 19 | + |
| 20 | + |
| 21 | +@eg043.route(f"/{eg}", methods=["POST"]) |
| 22 | +@authenticate(eg=eg, api=api) |
| 23 | +@ensure_manifest(manifest_url=DS_CONFIG["example_manifest_url"]) |
| 24 | +def create_agent(): |
| 25 | + args = { |
| 26 | + "account_id": session["ds_account_id"], |
| 27 | + "base_path": session["ds_base_path"], |
| 28 | + "access_token": session["ds_access_token"], |
| 29 | + "email": request.form.get("email"), |
| 30 | + "user_name": pattern.sub("", request.form.get("user_name")), |
| 31 | + "activation": pattern.sub("", request.form.get("activation")) |
| 32 | + } |
| 33 | + try: |
| 34 | + # 1. Create the agent user |
| 35 | + results = Eg043SharedAccessController.create_agent(args) |
| 36 | + except ApiException as err: |
| 37 | + return process_error(err) |
| 38 | + |
| 39 | + session["agent_user_id"] = results.user_id |
| 40 | + |
| 41 | + example = get_example_by_number(session["manifest"], example_number, api) |
| 42 | + return render_template( |
| 43 | + "example_done.html", |
| 44 | + title="Agent user created", |
| 45 | + message=example["ResultsPageText"], |
| 46 | + json=json.dumps(json.dumps(results.to_dict())), |
| 47 | + redirect_url=f"/{eg}auth" |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +@eg043.route(f"/{eg}auth", methods=["GET"]) |
| 52 | +@authenticate(eg=eg, api=api) |
| 53 | +@ensure_manifest(manifest_url=DS_CONFIG["example_manifest_url"]) |
| 54 | +def create_authorization(): |
| 55 | + user_info = get_user_info( |
| 56 | + session["ds_access_token"], |
| 57 | + session["ds_base_path"], |
| 58 | + DS_CONFIG["authorization_server"].replace("https://", "") |
| 59 | + ) |
| 60 | + args = { |
| 61 | + "account_id": session["ds_account_id"], |
| 62 | + "base_path": session["ds_base_path"], |
| 63 | + "access_token": session["ds_access_token"], |
| 64 | + "user_id": user_info.sub, |
| 65 | + "agent_user_id": session["agent_user_id"] |
| 66 | + } |
| 67 | + try: |
| 68 | + # 2. Create the authorization for agent user |
| 69 | + Eg043SharedAccessController.create_authorization(args) |
| 70 | + except ApiException as err: |
| 71 | + error_body_json = err and hasattr(err, "body") and err.body |
| 72 | + error_body = json.loads(error_body_json) |
| 73 | + error_code = error_body and "errorCode" in error_body and error_body["errorCode"] |
| 74 | + if error_code == "USER_NOT_FOUND": |
| 75 | + example = get_example_by_number(session["manifest"], example_number, api) |
| 76 | + additional_page_data = next((p for p in example["AdditionalPage"] if p["Name"] == "user_not_found"), |
| 77 | + None) |
| 78 | + return render_template( |
| 79 | + "example_done.html", |
| 80 | + title="Agent user created", |
| 81 | + message=additional_page_data["ResultsPageText"], |
| 82 | + redirect_url=f"/{eg}auth" |
| 83 | + ) |
| 84 | + |
| 85 | + return process_error(err) |
| 86 | + |
| 87 | + session["principal_user_id"] = user_info.sub |
| 88 | + |
| 89 | + example = get_example_by_number(session["manifest"], example_number, api) |
| 90 | + additional_page_data = next((p for p in example["AdditionalPage"] if p["Name"] == "authenticate_as_agent"), None) |
| 91 | + return render_template( |
| 92 | + "example_done.html", |
| 93 | + title="Authenticate as the agent", |
| 94 | + message=additional_page_data["ResultsPageText"], |
| 95 | + redirect_url=f"/{eg}reauthenticate" |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +@eg043.route(f"/{eg}reauthenticate", methods=["GET"]) |
| 100 | +def reauthenticate(): |
| 101 | + # 3. Logout principal user and redirect to page with the list of envelopes, login as agent user |
| 102 | + ds_logout_internal() |
| 103 | + current_app.config["isLoggedIn"] = False |
| 104 | + return redirect(url_for(f"{eg}.list_envelopes")) |
| 105 | + |
| 106 | + |
| 107 | +@eg043.route(f"/{eg}envelopes", methods=["GET"]) |
| 108 | +@ensure_manifest(manifest_url=DS_CONFIG["example_manifest_url"]) |
| 109 | +@authenticate_agent(eg=eg) |
| 110 | +def list_envelopes(): |
| 111 | + args = { |
| 112 | + "account_id": session["ds_account_id"], |
| 113 | + "base_path": session["ds_base_path"], |
| 114 | + "access_token": session["ds_access_token"], |
| 115 | + "user_id": session["principal_user_id"] |
| 116 | + } |
| 117 | + try: |
| 118 | + # 4. Retrieve the list of envelopes |
| 119 | + results = Eg043SharedAccessController.get_envelopes(args) |
| 120 | + except ApiException as err: |
| 121 | + return process_error(err) |
| 122 | + |
| 123 | + example = get_example_by_number(session["manifest"], example_number, api) |
| 124 | + |
| 125 | + if int(results.result_set_size) > 0: |
| 126 | + additional_page = next((p for p in example["AdditionalPage"] if p["Name"] == "list_status_successful"), None) |
| 127 | + return render_template( |
| 128 | + "example_done.html", |
| 129 | + title="Principal's envelopes visible in the agent's Shared Access UI", |
| 130 | + message=additional_page["ResultsPageText"], |
| 131 | + json=json.dumps(json.dumps(results.to_dict())) |
| 132 | + ) |
| 133 | + |
| 134 | + additional_page = next((p for p in example["AdditionalPage"] if p["Name"] == "list_status_unsuccessful"), None) |
| 135 | + return render_template( |
| 136 | + "example_done.html", |
| 137 | + title="No envelopes in the principal user's account", |
| 138 | + message=additional_page["ResultsPageText"] |
| 139 | + ) |
| 140 | + |
| 141 | + |
| 142 | +@eg043.route(f"/{eg}", methods=["GET"]) |
| 143 | +@ensure_manifest(manifest_url=DS_CONFIG["example_manifest_url"]) |
| 144 | +@authenticate(eg=eg, api=api) |
| 145 | +def get_view(): |
| 146 | + """responds with the form for the example""" |
| 147 | + example = get_example_by_number(session["manifest"], example_number, api) |
| 148 | + |
| 149 | + return render_template( |
| 150 | + "eSignature/eg043_shared_access.html", |
| 151 | + title=example["ExampleName"], |
| 152 | + example=example, |
| 153 | + source_file="eg043_shared_access.py", |
| 154 | + source_url=DS_CONFIG["github_example_url"] + "eg043_shared_access.py", |
| 155 | + documentation=DS_CONFIG["documentation"] + eg, |
| 156 | + show_doc=DS_CONFIG["documentation"], |
| 157 | + signer_name=DS_CONFIG["signer_name"], |
| 158 | + signer_email=DS_CONFIG["signer_email"] |
| 159 | + ) |
0 commit comments