Using Pillar's subquery module.

This commit is contained in:
2016-10-18 15:35:23 +02:00
parent b0b5be0df8
commit 70f756fb7d
5 changed files with 9 additions and 46 deletions

View File

@@ -3,6 +3,7 @@ import logging
import flask import flask
from werkzeug.local import LocalProxy from werkzeug.local import LocalProxy
from pillar.extension import PillarExtension from pillar.extension import PillarExtension
import pillar.web.subquery
from pillar.web.system_util import pillar_api from pillar.web.system_util import pillar_api
import pillarsdk import pillarsdk
@@ -84,13 +85,12 @@ class AttractExtension(PillarExtension):
def setup_app(self, app): def setup_app(self, app):
"""Connects Blinker signals and sets up other app-dependent stuff in submodules.""" """Connects Blinker signals and sets up other app-dependent stuff in submodules."""
from . import subversion, tasks, eve_hooks, subquery, shots from . import subversion, tasks, eve_hooks, shots
subversion.task_logged.connect(self.task_manager.task_logged_in_svn) subversion.task_logged.connect(self.task_manager.task_logged_in_svn)
tasks.setup_app(app) tasks.setup_app(app)
shots.setup_app(app) shots.setup_app(app)
eve_hooks.setup_app(app) eve_hooks.setup_app(app)
subquery.setup_app(app)
# Imports for side-effects # Imports for side-effects
from . import node_url_finders from . import node_url_finders
@@ -165,8 +165,6 @@ class AttractExtension(PillarExtension):
:returns: {'_items': [task, task, ...], '_meta': {Eve metadata}} :returns: {'_items': [task, task, ...], '_meta': {Eve metadata}}
""" """
from . import subquery
api = pillar_api() api = pillar_api()
activities = pillarsdk.Activity.all({ activities = pillarsdk.Activity.all({
'where': { 'where': {
@@ -180,7 +178,7 @@ class AttractExtension(PillarExtension):
# Fetch more info for each activity. # Fetch more info for each activity.
for act in activities['_items']: for act in activities['_items']:
act.actor_user = subquery.get_user_info(act.actor_user) act.actor_user = pillar.web.subquery.get_user_info(act.actor_user)
return activities return activities

View File

@@ -5,10 +5,11 @@ from flask import Blueprint, render_template, url_for
import flask_login import flask_login
from pillar.api.utils import jsonify from pillar.api.utils import jsonify
import pillar.web.subquery
from pillar.web.system_util import pillar_api from pillar.web.system_util import pillar_api
import pillarsdk import pillarsdk
from attract import current_attract, subquery from attract import current_attract
from attract.node_types.task import node_type_task from attract.node_types.task import node_type_task
from attract.node_types.shot import node_type_shot from attract.node_types.shot import node_type_shot
@@ -47,7 +48,7 @@ def index():
node_type_task_name = node_type_task['name'] node_type_task_name = node_type_task['name']
node_type_shot_name = node_type_shot['name'] node_type_shot_name = node_type_shot['name']
for act in activities['_items']: for act in activities['_items']:
act.actor_user = subquery.get_user_info(act.actor_user) act.actor_user = pillar.web.subquery.get_user_info(act.actor_user)
act.project = id_to_proj[act.project] act.project = id_to_proj[act.project]
if act.node_type == node_type_task_name: if act.node_type == node_type_task_name:

View File

@@ -1,36 +0,0 @@
"""Sub-query stuff, for things we would otherwise let Eve embed (but don't want to).
Uses app.cache.memoize() to cache the results. However, since this decorator needs
to run in Flask Application context, it is manually applied in setup_app().
"""
import pillarsdk
from pillar.web.system_util import pillar_api
def get_user_info(user_id):
"""Returns email & full name of the user.
Only returns those two fields, so the return value is the same
for authenticated & non-authenticated users, which is why we're
allowed to cache it globally.
Returns an empty dict when the user cannot be found.
"""
if user_id is None:
return {}
user = pillarsdk.User.find(user_id, api=pillar_api())
if not user:
return {}
return {'email': user.email,
'full_name': user.full_name}
def setup_app(app):
global get_user_info
decorator = app.cache.memoize(timeout=300, make_name='%s.get_user_info' % __name__)
get_user_info = decorator(get_user_info)

View File

@@ -9,7 +9,6 @@ import pillar.api.utils.authentication
import pillar.web.jinja import pillar.web.jinja
from attract.node_types.task import node_type_task from attract.node_types.task import node_type_task
from attract import subquery
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
only_for_task = only_for_node_type_decorator(node_type_task['name']) only_for_task = only_for_node_type_decorator(node_type_task['name'])

View File

@@ -8,10 +8,11 @@ import werkzeug.exceptions as wz_exceptions
import pillarsdk import pillarsdk
from pillar.web.system_util import pillar_api from pillar.web.system_util import pillar_api
import pillar.api.utils import pillar.api.utils
import pillar.web.subquery
from attract.routes import attract_project_view from attract.routes import attract_project_view
from attract.node_types.task import node_type_task from attract.node_types.task import node_type_task
from attract import current_attract, ROLES_REQUIRED_TO_VIEW_ITEMS, subquery from attract import current_attract, ROLES_REQUIRED_TO_VIEW_ITEMS
blueprint = Blueprint('attract.tasks', __name__, url_prefix='/tasks') blueprint = Blueprint('attract.tasks', __name__, url_prefix='/tasks')
perproject_blueprint = Blueprint('attract.tasks.perproject', __name__, perproject_blueprint = Blueprint('attract.tasks.perproject', __name__,
@@ -70,7 +71,7 @@ def view_task(project, attract_props, task_id):
users = project.get_users(api=api) users = project.get_users(api=api)
project.users = users['_items'] project.users = users['_items']
else: else:
task.properties.assigned_to.users = [subquery.get_user_info(uid) task.properties.assigned_to.users = [pillar.web.subquery.get_user_info(uid)
for uid in task.properties.assigned_to.users] for uid in task.properties.assigned_to.users]
return render_template('attract/tasks/view_task_embed.html', return render_template('attract/tasks/view_task_embed.html',