From eaaa6445abc2ddae6e78fd52949c4f6b2850a495 Mon Sep 17 00:00:00 2001
From: Will Thompson <will@willthompson.co.uk>
Date: Wed, 27 Jan 2016 12:25:42 +0000
Subject: [PATCH] findEntities: fix negated conditions, fixes #2876

---
 pandora/entity/managers.py | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/pandora/entity/managers.py b/pandora/entity/managers.py
index 47b725f..718235a 100644
--- a/pandora/entity/managers.py
+++ b/pandora/entity/managers.py
@@ -27,14 +27,18 @@ def parseCondition(condition, user, item=None):
 
     v = condition['value']
     op = condition.get('operator')
-    find_key = None
     if not op:
         op = '='
+
     if op.startswith('!'):
-        op = op[1:]
-        exclude = True
+        return ~buildCondition(k, op[1:], v)
     else:
-        exclude = False
+        return buildCondition(k, op, v)
+
+
+def buildCondition(k, op, v):
+    find_key = None
+
     if k == 'id':
         v = ox.fromAZ(v)
         return Q(**{k: v})
@@ -55,16 +59,10 @@ def parseCondition(condition, user, item=None):
         }.get(op, '__icontains'))
     key = str(key)
     if find_key:
-        if exclude:
-            q = Q(**{'find__key': find_key, key: v})
-        else:
-            q = Q(**{'find__key': find_key, key: v})
+        return Q(**{'find__key': find_key, key: v})
     else:
-        if exclude:
-            q = ~Q(**{key: v})
-        else:
-            q = Q(**{key: v})
-    return q
+        return Q(**{key: v})
+
 
 def parseConditions(conditions, operator, user, item=None):
     '''
-- 
2.5.0