search function basics
This commit is contained in:
@@ -34,5 +34,6 @@ urlpatterns = [
|
|||||||
path('api/v1/user/update/<int:pk>/', core_views.UpdateUserView.as_view(), name="update-user"),
|
path('api/v1/user/update/<int:pk>/', core_views.UpdateUserView.as_view(), name="update-user"),
|
||||||
path('api/v1/load_coops/', core_views.load_coop_managers, name='coop-loader'),
|
path('api/v1/load_coops/', core_views.load_coop_managers, name='coop-loader'),
|
||||||
path('api/v1/load_products/', product_views.load_coop_products, name='product-loader'),
|
path('api/v1/load_products/', product_views.load_coop_products, name='product-loader'),
|
||||||
|
path('api/v1/search_products/', product_views.product_search, name='product-search'),
|
||||||
path('api/v1/', include(router.urls)),
|
path('api/v1/', include(router.urls)),
|
||||||
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ import random
|
|||||||
import string
|
import string
|
||||||
import json
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
|
from urllib.parse import quote
|
||||||
|
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
from rest_framework.test import APITestCase
|
from rest_framework.test import APITestCase
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
@@ -334,3 +336,35 @@ class LoadCoopProductsTestCase(APITestCase):
|
|||||||
# check for object creation
|
# check for object creation
|
||||||
self.assertEqual(0, self.model.objects.count())
|
self.assertEqual(0, self.model.objects.count())
|
||||||
|
|
||||||
|
|
||||||
|
class ProductSearchTest(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
"""Tests setup
|
||||||
|
"""
|
||||||
|
self.endpoint = '/api/v1/search_products/'
|
||||||
|
self.model = Product
|
||||||
|
self.factory = ProductFactory
|
||||||
|
# create admin user
|
||||||
|
self.admin_email = f"admin_user@mail.com"
|
||||||
|
self.password = ''.join(random.choices(string.ascii_uppercase, k = 10))
|
||||||
|
self.admin_user = CustomUserFactory(email=self.admin_email, password=self.password, is_staff=True, is_active=True)
|
||||||
|
# create regular user
|
||||||
|
self.reg_email = f"user@mail.com"
|
||||||
|
self.user = CustomUserFactory(email=self.reg_email, is_active=True)
|
||||||
|
self.user.set_password(self.password)
|
||||||
|
self.user.save()
|
||||||
|
|
||||||
|
def test_anon_user_can_search(self):
|
||||||
|
query_string = quote("zapatos rojos")
|
||||||
|
|
||||||
|
url = f"{self.endpoint}?query_string={query_string}"
|
||||||
|
# send in request
|
||||||
|
response = self.client.get(url)
|
||||||
|
import ipdb; ipdb.set_trace()
|
||||||
|
|
||||||
|
# check re sponse
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
# check for object creation
|
||||||
|
self.assertEquals(5, self.model.objects.count())
|
||||||
|
|
||||||
|
|||||||
@@ -100,3 +100,42 @@ def load_coop_products(request):
|
|||||||
return Response({"errors": {"details": str(type(e))}})
|
return Response({"errors": {"details": str(type(e))}})
|
||||||
|
|
||||||
|
|
||||||
|
@api_view(['GET',]) # include allowed methods
|
||||||
|
def product_search(request):
|
||||||
|
"""
|
||||||
|
Takes a string of data, return relevant products
|
||||||
|
"""
|
||||||
|
query_string = request.GET.get('query_string', None)
|
||||||
|
if query_string is None:
|
||||||
|
return Response({"errors": {"details": "No query string to parse"}})
|
||||||
|
try:
|
||||||
|
chunks = query_string.split(' ')
|
||||||
|
|
||||||
|
result_set = set()
|
||||||
|
import ipdb; ipdb.set_trace()
|
||||||
|
for chunk in chunks:
|
||||||
|
import ipdb; ipdb.set_trace()
|
||||||
|
# search in name
|
||||||
|
products = Product.objects.filter(name__in=chunk)
|
||||||
|
for item in products:
|
||||||
|
result_set.add(item)
|
||||||
|
# search in description
|
||||||
|
products = Product.objects.filter(description__in=chunk)
|
||||||
|
for item in products:
|
||||||
|
result_set.add(item)
|
||||||
|
# search in tags
|
||||||
|
products = Product.objects.filter(tags__in=chunk)
|
||||||
|
for item in products:
|
||||||
|
result_set.add(item)
|
||||||
|
# search in category
|
||||||
|
products = Product.objects.filter(category__in=chunk)
|
||||||
|
for item in products:
|
||||||
|
result_set.add(item)
|
||||||
|
# search in attributes
|
||||||
|
products = Product.objects.filter(attributes__in=chunk)
|
||||||
|
for item in products:
|
||||||
|
result_set.add(item)
|
||||||
|
|
||||||
|
return Response(data=result_set)
|
||||||
|
except Exception as e:
|
||||||
|
return Response({"errors": {"details": str(type(e))}})
|
||||||
|
|||||||
Reference in New Issue
Block a user