@xc-ventures/xc-platform-documents v1.0.0
Documents
This is a python package to generate Google Docs and Sign Now documents.
Dev
Prerequisites
- Python 3
- Key for the Google Service Account (with access to Google Docs APIs). The json must be in root folder, named
key.json
- A Sign Now Sandbox account with an API key.
.env
file must be setup using.env.template
- We recommend to use a virtual environment for python. There are many different tools to do that, but here, we will demonstrate with VirtualEnv
How to get started
- Let's start by creating a new virtual environment in the root of this repo. You shouldn't have to execute that again, unless you reset it.
python -m venv venv
- To start the virtual env (macOS):
source venv/bin/activate
- Now it's time to install all the dependencies to run this project. You won't need to execute that again, unless you reset your virtual env or the requirements changed.
pip install -r requirements.txt
- To run the project
python start.py
Tests
To run the unit tests, run this command line in your virtual env.
python -m pytest
Tips
Inside the virtual env, if you want to quit it:
$ deactivate
Installation
Using pip
, it is possible to install the tools in this repo as a package so you can import them in other projects.
Prerequisites
- Same prerequisites as mentionned above
- Active SSH key pointing to repo with read access
How to install
pip install git+ssh://git@bitbucket.org/decksign/documents.git
You can specify a version using git tags. E.g.:
pip install git+ssh://git@bitbucket.org/decksig/documents.git@0.1.0
Templating Guidelines
The Google Doc templates we use with this repo must respect a certain nomenclature in order to work properly.
Variables
Variables must be defined with doubled curly brackets, like so:
Hello World, my name is {{first_name}}!
Here, first_name
would be the var name.
Signature
Signature tags are for Google Docs that will be uploaded to Sign Now. They represent a full signature. They must be defined with doubled brackets and a role, like so:
Signature:
[[signature:ceo]]
CEO
Here, the role would be ceo
.
Initials
Initials tags are for Google Docs that will be uploaded to Sign Now. They represent an initials. They must be defined with doubled brackets and a role, like so:
Initials:
[[initials:ceo]]
CEO
Here, the role would be ceo
.
Note about Signature and Initials
In the process to uploading it to Sign Now, this package will update the Google Doc to replace the tags with a line of the same size of the tag itself. It is possible to make the line bigger by adding "_
" chars following by a ":
" in the first part of the tag, like so:
[[_______:signature:ceo]]
[[________:initials:ceo]]
Documentation
Class Document
This class provides a few tools allowing you to manipulate Google Docs templates (enter data), copy, share it. You can also use this class to upload PDF version of that Google Doc to Sign Now and invite people to sign it.
from documents import Document
Constructor args
name | type | required | description |
---|---|---|---|
google_doc_id | str | No | The document ID of the Google Doc we want to fetch. If not present property google_doc will be None . |
signnow_doc_id | str | No | The document ID of the Sign Now Document we want to fetch. If not present property signnow_doc will be None . |
Properties
name | type | description |
---|---|---|
google_doc | GoogleDoc | The Google Doc instance. Default to None . |
signnow_doc | SignNowDocument | The Sign Now Document instance. Default to None . |
methods
name | return type | description | args |
---|---|---|---|
init_google_doc | None | Instanciate the property google_doc with the specified id. Will overwrite the instanciation done the constructor | google_doc_id (str) The document ID of the Google Doc |
init_signnow_doc | None | Instanciate the property signnow_doc with the specified id. Will overwrite the instanciation done the constructor | signnow_doc_id (str) The document ID of the Sign Now Document |
create_google_doc | None | Creates a new empty Google Doc to instanciate the property google_doc with | title (str) The title of the new Google Doc |
share | None | Gives writer (Read/Write) role to the specified users | recipients (Liststr) The emails of the users to give access to. |
move | None | Moves the google_doc into the specified folder. | folder_id (str) The Google Drive Folder ID to move the Google Doc to. |
lock | None | Updates the google_doc file to be Read-Only | |
unlock | None | Updates the google_doc file to disable the Read-Only mode | |
copy_with_data | GoogleDoc | Create a new Google Doc by copying google_doc and entering data | data (Dictstr, str) The vars and their value. title (str) The title of the new Google Doc. |
generate_signnow_doc | None | Updates the Google Doc to replace signature/initial tags with lines, upload PDF to Sign Now and update it to add signature/initial areas. Sets the signnow_doc with the new Sign Now Document | |
invite_signers | Dict | Send Sign Now invites to the specified emails. Returns a dict for email:user_id mapping. | invites (ListSignNowInvite) |
download | bytes | Downloads the instance of signnow_doc in PDF format (bytes) | |
get_preview | bytes | Downloads the instance of google_doc in PDF format (bytes) |
Class SignNowInvite
This class represents an invitation for a Sign Now document.
from documents.services.signnow.document import SignNowInvite
Constructor args
name | type | required | description |
---|---|---|---|
str | Yes | The email address of the signatory. | |
role | str | Yes | The signatory role in the Sign Now Document. |
order | int | No | The order the signatory must sign the file. E.g. Signatory 1 must sign before Signatory 2. Default to 1 . |
Properties
name | type | description |
---|---|---|
str | The email address of the signatory. | |
role | str | The signatory role in the Sign Now Document. |
order | int | The order the signatory must sign the file. E.g. Signatory 1 must sign before Signatory 2. Default to 1 . |
Class GoogleDoc
This class represents a Google Doc file.
from documents.services.google.doc import GoogleDoc
Constructor args
name | type | required | description |
---|---|---|---|
document_id | str | Yes | The file id of the Google Doc. |
Properties
name | type | description |
---|---|---|
document | json | The JSON of the whole Google Doc. |
batch_update_commands | List | The batch update commands awaiting to be executed. |
Methods
name | return type | description | args |
---|---|---|---|
get_id | str | Returns the file/document id of the Google Doc | |
get_title | str | Returns the title of the document (filename) | |
download_pdf | BytesIO | Converts the Google Doc in PDF format and downloads it | |
copy | GoogleDoc | Copy the Google Doc in the same folder | title (str) The title of the copy |
move | None | Moves the Google Doc in the specified folder | folder_id (str) The folder to move the Google Doc into |
share | None | Shares the Google Doc to the specified users with the writer role | recipients (Liststr) The emails of the users to share the Google Doc with |
lock | None | Updates the Google Doc to be read-only | |
unlock | None | Updates the Google Doc to disable the read-only mode | |
delete | None | Deletes the Google Doc | |
get_missing_vars | Liststr | Compares the required variables in the Google Doc and the given data to return the missing variables in the data in order to complete the document | data (Dictstr, str) var_name:value mapping |
is_data_valid_for_vars | bool | Compares the required variables in the Google Doc and the given data to return if the data is sufficient or not | data (Dictstr, str) var_name:value mapping |
Module documents.services.signnow.auth
Function verify_access_token
Verifies that the given access token is valid for your account. This is useful for webhooks, to verify that the request is legitimate (webhook callbacks contain an access_token
).
args
name | type | required | description |
---|---|---|---|
token | str | Yes | The access token to verify. |
Module documents.services.signnow.webhook
Function create_doc_completion_webhook
Creates an event subcription for a document completion (when all parties completed their signature). The webhook will call the URL given in the SIGNNOW_API_DOC_COMPLETION_CALLBACK_URL
env var when the event is triggered.
args
name | type | required | description |
---|---|---|---|
document_id | str | Yes | The Document ID to link the subcription to. |
Function create_doc_opening_webhook
Creates an event subcription for a document being opened. The webhook will call the URL given in the SIGNNOW_API_DOC_OPEN_CALLBACK_URL
env var when the event is triggered.
args
name | type | required | description |
---|---|---|---|
document_id | str | Yes | The Document ID to link the subcription to. |
Function create_doc_update_webhook
Creates an event subcription for a document being updated (including an user signing the doc). The webhook will call the URL given in the SIGNNOW_API_DOC_UPDATE_CALLBACK_URL
env var when the event is triggered.
args
name | type | required | description |
---|---|---|---|
document_id | str | Yes | The Document ID to link the subcription to. |
Function delete_webhook
Removes a webhook from your Sign Now account. This can be useful when the event subscription is no longer needed or is no longer possible.
args
name | type | required | description |
---|---|---|---|
event_subscription_id | str | Yes | The ID given of the response of the webhook's creation. |
Function list_webhooks
Lists all the webhooks of your Sign Now account.
Module documents.services.google.drive
Function copy_file
Duplicate the specified file. The copy will be in the same folder as the original.
args
name | type | required | description |
---|---|---|---|
file_id | str | Yes | The id of the file to copy. |
title | str | Yes | The file name of the copy. |
Function lock_file
Updates the specified file to be read-only.
args
name | type | required | description |
---|---|---|---|
file_id | str | Yes | The id of the file to be read-only. |
Function unlock_file
Updates the specified file to disable the Read-Only mode.
args
name | type | required | description |
---|---|---|---|
file_id | str | Yes | The id of the file to disable the Read-Only. |
Function delete_file
Deletes the specified file.
args
name | type | required | description |
---|---|---|---|
file_id | str | Yes | The id of the file to be deleted. |
Function move_file
Moves the specified file into the specified folder.
args
name | type | required | description |
---|---|---|---|
file_id | str | Yes | The id of the file to move. |
folder_id | str | Yes | The id of the folder to move the file into. |
Function create_folder
Creates a new folder inside the given folder.
args
name | type | required | description |
---|---|---|---|
file_id | str | Yes | The id of the folder to create the new folder into. |
Function share_file
Gives the specified role to the specified users.
args
name | type | required | description |
---|---|---|---|
file_id | str | Yes | The id of the file to give permissions to |
recipients | Liststr | Yes | The email of the users to give the role to |
role | str | Yes | The role to give the users. E.g.: writer |
Function upload_pdf
Upload a PDF file with the given name to the specified folder.
args
name | type | required | description |
---|---|---|---|
data | bytes | Yes | The bytes of the file PDF. |
name | str | Yes | The name of the file that will be in Google Drive. |
folder_id | str | Yes | The id of the folder. Your service account must be allowed to write into it. |
Function download_pdf
Downloads the specified file in PDF format. Returns the bytes.
args
name | type | required | description |
---|---|---|---|
file_id | str | Yes | The id of the file to download in PDF. Can be a Google Doc. |
1 year ago