Below are several code examples of how to perform common actions.
All examples assume that they are to be used in an operation or code where the variable 'fil' is the one containing the document on which the operation is executed.
Basic attributes of a document
author = fil.author # Returns an object of type User
document_type = fil.doctype #Returns an object of type DocumentType
space = fil.serie # Returns an object of type Series (Space)
creation_date = fil.creation_date
modification_date = fil.modified
last_contributor = fil.modified_by
lifecycle_state = fil.life_cycle_state # Returns a State object of the life cycle type
state = fil.state # This is a string about the processing status of the document.
locked = fil.locked # A Boolean that indicates whether the document is locked.
size = fil.size
Get information from a metadata of type User
# The following code shows how to get the user that is assigned
# to a field of type 'User'.
from django.contrib.auth.models import User
username = fil.gmv('metadata.example_user')
if username:
user = User.objects.get(username=username)
# The user object is already in the user variable.
# Now we can access some of its elements
teams = user.get_teams()
groups = user.get_groups()
spaces = user.get_series()
queues = user.get_queues()
email = user.email
given_name = user.given_name
last_name = user.last_name
Access the title of a document and modify it.
# The following code shows how to access and modify the title of a document.
title = fil.filename
title = title + 'example title modification'.
fil.filename = title
# The modification is then saved, indicating which field has been modified.
fil.save(update_fields=['filename'])
Operate with metadata and features
# The following code shows examples of how to operate with metadata and features
value = fil.gmv('metadata.name') # To obtain the value of a metadata with name 'metadata.name'.
fil.set_metadata('metadata.name', value, overwrite=True) # To save the value of a metadata
metadata = fil.get_metadata('metadata.name') # To obtain the metadata, returns an object of type Metadata
value = metadata.value
file_value = metadata.file_value
date_value = metadata.date_value
datetime_value = metadata.datetime_value
metadata_creation_date = metadata.creation_date
value = fil.gfv('feature.name') # To obtain a feature
fil.set_feature('feature.name', value) # To save a feature
Operating with the life cycle
# Modify life cycle status
fil.change_life_cycle_state(New State)
# The value of 'New Status' must be the name of an existing metadata in the life cycle.
# The document must have an active life cycle.
# Examples of life cycle information
fil.get_life_cycle_start_date()
fil.get_life_cycle_end_date()
fil.get_life_cycle_history()
fil.get_time_in_life_cycle_state()
fil.get_max_time_in_life_cycle_state()
fil.get_time_left_in_life_cycle_state()
# Check if a lifecycle task is complete
fil.check_workflow_tasks_completed('Task name')
Operating with comments
# Add a comment to a document.
# The first parameter is a variable of type User
fil.add_comment(fil.author, 'mi comentario')
Operating with relationships
# Add document as related to another
#
doc_hijo = File.objects.get(filename='Document child')
doc_padre = File.objects.get(filename='Parent document')
doc_padre.add_related(doc_child)
Create document
# Add document as related to another
#
doc = File.objects.get(filename='Document')
new_doc = doc.serie.create_document(binary_path, 'Title', 'TypeForm')
Operating with folders
# The following code shows how to create a folder inside another folder
folder_type = DocumentType.objects.get(uuid='986983f9-1bc0-4e23-99b7-c0d62ad60ec1')
# folder_type would be the document type that is marked with facet 'folderish'.
customer_space = Series.objects.get(uuid='626c4ef6-5420-4e9a-994b-6bed6409bb8a')
folder_2018 = espacio_clientes.create_document(filename='Clientes 2018', doctype=tipo_carpeta)
# The newly created folder is passed as a parent
folder_2018_January = customer_space.create_document(filename='January 2018', doctype=folder_type, parent=folder_2018)
folder_2018_January.add_related(fil) # Add fil as related 'son' de folder_2018_January
To create a normal folder (no custom form type), use the line:
# The following line creates a normal folder in a space/series
folder = serie.create_folder('Folder title', author) # where author is a variable of type User
# To create a folder inside another folder
serie.create_folder('Subfolder title', author, folder)
Obtain other document information
# The following examples show how to get entities related to the document.
metadatas = fil.get_all_metadatas() # metadatas will contain a list of Metadata objects.
related_documents = fil.get_all_relations() # related_documents will contain the list of documents related to the current one.
features = fil.get_all_features()
doctypes = fil.get_required_doctypes() # Get a list of required DocumentType, as configured in the DocumentType of this document.
parent = fil.get_parent() # Get the parent folder of a document
#
fil.get_extension() # Returns the extension that the document has in its name
fil.get_mime() # Calculates the mime type of the document
fil.get_creation_date_timezone() # Get creation date in service time zone
fil.get_versions() # Returns a list of versions DocumentVersion
fil.get_content() # Get the content (binary) of a document
# Ejemplos de otras acciones sobre documentos.
notification_name = 'example_notification' # Es nombre de la notificación a enviar
fil.send_notification(notification_name, 'ejemplo@athento.com')
fil.generate_from_template() # Genera el documento desde la plantilla que esté configurada en el tipo documental
fil.create_version() # Crea version del documento
fil.has_content() # Devuelve True o False dependiendo de si el documento tiene contenido o no.
# To query the content type
fil.is_pdf()
fil.is_image()
fil.is_txt()
# To get other documents that are duplicated according to the set of fields marked as Unique.
fil.is_duplicated()
fil.days_old() # Number of days since document created
fil.minutes_old() # Number of minutes since the document was created
fil.days_old_modified() # Number of days since the document was modified
fil.minutes_old_modified() # Number of minutes since document modified
Obtain information from an e-mail type document
# When the document is an email, these are several useful methods.
fil.is_mail() # Indicate if the document is an email
fil.get_mail_from() # Get the originating mail
fil.get_mail_subject() # Get subject line
fil.get_mail_body() # Get the body (flat)
fil.get_mail_message_date() # Get received date
fil.get_mail_text() # Get content in text format
fil.get_mail_html() # Get the content in HTML format
Filter documents for a period of time
Sometimes it is necessary from an operation to search for documents that have a certain amount of time, this can be done in the following way:
# We need to apply the specific filter to start from a group of documents
fils = File.objects.filter(serie__uuid='XXXX')
fils.older_than(10) # Documents created more than 10 days ago
fils.last_x_days(30) # Documents created in the last 30 days
fils.last_x_hours(12) # Documents created in the last 12 hours
fils.last_x_minutes(10) # Documents created in the last 10 minutes
fils.validated_last_x_days(30) # Documents validated in the last 30 days
fils.validated_last_x_hours(12) # Documents validated in the last 12 hours
fils.validated_last_x_minutes(10) # Documents validated in the last 10 minutes
fils.modified_last_x_days(30) # Documents modified in the last 30 days
fils.modified_last_x_hours(12) # Documents modified in last 12 hours
fils.modified_last_x_minutes(10) # Documents modified in the last 10 minutes
state = State.objects.get(name='Approved')
fils.last_x_days_in_state(state, 10) # Documents that have been in Approved state for 10 days
fils.life_cycle_changed_last_x_minutes(10) # Documents whose life cycle has changed in last 10 minutes
Throwing an exception
Operations are launched sequentially and, when there are several of them, any system exception is caught by Athento to avoid interrupting the execution of all operations.
However, sometimes it may be necessary to raise an exception and have the user see the error message, in this case it should be done as shown in the example:
from core.exceptions import AthentoseError
raise AthentoseError('Could not execute operation X')
Mastertables
Sometimes it is useful to obtain a dictionary from mastertables, for that, you can use a code like this one:
from mastertables import mastertables
mt = mastertables.MasterTablesClient("API_KEY_MASTERTABLES")
vocabulario = mt.get_vocabulary("UUID_VOCABULARIO")
In this way, we can have an operation whose behavior will be in accordance with the vocabulary that business users can manage.
Comments
0 comments
Please sign in to leave a comment.