The pycerebro.database module contains descriptions of classes used to access the database.
classpycerebro.database.Database(db_host = "", db_port = None, db_timeout=5, db_reconn_count=3)
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.
import pycerebro
# set up connection to database
db = pycerebro.database.Database()
if db.connect_from_cerebro_client() != 0: # trying to establish a connection using the running Cerebro client.
# If it doesn't work, a connection using the login and password is established
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.
# Using ID arrays
to_do_task_list = db.to_do_task_list(db.current_user_id(), True) # get the current user's task list
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) # prioritize multiple tasks above normal.
activities()
Returns: | table of activities. |
Table fields described in dbtypes module: ACTIVITY_DATA_...
add_attachment()
add_attachment(message_id, carga, filename, thumbnails, description, as_link)
Parameters: |
|
Attaching a file to a message.
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
es_code = subprocess.call([mirada_path, filename, '--temp', gen_path, '--hide', '--mode', 'thumbstandalone'])
#-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()
add_client_review(task_id, message_id, text)
Parameters: |
|
Returns: | new message ID. |
Return type: | int |
Adds a message of “Client Review” type.
add_definition()
add_definition(task_id, text)
Parameters: |
|
Returns: | new message ID |
Return type: | int |
Adds a message of “Definition” type.
add_note()
add_note(task_id, message_id, text)
Parameters: |
|
Returns: | new message ID. |
Return type: | int |
Adds a message of “Note” type.
add_report()
add_report(task_id, message_id, text, minutes)
Parameters: |
|
Returns: | new message ID. |
Return type: | int |
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()
add_resource_report(task_id, message_id, resource_id, text, minutes)
Parameters: |
|
Returns: | new message ID |
Return type: | int |
Adds a message of the “Resource Report” type.
add_review()
add_review(task_id, message_id, text, minutes=None)
Parameters: |
|
Returns: | new message ID |
Returns type: | int |
Adds a message of the “Review” type.
add_task()
add_task(parent_id, name, activity_id=0)
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 "Definition" type message in the task.
attachment_hashtags(attachment_id)
Parameters: | attachment_id (int, set(int, ) или list(int,)) – attachment ID or array of attachment IDs. |
Note
Recommended for attachments with tag ATTACHMENT_DATA_TAG value: ATTACHMENT_TAG_FILE or ATTACHMENT_TAG_LINK.
attachment_remove_hashtags(attachment_id, hashtags)
Parameters: |
|
Note
Recommended for attachments with tag ATTACHMENT_DATA_TAG value: ATTACHMENT_TAG_FILE or ATTACHMENT_TAG_LINK.
attachment_set_hashtags(attachment_id, hashtags)
Parameters: |
|
Sets the attachment hashtags.
Note
Recommended for attachments with tag ATTACHMENT_DATA_TAG value: ATTACHMENT_TAG_FILE or ATTACHMENT_TAG_LINK.
connect()
connect(db_user, db_password)
Connection and authentification.
connect_from_cerebro_client()
connect_from_cerebro_client()
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.
Result: | status connection: 0 - connection established; 1 - connection not established (Cerebro client is running, but user is not logged in); 2 - connection not established (Cerebro client 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()
copy_tasks(task_id, tasks_list, flags = COPY_TASKS_SUB_TASKS|COPY_TASKS_INTERNAL_LINKS|COPY_TASKS_TAGS|COPY_TASKS_ASSIGNED_USERS|COPY_TASKS_EVENTS)
Parameters: |
|
Returns: | a list of new tasks IDs. |
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 новых задачи
current_user_id()
current_user_id()
Returns: | the logged in user’s ID. |
drop_link_tasks()
drop_link_tasks(link_id)
Parameters: | link_id (int) – connection ID. |
execute()
execute(query, *parameters)
Parameters: |
|
Executes the query and returns the result. The result has a form of a table (list pf tuples).
message()
message(message_id)
Parameters: | message_id (int) – message ID. |
Returns: | данные сообщения. |
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_attachments(message_id)
Parameters: | message_id (int, set(int, ) или 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 with ATTACHMENT_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 with ATTACHMENT_TAG_LINK. This entry has no hash, it contains a full path to file ATTACHMENT_DATA_FILE_NAME in its name field. The entry tagged with ATTACHMENT_TAG_REVIEW is available only if the file has a Mirada review over it. The entry with a thumbnail tag ATTACHMENT_TAG_THUMB..., is available only if the file is a picture or video. If the file is a picture, it has only one entry ATTACHMENT_TAG_THUMB1, if it is a video – three entries.
message_hashtags(message_id)
Parameters: | message_id (int, set(int, ) или list(int, )) – message ID or array of message IDs. |
Gets the message hashtags.
message_remove_hashtags(message_id, hashtags)
Parameters: |
|
message_set_hashtags(message_id, hashtags)
Parameters: |
|
messages()
messages(message_ids)
Parameters: | message_id (int, set(int, ) или list(int, )) – 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_...
project_tags(project_id)
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()
root_tasks()
Returns: | a table of root tasks |
The table fields are described in the module dbtypes: TASK_DATA_...
set_link_tasks()
set_link_tasks(first_task_id, second_task_id)
Parameters: |
|
Returns: | link ID |
Return type: | int |
Creates a sequence of tasks.
statuses()
statuses()
Result: | a table of all statuses. |
The table fields are described in the module dbtypes: STATUS_DATA_...
tag_enums()
tag_enums(tag_id)
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 dbtypes module: TAG_ENUM_DATA_...
task()
task(task_id)
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_allocated(task_id)
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_attachments(task_id)
Parameters: | task_id (int, set(int, ) или 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 with ATTACHMENT_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 with ATTACHMENT_TAG_LINK. This entry has no hash, it contains a full path to file ATTACHMENT_DATA_FILE_NAME in its name field. The entry tagged with ATTACHMENT_TAG_REVIEW is available only if the file has a Mirada review over it. The entry with a thumbnail tag ATTACHMENT_TAG_THUMB..., is available only if the file is a picture or video. If the file is a picture, it has only one entry ATTACHMENT_TAG_THUMB1, if it is a video – three entries.
task_by_url()
task_by_url(url)
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_children(task_id)
Parameters: | task_id (int) – task ID. |
Returns: | subtasks table. |
task_definition()
task_definition(task_id)
If the task contains several Definition messages, the result is one last message by date of creation
Parameters: | task_id (int) – task ID. |
Returns: | data of Definition message type. |
The table fields are described in the module dbtypes: MESSAGE_DATA_...
task_hashtags(task_id)
Parameters: | task_id (int, set(int, ) или list(int, )) – task ID or array of task IDs. |
Gets the task hashtags.
# Working with 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'}) # assign an array of hashtags to the task db.task_remove_hashtags(to_do_task_list[0][dbtypes.TASK_DATA_ID], 'хэштег2') # remove hashtag hashtags = db.task_hashtags(to_do_task_list[0][dbtypes.TASK_DATA_ID]) # get task hashtags print('Хэштеги задачи', hashtags) # print hashtags
task_links()
task_links(task_id)
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_messages(task_id)
Parameters: | task_id (int) – task ID. |
Returs: | 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_possible_statuses(task_id)
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_remove_allocated(task_id, user_id)
Parameters: |
|
task_remove_hashtags(task_id, hashtags)
Parameters: |
|
task_set_activity()
task_set_activity(task_id, activity_id)
Parameters: |
|
Sets an activity type for a task. Activity ID = 0 sets the task activity type to ‘No activity’.
task_set_allocated()
task_set_allocated(task_id, user_id)
Parameters: |
|
Note
To send a notice to the user about the assigned task, you must have the"Definition" type message in the task.
task_set_budget()
task_set_budget(task_id, budget)
Parameters: |
|
If set to None, the current budget value is reset to the sum of its subtask budgets.
task_set_finish()
task_set_finish(task_id, time)
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_set_flag(task_id, flag, is_set)
Parameters: |
|
# 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_...
task_set_hashtags(task_id, hashtags)
Parameters: |
|
task_set_name()
task_set_name(task_id, name)
Parameters: |
|
Sets a new name for a task. It cannot contain the following symbols: \ / # : ? & ‘ ” , + |.
task_set_planned_time()
task_set_planned_time(task_id, hours)
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_set_priority(task_id, prior)
Parameters: |
|
Sets task priority level.
Priority values are described in the module dbtypes: TASK_PRIORITY_...
task_set_progress()
task_set_progress(task_id, progress)
Parameters: |
|
task_set_start()
task_set_start(task_id, time)
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_set_status(task_id, status_id)
Parameters: |
|
Sets a status for a task. Status ID = None sets the task status to ‘No Status’.
task_set_tag_enum()
task_set_tag_enum(task_id, enum_id, is_set)
Parameters: |
|
Sets or removes tag value of enumeration or multiple enumeration type for a task.
task_set_tag_float()
task_set_tag_float(task_id, tag_id, value)
Parameters: |
|
task_set_tag_int()
task_set_tag_int(task_id, tag_id, value)
Parameters: |
|
Sets tag value of integer number type for a task.
task_set_tag_string()
task_set_tag_string(task_id, tag_id, value)
Parameters: |
|
Sets tag value of string type for a task.
task_tag_enums()
task_tag_enums(task_id, tag_id)
Parameters: |
|
Result: | table of tag enumerations set on a task. |
The table fields are described in the module dbtypes: TASK_TAG_ENUM_...
task_tag_reset()
task_tag_reset(task_id, tag_id)
Parameters: |
|
Removes tag value from a task(s).
task_tags(task_id)
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()
tasks(task_ids)
Parameters: | task_ids (array) – array of task IDs. |
Result: | tasks data. |
The table fields are described in the module dbtypes: TASK_DATA_...
See also
to_do_task_list()
to_do_task_list(user_id, with_done_task)
Parameters: |
|
Returns: | table of tasks the user is allocated to. |
The table fields are described in the module dbtypes: TASK_DATA_...
users()
users()
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 function cclib.has_flag:
if cclib.has_flag(user[dbtypes.USER_DATA_FLAGS], dbtypes.USER_FLAG_IS_RESOURCE): #if it is material resource
# actions