|
| 1 | +""" Example 045: Delete and undelete an Envelope """ |
| 2 | + |
| 3 | +fromosimportpath |
| 4 | + |
| 5 | +fromdocusign_esign.client.api_exceptionimportApiException |
| 6 | +fromflaskimportrender_template,session,Blueprint,request,redirect |
| 7 | + |
| 8 | +from ..examples.eg045_delete_restore_envelopeimportEg045DeleteRestoreEnvelopeController |
| 9 | +from ..utilsimportget_folder_id_by_name |
| 10 | +from ...docusignimportauthenticate,ensure_manifest,get_example_by_number |
| 11 | +from ...ds_configimportDS_CONFIG |
| 12 | +from ...error_handlersimportprocess_error |
| 13 | +from ...constsimportpattern,API_TYPE |
| 14 | + |
| 15 | +example_number=45 |
| 16 | +api=API_TYPE["ESIGNATURE"] |
| 17 | +eg=f"eg0{example_number}"# reference (and url) for this example |
| 18 | +restore_endpoint=f"{eg}restore" |
| 19 | +delete_folder_id="recyclebin" |
| 20 | +restore_folder_id="sentitems" |
| 21 | +eg045=Blueprint(eg,__name__) |
| 22 | + |
| 23 | +@eg045.route(f"/{eg}",methods=["POST"]) |
| 24 | +@authenticate(eg=eg,api=api) |
| 25 | +@ensure_manifest(manifest_url=DS_CONFIG["example_manifest_url"]) |
| 26 | +defdelete_envelope(): |
| 27 | +""" |
| 28 | + 1. Get required arguments |
| 29 | + 2. Call the worker method |
| 30 | + 3. Render success response |
| 31 | + """ |
| 32 | + |
| 33 | +# 1. Get required arguments |
| 34 | +args= { |
| 35 | +"account_id":session["ds_account_id"], |
| 36 | +"base_path":session["ds_base_path"], |
| 37 | +"access_token":session["ds_access_token"], |
| 38 | +"envelope_id":pattern.sub("",request.form.get("envelope_id")), |
| 39 | +"delete_folder_id":delete_folder_id |
| 40 | + } |
| 41 | +try: |
| 42 | +# 2. Call the worker method |
| 43 | +Eg045DeleteRestoreEnvelopeController.delete_envelope(args) |
| 44 | +exceptApiExceptionaserr: |
| 45 | +returnprocess_error(err) |
| 46 | + |
| 47 | +session["envelope_id"]=args["envelope_id"]# Save for use by second part of example |
| 48 | + |
| 49 | +# 3. Render success response |
| 50 | +example=get_example_by_number(session["manifest"],example_number,api) |
| 51 | +additional_page_data=next( |
| 52 | + (pforpinexample["AdditionalPage"]ifp["Name"]=="envelope_is_deleted"), |
| 53 | +None |
| 54 | + ) |
| 55 | +returnrender_template( |
| 56 | +"example_done.html", |
| 57 | +title=example["ExampleName"], |
| 58 | +message=additional_page_data["ResultsPageText"].format(args["envelope_id"]), |
| 59 | +redirect_url=restore_endpoint |
| 60 | + ) |
| 61 | + |
| 62 | +@eg045.route(f"/{restore_endpoint}",methods=["POST"]) |
| 63 | +@authenticate(eg=eg,api=api) |
| 64 | +@ensure_manifest(manifest_url=DS_CONFIG["example_manifest_url"]) |
| 65 | +defrestore_envelope(): |
| 66 | +""" |
| 67 | + 1. Get required arguments |
| 68 | + 2. Call the worker method |
| 69 | + 3. Render success response |
| 70 | + """ |
| 71 | + |
| 72 | +# 1. Get required arguments |
| 73 | +folder_name=pattern.sub("",request.form.get("folder_name")) |
| 74 | +args= { |
| 75 | +"account_id":session["ds_account_id"], |
| 76 | +"base_path":session["ds_base_path"], |
| 77 | +"access_token":session["ds_access_token"], |
| 78 | +"envelope_id":pattern.sub("",session.get("envelope_id")), |
| 79 | +"from_folder_id":delete_folder_id |
| 80 | + } |
| 81 | + |
| 82 | +example=get_example_by_number(session["manifest"],example_number,api) |
| 83 | +try: |
| 84 | +# 2. Call the worker method |
| 85 | +folders=Eg045DeleteRestoreEnvelopeController.get_folders(args) |
| 86 | +args["folder_id"]=get_folder_id_by_name(folders.folders,folder_name) |
| 87 | + |
| 88 | +ifargs["folder_id"]isNone: |
| 89 | +additional_page_data=next( |
| 90 | + (pforpinexample["AdditionalPage"]ifp["Name"]=="folder_does_not_exist"), |
| 91 | +None |
| 92 | + ) |
| 93 | + |
| 94 | +returnrender_template( |
| 95 | +"example_done.html", |
| 96 | +title=example["ExampleName"], |
| 97 | +message=additional_page_data["ResultsPageText"].format(folder_name), |
| 98 | +redirect_url=restore_endpoint |
| 99 | + ) |
| 100 | + |
| 101 | +Eg045DeleteRestoreEnvelopeController.move_envelope_to_folder(args) |
| 102 | +exceptApiExceptionaserr: |
| 103 | +returnprocess_error(err) |
| 104 | + |
| 105 | +# 3. Render success response with envelopeId |
| 106 | +returnrender_template( |
| 107 | +"example_done.html", |
| 108 | +title=example["ExampleName"], |
| 109 | +message=example["ResultsPageText"].format(session.get("envelope_id",""),args["folder_id"],folder_name) |
| 110 | + ) |
| 111 | + |
| 112 | +@eg045.route(f"/{eg}",methods=["GET"]) |
| 113 | +@ensure_manifest(manifest_url=DS_CONFIG["example_manifest_url"]) |
| 114 | +@authenticate(eg=eg,api=api) |
| 115 | +defget_view(): |
| 116 | +"""responds with the form for the example""" |
| 117 | +example=get_example_by_number(session["manifest"],example_number,api) |
| 118 | + |
| 119 | +returnrender_template( |
| 120 | +"eSignature/eg045_delete_envelope.html", |
| 121 | +title=example["ExampleName"], |
| 122 | +example=example, |
| 123 | +envelope_id=session.get("envelope_id",""), |
| 124 | +submit_button_text=session["manifest"]["SupportingTexts"]["HelpingTexts"]["SubmitButtonDeleteText"], |
| 125 | +source_file="eg045_delete_restore_envelope.py", |
| 126 | +source_url=DS_CONFIG["github_example_url"]+"eg045_delete_restore_envelope.py", |
| 127 | +documentation=DS_CONFIG["documentation"]+eg, |
| 128 | +show_doc=DS_CONFIG["documentation"], |
| 129 | +signer_name=DS_CONFIG["signer_name"], |
| 130 | +signer_email=DS_CONFIG["signer_email"] |
| 131 | + ) |
| 132 | + |
| 133 | +@eg045.route(f"/{restore_endpoint}",methods=["GET"]) |
| 134 | +@ensure_manifest(manifest_url=DS_CONFIG["example_manifest_url"]) |
| 135 | +@authenticate(eg=eg,api=api) |
| 136 | +defget_restore_view(): |
| 137 | +"""responds with the form for the example""" |
| 138 | +example=get_example_by_number(session["manifest"],example_number,api) |
| 139 | + |
| 140 | +ifnotsession.get("envelope_id"): |
| 141 | +returnredirect(eg) |
| 142 | + |
| 143 | +returnrender_template( |
| 144 | +"eSignature/eg045_restore_envelope.html", |
| 145 | +title=example["ExampleName"], |
| 146 | +example=example, |
| 147 | +envelope_id=session.get("envelope_id"), |
| 148 | +submit_button_text=session["manifest"]["SupportingTexts"]["HelpingTexts"]["SubmitButtonRestoreText"], |
| 149 | +source_file="eg045_delete_restore_envelope.py", |
| 150 | +source_url=DS_CONFIG["github_example_url"]+"eg045_delete_restore_envelope.py", |
| 151 | +documentation=DS_CONFIG["documentation"]+eg, |
| 152 | +show_doc=DS_CONFIG["documentation"], |
| 153 | +signer_name=DS_CONFIG["signer_name"], |
| 154 | +signer_email=DS_CONFIG["signer_email"] |
| 155 | + ) |