def get_view(): r = py_001_embedded_signing.get_view() redirect_url = ds_authentication.reauthenticate_check(r, ds_recipe_lib.get_base_url()) if redirect_url: return redirect(redirect_url) if r["err"]: flash(r["err"]) return redirect(ds_recipe_lib.get_base_url(2)) else: return redirect(r["redirect_url"])
def return_url(): r = py_012_embedded_tagging.return_url() if r["err"]: flash(r["err"]) return redirect(ds_recipe_lib.get_base_url(2)) else: return render_template('generic_sent.html', title='Embedded Tagging--Python', data=r, base_url=ds_recipe_lib.get_base_url(2))
def index(): r = py_001_embedded_signing.send() redirect_url = ds_authentication.reauthenticate_check(r, ds_recipe_lib.get_base_url()) if redirect_url: return redirect(redirect_url) if r["err"]: flash(r["err"]) return redirect(ds_recipe_lib.get_base_url(2)) else: return render_template('generic_sent.html', title='Embedded Signing--Python', data=r, base_url=ds_recipe_lib.get_base_url(2))
def index(): r = py_007_envelope_recipient_status_lib.start() redirect_url = ds_authentication.reauthenticate_check(r, ds_recipe_lib.get_base_url()) if redirect_url: return redirect(redirect_url) if r["err"]: flash(r["err"]) return redirect(ds_recipe_lib.get_base_url(2)) else: return render_template('generic_show_response.html', title='EnvelopeRecipients: list--Python', data=r, base_url=ds_recipe_lib.get_base_url(2))
def index(): r = py_012_embedded_tagging.send() redirect_url = ds_authentication.reauthenticate_check(r, ds_recipe_lib.get_base_url()) if redirect_url: return redirect(redirect_url) if r["err"]: flash(r["err"]) return redirect(ds_recipe_lib.get_base_url(2)) else: return render_template('generic_sent.html', title='Embedded Tagging--Python', data=r, base_url=ds_recipe_lib.get_base_url(2))
def get_view(): r = py_012_embedded_tagging.get_view() redirect_url = ds_authentication.reauthenticate_check(r, ds_recipe_lib.get_base_url()) if redirect_url: return redirect(redirect_url) if r["err"]: flash(r["err"]) return redirect(ds_recipe_lib.get_base_url(2)) else: return redirect(r["redirect_url"])
def get_doc(): r = py_001_embedded_signing.get_doc() redirect_url = ds_authentication.reauthenticate_check(r, ds_recipe_lib.get_base_url()) if redirect_url: return redirect(redirect_url) if r["err"]: flash(r["err"]) return redirect(ds_recipe_lib.get_base_url(2)) else: response = make_response(r["pdf"]) response.headers['Content-Type'] = 'application/pdf' response.headers['Content-Disposition'] = 'inline; filename={}.pdf'.format(r['filename']) return response
def get_view(): """Obtains a sending view from DocuSign. The user will then be redirected to the view url Uses the information stored in the session to request the view. Query parameter: send = 0 or 1. See https://goo.gl/aLNjJH RETURNS {err, redirect_url} """ err = False # No problems so far! auth = ds_authentication.get_auth() if auth["err"]: return {"err": auth["err"], "err_code": auth["err_code"]} if not embedded_tagging_key in session: return { "err": "Embedded signing information missing from session! Please re-send." } embedding_info = session[embedded_tagging_key] # Obtain the "sender's view" # See https://docs.docusign.com/esign/restapi/Envelopes/EnvelopeViews/createSender/ return_url = ds_recipe_lib.get_base_url(2) + return_uri data = {"returnUrl": return_url} send = request.args.get('send') # append "/envelopes/{envelopeId}/views/sender" to the baseUrl and use in the request url = auth["base_url"] + '/envelopes/{}/views/sender?send={}'.format( embedding_info["envelopeId"], send) ds_headers = { 'Accept': 'application/json', auth["auth_header_key"]: auth["auth_header_value"], trace_key: trace_value } try: r = requests.post(url, headers=ds_headers, json=data) except requests.exceptions.RequestException as e: return {'err': "Error calling EnvelopeViews:createSender: " + str(e)} status = r.status_code if (status != 201): return ({ 'err': "Error calling DocuSign EnvelopeViews:createSender<br/>Status is: " + str(status) + ". Response: <pre><code>" + r.text + "</code></pre>" }) data = r.json() redirect_url = data['url'] # Update the send parameter in the url # An example url: # https://demo.docusign.net/Member/StartInSession.aspx?StartConsole=1&t=2fABCXYZ6197&DocuEnvelope=2dABCXYZ&send=1 # We search for send=0|1 and replace it per our incoming "send" parameter send_re = re.compile("(\&send=[01])") redirect_url = send_re.sub("&send={}".format(request.args.get('send')), redirect_url) return {"err": err, "redirect_url": redirect_url}
def status_page(envelope_id): """Information for the status (notifications) page Returns: err: False or an error msg envelope: The envelope status info received ds_params: JSON string format for use by the Javascript on the page. {status_envelope_id, url} # url is the base url for this app """ if 'auth' in session: auth = session['auth'] if not auth["authenticated"]: return {"err": "Please authenticate with DocuSign."} else: return {"err": "Please authenticate with DocuSign."} # Calls Envelopes: get. See https://docs.docusign.com/esign/restapi/Envelopes/Envelopes/get/ # Calls GET /accounts/{accountId}/envelopes/{envelopeId} url = auth["base_url"] + "/envelopes/{}".format(envelope_id) ds_headers = {'Accept': 'application/json', auth["auth_header_key"]: auth["auth_header_value"], trace_key: trace_value} try: r = requests.get(url, headers=ds_headers) except requests.exceptions.RequestException as e: return {'err': "Error calling Templates:list: " + str(e)} status = r.status_code if (status != 200): return ({'err': "Error calling DocuSign Envelopes:get<br/>Status is: " + str(status) + ". Response: <pre><code>" + r.text + "</code></pre>"}) ds_params = json.dumps( {"status_envelope_id": envelope_id, "url": ds_recipe_lib.get_base_url(2)}) return {"err": False, "envelope": r.json(), "ds_params": ds_params}
def process_new_log_entries(): """Process the new log entries in the temp dir. Returns the entries""" entries = [] temp_log_path = get_temp_log_path() log_path = get_log_path() time = datetime.datetime.now() time = time.replace(microsecond=0) # clear the microseconds time = time.isoformat() # Convert to string. Eg '2016-07-17T10:24:03' time = "T" + time.replace( ':', '_') # substitute _ for : for windows-land eg. 'T2016-07-17T10_24_03' log_path_url = ds_recipe_lib.get_base_url( 1) + "/" + log_storage_uri + "/" + account_id_to_dir( auth["account_id"]) + "/" # Walk the dir for i in os.listdir(temp_log_path): if i.endswith(".txt"): new_file_name = time + "__" + i new_path = os.path.join(log_path, new_file_name) os.rename(os.path.join(temp_log_path, i), new_path) entries.append(api_log_item(new_path, new_file_name, log_path_url)) return entries
def return_url(): r = py_012_embedded_tagging.return_url() if r["err"]: flash(r["err"]) return redirect(ds_recipe_lib.get_base_url(2)) else: return render_template('generic_sent.html', title='Embedded Tagging--Python', data=r, base_url=ds_recipe_lib.get_base_url(2))
def logs_list(): """Returns log_entries for all current logs that were previously downloaded {"err": an error or false "entries": Array of log_entry log_entry: {file_name: the filename of the entry url: for retrieving the file head: first 1500 bytes of the entry, base64 encoded} } Strategy: parse the log files on the clients. """ global auth auth = ds_authentication.get_auth() if auth["err"]: return {"err": auth["err"], "err_code": auth["err_code"]} entries = [] log_path = get_log_path() log_path_url = ds_recipe_lib.get_base_url( 1) + "/" + log_storage_uri + "/" + account_id_to_dir( auth["account_id"]) + "/" # Walk the dir for i in os.listdir(log_path): if i.endswith(".txt"): entries.append( api_log_item(os.path.join(log_path, i), i, log_path_url)) return {"err": False, "entries": entries}
def get_view(): """Obtains a view from DocuSign. The user will then be redirected to the view url Uses the information stored in the session to request the view. RETURNS {err, redirect_url} """ err = False # No problems so far! auth = ds_authentication.get_auth() if auth["err"]: return {"err": auth["err"], "err_code": auth["err_code"]} if not embedded_signing_key in session: return { "err": "Embedded signing information missing from session! Please re-send." } embedding_info = session[embedded_signing_key] # Obtain the "recipient's view" (In this case, its the signer's view) # See https://docs.docusign.com/esign/restapi/Envelopes/EnvelopeViews/createRecipient/ return_url = ds_recipe_lib.get_base_url(2) + return_uri data = { "authenticationMethod": "Password", # How was this recipient authenticated. Pick from list of values "clientUserId": embedding_info["clientUserId"], "email": embedding_info["email"], "userName": embedding_info["name"], "returnUrl": return_url } # append "/envelopes/{envelopeId}/views/recipient" to the baseUrl and use in the request url = auth["base_url"] + '/envelopes/{}/views/recipient'.format( embedding_info["envelopeId"]) ds_headers = { 'Accept': 'application/json', auth["auth_header_key"]: auth["auth_header_value"], trace_key: trace_value } try: r = requests.post(url, headers=ds_headers, json=data) except requests.exceptions.RequestException as e: return { 'err': "Error calling EnvelopeViews:createRecipient: " + str(e) } status = r.status_code if (status != 201): return ({ 'err': "Error calling DocuSign EnvelopeViews:createRecipient<br/>Status is: " + str(status) + ". Response: <pre><code>" + r.text + "</code></pre>" }) data = r.json() redirect_url = data['url'] return {"err": err, "redirect_url": redirect_url}
def logs_download(): r = ds_api_logging.logs_download() redirect_url = ds_authentication.reauthenticate_check(r, ds_recipe_lib.get_base_url(1) + "/logging_page") if redirect_url: r["err"] = "Please authenticate" r["err_code"] = "PLEASE_REAUTHENTICATE" r["redirect_url"] = redirect_url return jsonify(r)
def status_page(): """Information for the status (notifications) page Returns: ds_params: JSON string format for use by the Javascript on the page. {status_envelope_id, url} # url is the base url for this app """ ds_params = json.dumps({"url": ds_recipe_lib.get_base_url(1)}) return {"ds_params": ds_params}
def logs_list(): r = ds_api_logging.logs_list() redirect_url = ds_authentication.reauthenticate_check( r, ds_recipe_lib.get_base_url(1) + "/logging_page") if redirect_url: r["err"] = "Please authenticate" r["err_code"] = "PLEASE_REAUTHENTICATE" r["redirect_url"] = redirect_url return jsonify(r)
def send(): r = py_002_email_send_template_lib.send() redirect_url = ds_authentication.reauthenticate_check(r, ds_recipe_lib.get_base_url()) if redirect_url: return redirect(redirect_url) if r["err"]: flash(r["err"]) return redirect(url_for('.index')) # Note: redirect to this recipe's index page/form if there's a problem else: return render_template('generic_sent.html', title='Send Template--Python', data=r, base_url=ds_recipe_lib.get_base_url(2))
def webhook_default(): """Initialize the webook settings. If on Heroku, then default to enabled""" on_heroku = heroku_env in os.environ if on_heroku: webhook = { 'enabled': True, 'status': 'yes', 'url_begin': ds_recipe_lib.get_base_url(1), 'url_end': webhook_path, 'listener_url': ds_recipe_lib.get_base_url(1) + webhook_path } else: webhook = { 'enabled': False, 'status': 'ask', 'url_begin': ds_recipe_lib.get_base_url(1), 'url_end': webhook_path, 'listener_url': False } return webhook
def webhook_default(): """Initialize the webook settings. For this app, we always need the webhook on... """ webhook = { 'enabled': False, # For this app, we just receive webhook data. # The webhook subscription is started manually so we don't need the info 'status': 'no', 'url_begin': ds_recipe_lib.get_base_url(1), 'url_end': webhook_path, 'listener_url': False } return webhook
def webhook_default(): """Initialize the webook settings. If on Heroku, then default to enabled""" on_heroku = heroku_env in os.environ if on_heroku: webhook = { 'enabled': True, 'status': 'yes', 'url_begin': ds_recipe_lib.get_base_url(1), 'url_end': webhook_path, 'listener_url': ds_recipe_lib.get_base_url(1) + webhook_path } else: webhook = { 'enabled': False, 'status': 'ask', 'url_begin': ds_recipe_lib.get_base_url(1), 'url_end': webhook_path, 'listener_url': False } return webhook
def webhook_instructions(envelope_id): """ Instructions for reading the notifications""" html = ("<h3>1. View the incoming notifications and documents</h3>" + "<p><a href='" + ds_recipe_lib.get_base_url(2) + "/webhook_status_page/" + envelope_id + "'" + " class='btn btn-primary wh' role='button' target='_blank'>" + "View Webhook Notifications</a> (A new tab/window will be used.)</p>") webhook = session['webhook'] if not webhook['enabled']: return False else: return html
def webhook_instructions(envelope_id): """ Instructions for reading the notifications""" html = ( "<h3>1. View the incoming notifications and documents</h3>" + "<p><a href='" + ds_recipe_lib.get_base_url(2) + "/webhook_status_page/" + envelope_id + "'" + " class='btn btn-primary wh' role='button' target='_blank'>" + "View Webhook Notifications</a> (A new tab/window will be used.)</p>") webhook = session['webhook'] if not webhook['enabled']: return False else: return html
def status_items(envelope_id): """List of info about the envelope's event items that were received""" files_dir_url = ds_recipe_lib.get_base_url(2) + "/static/files/" + envelope_id_to_dir(envelope_id) env_dir = get_envelope_dir(envelope_id) results = [] if (not os.path.isdir(env_dir)): return results # early return. for i in os.listdir(env_dir): if i.endswith(".xml"): results.append(status_item(os.path.join(env_dir, i), i, files_dir_url)) continue return results
def get_view(): """Obtains a sending view from DocuSign. The user will then be redirected to the view url Uses the information stored in the session to request the view. Query parameter: send = 0 or 1. See https://goo.gl/aLNjJH RETURNS {err, redirect_url} """ err = False # No problems so far! auth = ds_authentication.get_auth() if auth["err"]: return {"err": auth["err"], "err_code": auth["err_code"]} if not embedded_tagging_key in session: return {"err": "Embedded signing information missing from session! Please re-send."} embedding_info = session[embedded_tagging_key] # Obtain the "sender's view" # See https://docs.docusign.com/esign/restapi/Envelopes/EnvelopeViews/createSender/ return_url = ds_recipe_lib.get_base_url(2) + return_uri data = {"returnUrl": return_url} send = request.args.get('send') # append "/envelopes/{envelopeId}/views/sender" to the baseUrl and use in the request url = auth["base_url"] + '/envelopes/{}/views/sender?send={}'.format( embedding_info["envelopeId"], send) ds_headers = {'Accept': 'application/json', auth["auth_header_key"]: auth["auth_header_value"], trace_key: trace_value} try: r = requests.post(url, headers=ds_headers, json=data) except requests.exceptions.RequestException as e: return {'err': "Error calling EnvelopeViews:createSender: " + str(e)} status = r.status_code if (status != 201): return ({'err': "Error calling DocuSign EnvelopeViews:createSender<br/>Status is: " + str(status) + ". Response: <pre><code>" + r.text + "</code></pre>"}) data = r.json() redirect_url = data['url'] # Update the send parameter in the url # An example url: # https://demo.docusign.net/Member/StartInSession.aspx?StartConsole=1&t=2fABCXYZ6197&DocuEnvelope=2dABCXYZ&send=1 # We search for send=0|1 and replace it per our incoming "send" parameter send_re = re.compile("(\&send=[01])") redirect_url = send_re.sub("&send={}".format(request.args.get('send')), redirect_url) return {"err": err, "redirect_url": redirect_url}
def status_items(envelope_id): """List of info about the envelope's event items that were received""" files_dir_url = ds_recipe_lib.get_base_url( 2) + "/static/files/" + envelope_id_to_dir(envelope_id) env_dir = get_envelope_dir(envelope_id) results = [] if (not os.path.isdir(env_dir)): return results # early return. for i in os.listdir(env_dir): if i.endswith(".xml"): results.append( status_item(os.path.join(env_dir, i), i, files_dir_url)) continue return results
def get_view(): """Obtains a view from DocuSign. The user will then be redirected to the view url Uses the information stored in the session to request the view. RETURNS {err, redirect_url} """ err = False # No problems so far! auth = ds_authentication.get_auth() if auth["err"]: return {"err": auth["err"], "err_code": auth["err_code"]} if not embedded_signing_key in session: return {"err": "Embedded signing information missing from session! Please re-send."} embedding_info = session[embedded_signing_key] # Obtain the "recipient's view" (In this case, its the signer's view) # See https://docs.docusign.com/esign/restapi/Envelopes/EnvelopeViews/createRecipient/ return_url = ds_recipe_lib.get_base_url(2) + return_uri data = {"authenticationMethod": "Password", # How was this recipient authenticated. Pick from list of values "clientUserId": embedding_info["clientUserId"], "email": embedding_info["email"], "userName": embedding_info["name"], "returnUrl": return_url } # append "/envelopes/{envelopeId}/views/recipient" to the baseUrl and use in the request url = auth["base_url"] + '/envelopes/{}/views/recipient'.format(embedding_info["envelopeId"]) ds_headers = {'Accept': 'application/json', auth["auth_header_key"]: auth["auth_header_value"], trace_key: trace_value} try: r = requests.post(url, headers=ds_headers, json=data) except requests.exceptions.RequestException as e: return {'err': "Error calling EnvelopeViews:createRecipient: " + str(e)} status = r.status_code if (status != 201): return ({'err': "Error calling DocuSign EnvelopeViews:createRecipient<br/>Status is: " + str(status) + ". Response: <pre><code>" + r.text + "</code></pre>"}) data = r.json() redirect_url = data['url'] return {"err": err, "redirect_url": redirect_url}
def status_items(): """List of info about the envelope event items that were received""" auth = session["auth"] account_id = auth["account_id"] files_dir_url = ds_recipe_lib.get_base_url( 2) + "/static/files/" + account_id_to_dir(account_id) account_dir = get_account_dir(account_id) results = [] if (not os.path.isdir(account_dir)): return results # early return. for i in os.listdir(account_dir): if i.endswith(".xml"): results.append( status_item(os.path.join(account_dir, i), i, files_dir_url)) continue return results
def status_page(envelope_id): """Information for the status (notifications) page Returns: err: False or an error msg envelope: The envelope status info received ds_params: JSON string format for use by the Javascript on the page. {status_envelope_id, url} # url is the base url for this app """ if 'auth' in session: auth = session['auth'] if not auth["authenticated"]: return {"err": "Please authenticate with DocuSign."} else: return {"err": "Please authenticate with DocuSign."} # Calls Envelopes: get. See https://docs.docusign.com/esign/restapi/Envelopes/Envelopes/get/ # Calls GET /accounts/{accountId}/envelopes/{envelopeId} url = auth["base_url"] + "/envelopes/{}".format(envelope_id) ds_headers = { 'Accept': 'application/json', auth["auth_header_key"]: auth["auth_header_value"], trace_key: trace_value } try: r = requests.get(url, headers=ds_headers) except requests.exceptions.RequestException as e: return {'err': "Error calling Templates:list: " + str(e)} status = r.status_code if (status != 200): return ({ 'err': "Error calling DocuSign Envelopes:get<br/>Status is: " + str(status) + ". Response: <pre><code>" + r.text + "</code></pre>" }) ds_params = json.dumps({ "status_envelope_id": envelope_id, "url": ds_recipe_lib.get_base_url(2) }) return {"err": False, "envelope": r.json(), "ds_params": ds_params}
def auth_redirect(): err = ds_authentication.auth_redirect() # err is False or an error message # We will use the Flash technique to show the message on the home page. # Or a simpler alternative would be to show the error message on an intermediate # page, with a "Continue" link to the home page if err: flash(err) # flash("Debug info: " + str(request.headers)) # Authentication / re-authentication was successful # Figure out what to do next if "auth_redirect" in session: auth_redirect = session["auth_redirect"] if auth_redirect: session["auth_redirect"] = False return redirect(auth_redirect) return redirect(ds_recipe_lib.get_base_url(1))
def auth_redirect(): err = ds_authentication.auth_redirect() # err is False or an error message # We will use the Flash technique to show the message on the home page. # Or a simpler alternative would be to show the error message on an intermediate # page, with a "Continue" link to the home page if err: flash(err) # flash("Debug info: " + str(request.headers)) # Authentication / re-authentication was successful # Figure out what to do next if "auth_redirect" in session: auth_redirect = session["auth_redirect"] if auth_redirect: session["auth_redirect"] = False return redirect(auth_redirect) return redirect(ds_recipe_lib.get_base_url(1))
def r_index(): return redirect(ds_recipe_lib.get_base_url(1))
def oauth_force_reauthenticate(): session["oauth_force_re_auth"] = True flash("OAuth will be forced to re-authenticate") return redirect(ds_recipe_lib.get_base_url(1))
def delete_logs(): r = ds_api_logging.delete_logs() if r["err"]: flash(r["err"]) return redirect(ds_recipe_lib.get_base_url(1) + "/logging_page")
def index(): return render_template('home.html', title='Home - Python Recipes', base_url=ds_recipe_lib.get_base_url(0))
def logging_page(): return render_template('log_status_page.html', title='API Logging', base_url=ds_recipe_lib.get_base_url(1))
def r_index(): return redirect(ds_recipe_lib.get_base_url(1))
def delete_logs(): r = ds_api_logging.delete_logs() if r["err"]: flash(r["err"]) return redirect(ds_recipe_lib.get_base_url(1) + "/logging_page")
def oauth_force_reauthenticate(): session["oauth_force_re_auth"] = True flash( "OAuth will be forced to re-authenticate with your next request to DocuSign" ) return redirect(ds_recipe_lib.get_base_url(1))
def index(): return render_template('py_002_form_1.html', title='Template name--Python', base_url=ds_recipe_lib.get_base_url(2))
def webhook_status_page(envelope_id): r = ds_webhook.status_page(envelope_id) return render_template('webhook_status_page.html', title='Notifications - Webhook--Python', data=r, base_url=ds_recipe_lib.get_base_url(2))
def index(): return render_template('home.html', title='Home - Python Recipes', base_url=ds_recipe_lib.get_base_url(0))
def logging_page(): return render_template('log_status_page.html', title='API Logging', base_url=ds_recipe_lib.get_base_url(1))
def webhook_status_page(envelope_id): r = ds_webhook.status_page(envelope_id) return render_template('webhook_status_page.html', title='Notifications - Webhook--Python', data=r, base_url=ds_recipe_lib.get_base_url(2))
def oauth_force_reauthenticate(): session["oauth_force_re_auth"] = True flash("OAuth will be forced to re-authenticate with your next request to DocuSign") return redirect(ds_recipe_lib.get_base_url(1))