1.1. Module database¶
The py_cerebro.database module contains descriptions of classes used to access the database.
Classes
-
class
py_cerebro.database.
Database
(db_host, db_port, db_timeout=5, db_reconn_count=3)[source]¶ Parameters: The Database class is used for connection to the database, it contains a set of methods, executing Cerebro standard queries, and enables custom SQL queries.
# Устанавливаем соединение с базой данных if db.connect_from_cerebro_client() != 0: # Пробуем установить соединение с помощью запущенного клиента Cerebro. # Если не выходит, устанавливаем соединение с помощью логина и пароля db.connect('user', 'password')
Note
This class has functions that change task properties, which can take ID array as input. If it is required to set similar properties to several tasks, it is preferable to use ID array as an argument instead of using cycles for increased performance.
# Использование массивов идентификаторов to_do_task_list = db.to_do_task_list(db.current_user_id(), True) # получаем список задач текущего пользователя tsks = set() for task in to_do_task_list: tsks.add(task[dbtypes.TASK_DATA_ID]) db.task_set_priority(tsks, dbtypes.TASK_PRIORITY_ABOVE_NORMAL) # установили сразу нескольким задачам приоритет выше обычного
Methods
activities()
add_attachment()
add_client_review()
add_definition()
add_note()
add_report()
add_resource_report()
add_review()
add_task()
attachment_hashtags()
attachment_remove_hashtags()
attachment_set_hashtags()
connect()
connect_from_cerebro_client()
copy_tasks()
current_user_id()
drop_link_tasks()
execute()
message()
message_attachments()
message_hashtags()
message_remove_hashtags()
message_set_hashtags()
messages()
project_tags()
root_tasks()
set_link_tasks()
statuses()
tag_enums()
task()
task_allocated()
task_attachments()
task_by_url()
task_children()
task_definition()
task_hashtags()
task_links()
task_messages()
task_possible_statuses()
task_remove_allocated()
task_remove_hashtags()
task_set_activity()
task_set_allocated()
task_set_budget()
task_set_finish()
task_set_flag()
task_set_hashtags()
task_set_name()
task_set_planned_time()
task_set_priority()
task_set_progress()
task_set_start()
task_set_status()
task_set_tag_enum()
task_set_tag_float()
task_set_tag_int()
task_set_tag_string()
task_tag_enums()
task_tag_reset()
task_tags()
tasks()
to_do_task_list()
users()
-
activities
()[source]¶ Returns: table of activities. The table fields are described in the module dbtypes:
ACTIVITY_DATA_...
-
add_attachment
(message_id, carga, filename, thumbnails, description, as_link)[source]¶ Parameters: - message_id (int) – message ID.
- carga (py_cerebro.cargador.Cargador) – object of class
cargador.Cargador
, to import files to a file storage. - filename (string) – full path to file.
- thumbnails – a list of paths to thumbnail files (3 max). Thumbnail size must be 512x512 pixels. Format - JPG or PNG.
- description (string) – comments to the attachment.
- as_link (bool) – method of file attachment to the message: True - file is attached as a link; False - file is imported physically to a file storage.
Attaching a file to a message.
Using for thumbnail generation.
If a file is a picture or a video, it may have thumbnails to display in Cerebro forum. A video file may have up to 3 thumnails (the first frame, the middle frame or the last frame). By default thumbnails are generated by Mirada player, bundled with Cerebro.
#An example of generating thumbnails with Mirada. gen_path = os.path.dirname(filename) # Selecting the folder with the attached file as the folder to save thumbnails in mirada_path = './mirada' # path to Mirada executable # Executing Mirada with necessary keys res_code = subprocess.call([mirada_path, filename, '-temp', gen_path, '-hide']) #-temp - folder to generate thumbnails to #-hide - key to launch Mirada in hidden mode (without GUI) to generate thumbnails. if res_code != 0: raise Exception("Mirada returned bad exit-status.\n" + mirada_path); #Searching for thumbnails generated by Mirada. #Thumbnail filename is composed from the source file name, date and time of creation - filename_yyyymmdd_hhmmss_thumb[number].jpg #For example: test.mov_20120305_112354_thumb1.jpg - the thumbnail of the first frame of the test.mov videofile thumbnails = list() for f in os.listdir(gen_path): if fnmatch.fnmatch(f, os.path.basename(filename) + '_*_thumb?.jpg'): thumbnails.append(gen_path + '/' + f) thumbnails.sort()
Beside Mirada, some other software, for instance, ffmpeg, can be used to generate thumbnails.
#An example of generating thumbnails with ffmpeg. #Prior to generating thumbnails with ffmpeg it is necessary to resolve the duration of video, #in order to calculate the middle and the last frame correctly. #Let's take, for example, a 30-second long video. thumbnails = list() # file list for thumbnails thumbnails.append(filename + '_thumb1.jpg') thumbnails.append(filename + '_thumb2.jpg') thumbnails.append(filename + '_thumb3.jpg') subprocess.call(['ffmpeg', '-i', filename, '-s', '512x512', '-an', '-ss', '00:00:00', '-r', 1, '-vframes', 1, '-y', thumbnails[0]]) subprocess.call(['ffmpeg', '-i', filename, '-s', '512x512', '-an', '-ss', '15:00:00', '-r', 1, '-vframes', 1, '-y', thumbnails[1]]) subprocess.call(['ffmpeg', '-i', filename, '-s', '512x512', '-an', '-ss', '30:00:00', '-r', 1, '-vframes', 1, '-y', thumbnails[2]]) # The key descriptions can be found in ffmpeg documentation
-
add_client_review
(task_id, message_id, html_text)[source]¶ Parameters: Returns: new message ID.
Return type: Adds a message of “Client Review” type.
-
add_definition
(task_id, html_text)[source]¶ Parameters: Returns: new message ID.
Return type: Adds a message of “Definition” type.
-
add_note
(task_id, message_id, html_text)[source]¶ Parameters: Returns: new message ID.
Return type: Adds a message of “Note” type.
-
add_report
(task_id, message_id, html_text, minutes)[source]¶ Parameters: Returns: new message ID.
Return type: It’s very important to set minutes for report. If minutes 0 or None report will not be added to statistic.
Adds a message of “Report” type.
-
add_resource_report
(task_id, message_id, resource_id, html_text, minutes)[source]¶ Parameters: Returns: new message ID.
Return type: Adds a message of “Resource Report” type.
-
add_review
(task_id, message_id, html_text, minutes=None)[source]¶ Parameters: Returns: new message ID.
Return type: Adds a message of “Review” type.
-
add_task
(parent_id, name, activity_id=0)[source]¶ Parameters: Returns: new task ID.
New task creation. The symbols: \ / # : ? & ‘ ” , + | cannot be used in a task name.
Note
To send a notice to the user about the new task, you must create
the "Defenition" type message
in the task.
Parameters: attachment_id (int, set(int, ) or list(int, )) – attachment ID or array of attachment IDs. Gets the attachment hashtags.
Note
Recommended for attachments with tag ATTACHMENT_DATA_TAG value: ATTACHMENT_TAG_FILE or ATTACHMENT_TAG_LINK.
Parameters: Removes the attachment hashtags.
Note
Recommended for attachments with tag ATTACHMENT_DATA_TAG value: ATTACHMENT_TAG_FILE or ATTACHMENT_TAG_LINK.
Parameters: Sets the attachment hashtags.
Note
Recommended for attachments with tag ATTACHMENT_DATA_TAG value: ATTACHMENT_TAG_FILE or ATTACHMENT_TAG_LINK.
-
connect_from_cerebro_client
()[source]¶ Previously authentificated Cerebro user connects to the database. Such a connection may be established if the user has logged in Cerebro on the same workstation. Unlike the usual
Database.connect()
which may close the concurrent Cerebro client connection (if connected with the same credentials) this function will leave the Cerebro client connection alive.Returns: - connection status:
- 0 - connection established;
- 1 - connection not established (Cerebro client application is running, but user is not logged in);
- 2 - connection not established (Cerebro client application is not running).
# Establishing connection with the database if db.connect_from_cerebro_client() != 0: # Attempting to connect via Cerebro client application. # If failed, connecting with login and password db.connect(db_user, db_password)
See also
-
copy_tasks
(task_id, tasks_list, flags=79)[source]¶ Parameters: Returns: IDs of new tasks.
Copy tasks.
Full description of flags in dbtypes:
COPY_TASKS_...
If you need replicate task, you must set tasks_list as list of tuples with one task_id and different names. For example:
[(123, ‘test_task02’), (123, ‘test_task03’), (123, ‘test_task04’), (123, ‘test_task05’)]
123 - copied task ID. ‘test_task02’, ‘test_task03’, … - names of new tasks.
# Копируем в задачу 0 задачи 1(2 копии), 2 и 3 to_do_task_list = db.to_do_task_list(db.current_user_id(), True) lst_copy = [(to_do_task_list[1][dbtypes.TASK_DATA_ID], 'Копия задачи 1(1)'), (to_do_task_list[1][dbtypes.TASK_DATA_ID], 'Копия задачи 1(2)'), (to_do_task_list[2][dbtypes.TASK_DATA_ID], 'Копия задачи 2'), (to_do_task_list[3][dbtypes.TASK_DATA_ID], 'Копия задачи 3')] db.copy_tasks(to_do_task_list[0][dbtypes.TASK_DATA_ID], lst_copy) # В задачу 0 добавлено 4 новых задачи
-
drop_link_tasks
(link_id)[source]¶ Parameters: link_id (int) – connection ID. Breaks a sequential connection between tasks.
-
execute
(query, *parameters)[source]¶ Parameters: - query (string) – query text.
- parameters – query parameters list.
Executes the query and returns the result. The result has a form of a table (list pf tuples).
-
message
(message_id)[source]¶ Parameters: message_id (int) – message ID. Returns: message data. The table fields are described in the module dbtypes:
MESSAGE_DATA_...
The message types are described in the module dbtypes:MESSAGE_TYPE_...
-
message_attachments
(message_id)[source]¶ Parameters: message_id (int, set(int, ) or list(int, )) – message ID or array of message IDs. Returns: table of files attached to the message(s). The table fields are described in the module dbtypes:
ATTACHMENT_DATA_...
One attachment can take 1 to 5 entries in the table. The attachment records are grouped by the group ID -
ATTACHMENT_DATA_GROUP_ID
. The records of one attachment are tagged withATTACHMENT_DATA_TAG
, and stand for a particular parameter of the attachment.Attachments fall into two types: a file and a link to a file. In case of file the attachment has a tag
ATTACHMENT_TAG_FILE
, which contains the hash of the file on a Cargador-operated file storage. In case of a link, the attachment is tagged withATTACHMENT_TAG_LINK
. This entry has no hash, it contains a full path to fileATTACHMENT_DATA_FILE_NAME
in its name field. The entry tagged withATTACHMENT_TAG_REVIEW
is available only if the file has a Mirada review over it. The entry with a thumbnail tagATTACHMENT_TAG_THUMB...
, is available only if the file is a picture or video. If the file is a picture, it has only one entryATTACHMENT_TAG_THUMB1
, if it is a video – three entries.
Parameters: message_id (int, set(int, ) or list(int, )) – message ID or array of message IDs. Gets the message hashtags.
Parameters: Removes the message hashtags.
Parameters: Sets the message hashtags.
-
messages
(message_ids)[source]¶ Parameters: message_ids (array) – array of message IDs. Returns: messages data. The table fields are described in the module dbtypes:
MESSAGE_DATA_...
The message types are described in the module dbtypes:MESSAGE_TYPE_...
Parameters: project_id (int) – project ID. Returns: project tags table. The table contains all of the tags that can be set on project tasks. The table fields are described in the module dbtypes:
TAG_DATA_...
-
root_tasks
()[source]¶ Returns: table of root tasks. The table fields are described in the module dbtypes:
TASK_DATA_...
-
set_link_tasks
(first_task_id, second_task_id)[source]¶ Parameters: Returns: connection ID.
Return type: Creates a sequence of tasks.
-
statuses
()[source]¶ Returns: the table of all statuses. The table fields are described in the module dbtypes:
STATUS_DATA_...
-
tag_enums
(tag_id)[source]¶ Parameters: tag_id (int) – tag ID. Returns: tag enumeration table. The table contains enumerations that can be set as the tag value. The table fields are described in the module dbtypes:
TAG_ENUM_DATA_...
-
task
(task_id)[source]¶ Parameters: task_id (int) – task ID. Returns: task data. The table fields are described in the module dbtypes:
TASK_DATA_...
See also
-
task_allocated
(task_id)[source]¶ Parameters: task_id (int) – task ID. Returns: the table of users allocated to the task. The table fields are described in the module dbtypes:
TASK_ALLOCATED_...
-
task_attachments
(task_id)[source]¶ Parameters: task_id (int, set(int, ) or list(int, )) – task ID or array of task IDs. Returns: table of files attached to the task(s). The table fields are described in the module dbtypes:
ATTACHMENT_DATA_...
One attachment can take 1 to 5 entries in the table. The attachment records are grouped by the group ID -
ATTACHMENT_DATA_GROUP_ID
. The records of one attachment are tagged withATTACHMENT_DATA_TAG
, and stand for a particular parameter of the attachment.Attachments fall into two types: a file and a link to a file. In case of file the attachment has a tag
ATTACHMENT_TAG_FILE
, which contains the hash of the file on a Cargador-operated file storage. In case of a link, the attachment is tagged withATTACHMENT_TAG_LINK
. This entry has no hash, it contains a full path to fileATTACHMENT_DATA_FILE_NAME
in its name field. The entry tagged withATTACHMENT_TAG_REVIEW
is available only if the file has a Mirada review over it. The entry with a thumbnail tagATTACHMENT_TAG_THUMB...
, is available only if the file is a picture or video. If the file is a picture, it has only one entryATTACHMENT_TAG_THUMB1
, if it is a video – three entries.
-
task_by_url
(url)[source]¶ Parameters: url (string) – url of task. Returns: task ID. Return task ID by url. Url example: ‘/Test project/test’
Note
Task paths are case-sensitive.
-
task_children
(task_id)[source]¶ Parameters: task_id (int) – task ID. Returns: subtasks table. The table fields are described in the module dbtypes:
TASK_DATA_...
-
task_definition
(task_id)[source]¶ Parameters: task_id (int) – task ID. Returns: data of Definition message type. The table fields are described in the module dbtypes:
MESSAGE_DATA_...
Parameters: task_id (int, set(int, ) or list(int, )) – task ID or array of task IDs. Gets the task hashtags.
- ::
- # Работа с хэштегами задачи to_do_task_list = db.to_do_task_list(db.current_user_id(), True) db.task_set_hashtags(to_do_task_list[0][dbtypes.TASK_DATA_ID], {‘хэштег1’, ‘хэштег2’, ‘хэштег3’}) # присваиваем задаче массив хэштегов db.task_remove_hashtags(to_do_task_list[0][dbtypes.TASK_DATA_ID], ‘хэштег2’) # удаляем хэштег hashtags = db.task_hashtags(to_do_task_list[0][dbtypes.TASK_DATA_ID]) # получаем хэштеги задачи print(‘Хэштеги задачи ‘, hashtags) # распечатываем хэштеги
-
task_links
(task_id)[source]¶ Parameters: task_id (int) – task ID. Returns: the table of task connections with other tasks. The table fields are described in the module dbtypes:
TASK_LINK_...
-
task_messages
(task_id)[source]¶ Parameters: task_id (int) – task ID. Returns: the table of messages in the task. The table fields are described in the module dbtypes:
MESSAGE_DATA_...
The message types are described in the module dbtypes:MESSAGE_TYPE_...
-
task_possible_statuses
(task_id)[source]¶ Parameters: task_id (int) – task ID. Returns: the table of statuses, which can be set for the task. The table fields are described in the module dbtypes:
STATUS_DATA_...
In the Cerebro system for each status, permissions for switching each status are set. In addition, each status has a flag of inheritance. On the task-containers you can set only the statuses that have this flag enabled. Therefore, the list of possible statuses depends on user rights, the current status, as well as the presence / lack of sub-tasks in the task.
-
task_remove_allocated
(task_id, user_id)[source]¶ Parameters: Dismisses an allocated user from a task.
Parameters: Removes the task hashtags.
-
task_set_activity
(task_id, activity_id)[source]¶ Parameters: Sets an activity type for a task. Activity ID = 0 sets the task activity type to ‘No activity’.
-
task_set_allocated
(task_id, user_id)[source]¶ Parameters: Allocates a user to a task.
Note
To send a notice to the user about the assigned task, you must have
the "Defenition" type message
in the task.
-
task_set_budget
(task_id, budget)[source]¶ Parameters: Sets the task budget.
If set to None, the current budget value is reset to the sum of its subtask budgets.
-
task_set_finish
(task_id, time)[source]¶ Parameters: Sets the task finish time, in days from 01.01.2000 (UTC time).
If time = None, the finish time value is reset. After resetting the finish time is calculated according to the schedule and the planned working time.
db.task_set_finish(task_id, 4506.75) # the finish time is 03.05.2012 18:00 UTC
An example of setting the task finish time in 3 days ahead from current time
import datetime datetime_now = datetime.datetime.utcnow() datetime_2000 = datetime.datetime(2000, 1, 1) timedelta = datetime_now - datetime_2000 days = timedelta.total_seconds()/(24*60*60) + 3 db.task_set_finish(task_id, days)
-
task_set_flag
(task_id, flag, is_set)[source]¶ Parameters: Sets a flag for a task. If is_set is True, the flag sets, otherwise - resets.
# Mark a task as archived db.task_set_flag(task_id, dbtypes.TASK_FLAG_ARCHIVED, True)
Flag values are described in the module dbtypes:
TASK_FLAG_...
Parameters: Sets the task hashtags.
-
task_set_name
(task_id, name)[source]¶ Parameters: Sets a new name for a task. It cannot contain the following symbols: \ / # : ? & ‘ ” , + |.
-
task_set_planned_time
(task_id, hours)[source]¶ Parameters: Sets the time planned to fulfill the task in hours. If hours argument is set to None, the planned time is reset. After that the planned time is calculated according to calendar time frames and current work schedule of allocated user(s).
-
task_set_priority
(task_id, prior)[source]¶ Parameters: Sets task priority level.
Priority values are described in the module dbtypes:
TASK_PRIORITY_...
-
task_set_progress
(task_id, progress)[source]¶ Parameters: Sets progress value for a task. If value is 100, the task is considered complete (Done). If value is set to None, the task progress value is reset to a sum of progress values of its subtasks.
-
task_set_start
(task_id, time)[source]¶ Parameters: Sets the starting moment for a task, in days from 01.01.2000 (UTC time)
If time = None, the start time value is reset. After resetting the starting point is calculated according to the task connections and the schedule.
db.task_set_start({task_id, task_id1}, 4506.375) # starting point is May 03, 2012 9:00am UTC
An example of setting the starting time equal to current time
import datetime datetime_now = datetime.datetime.utcnow() datetime_2000 = datetime.datetime(2000, 1, 1) timedelta = datetime_now - datetime_2000 days = timedelta.total_seconds()/(24*60*60) db.task_set_start({task_id, task_id1}, days)
-
task_set_status
(task_id, status_id)[source]¶ Parameters: Sets a status for a task. Status ID = None sets the task status to ‘No Status’.
-
task_set_tag_enum
(task_id, enum_id, is_set)[source]¶ Parameters: Sets or removes tag value of enumeration or multiple enumeration type for a task.
-
task_set_tag_float
(task_id, tag_id, value)[source]¶ Parameters: Sets tag value of floating-point number type for a task.
-
task_set_tag_int
(task_id, tag_id, value)[source]¶ Parameters: Sets tag value of integer number type for a task.
-
task_set_tag_string
(task_id, tag_id, value)[source]¶ Parameters: Sets tag value of string type for a task.
-
task_tag_enums
(task_id, tag_id)[source]¶ Parameters: Returns: table of tag enumerations set on a task.
The table fields are described in the module dbtypes:
TASK_TAG_ENUM_...
Parameters: task_id (int) – task ID. Returns: table of tag values set on a task. The table fields are described in the module dbtypes:
TASK_TAG_DATA_...
-
tasks
(task_ids)[source]¶ Parameters: task_ids (array) – array of task IDs. Returns: tasks data. The table fields are described in the module dbtypes:
TASK_DATA_...
See also
-
to_do_task_list
(user_id, with_done_task)[source]¶ Returns: table of tasks the user is allocated to.
Parameters: The table fields are described in the module dbtypes:
TASK_DATA_...
-
users
()[source]¶ Returns: table of users/material resources. The table fields are described in the module dbtypes:
USER_DATA_...
A material resource has a flag set
USER_FLAG_IS_RESOURCE
. You can check the state of the flag with the functioncclib.has_flag
:if cclib.has_flag(user[dbtypes.USER_DATA_FLAGS], dbtypes.USER_FLAG_IS_RESOURCE): #if it is material resource # actions