[Patchew-devel] [PATCH v2 07/10] search: add search by result

Paolo Bonzini pbonzini at redhat.com
Fri Sep 28 14:45:34 UTC 2018


Signed-off-by: Paolo Bonzini <pbonzini at redhat.com>
---
 api/search.py | 45 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)

diff --git a/api/search.py b/api/search.py
index 90e6596..a2644c5 100644
--- a/api/search.py
+++ b/api/search.py
@@ -8,10 +8,23 @@
 # This work is licensed under the MIT License.  Please see the LICENSE file or
 # http://opensource.org/licenses/MIT.
 
-from .models import Message, MessageProperty, Result
+from .models import Message, MessageProperty, MessageResult, Result
 from functools import reduce
 from django.db.models import Q
 
+from django.db.models import Lookup
+from django.db.models.fields import Field
+
+ at Field.register_lookup
+class NotEqual(Lookup):
+    lookup_name = 'ne'
+
+    def as_sql(self, compiler, connection):
+        lhs, lhs_params = self.process_lhs(compiler, connection)
+        rhs, rhs_params = self.process_rhs(compiler, connection)
+        params = lhs_params + rhs_params
+        return '%s <> %s' % (lhs, rhs), params
+
 class InvalidSearchTerm(Exception):
     pass
 
@@ -94,6 +107,21 @@ Compare the address info of message. Example:
 
 ---
 
+### Search by result
+
+Syntax:
+
+ - pending:NAME, success:NAME, failure:NAME, running:NAME
+
+where NAME can be e.g. "git", "testing", "testing.TEST-NAME"
+
+Example:
+
+    success:git
+    failure:testing.FreeBSD
+
+---
+
 ### Reverse condition
 
  - Syntax: !TERM
@@ -132,6 +160,10 @@ Search text keyword in the email message. Example:
         message_ids = model.objects.filter(q).values('message_id')
         return Q(id__in=message_ids)
 
+    def _make_filter_result(self, term, **kwargs):
+        q = Q(name=term, **kwargs) | Q(name__startswith=term+'.', **kwargs)
+        return self._make_filter_subquery(MessageResult, q)
+
     def _make_filter_age(self, cond):
         import datetime
         def human_to_seconds(n, unit):
@@ -217,6 +249,17 @@ Search text keyword in the email message. Example:
                 return Q(last_comment_date__isnull=False)
             else:
                 return Q(properties__name=cond)
+        elif term.startswith("failure:"):
+            return self._make_filter_result(term[8:], status=Result.FAILURE)
+        elif term.startswith("success:"):
+            # What we want is "all results are successes", but the only way to
+            # express it is "there is a result and not (any result is not a success)".
+            return self._make_filter_result(term[8:]) \
+                & ~self._make_filter_result(term[8:], status__ne=Result.SUCCESS)
+        elif term.startswith("pending:"):
+            return self._make_filter_result(term[8:], status=Result.PENDING)
+        elif term.startswith("running:"):
+            return self._make_filter_result(term[8:], status=Result.RUNNING)
         elif term.startswith("project:"):
             cond = term[term.find(":") + 1:]
             self._projects.add(cond)
-- 
2.17.1





More information about the Patchew-devel mailing list