Page MenuHome

eve_hooks.py
No OneTemporary

File Metadata

Created
Tue, Nov 19, 5:06 AM

eve_hooks.py

import logging
from flask import current_app
from attract.node_types.task import node_type_task
from pillar.api.nodes import only_for_node_type_decorator
import pillar.api.activities
import pillar.api.utils.authentication
log = logging.getLogger(__name__)
only_for_task = only_for_node_type_decorator(node_type_task['name'])
@only_for_task
def fetch_task_extra_info(node):
"""Extends the node with some info about its parent and project.
This allows us to link to the shot the task is attached to.
However, such a link requires at least knowing the parent node type,
which we thus embed here.
"""
fetch_task_parent_info(node)
fetch_task_project_info(node)
def fetch_task_parent_info(node):
"""Store node parent info in node['_parent_info']."""
parent_id = node.get('parent')
if not parent_id:
return
nodes_coll = current_app.db()['nodes']
parent = nodes_coll.find_one({'_id': parent_id},
projection={'node_type': 1,
'name': 1})
if parent is None:
log.warning("Task node %s has parent %s, but the parent doesn't exist.",
node['_id'], parent_id)
return
parent.pop('_id') # always there, but also already included in the node.
node['_parent_info'] = parent
def fetch_task_project_info(node):
"""Store node project info in node['_project_info']."""
project_id = node.get('project')
if not project_id:
log.warning('Task node %s has no project!', node['_id'])
return
proj_coll = current_app.db()['projects']
project = proj_coll.find_one({'_id': project_id},
projection={'name': 1,
'url': 1})
if project is None:
log.warning("Task node %s has project %s, but the project doesn't exist.",
node['_id'], project_id)
return
project.pop('_id') # always there, but also already included in the node.
node['_project_info'] = project
def fetch_tasks_parent_info(nodes):
for node in nodes['_items']:
fetch_task_extra_info(node)
# Activity logging
@only_for_task
def activity_after_replacing_task(task, original):
user_id = pillar.api.utils.authentication.current_user_id()
pillar.api.activities.register_activity(
user_id,
'edited task',
'node', task['_id'],
'project',
task['project'],
task['project'],
)
@only_for_task
def activity_after_creating_task(task):
user_id = pillar.api.utils.authentication.current_user_id()
pillar.api.activities.register_activity(
user_id,
'created a new task',
'node', task['_id'],
'project',
task['project'],
task['project'],
)
def activity_after_creating_tasks(nodes):
for node in nodes:
activity_after_creating_task(node)
@only_for_task
def activity_after_deleting_task(task):
user_id = pillar.api.utils.authentication.current_user_id()
pillar.api.activities.register_activity(
user_id,
'deleted task',
'node', task['_id'],
'project',
task['project'],
task['project'],
)
def setup_app(app):
app.on_fetched_item_nodes += fetch_task_extra_info
app.on_fetched_resource_nodes += fetch_tasks_parent_info
app.on_replaced_nodes += activity_after_replacing_task
app.on_inserted_nodes += activity_after_creating_tasks
app.on_deleted_item_nodes += activity_after_deleting_task
app.on_deleted_resource_nodes += activity_after_deleting_task

Event Timeline