Преглед на файлове

+ add or\and option to filter

ilya_shyian преди 2 години
родител
ревизия
0d7cc186e5
променени са 4 файла, в които са добавени 106 реда и са изтрити 23 реда
  1. 16 3
      store_back/apps/authAPI/schema.py
  2. 16 4
      store_back/apps/categories/schema.py
  3. 31 7
      store_back/apps/goods/schema.py
  4. 43 9
      store_back/apps/orders/schema.py

+ 16 - 3
store_back/apps/authAPI/schema.py

@@ -101,8 +101,14 @@ class Query(graphene.ObjectType):
 
 
         if len(filter_params):
-            query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
-
+            or_filter_params = filter_params.pop("or",{})
+            and_filter_params= filter_params.pop("and",{})
+            and_filter_params |= filter_params
+            if len(or_filter_params):
+                query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
+            if len(and_filter_params):
+                query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
+                
         query_set = query_set.order_by(order_by)[skip:skip+limit]
         return query_set
 
@@ -127,7 +133,14 @@ class Query(graphene.ObjectType):
 
 
         if len(filter_params):
-            query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
+            or_filter_params = filter_params.pop("or",{})
+            and_filter_params= filter_params.pop("and",{})
+            and_filter_params |= filter_params
+            if len(or_filter_params):
+                query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
+            if len(and_filter_params):
+                query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
+                
         return query_set.first()
 
 

+ 16 - 4
store_back/apps/categories/schema.py

@@ -72,8 +72,14 @@ class Query(graphene.ObjectType):
         query_set = Category.objects.all()
 
         if len(filter_params):
-            query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
-
+            or_filter_params = filter_params.pop("or",{})
+            and_filter_params= filter_params.pop("and",{})
+            and_filter_params |= filter_params
+            if len(or_filter_params):
+                query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
+            if len(and_filter_params):
+                query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
+                
         query_set = query_set.order_by(order_by)[skip:skip+limit]
         return query_set
 
@@ -92,8 +98,14 @@ class Query(graphene.ObjectType):
         query_set = Category.objects.all()
 
         if len(filter_params):
-            query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
-
+            or_filter_params = filter_params.pop("or",{})
+            and_filter_params= filter_params.pop("and",{})
+            and_filter_params |= filter_params
+            if len(or_filter_params):
+                query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
+            if len(and_filter_params):
+                query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
+                
         return query_set.first()
 
 

+ 31 - 7
store_back/apps/goods/schema.py

@@ -101,8 +101,14 @@ class Query(graphene.ObjectType):
 
 
         if len(filter_params):
-            query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
-
+            or_filter_params = filter_params.pop("or",{})
+            and_filter_params= filter_params.pop("and",{})
+            and_filter_params |= filter_params
+            if len(or_filter_params):
+                query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
+            if len(and_filter_params):
+                query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
+                
         if order_by == 'popular':
             query_set = query_set.annotate(order_count=Count('orderGoods')).order_by("-order_count")[skip:skip+limit]
         else:
@@ -125,8 +131,14 @@ class Query(graphene.ObjectType):
         query_set = Good.objects.all()
 
         if len(filter_params):
-            query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
-
+            or_filter_params = filter_params.pop("or",{})
+            and_filter_params= filter_params.pop("and",{})
+            and_filter_params |= filter_params
+            if len(or_filter_params):
+                query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
+            if len(and_filter_params):
+                query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
+                
         return query_set.first()
 
 
@@ -145,8 +157,14 @@ class Query(graphene.ObjectType):
         query_set = Image.objects.all()
 
         if len(filter_params):
-            query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
-
+            or_filter_params = filter_params.pop("or",{})
+            and_filter_params= filter_params.pop("and",{})
+            and_filter_params |= filter_params
+            if len(or_filter_params):
+                query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
+            if len(and_filter_params):
+                query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
+                
 
         return query_set
 
@@ -166,7 +184,13 @@ class Query(graphene.ObjectType):
 
         if len(filter_params):
             try:
-                query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
+                or_filter_params = filter_params.pop("or",{})
+                and_filter_params= filter_params.pop("and",{})
+                and_filter_params |= filter_params
+                if len(or_filter_params):
+                    query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
+                if len(and_filter_params):
+                    query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
             except:
                 raise Exception("Не вірні дані")
         return query_set.first()

+ 43 - 9
store_back/apps/orders/schema.py

@@ -137,9 +137,16 @@ class Query(graphene.ObjectType):
             query_set = Order.objects.filter(owner = user)
 
 
-        if len(filter_params):
-            query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
 
+        if len(filter_params):
+            or_filter_params = filter_params.pop("or",{})
+            and_filter_params= filter_params.pop("and",{})
+            and_filter_params |= filter_params
+            if len(or_filter_params):
+                query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
+            if len(and_filter_params):
+                query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
+                
         query_set = query_set.order_by(order_by)[skip:skip+limit]
         return query_set
 
@@ -162,8 +169,14 @@ class Query(graphene.ObjectType):
             query_set = Order.objects.filter(owner = user)
 
         if len(filter_params):
-            query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
-
+            or_filter_params = filter_params.pop("or",{})
+            and_filter_params= filter_params.pop("and",{})
+            and_filter_params |= filter_params
+            if len(or_filter_params):
+                query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
+            if len(and_filter_params):
+                query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
+                
         return query_set.first()
 
 
@@ -181,9 +194,16 @@ class Query(graphene.ObjectType):
 
         query_set = OrderGood.objects.all()
 
-        if len(filter_params):
-            query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
 
+        if len(filter_params):
+            or_filter_params = filter_params.pop("or",{})
+            and_filter_params= filter_params.pop("and",{})
+            and_filter_params |= filter_params
+            if len(or_filter_params):
+                query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
+            if len(and_filter_params):
+                query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
+                
 
         return query_set
 
@@ -202,8 +222,14 @@ class Query(graphene.ObjectType):
         query_set = OrderGood.objects.all()
 
         if len(filter_params):
-            query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
-
+            or_filter_params = filter_params.pop("or",{})
+            and_filter_params= filter_params.pop("and",{})
+            and_filter_params |= filter_params
+            if len(or_filter_params):
+                query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
+            if len(and_filter_params):
+                query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
+                
         return query_set.first()
 
 
@@ -240,8 +266,10 @@ class OrderUpsert(graphene.Mutation):
         if "owner" in order:
             try:
                 owner = User.objects.get(pk =order.pop("owner")["_id"])
+
             except:
                 raise Exception("Не вірні дані")
+        
 
         try:
             if "_id" in order:
@@ -258,7 +286,13 @@ class OrderUpsert(graphene.Mutation):
 
             else:
                 new_order = Order(**order)
-                new_order.owner = user
+                if owner:
+                    if not user.is_superuser:
+                        raise Exception("Authentication credentials were not provided")
+
+                    new_order.owner = owner
+                else:
+                    new_order.owner = user
 
         except Exception as e:
             raise Exception("Не вірні дані")