10.6.20. Class cerebro.events.BeforeEventMessage

class cerebro.events.BeforeEventMessage(event_type, event_id)[source]

Bases: cerebro.events.Event, cerebro.aclasses.AbstractMessage

The base class for the message creation/change events. It grants access to the data of the message being created/changed before the data is written to the database.

Methods

See also

AfterEventMessage.

add_attachment(file_path, comment='', name='', hashtags=None)[source]
Parameters:
  • file_path (string) – file path.
  • comment (string) – text comment.
  • name (string) – attachment name. If the name is not specified, the file name is taken.
  • hashtags (string, set(string, ) or list(string, )) – hashtag or array of hashtags (every hashtag should be written in one word without spaces).

Adds the attachment to the message.

def before_event(event):        

        evtype = event.event_type()
        if evtype == event.EVENT_CREATION_OF_MESSAGE or evtype == event.EVENT_CHANGING_OF_MESSAGE:
                file_path = cerebro.core.python_api_dir() + '/examples/icon.png'
                event.add_attachment(file_path, 'Added with Cerebro Python API')        
Parameters:
  • file_path (string) – file path.
  • comment (string) – text comment.
  • hashtags (string, set(string, ) or list(string, )) – hashtag or array of hashtags (every hashtag should be written in one word without spaces).

It adds an attachment as a link. The attachment itself is not being copied to the Cargador file storage.

The file_path stands for the attachment name in this case.

See also

add_attachment().

new_attachments()[source]
Returns:a list of new attchments to the message.
Return type:list(cerebro.aclasses.NewAttachment,)
new_task_status()[source]
Returns:the new status of the task, which the user sets when creating / editing a message. ‘(0, ‘’)’ means, that the user sets the task status into ‘No Status’.
Return type:tuple(status_id, status_name) - a list of tuples consisting of two fields: a status ID and a status name.

In the graphical interface of creating / editing the message, you can choose the status in which the task will go after the message is sent. If the user does not change the status, the new status will be equal to :py:meth:’the current task status <cerebro.aclasses.Task.status>’.

resources()[source]
Returns:the list of resources being reported for.
Return type:list(tuple,) - [(resource_id, resource_name),] - a list of tuples consisting of two fields: a resource ID and a resource name.

Resources are available in the "Resource Report" type only. If the message type is not the “Resource Report”, “None” is being returned.

if event.type() == event.TYPE_RESOURCE_REPORT:
        print('resources()', event.resources()) 
set_approved(is_approved)[source]
Parameters:is_approved (bool) – If its value is True, the Report is being marked ‘Approved’, otherwise the ‘Approved’ is cleared.

Sets the ‘Approved’ flag for a Report.

The ‘Approved’ flag means that, the declared working time to sign off is approved. The GUI marks the message with a green check mark.

This flag makes sense only for the "Report" or "Resource Report" message types.

if event.type() == event.TYPE_RESOURCE_REPORT:
        event.set_approved(True)
set_client_visible(is_visible)[source]
Parameters:is_visible (bool) – If the value is True, the message is marked as visible for clients, otherwise it is invisible for clients.

Defines if the message may be seen or not by the clients.

def before_event(event):        

        evtype = event.event_type()
        if evtype == event.EVENT_CREATION_OF_MESSAGE:
                event.set_client_visible(True)                  
set_html_text(text)[source]
Parameters:text (string) – html-formatted or unformatted text.

Sets a new message text.

set_new_task_status(status_id)[source]
Parameters:status_id (int) – status ID.

Sets a new status, in which the task will go after the message is sent. ‘0’ translates the task status into’ No Status’.

# If a report is written and the new status is not equal to 'pending review',
# then setting this status
if event.type() == event.TYPE_REPORT and event.new_task_status()[1] != 'pending review':
                task = cerebro.core.task(event.task_id())
                possible_statuses = task.possible_statuses()
                for status in possible_statuses:
                        if status[cerebro.aclasses.Statuses.DATA_NAME] == 'pending review': # Verify that the user can switch to the status
                                event.set_new_task_status(status[cerebro.aclasses.Statuses.DATA_ID])
                                break

See also

new_task_status().

set_work_time(minutes)[source]
Parameters:minutes (float) – time (minutes).

Sets the amount of the working time, signed off by the message, to be indicated to clients.

If the type of the message is "Report" or "Resource Report", it indicates the time signed off (declared by the current message author). If the type of the message is "Review", it indicates the time signed off and approved for the previous Report. For all the rest of the message types it is absurd to set working time.

if event.type() == event.TYPE_REPORT:
        if event.work_time() > 60000: # if a user tries to sign off more than 1000 hours for the Report
                raise Exception('This is too much, isn't it?') # an exception is being raised (the user will be displayed this message)
        else event.work_time() < 60: # if the user tries to sign off less than 1 hour
                event.set_work_time(60) # we're setting 1 hour (60 minutes) anyway