diff --git a/app/eg024_brand_creating.py b/app/eg024_brand_creating.py
new file mode 100644
index 00000000..cb57a79e
--- /dev/null
+++ b/app/eg024_brand_creating.py
@@ -0,0 +1,165 @@
+"""Example 024: Creating a brand"""
+
+import json
+from os import path
+
+from docusign_esign import ApiClient, AccountsApi, Brand
+from docusign_esign.client.api_exception import ApiException
+from flask import render_template, url_for, redirect, session, flash, request
+
+from app import app, ds_config, views
+
+eg = 'eg024' # reference and url for this example
+
+LANGUAGES = {
+ "Arabic": "ar",
+ "Armenian": "hy",
+ "Bahasa Indonesia": "id",
+ "Bahasa Malay": "ms",
+ "Bulgarian": "bg",
+ "Chinese Simplified": "zh_CN",
+ "Chinese Traditional": "zh_TW",
+ "Croatian": "hr",
+ "Czech": "cs",
+ "Danish": "da",
+ "Dutch": "nl",
+ "English UK": "en_GB",
+ "English US": "en",
+ "Estonian": "et",
+ "Farsi": "fa",
+ "Finnish": "fi",
+ "French": "fr",
+ "French Canada": "fr_CA",
+ "German": "de",
+ "Greek": "el",
+ "Hebrew": "he",
+ "Hindi": "hi",
+ "Hungarian": "hu",
+ "Italian": "it",
+ "Japanese": "ja",
+ "Korean": "ko",
+ "Latvian": "lv",
+ "Lithuanian": "lt",
+ "Norwegian": "no",
+ "Polish": "pl",
+ "Portuguese": "pt",
+ "Portuguese Brasil": "pt_BR",
+ "Romanian": "ro",
+ "Russian": "ru",
+ "Serbian": "sr",
+ "Slovak": "sk",
+ "Slovenian": "sl",
+ "Spanish": "es",
+ "Spanish Latin America": "es_MX",
+ "Swedish": "sv",
+ "Thai": "th",
+ "Turkish": "tr",
+ "Ukrainian": "uk",
+ "Vietnamese": "vi"
+ }
+
+
+def controller():
+ """Controller router using the HTTP method"""
+ if request.method == "GET":
+ return get_controller()
+ elif request.method == "POST":
+ return create_controller()
+ else:
+ return render_template("404.html"), 404
+
+
+def create_controller():
+ """
+ 1. Check the token
+ 2. Call the worker method
+ 3. Render response
+ """
+ minimum_buffer_min = 3
+ if views.ds_token_ok(minimum_buffer_min):
+ # Step 1: Obtain your OAuth token
+ args = {
+ 'account_id': '213', # represent your {ACCOUNT_ID}
+ 'base_path': session['ds_base_path'],
+ 'access_token': session['ds_access_token'], # represent your {ACCESS_TOKEN}
+ 'brand_name': request.form.get('brand_name'),
+ 'default_language': request.form.get('default_language')
+ }
+ try:
+ # Step 2: Call the worker method to create a new brand
+ response = worker(args)
+ brand_id = response.brands[0].brand_id
+ app.logger.info(f"Brand has been created. Brand id {brand_id}")
+
+ # Step 3: Render response
+ return render_template('example_done.html',
+ title='Brand creating',
+ h1='Brand creating',
+ message=f"""The brand has been created and sent!
+ Brand ID {brand_id}."""
+ )
+
+ except ApiException as err:
+ error_body_json = err and hasattr(err, 'body') and err.body
+ # We can pull the DocuSign error code and message from the response body
+ error_body = json.loads(error_body_json)
+ error_code = error_body and 'errorCode' in error_body and error_body['errorCode']
+ error_message = error_body and "message" in error_body and error_body["message"]
+ # In production, you may want to provide customized error messages and
+ # remediation advice to the user
+ return render_template('error.html',
+ err=err,
+ error_code=error_code,
+ error_message=error_message
+ )
+
+ else:
+ flash("Sorry, you need to re-authenticate.")
+ # We could store the parameters of the requested operation so it could be restarted
+ # automatically. But since it should be rare to have a token issue here,
+ # we'll make the user re-enter the form data after authentication
+ session["eg"] = url_for(eg)
+ return redirect(url_for("ds_must_authenticate"))
+
+
+def worker(args):
+ """
+ 1. Create api client with headers
+ 2. Create a brand object
+ 3. Post the brand using SDK
+ """
+
+ # Step 1: create API client
+ api_client = ApiClient()
+ api_client.host = args['base_path']
+ api_client.set_default_header(header_name="Authorization", header_value=f"Bearer {args['access_token']}")
+
+ # Step 2: Create a brand object
+ brand = Brand(
+ brand_name=args['brand_name'],
+ default_brand_language=args['default_language'],
+ )
+
+ # Step 3: a) Call the eSignature SDK
+ # b) Display the JSON response
+ account_api = AccountsApi(api_client)
+ response = account_api.create_brand(account_id=args['account_id'], brand=brand)
+ return response
+
+
+def get_controller():
+ """Responds with the form for the example"""
+
+ if views.ds_token_ok():
+ return render_template("eg024_brand_creating.html",
+ title="Brand creating",
+ source_file=path.basename(__file__),
+ source_url=ds_config.DS_CONFIG["github_example_url"] + path.basename(__file__),
+ documentation=ds_config.DS_CONFIG["documentation"] + eg,
+ show_doc=ds_config.DS_CONFIG["documentation"],
+ languages=LANGUAGES
+ )
+ else:
+ # Save the current operation so it will be resumed after authentication
+ session["eg"] = url_for(eg)
+ return redirect(url_for("ds_must_authenticate"))
diff --git a/app/eg025_brands_apply_to_envelope.py b/app/eg025_brands_apply_to_envelope.py
new file mode 100644
index 00000000..a4f891f1
--- /dev/null
+++ b/app/eg025_brands_apply_to_envelope.py
@@ -0,0 +1,212 @@
+"""Example 025: Applying a brand to an envelope"""
+
+from flask import render_template, url_for, redirect, session, flash, request
+from os import path
+from app import app, ds_config, views
+import base64
+import json
+import re
+from docusign_esign import ApiClient, EnvelopesApi, Document, Signer, SignHere, EnvelopeDefinition, Tabs, Recipients, \
+ AccountsApi
+from docusign_esign.client.api_exception import ApiException
+
+eg = 'eg025' # reference and url for this example
+demo_docs_path = path.abspath(path.join(path.dirname(path.realpath(__file__)), "static/demo_documents"))
+doc_file = "World_Wide_Corp_lorem.pdf"
+
+
+def controller():
+ """Controller router using the HTTP method"""
+ if request.method == "GET":
+ return get_controller()
+ elif request.method == "POST":
+ return create_controller()
+ else:
+ return render_template("404.html"), 404
+
+
+def create_controller():
+ """
+ 1. Check the token
+ 2. Call the worker method
+ 3. Render response
+ """
+ minimum_buffer_min = 3
+ if views.ds_token_ok(minimum_buffer_min):
+
+ # More data validation would be a good idea here
+ # Strip anything other than the characters listed
+ pattern = re.compile("([^\w \-\@\.\,])+")
+ signer_email = pattern.sub("", request.form.get("signer_email"))
+ signer_name = pattern.sub("", request.form.get("signer_name"))
+
+ # Step 1: Obtain your OAuth token
+ args = {
+ 'account_id': session['ds_account_id'], # represent your {ACCOUNT_ID}
+ 'base_path': session['ds_base_path'],
+ 'access_token': session['ds_access_token'], # represent your {ACCESS_TOKEN}
+ 'envelope_args': {
+ 'signer_name': signer_name,
+ 'signer_email': signer_email,
+ 'brand_id': request.form.get('brand')
+ }
+
+ }
+ try:
+ # Step 2: Call the worker method to apply brand to the envelope
+ response = worker(args)
+ envelope_id = response.envelope_id
+ app.logger.info(f"Brand has been applied to envelope. Envelope id {envelope_id}")
+
+ # Step 3: Render a response
+ return render_template('example_done.html',
+ title='Brand applying to envelope',
+ h1='Brand applying to envelope',
+ message=f"""The brand has been applied to envelope!
+ Envelope ID: {envelope_id}."""
+ )
+
+ except ApiException as err:
+ error_body_json = err and hasattr(err, 'body') and err.body
+ # We can pull the DocuSign error code and message from the response body
+ error_body = json.loads(error_body_json)
+ error_code = error_body and 'errorCode' in error_body and error_body['errorCode']
+ error_message = error_body and "message" in error_body and error_body["message"]
+ # In production, you may want to provide customized error messages and
+ # remediation advice to the user
+ return render_template('error.html',
+ err=err,
+ error_code=error_code,
+ error_message=error_message
+ )
+
+ else:
+ flash("Sorry, you need to re-authenticate.")
+ # We could store the parameters of the requested operation so it could be restarted
+ # automatically. But since it should be rare to have a token issue here,
+ # we'll make the user re-enter the form data after authentication
+ session["eg"] = url_for(eg)
+ return redirect(url_for("ds_must_authenticate"))
+
+
+def worker(args):
+ """
+ 1. Call create_api_client method
+ 2. Call make_envelope method
+ 3. Apply the brand to the envelope using SDK
+ """
+
+ # Step 1: Call create_api_client method
+ api_client = create_api_client(args)
+
+ # Step 2: Call make_envelope method
+ envelope_api = EnvelopesApi(api_client)
+ envelope_definition = make_envelope(args['envelope_args'])
+
+ # Step 3: Apply the brand to the envelope using SDK
+ response = envelope_api.create_envelope(args['account_id'], envelope_definition=envelope_definition)
+
+ return response
+
+
+def create_api_client(args):
+ """Create api client and construct API headers"""
+ api_client = ApiClient()
+ api_client.host = args['base_path']
+ api_client.set_default_header(header_name="Authorization", header_value=f"Bearer {args['access_token']}")
+
+ return api_client
+
+
+def make_envelope(args):
+ """
+ Creates envelope
+ """
+
+ # Open example file
+ with open(path.join(demo_docs_path, doc_file), "rb") as file:
+ content_bytes = file.read()
+ base64_file_content = base64.b64encode(content_bytes).decode("ascii")
+
+ document = Document(
+ document_base64=base64_file_content,
+ name='lorem',
+ file_extension='pdf',
+ document_id=1
+ )
+
+ signer = Signer(
+ email=args['signer_email'],
+ name=args['signer_name'],
+ recipient_id="1",
+ routing_order="1"
+ )
+
+ sign_here = SignHere(
+ anchor_string='/sn1/',
+ anchor_units='pixels',
+ anchor_y_offset="572",
+ anchor_x_offset="75"
+ )
+
+ signer.tabs = Tabs(sign_here_tabs=[sign_here])
+
+ envelope_definition = EnvelopeDefinition(
+ email_subject='Please Sign',
+ documents=[document],
+ recipients=Recipients(signers=[signer]),
+ status='sent',
+ brand_id=args['brand_id'],
+ )
+
+ return envelope_definition
+
+
+def get_brands():
+ """Retrieve all brands using the AccountBrands::List"""
+ args = {
+ 'account_id': session['ds_account_id'], # represent your {ACCOUNT_ID}
+ 'base_path': session['ds_base_path'],
+ 'access_token': session['ds_access_token'], # represent your {ACCESS_TOKEN}
+
+ }
+ api_client = create_api_client(args)
+ try:
+ account_api = AccountsApi(api_client)
+ response = account_api.list_brands(args['account_id'])
+ return response.brands
+ except ApiException as err:
+ error_body_json = err and hasattr(err, 'body') and err.body
+ # We can pull the DocuSign error code and message from the response body
+ error_body = json.loads(error_body_json)
+ error_code = error_body and 'errorCode' in error_body and error_body['errorCode']
+ error_message = error_body and "message" in error_body and error_body["message"]
+ # In production, you may want to provide customized error messages and
+ # remediation advice to the user
+ return render_template('error.html',
+ err=err,
+ error_code=error_code,
+ error_message=error_message
+ )
+
+
+def get_controller():
+ """Responds with the form for the example"""
+ if views.ds_token_ok():
+ # Get all brands to render it in the template
+ brands = get_brands()
+
+ return render_template("eg025_brands_apply_to_envelope.html",
+ title="Applying a brand to a template",
+ source_file=path.basename(__file__),
+ source_url=ds_config.DS_CONFIG["github_example_url"] + path.basename(__file__),
+ documentation=ds_config.DS_CONFIG["documentation"] + eg,
+ show_doc=ds_config.DS_CONFIG["documentation"],
+ brands=brands,
+ signer_name=ds_config.DS_CONFIG["signer_name"],
+ signer_email=ds_config.DS_CONFIG["signer_email"]
+ )
+ else:
+ # Save the current operation so it will be resumed after authentication
+ session["eg"] = url_for(eg)
+ return redirect(url_for("ds_must_authenticate"))
diff --git a/app/eg026_brands_apply_to_template.py b/app/eg026_brands_apply_to_template.py
new file mode 100644
index 00000000..3cac66db
--- /dev/null
+++ b/app/eg026_brands_apply_to_template.py
@@ -0,0 +1,218 @@
+"""Example 026: Applying a brand to an envelope"""
+
+from flask import render_template, url_for, redirect, session, flash, request
+from os import path
+from app import app, ds_config, views
+import json
+import re
+from docusign_esign import ApiClient, EnvelopesApi, EnvelopeDefinition, TemplateRole, AccountsApi, TemplatesApi
+from docusign_esign.client.api_exception import ApiException
+
+eg = 'eg026' # reference and url for this example
+
+
+def controller():
+ """Controller router using the HTTP method"""
+ if request.method == "GET":
+ return get_controller()
+ elif request.method == "POST":
+ return create_controller()
+ else:
+ return render_template("404.html"), 404
+
+
+def create_controller():
+ """
+ 1. Check the token
+ 2. Call the worker method
+ 3. Render a response
+ """
+ minimum_buffer_min = 3
+ if views.ds_token_ok(minimum_buffer_min):
+
+ # More data validation would be a good idea here
+ # Strip anything other than the characters listed
+ pattern = re.compile("([^\w \-\@\.\,])+")
+ signer_email = pattern.sub("", request.form.get("signer_email"))
+ signer_name = pattern.sub("", request.form.get("signer_name"))
+ cc_email = request.form.get('cc_email')
+ cc_name = request.form.get('cc_name')
+
+ if cc_email and cc_name:
+ cc_email = pattern.sub("", cc_email)
+ cc_name = pattern.sub("", cc_name)
+
+ # Step 1: Obtain your OAuth token
+ args = {
+ 'account_id': session['ds_account_id'], # represent your {ACCOUNT_ID}
+ 'base_path': session['ds_base_path'],
+ 'access_token': session['ds_access_token'], # represent your {ACCESS_TOKEN}
+ 'envelope_args': {
+ 'signer_name': signer_name,
+ 'signer_email': signer_email,
+ 'cc_name': cc_name,
+ 'cc_email': cc_email,
+ 'brand_id': request.form.get('brand'),
+ 'template_id': request.form.get('envelope_template')
+ }
+ }
+ try:
+ # Step 2: Call the worker method to apply the brand to the template
+ response = worker(args)
+ envelope_id = response.envelope_id
+ app.logger.info(f"Brand has been applied to envelope. Envelope id {envelope_id}")
+
+ return render_template('example_done.html',
+ title='Brand applying to a template',
+ h1='Brand applying to a template',
+ message=f"""The brand has been applied to the template!
+ Envelope ID: {envelope_id}."""
+ )
+
+ except ApiException as err:
+ error_body_json = err and hasattr(err, 'body') and err.body
+ # We can pull the DocuSign error code and message from the response body
+ error_body = json.loads(error_body_json)
+ error_code = error_body and 'errorCode' in error_body and error_body['errorCode']
+ error_message = error_body and "message" in error_body and error_body["message"]
+ # In production, you may want to provide customized error messages and
+ # remediation advice to the user
+ return render_template('error.html',
+ err=err,
+ error_code=error_code,
+ error_message=error_message
+ )
+
+ else:
+ flash("Sorry, you need to re-authenticate.")
+ # We could store the parameters of the requested operation so it could be restarted
+ # automatically. But since it should be rare to have a token issue here,
+ # we'll make the user re-enter the form data after authentication
+ session["eg"] = url_for(eg)
+ return redirect(url_for("ds_must_authenticate"))
+
+
+def worker(args):
+ """
+ 1. Call create_api_client method
+ 2. Call make_envelope method
+ 3. Apply the brand to the envelope using SDK
+ """
+
+ # Step 1: Call create_api_client method
+ api_client = create_api_client(args)
+
+ # Step 2: Call make_envelope method to create an envelope definition with Signer
+ envelope_api = EnvelopesApi(api_client)
+ envelope_definition = make_envelope(args['envelope_args'])
+
+ # Step 3: Apply the brand to the envelope using SDK
+ response = envelope_api.create_envelope(args['account_id'], envelope_definition=envelope_definition)
+
+ return response
+
+
+def create_api_client(args):
+ """Create api client and construct API headers"""
+ api_client = ApiClient()
+ api_client.host = args['base_path']
+ api_client.set_default_header(header_name="Authorization", header_value=f"Bearer {args['access_token']}")
+
+ return api_client
+
+
+def make_envelope(args):
+ """
+ Creates envelope
+ args -- parameters for the envelope:
+ signer_email, signer_name, signer_client_id
+ returns an envelope definition
+ """
+ # Create the envelope definition
+ envelope_definition = EnvelopeDefinition(
+ status='sent',
+ template_id=args['template_id'],
+ brand_id=args['brand_id']
+ )
+
+ signer = TemplateRole(
+ email=args['signer_email'],
+ name=args['signer_name'],
+ role_name='signer'
+ )
+
+ # In case, we have cc we add him to envelope definition
+ if args['cc_email'] and args['cc_name']:
+ cc = TemplateRole(
+ email=args['cc_email'],
+ name=args['cc_name'],
+ role_name='cc'
+ )
+
+ envelope_definition.template_roles = [signer, cc]
+
+ else:
+ envelope_definition.template_roles = [signer]
+
+ return envelope_definition
+
+
+def get_data():
+ """Retrieve brands and envelope templates"""
+ args = {
+ 'account_id': session['ds_account_id'], # represent your {ACCOUNT_ID}
+ 'base_path': session['ds_base_path'],
+ 'access_token': session['ds_access_token'], # represent your {ACCESS_TOKEN}
+ }
+ api_client = create_api_client(args)
+
+ try:
+ """Retrieve all brands using the AccountBrands::List"""
+ account_api = AccountsApi(api_client)
+ brands = account_api.list_brands(args['account_id']).brands
+
+ """Retrieve all templates using the Templates::List"""
+ template_api = TemplatesApi(api_client)
+ envelope_templates = template_api.list_templates(args['account_id']).envelope_templates
+
+ return brands, envelope_templates
+
+ except ApiException as err:
+ error_body_json = err and hasattr(err, 'body') and err.body
+ # We can pull the DocuSign error code and message from the response body
+ error_body = json.loads(error_body_json)
+ error_code = error_body and 'errorCode' in error_body and error_body['errorCode']
+ error_message = error_body and "message" in error_body and error_body["message"]
+ # In production, you may want to provide customized error messages and
+ # remediation advice to the user
+ return render_template('error.html',
+ err=err,
+ error_code=error_code,
+ error_message=error_message
+ )
+
+
+def get_controller():
+ """Responds with the form for the example"""
+ if views.ds_token_ok():
+ brands, templates = get_data()
+ return render_template("eg026_brands_apply_to_template.html",
+ title="Applying a brand to a template",
+ source_file=path.basename(__file__),
+ source_url=ds_config.DS_CONFIG["github_example_url"] + path.basename(__file__),
+ documentation=ds_config.DS_CONFIG["documentation"] + eg,
+ show_doc=ds_config.DS_CONFIG["documentation"],
+ brands=brands,
+ templates=templates,
+ signer_name=ds_config.DS_CONFIG["signer_name"],
+ signer_email=ds_config.DS_CONFIG["signer_email"],
+ )
+ else:
+ # Save the current operation so it will be resumed after authentication
+ session["eg"] = url_for(eg)
+ return redirect(url_for("ds_must_authenticate"))
+
+
+
+
+
diff --git a/app/templates/eg024_brand_creating.html b/app/templates/eg024_brand_creating.html
new file mode 100644
index 00000000..e8656b82
--- /dev/null
+++ b/app/templates/eg024_brand_creating.html
@@ -0,0 +1,46 @@
+ {% extends "base.html" %} {% block content %}
+
+
+ The brand includes a Brand Name Configure Brands +
++ This code example demonstrates how to create a brand. +
+ +{% if show_doc %} +Documentation about this example.
+{% endif %} + ++ API method used: + AccountBrands::create. +
+ ++ View source file {{ source_file }} on GitHub. +
+ + + +{% endblock %} diff --git a/app/templates/eg025_brands_apply_to_envelope.html b/app/templates/eg025_brands_apply_to_envelope.html new file mode 100644 index 00000000..50f47600 --- /dev/null +++ b/app/templates/eg025_brands_apply_to_envelope.html @@ -0,0 +1,56 @@ + {% extends "base.html" %} {% block content %} + ++ The envelope includes a pdf document. +
+ ++ This code example demonstrates how to apply a brand to an envelope. +
+ + {% if show_doc %} +Documentation about this example.
+ {% endif %} + ++ Api method used: + Envelopes::Create and + AccountBrands::List +
+ ++ View source file {{ source_file }} on GitHub. +
+ + +{% endblock %} \ No newline at end of file diff --git a/app/templates/eg026_brands_apply_to_template.html b/app/templates/eg026_brands_apply_to_template.html new file mode 100644 index 00000000..c4b706f7 --- /dev/null +++ b/app/templates/eg026_brands_apply_to_template.html @@ -0,0 +1,78 @@ + {% extends "base.html" %} {% block content %} + ++ This code example demonstrates how to apply a brand to a template. +
+ + {% if show_doc %} +Documentation about this example.
+ {% endif %} + ++ Api method used: + Envelopes::Create, + AccountBrands::List and + Templates::List +
+ ++ View source file {{ source_file }} on GitHub. +
+ + +{% endblock %} \ No newline at end of file diff --git a/app/templates/home.html b/app/templates/home.html index 6db4eabf..0db818a6 100644 --- a/app/templates/home.html +++ b/app/templates/home.html @@ -231,6 +231,39 @@+ Creating a brand +
++ API method used: + AccountBrands::create. +
+ ++ Applies brand to an envelop +
++ API method used: + Envelopes::Create and + AccountBrands::List +
+ ++ Applies brand to a template +
++ API method used: + Envelopes::Create, + AccountBrands::List and + Templates::List +
+ diff --git a/app/views.py b/app/views.py index 70912997..dc7e2f00 100644 --- a/app/views.py +++ b/app/views.py @@ -16,7 +16,9 @@ eg016_set_tab_values, eg017_set_template_tab_values, \ eg018_envelope_custom_field_data, eg019_access_code_authentication, \ eg020_sms_authentication, eg021_phone_authentication, \ - eg022_kba_authentication, eg023_idv_authentication + eg022_kba_authentication, eg023_idv_authentication, \ + eg024_brand_creating, eg025_brands_apply_to_envelope, \ + eg026_brands_apply_to_template @app.route("/") @@ -149,6 +151,21 @@ def eg023(): return eg023_idv_authentication.controller() +@app.route("/eg024", methods=["GET", "POST"]) +def eg024(): + return eg024_brand_creating.controller() + + +@app.route("/eg025", methods=["GET", "POST"]) +def eg025(): + return eg025_brands_apply_to_envelope.controller() + + +@app.route("/eg026", methods=["GET", "POST"]) +def eg026(): + return eg026_brands_apply_to_template.controller() + + @app.route("/ds_return") def ds_return(): event = request.args.get("event")