mirror of
				https://github.com/magnum-opus-tender-hack/backend.git
				synced 2025-11-04 00:27:27 +03:00 
			
		
		
		
	added exclude autocomplete
This commit is contained in:
		
							parent
							
								
									b301b8103c
								
							
						
					
					
						commit
						c68dfce10a
					
				| 
						 | 
					@ -58,6 +58,7 @@ class HintResponseSerializer(serializers.Serializer):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class AutoCompleteRequestSerializer(serializers.Serializer):
 | 
					class AutoCompleteRequestSerializer(serializers.Serializer):
 | 
				
			||||||
    content = serializers.CharField(validators=[MinLengthValidator(3)])
 | 
					    content = serializers.CharField(validators=[MinLengthValidator(3)])
 | 
				
			||||||
 | 
					    exclude = serializers.ListSerializer(child=QueryFilterSerializer())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def create(self, validated_data):
 | 
					    def create(self, validated_data):
 | 
				
			||||||
        raise NotImplementedError
 | 
					        raise NotImplementedError
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -64,6 +64,6 @@ class AutoCompleteApi(APIView):
 | 
				
			||||||
        serializer = AutoCompleteRequestSerializer(data=request.data)
 | 
					        serializer = AutoCompleteRequestSerializer(data=request.data)
 | 
				
			||||||
        serializer.is_valid(raise_exception=True)
 | 
					        serializer.is_valid(raise_exception=True)
 | 
				
			||||||
        return Response(
 | 
					        return Response(
 | 
				
			||||||
            {"nodes": autocomplete_schema(serializer.data["content"])},
 | 
					            {"nodes": autocomplete_schema(serializer.data["content"], serializer.data["exclude"])},
 | 
				
			||||||
            status=status.HTTP_200_OK,
 | 
					            status=status.HTTP_200_OK,
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,8 +1,16 @@
 | 
				
			||||||
 | 
					from typing import List, Dict
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from search.models import Product, Category, Characteristic
 | 
					from search.models import Product, Category, Characteristic
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def autocomplete_schema(val: str):
 | 
					def autocomplete_schema(val: str, exclude: List[Dict]):
 | 
				
			||||||
    schema = [
 | 
					    exclude = [dict(x) for x in exclude]
 | 
				
			||||||
 | 
					    name_exclude = [x["value"] for x in exclude if x["type"] == "Name"]
 | 
				
			||||||
 | 
					    category_exclude = [x["value"] for x in exclude if x["type"] == "Category"]
 | 
				
			||||||
 | 
					    schema = []
 | 
				
			||||||
 | 
					    if not name_exclude:
 | 
				
			||||||
 | 
					        schema.extend(
 | 
				
			||||||
 | 
					            [
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    "coordinate": product["name"].lower().index(val.lower()),
 | 
					                    "coordinate": product["name"].lower().index(val.lower()),
 | 
				
			||||||
                    "value": {
 | 
					                    "value": {
 | 
				
			||||||
| 
						 | 
					@ -10,19 +18,21 @@ def autocomplete_schema(val: str):
 | 
				
			||||||
                        "value": product["name"],
 | 
					                        "value": product["name"],
 | 
				
			||||||
                    },
 | 
					                    },
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
        for product in Product.objects.filter(name__unaccent__icontains=val).values(
 | 
					                for product in Product.objects.filter(
 | 
				
			||||||
            "name"
 | 
					                    name__unaccent__icontains=val
 | 
				
			||||||
        )
 | 
					                ).values("name")
 | 
				
			||||||
            ]
 | 
					            ]
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					    if not category_exclude:
 | 
				
			||||||
        schema.extend(
 | 
					        schema.extend(
 | 
				
			||||||
            [
 | 
					            [
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    "coordinate": cat["name"].lower().index(val.lower()),
 | 
					                    "coordinate": cat["name"].lower().index(val.lower()),
 | 
				
			||||||
                    "value": {"type": "Category", "value": cat["name"]},
 | 
					                    "value": {"type": "Category", "value": cat["name"]},
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            for cat in Category.objects.filter(name__unaccent__icontains=val).values(
 | 
					                for cat in Category.objects.filter(
 | 
				
			||||||
                "name"
 | 
					                    name__unaccent__icontains=val
 | 
				
			||||||
            )
 | 
					                ).values("name")
 | 
				
			||||||
            ]
 | 
					            ]
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
    schema.extend(
 | 
					    schema.extend(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user