Return-Path: Received: by yocto-www.yoctoproject.org (Postfix, from userid 118) id 0766CE00C36; Fri, 15 Jan 2016 03:44:38 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on yocto-www.yoctoproject.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 X-Spam-HAM-Report: * -5.0 RCVD_IN_DNSWL_HI RBL: Sender listed at http://www.dnswl.org/, high * trust * [134.134.136.20 listed in list.dnswl.org] * -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% * [score: 0.0000] Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by yocto-www.yoctoproject.org (Postfix) with ESMTP id 67002E00BDF for ; Fri, 15 Jan 2016 03:44:20 -0800 (PST) Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga101.jf.intel.com with ESMTP; 15 Jan 2016 03:44:20 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.22,299,1449561600"; d="scan'208";a="633885672" Received: from ffolivei-mobl2.ger.corp.intel.com (HELO lp.ger.corp.intel.com) ([10.252.20.40]) by FMSMGA003.fm.intel.com with ESMTP; 15 Jan 2016 03:44:19 -0800 From: Elliot Smith To: toaster@yoctoproject.org Date: Fri, 15 Jan 2016 11:43:42 +0000 Message-Id: <1452858230-31457-16-git-send-email-elliot.smith@intel.com> X-Mailer: git-send-email 1.9.3 In-Reply-To: <1452858230-31457-1-git-send-email-elliot.smith@intel.com> References: <1452858230-31457-1-git-send-email-elliot.smith@intel.com> Subject: [review-request][PATCH 15/23] toaster: toastergui: streamline construction of filter objects X-BeenThere: toaster@yoctoproject.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Web based interface for BitBake List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 15 Jan 2016 11:44:38 -0000 In line with comments from review, remove the QuerysetFilter class (redundant) and convert ProjectFilters into a class with static methods. [YOCTO #8738] Signed-off-by: Elliot Smith --- bitbake/lib/toaster/toastergui/querysetfilter.py | 24 --------------- bitbake/lib/toaster/toastergui/tablefilter.py | 38 ++++++++++-------------- bitbake/lib/toaster/toastergui/tables.py | 35 +++++++++++----------- bitbake/lib/toaster/toastergui/widgets.py | 1 - 4 files changed, 32 insertions(+), 66 deletions(-) delete mode 100644 bitbake/lib/toaster/toastergui/querysetfilter.py diff --git a/bitbake/lib/toaster/toastergui/querysetfilter.py b/bitbake/lib/toaster/toastergui/querysetfilter.py deleted file mode 100644 index 10cc988..0000000 --- a/bitbake/lib/toaster/toastergui/querysetfilter.py +++ /dev/null @@ -1,24 +0,0 @@ -class QuerysetFilter(object): - """ Filter for a queryset """ - - def __init__(self, criteria=None): - self.criteria = None - if criteria: - self.set_criteria(criteria) - - def set_criteria(self, criteria): - """ - criteria is an instance of django.db.models.Q; - see https://docs.djangoproject.com/en/1.9/ref/models/querysets/#q-objects - """ - self.criteria = criteria - - def filter(self, queryset): - """ - Filter queryset according to the criteria for this filter, - returning the filtered queryset - """ - if self.criteria: - return queryset.filter(self.criteria) - else: - return queryset diff --git a/bitbake/lib/toaster/toastergui/tablefilter.py b/bitbake/lib/toaster/toastergui/tablefilter.py index bd8decd..9d15bcf 100644 --- a/bitbake/lib/toaster/toastergui/tablefilter.py +++ b/bitbake/lib/toaster/toastergui/tablefilter.py @@ -22,7 +22,6 @@ from django.db.models import Q, Max, Min from django.utils import dateparse, timezone from datetime import timedelta -from querysetfilter import QuerysetFilter class TableFilter(object): """ @@ -118,10 +117,10 @@ class TableFilterAction(object): ToasterTable """ - def __init__(self, name, title, queryset_filter): + def __init__(self, name, title, criteria): self.name = name self.title = title - self.queryset_filter = queryset_filter + self.criteria = criteria # set in subclasses self.type = None @@ -132,11 +131,13 @@ class TableFilterAction(object): the structure of this string depends on the type of action; it's ignored for a toggle filter action, which is just on or off """ - if not params: - return + pass def filter(self, queryset): - return self.queryset_filter.filter(queryset) + if self.criteria: + return queryset.filter(self.criteria) + else: + return queryset def to_json(self, queryset): """ Dump as a JSON object """ @@ -167,16 +168,12 @@ class TableFilterActionDay(TableFilterAction): YESTERDAY = 'yesterday' def __init__(self, name, title, field, day, - queryset_filter = QuerysetFilter(), query_helper = TableFilterQueryHelper()): + query_helper = TableFilterQueryHelper()): """ field: (string) the datetime field to filter by day: (string) "today" or "yesterday" """ - super(TableFilterActionDay, self).__init__( - name, - title, - queryset_filter - ) + super(TableFilterActionDay, self).__init__(name, title, None) self.type = 'day' self.field = field self.day = day @@ -189,8 +186,6 @@ class TableFilterActionDay(TableFilterAction): depending on when the filtering is applied """ - criteria = None - date_str = None now = timezone.now() if self.day == self.YESTERDAY: @@ -201,15 +196,13 @@ class TableFilterActionDay(TableFilterAction): wanted_date_str = wanted_date.strftime('%Y-%m-%d') - criteria = self.query_helper.dateStringsToQ( + self.criteria = self.query_helper.dateStringsToQ( self.field, wanted_date_str, wanted_date_str ) - self.queryset_filter.set_criteria(criteria) - - return self.queryset_filter.filter(queryset) + return queryset.filter(self.criteria) class TableFilterActionDateRange(TableFilterAction): """ @@ -218,14 +211,14 @@ class TableFilterActionDateRange(TableFilterAction): """ def __init__(self, name, title, field, - queryset_filter = QuerysetFilter(), query_helper = TableFilterQueryHelper()): + query_helper = TableFilterQueryHelper()): """ field: (string) the field to find the max/min range from in the queryset """ super(TableFilterActionDateRange, self).__init__( name, title, - queryset_filter + None ) self.type = 'daterange' @@ -248,17 +241,16 @@ class TableFilterActionDateRange(TableFilterAction): try: date_from_str, date_to_str = params.split(',') except ValueError: - self.queryset_filter.set_criteria(None) + self.criteria = None return # one of the values required for the filter is missing, so set # it to the one which was supplied - criteria = self.query_helper.dateStringsToQ( + self.criteria = self.query_helper.dateStringsToQ( self.field, date_from_str, date_to_str ) - self.queryset_filter.set_criteria(criteria) def to_json(self, queryset): """ Dump as a JSON object """ diff --git a/bitbake/lib/toaster/toastergui/tables.py b/bitbake/lib/toaster/toastergui/tables.py index d0ed496..b7d977e 100644 --- a/bitbake/lib/toaster/toastergui/tables.py +++ b/bitbake/lib/toaster/toastergui/tables.py @@ -20,7 +20,6 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. from toastergui.widgets import ToasterTable -from toastergui.querysetfilter import QuerysetFilter from orm.models import Recipe, ProjectLayer, Layer_Version, Machine, Project from orm.models import CustomImageRecipe, Package, Build, LogMessage, Task from orm.models import ProjectTarget @@ -37,9 +36,13 @@ from toastergui.tablefilter import TableFilterActionDateRange from toastergui.tablefilter import TableFilterActionDay class ProjectFilters(object): - def __init__(self, project_layers): - self.in_project = QuerysetFilter(Q(layer_version__in=project_layers)) - self.not_in_project = QuerysetFilter(~Q(layer_version__in=project_layers)) + @staticmethod + def in_project(project_layers): + return Q(layer_version__in=project_layers) + + @staticmethod + def not_in_project(project_layers): + return ~(ProjectFilters.in_project(project_layers)) class LayersTable(ToasterTable): """Table of layers in Toaster""" @@ -71,13 +74,13 @@ class LayersTable(ToasterTable): in_project_action = TableFilterActionToggle( "in_project", "Layers added to this project", - QuerysetFilter(criteria) + criteria ) not_in_project_action = TableFilterActionToggle( "not_in_project", "Layers not added to this project", - QuerysetFilter(~criteria) + ~criteria ) in_current_project_filter.add_action(in_project_action) @@ -217,8 +220,6 @@ class MachinesTable(ToasterTable): def setup_filters(self, *args, **kwargs): project = Project.objects.get(pk=kwargs['pid']) - project_filters = ProjectFilters(self.project_layers) - in_current_project_filter = TableFilter( "in_current_project", "Filter by project machines" @@ -227,13 +228,13 @@ class MachinesTable(ToasterTable): in_project_action = TableFilterActionToggle( "in_project", "Machines provided by layers added to this project", - project_filters.in_project + ProjectFilters.in_project(self.project_layers) ) not_in_project_action = TableFilterActionToggle( "not_in_project", "Machines provided by layers not added to this project", - project_filters.not_in_project + ProjectFilters.not_in_project(self.project_layers) ) in_current_project_filter.add_action(in_project_action) @@ -350,8 +351,6 @@ class RecipesTable(ToasterTable): return context def setup_filters(self, *args, **kwargs): - project_filters = ProjectFilters(self.project_layers) - table_filter = TableFilter( 'in_current_project', 'Filter by project recipes' @@ -360,13 +359,13 @@ class RecipesTable(ToasterTable): in_project_action = TableFilterActionToggle( 'in_project', 'Recipes provided by layers added to this project', - project_filters.in_project + ProjectFilters.in_project(self.project_layers) ) not_in_project_action = TableFilterActionToggle( 'not_in_project', 'Recipes provided by layers not added to this project', - project_filters.not_in_project + ProjectFilters.not_in_project(self.project_layers) ) table_filter.add_action(in_project_action) @@ -1140,13 +1139,13 @@ class BuildsTable(ToasterTable): successful_builds_action = TableFilterActionToggle( 'successful_builds', 'Successful builds', - QuerysetFilter(Q(outcome=Build.SUCCEEDED)) + Q(outcome=Build.SUCCEEDED) ) failed_builds_action = TableFilterActionToggle( 'failed_builds', 'Failed builds', - QuerysetFilter(Q(outcome=Build.FAILED)) + Q(outcome=Build.FAILED) ) outcome_filter.add_action(successful_builds_action) @@ -1226,13 +1225,13 @@ class BuildsTable(ToasterTable): with_failed_tasks_action = TableFilterActionToggle( 'with_failed_tasks', 'Builds with failed tasks', - QuerysetFilter(criteria) + criteria ) without_failed_tasks_action = TableFilterActionToggle( 'without_failed_tasks', 'Builds without failed tasks', - QuerysetFilter(~criteria) + ~criteria ) failed_tasks_filter.add_action(with_failed_tasks_action) diff --git a/bitbake/lib/toaster/toastergui/widgets.py b/bitbake/lib/toaster/toastergui/widgets.py index bc081b8..d9328d4 100644 --- a/bitbake/lib/toaster/toastergui/widgets.py +++ b/bitbake/lib/toaster/toastergui/widgets.py @@ -32,7 +32,6 @@ from django.template import Context, Template from django.core.serializers.json import DjangoJSONEncoder from django.core.exceptions import FieldError from django.conf.urls import url, patterns -from toastergui.querysetfilter import QuerysetFilter import types import json -- Elliot Smith Software Engineer Intel OTC --------------------------------------------------------------------- Intel Corporation (UK) Limited Registered No. 1134945 (England) Registered Office: Pipers Way, Swindon SN3 1RJ VAT No: 860 2173 47 This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.