|
| 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 |
| 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": pattern.sub("", 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.new_users[0].user_id |
| 40 | + |
| 41 | + example = get_example_by_number(session["manifest"], example_number, api) |
| 42 | + return render_template( |
| 43 | + "eSignature/eg043_shared_access/eg043_shared_access_agent_created.html", |
| 44 | + title="Agent user created", |
| 45 | + message=example["ResultsPageText"], |
| 46 | + json=json.dumps(json.dumps(results.to_dict())) |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +@eg043.route(f"/{eg}auth", methods=["GET"]) |
| 51 | +@authenticate(eg=eg, api=api) |
| 52 | +@ensure_manifest(manifest_url=DS_CONFIG["example_manifest_url"]) |
| 53 | +def create_authorization(): |
| 54 | + args = { |
| 55 | + "account_id": session["ds_account_id"], |
| 56 | + "base_path": session["ds_base_path"], |
| 57 | + "access_token": session["ds_access_token"], |
| 58 | + "user_id": DS_JWT["ds_impersonated_user_id"], |
| 59 | + "agent_user_id": session["agent_user_id"] |
| 60 | + } |
| 61 | + try: |
| 62 | + # 2. Create the authorization for agent user |
| 63 | + Eg043SharedAccessController.create_authorization(args) |
| 64 | + except ApiException as err: |
| 65 | + return process_error(err) |
| 66 | + |
| 67 | + example = get_example_by_number(session["manifest"], example_number, api) |
| 68 | + additional_page_data = next((p for p in example["AdditionalPage"] if p["Name"] == "authenticate_as_agent"), None) |
| 69 | + return render_template( |
| 70 | + "eSignature/eg043_shared_access/eg043_shared_access_reauthenticate.html", |
| 71 | + title="Authenticate as the agent", |
| 72 | + message=additional_page_data["ResultsPageText"] |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +@eg043.route(f"/{eg}reauthenticate", methods=["GET"]) |
| 77 | +def reauthenticate(): |
| 78 | + # 3. Logout principal user and redirect to page with the list of envelopes, login as agent user |
| 79 | + ds_logout_internal() |
| 80 | + current_app.config["isLoggedIn"] = False |
| 81 | + return redirect(url_for(f"{eg}.list_envelopes")) |
| 82 | + |
| 83 | + |
| 84 | +@eg043.route(f"/{eg}envelopes", methods=["GET"]) |
| 85 | +@ensure_manifest(manifest_url=DS_CONFIG["example_manifest_url"]) |
| 86 | +@authenticate_agent(eg=eg) |
| 87 | +def list_envelopes(): |
| 88 | + args = { |
| 89 | + "account_id": session["ds_account_id"], |
| 90 | + "base_path": session["ds_base_path"], |
| 91 | + "access_token": session["ds_access_token"], |
| 92 | + "user_id": DS_JWT["ds_impersonated_user_id"] |
| 93 | + } |
| 94 | + try: |
| 95 | + # 4. Retrieve the list of envelopes |
| 96 | + results = Eg043SharedAccessController.get_envelopes(args) |
| 97 | + except ApiException as err: |
| 98 | + return process_error(err) |
| 99 | + |
| 100 | + example = get_example_by_number(session["manifest"], example_number, api) |
| 101 | + |
| 102 | + if int(results.result_set_size) > 0: |
| 103 | + additional_page = next((p for p in example["AdditionalPage"] if p["Name"] == "list_status_successful"), None) |
| 104 | + return render_template( |
| 105 | + "example_done.html", |
| 106 | + title="Principal's envelopes visible in the agent's Shared Access UI", |
| 107 | + message=additional_page["ResultsPageText"], |
| 108 | + json=json.dumps(json.dumps(results.to_dict())) |
| 109 | + ) |
| 110 | + |
| 111 | + additional_page = next((p for p in example["AdditionalPage"] if p["Name"] == "list_status_unsuccessful"), None) |
| 112 | + return render_template( |
| 113 | + "example_done.html", |
| 114 | + title="No envelopes in the principal user's account", |
| 115 | + message=additional_page["ResultsPageText"] |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +@eg043.route(f"/{eg}", methods=["GET"]) |
| 120 | +@ensure_manifest(manifest_url=DS_CONFIG["example_manifest_url"]) |
| 121 | +@authenticate(eg=eg, api=api) |
| 122 | +def get_view(): |
| 123 | + """responds with the form for the example""" |
| 124 | + example = get_example_by_number(session["manifest"], example_number, api) |
| 125 | + |
| 126 | + return render_template( |
| 127 | + "eSignature/eg043_shared_access/eg043_shared_access.html", |
| 128 | + title=example["ExampleName"], |
| 129 | + example=example, |
| 130 | + source_file="eg043_shared_access.py", |
| 131 | + source_url=DS_CONFIG["github_example_url"] + "eg043_shared_access.py", |
| 132 | + documentation=DS_CONFIG["documentation"] + eg, |
| 133 | + show_doc=DS_CONFIG["documentation"], |
| 134 | + signer_name=DS_CONFIG["signer_name"], |
| 135 | + signer_email=DS_CONFIG["signer_email"] |
| 136 | + ) |
0 commit comments