From 2954391460e7e6d824ab21bff265929503122c39 Mon Sep 17 00:00:00 2001 From: Antoine Abt Date: Thu, 31 Jul 2014 12:19:20 +0200 Subject: [PATCH] Reduce number of queries when loading home page --- osmtm/templates/home.mako | 8 +++++++- osmtm/views/views.py | 19 ++++++------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/osmtm/templates/home.mako b/osmtm/templates/home.mako index d608f510..17b77f84 100644 --- a/osmtm/templates/home.mako +++ b/osmtm/templates/home.mako @@ -97,9 +97,15 @@ sorts = [('priority', 'asc', _('High priority first')), <% import markdown import bleach + from sqlalchemy.orm import joinedload + from osmtm.models import DBSession, Project, ProjectTranslation + + priority = priorities[project.priority] + project = DBSession.query(Project).\ + options(joinedload(Project.translations[request.locale_name])).\ + filter(Project.id == project.id).first() if request.locale_name: project.locale = request.locale_name - priority = priorities[project.priority] if project.status == project.status_archived: status = 'Archived' diff --git a/osmtm/views/views.py b/osmtm/views/views.py index 5791b52a..e1c13af8 100644 --- a/osmtm/views/views.py +++ b/osmtm/views/views.py @@ -7,6 +7,7 @@ or_, and_, ) +from sqlalchemy.orm import joinedload from ..models import ( DBSession, @@ -29,6 +30,7 @@ @view_config(route_name='home', renderer='home.mako') def home(request): + check_task_expiration() check_project_expiration() @@ -63,22 +65,13 @@ def home(request): search_filter = or_(PT.name.ilike('%%%s%%' % s), PT.short_description.ilike('%%%s%%' % s), PT.description.ilike('%%%s%%' % s),) - ids = DBSession.query(ProjectTranslation.id) \ - .filter(search_filter) \ - .all() - filter = and_(Project.id.in_(ids), filter) + query.options(joinedload(Project.translations)) + filter = and_(filter, search_filter) # filter projects on which the current user worked on if request.params.get('my_projects', '') == 'on': - ids = DBSession.query(TaskLock.project_id) \ - .filter(TaskLock.user_id == user_id) \ - .all() - - if len(ids) > 0: - filter = and_(Project.id.in_(ids), filter) - else: - # IN-predicate with emty sequence can be expensive - filter = and_(False == True) # noqa + query.join(TaskLock, Project.id==TaskLock.project_id) + filter = and_(filter, TaskLock.user_id==user_id) sort_by = 'project.%s' % request.params.get('sort_by', 'priority') direction = request.params.get('direction', 'asc')