diff --git a/attract/__init__.py b/attract/__init__.py index 10430e7..afa336b 100644 --- a/attract/__init__.py +++ b/attract/__init__.py @@ -5,6 +5,7 @@ from werkzeug.local import LocalProxy from pillar.extension import PillarExtension import pillar.web.subquery from pillar.web.system_util import pillar_api +from pillar.web.nodes.routes import url_for_node import pillarsdk @@ -193,6 +194,30 @@ class AttractExtension(PillarExtension): return activities + def link_for_activity(self, act): + """Returns the URL for the activity. + + :type act: pillarsdk.Activity + """ + + from .node_types.task import node_type_task + from .node_types.shot import node_type_shot + + if act.node_type == node_type_task['name']: + if act.context_object: + return flask.url_for('attract.shots.perproject.with_task', + project_url=act.project.url, + task_id=act.object) + return flask.url_for('attract.tasks.perproject.view_task', + project_url=act.project.url, + task_id=act.object) + elif act.node_type == node_type_shot['name']: + return flask.url_for('attract.shots.perproject.view_shot', + project_url=act.project.url, + shot_id=act.object) + + return url_for_node(node_id=act.object) + def _get_current_attract(): """Returns the Attract extension of the current application.""" diff --git a/attract/comments/eve_hooks.py b/attract/comments/eve_hooks.py index c0b14ec..863d648 100644 --- a/attract/comments/eve_hooks.py +++ b/attract/comments/eve_hooks.py @@ -1,5 +1,7 @@ import logging +import flask + from pillar.api.nodes import only_for_node_type_decorator import pillar.api.activities import pillar.api.utils.authentication @@ -18,6 +20,13 @@ def activity_after_creating_node(comment): log.warning('Comment %s created without parent.' % comment_id) return + db = flask.current_app.db() + parent = db['nodes'].find_one({'_id': parent_id}, + projection={'node_type': 1}) + if not parent: + log.warning('Comment %s has non-existing parent %s' % (comment_id, parent_id)) + return + log.debug('Recording creation of comment as activity on node %s', parent_id) pillar.api.activities.register_activity( @@ -27,6 +36,7 @@ def activity_after_creating_node(comment): 'node', parent_id, project_id=comment.get('project', None), node_type=comment['node_type'], + context_node_type=parent['node_type'], ) diff --git a/attract/routes.py b/attract/routes.py index b7d6c4c..219c1a8 100644 --- a/attract/routes.py +++ b/attract/routes.py @@ -1,7 +1,7 @@ import functools import logging -from flask import Blueprint, render_template, url_for +from flask import Blueprint, render_template import flask_login from pillar.web.utils import attach_project_pictures @@ -50,25 +50,10 @@ def index(): }, api=api) # Fetch more info for each activity. - node_type_task_name = node_type_task['name'] - node_type_shot_name = node_type_shot['name'] for act in activities['_items']: act.actor_user = pillar.web.subquery.get_user_info(act.actor_user) act.project = id_to_proj[act.project] - - if act.node_type == node_type_task_name: - if act.context_object: - act.link = url_for('attract.shots.perproject.with_task', - project_url=act.project.url, - task_id=act.object) - else: - act.link = url_for('attract.tasks.perproject.view_task', - project_url=act.project.url, - task_id=act.object) - elif act.node_type == node_type_shot_name: - act.link = url_for('attract.shots.perproject.view_shot', - project_url=act.project.url, - shot_id=act.object) + act.link = current_attract.link_for_activity(act) return render_template('attract/index.html', tasks=tasks,