|
| 1 | +import base64 |
| 2 | +from flask import session, request |
| 3 | +from os import path |
| 4 | +from docusign_esign import EnvelopesApi, TemplatesApi, EnvelopeDefinition, Document, Signer, SignHere, \ |
| 5 | + DateSigned, Tabs, Recipients, DocGenFormField, EnvelopeTemplate, TemplateRole, DocGenFormFields, \ |
| 6 | + DocGenFormFieldRequest, Envelope |
| 7 | + |
| 8 | +from ...consts import demo_docs_path, pattern |
| 9 | +from ...ds_config import DS_CONFIG |
| 10 | +from ...docusign import create_api_client |
| 11 | + |
| 12 | + |
| 13 | +class Eg042DocumentGenerationController: |
| 14 | + @staticmethod |
| 15 | + def get_args(): |
| 16 | + """Get request and session arguments""" |
| 17 | + envelope_args = { |
| 18 | + "candidate_email": pattern.sub("", request.form.get("candidate_email")), |
| 19 | + "candidate_name": pattern.sub("", request.form.get("candidate_name")), |
| 20 | + "manager_name": pattern.sub("", request.form.get("manager_name")), |
| 21 | + "job_title": pattern.sub("", request.form.get("job_title")), |
| 22 | + "salary": pattern.sub("", request.form.get("salary")), |
| 23 | + "start_date": pattern.sub("", request.form.get("start_date")), |
| 24 | + "doc_file": path.join(demo_docs_path, DS_CONFIG["doc_offer_letter"]) |
| 25 | + } |
| 26 | + args = { |
| 27 | + "account_id": session["ds_account_id"], |
| 28 | + "base_path": session["ds_base_path"], |
| 29 | + "access_token": session["ds_access_token"], |
| 30 | + "envelope_args": envelope_args |
| 31 | + } |
| 32 | + return args |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def worker(cls, args): |
| 36 | + """ |
| 37 | + 1. Create the template |
| 38 | + 2. Update template document |
| 39 | + 3. Update recipient tabs |
| 40 | + 4. Create draft envelope |
| 41 | + 5. Get the document id |
| 42 | + 6. Merge the data fields |
| 43 | + 7. Send the envelope |
| 44 | + """ |
| 45 | + |
| 46 | + api_client = create_api_client(base_path=args["base_path"], access_token=args["access_token"]) |
| 47 | + templates_api = TemplatesApi(api_client) |
| 48 | + envelopes_api = EnvelopesApi(api_client) |
| 49 | + |
| 50 | + account_id = args["account_id"] |
| 51 | + envelope_args = args["envelope_args"] |
| 52 | + |
| 53 | + # Step 2a start |
| 54 | + template_data = cls.make_template() |
| 55 | + template = templates_api.create_template(account_id, envelope_template=template_data) |
| 56 | + template_id = template.template_id |
| 57 | + |
| 58 | + # Step 2a end |
| 59 | + |
| 60 | + # Update template document |
| 61 | + # Step 3a start |
| 62 | + document_id = '1' |
| 63 | + templates_api.update_document( |
| 64 | + account_id, document_id, template_id, |
| 65 | + envelope_definition=cls.template_document(envelope_args) |
| 66 | + ) |
| 67 | + # Step 3a end |
| 68 | + |
| 69 | + # Update recipient tabs |
| 70 | + # Step 4a start |
| 71 | + recipient_id = '1' |
| 72 | + templates_api.create_tabs( |
| 73 | + account_id, recipient_id, template_id, |
| 74 | + template_tabs=cls.recipient_tabs() |
| 75 | + ) |
| 76 | + # Step 4a end |
| 77 | + |
| 78 | + # Create draft envelope |
| 79 | + # Step 5a start |
| 80 | + envelope_definition = cls.make_envelope(template_id, envelope_args) |
| 81 | + envelope = envelopes_api.create_envelope(account_id, envelope_definition=envelope_definition) |
| 82 | + envelope_id = envelope.envelope_id |
| 83 | + # Step 5a end |
| 84 | + |
| 85 | + # Get the document id |
| 86 | + # Step 6 start |
| 87 | + doc_gen_form_fields_response = envelopes_api.get_envelope_doc_gen_form_fields(account_id, envelope_id) |
| 88 | + document_id_guid = doc_gen_form_fields_response.doc_gen_form_fields[0].document_id |
| 89 | + # Step 6 end |
| 90 | + |
| 91 | + # Merge the data fields |
| 92 | + # Step 7a start |
| 93 | + form_fields = cls.form_fields(envelope_args, document_id_guid) |
| 94 | + envelopes_api.update_envelope_doc_gen_form_fields( |
| 95 | + account_id, |
| 96 | + envelope_id, |
| 97 | + doc_gen_form_field_request=form_fields |
| 98 | + ) |
| 99 | + # Step 7a end |
| 100 | + |
| 101 | + # Send the envelope |
| 102 | + # Step 8 start |
| 103 | + send_envelope_req = Envelope(status="sent") |
| 104 | + envelope = envelopes_api.update(account_id, envelope_id, envelope=send_envelope_req) |
| 105 | + # Step 8 end |
| 106 | + return envelope |
| 107 | + |
| 108 | + # Step 2b start |
| 109 | + @classmethod |
| 110 | + def make_template(cls): |
| 111 | + # Create recipient |
| 112 | + signer = Signer( |
| 113 | + role_name="signer", |
| 114 | + recipient_id="1", |
| 115 | + routing_order="1", |
| 116 | + ) |
| 117 | + recipients = Recipients( |
| 118 | + signers=[signer] |
| 119 | + ) |
| 120 | + |
| 121 | + # Create the envelope template model |
| 122 | + template_request = EnvelopeTemplate( |
| 123 | + name="Example document generation template", |
| 124 | + description="Example template created via the API", |
| 125 | + email_subject="Please sign this document", |
| 126 | + shared="false", |
| 127 | + recipients=recipients, |
| 128 | + status="created" |
| 129 | + ) |
| 130 | + return template_request |
| 131 | + |
| 132 | + # Step 2b end |
| 133 | + |
| 134 | + # Step 3b start |
| 135 | + @classmethod |
| 136 | + def template_document(cls, args): |
| 137 | + with open(args["doc_file"], "rb") as file: |
| 138 | + content_bytes = file.read() |
| 139 | + base64_file_content = base64.b64encode(content_bytes).decode("ascii") |
| 140 | + |
| 141 | + # Create the document model |
| 142 | + document = Document( |
| 143 | + document_base64=base64_file_content, |
| 144 | + name="OfferLetterDemo.docx", |
| 145 | + file_extension="docx", |
| 146 | + document_id=1, |
| 147 | + order=1, |
| 148 | + pages=1 |
| 149 | + ) |
| 150 | + |
| 151 | + envelope_definition = EnvelopeDefinition( |
| 152 | + documents=[document] |
| 153 | + ) |
| 154 | + return envelope_definition |
| 155 | + # Step 3b end |
| 156 | + |
| 157 | + # Step 4b start |
| 158 | + @classmethod |
| 159 | + def recipient_tabs(cls): |
| 160 | + # Create tabs |
| 161 | + sign_here = SignHere( |
| 162 | + anchor_string="Employee Signature", |
| 163 | + anchor_units="pixels", |
| 164 | + anchor_x_offset="5", |
| 165 | + anchor_y_offset="-22" |
| 166 | + ) |
| 167 | + date_signed = DateSigned( |
| 168 | + anchor_string="Date", |
| 169 | + anchor_units="pixels", |
| 170 | + anchor_y_offset="-22" |
| 171 | + ) |
| 172 | + tabs = Tabs( |
| 173 | + sign_here_tabs=[sign_here], |
| 174 | + date_signed_tabs=[date_signed] |
| 175 | + ) |
| 176 | + return tabs |
| 177 | + # Step 4b end |
| 178 | + |
| 179 | + # Step 5b start |
| 180 | + @classmethod |
| 181 | + def make_envelope(cls, template_id, args): |
| 182 | + # Create the signer model |
| 183 | + signer = TemplateRole( |
| 184 | + email=args["candidate_email"], |
| 185 | + name=args["candidate_name"], |
| 186 | + role_name="signer" |
| 187 | + ) |
| 188 | + |
| 189 | + # Create the envelope model |
| 190 | + envelope_definition = EnvelopeDefinition( |
| 191 | + template_roles=[signer], |
| 192 | + status="created", |
| 193 | + template_id=template_id |
| 194 | + ) |
| 195 | + return envelope_definition |
| 196 | + # Step 5b end |
| 197 | + |
| 198 | + # Step 7b start |
| 199 | + @classmethod |
| 200 | + def form_fields(cls, args, document_id_guid): |
| 201 | + doc_gen_form_field_request = DocGenFormFieldRequest( |
| 202 | + doc_gen_form_fields=[ |
| 203 | + DocGenFormFields( |
| 204 | + document_id=document_id_guid, |
| 205 | + doc_gen_form_field_list=[ |
| 206 | + DocGenFormField( |
| 207 | + name="Candidate_Name", |
| 208 | + value=args["candidate_name"] |
| 209 | + ), |
| 210 | + DocGenFormField( |
| 211 | + name="Manager_Name", |
| 212 | + value=args["manager_name"] |
| 213 | + ), |
| 214 | + DocGenFormField( |
| 215 | + name="Job_Title", |
| 216 | + value=args["job_title"] |
| 217 | + ), |
| 218 | + DocGenFormField( |
| 219 | + name="Salary", |
| 220 | + value=args["salary"] |
| 221 | + ), |
| 222 | + DocGenFormField( |
| 223 | + name="Start_Date", |
| 224 | + value=args["start_date"] |
| 225 | + ) |
| 226 | + ] |
| 227 | + ) |
| 228 | + ] |
| 229 | + ) |
| 230 | + return doc_gen_form_field_request |
| 231 | + # Step 7b end |
0 commit comments