Patchwork [bitbake-devel,44/94] bitbake: webhob: add search for build model

login
register
mail settings
Submitter Alexandru DAMIAN
Date Sept. 24, 2013, 4:52 p.m.
Message ID <22462956f2759c325a41c9b46e4e867e967e1740.1380041477.git.alexandru.damian@intel.com>
Download mbox | patch
Permalink /patch/58737/
State New
Headers show

Comments

Alexandru DAMIAN - Sept. 24, 2013, 4:52 p.m.
From: Calin Dragomir <calinx.l.dragomir@intel.com>

This patch enables search on the build model. However, the
search can be executed only on a predefined list of fields
allowed to be searched.

Signed-off-by: Calin Dragomir <calinx.l.dragomir@intel.com>
Signed-off-by: Alexandru DAMIAN <alexandru.damian@intel.com>
---
 bitbake/lib/webhob/bldviewer/views.py | 17 +++++++++++++++++
 bitbake/lib/webhob/orm/models.py      |  3 +++
 2 files changed, 20 insertions(+)

Patch

diff --git a/bitbake/lib/webhob/bldviewer/views.py b/bitbake/lib/webhob/bldviewer/views.py
index 3ef6940..423fdf7 100644
--- a/bitbake/lib/webhob/bldviewer/views.py
+++ b/bitbake/lib/webhob/bldviewer/views.py
@@ -1,3 +1,6 @@ 
+import operator
+
+from django.db.models import Q
 from django.shortcuts import render
 from orm.models import Build, Task, Layer, Layer_Version, Task_Dependency, Recipe, Package
 
@@ -93,6 +96,8 @@  def model_explorer(request, model_name):
     if invalid:
         return HttpResponseBadRequest()
 
+    search_term = request.GET.get('search', '')
+
     if filter_string:
         filter_terms = _get_filtering_terms(filter_string)
         try:
@@ -102,6 +107,9 @@  def model_explorer(request, model_name):
     else:
         queryset = model.objects.all()
 
+    if search_term:
+        queryset = _get_search_results(search_term, queryset, model)
+
     if offset and limit:
         queryset = queryset[offset:(offset+limit)]
     elif offset:
@@ -159,3 +167,12 @@  def _validate_input(input, model):
                 return None, invalid
 
     return input, invalid
+
+def _get_search_results(search_term, queryset, model):
+    q_map = map(lambda x: Q(**{x+'__icontains': search_term}),
+                model.search_allowed_fields)
+
+    search_object = reduce(operator.or_, q_map)
+    queryset = queryset.filter(search_object)
+
+    return queryset
diff --git a/bitbake/lib/webhob/orm/models.py b/bitbake/lib/webhob/orm/models.py
index 0d6cf15..a4721db 100644
--- a/bitbake/lib/webhob/orm/models.py
+++ b/bitbake/lib/webhob/orm/models.py
@@ -12,6 +12,9 @@  class Build(models.Model):
         (IN_PROGRESS, 'In Progress'),
     )
 
+    search_allowed_fields = ['target', 'machine__name',
+                             'cooker_log_path', 'image_fstypes']
+
     uuid = models.CharField(max_length=100, unique=True)
     target = models.CharField(max_length=100)
     is_image = models.BooleanField(default = False)