Added parent info to retrieved task nodes.
Added an Eve hook that adds {'parent_info: {'node_type': 'type', 'name': '...'}} to returned task nodes.
This commit is contained in:
45
attract/tasks/eve_hooks.py
Normal file
45
attract/tasks/eve_hooks.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import logging
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from attract.node_types.task import node_type_task
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def fetch_task_parent_info(node):
|
||||
"""Extends the node with some parent info.
|
||||
|
||||
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.
|
||||
"""
|
||||
|
||||
if node.get('node_type') != node_type_task['name']:
|
||||
return
|
||||
|
||||
parent_id = node.get('parent')
|
||||
if not parent_id:
|
||||
return
|
||||
|
||||
nodes_coll = current_app.data.driver.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_tasks_parent_info(nodes):
|
||||
for node in nodes['_items']:
|
||||
fetch_task_parent_info(node)
|
||||
|
||||
|
||||
def setup_app(app):
|
||||
app.on_fetched_item_nodes += fetch_task_parent_info
|
||||
app.on_fetched_resource_nodes += fetch_tasks_parent_info
|
Reference in New Issue
Block a user