diff --git a/core/tests.py b/core/tests.py index e28c6f7..dfbabfe 100644 --- a/core/tests.py +++ b/core/tests.py @@ -627,7 +627,6 @@ class CreateCompanyUserTest(APITestCase): } response = self.client.post(self.endpoint, data=data, format='json') - import ipdb; ipdb.set_trace() self.assertEquals(response.status_code, 201) self.assertEquals(len(mail.outbox), 1) diff --git a/core/views.py b/core/views.py index f69b2f0..53985d2 100644 --- a/core/views.py +++ b/core/views.py @@ -122,7 +122,6 @@ def create_company_user(request): # create company company_data = request.data['company'] - company_serializer = CompanySerializer( data=company_data, ) diff --git a/products/tests.py b/products/tests.py index a8c7cf2..f503677 100644 --- a/products/tests.py +++ b/products/tests.py @@ -7,7 +7,6 @@ from urllib.parse import quote from django.utils import timezone from django.test import TestCase from django.core import mail -from django.contrib.gis.geos import Point from rest_framework.test import APITestCase from rest_framework import status @@ -541,7 +540,7 @@ class ProductSearchTest(TestCase): company = CompanyFactory(company_name='Zapatos Rojos') expected_instances = [ self.factory(tags="lunares/rojos", category='zapatos', description="zapatos verdes"), - self.factory(tags="colores/rojos, tono/brillante"), # not showing up in results ??? + self.factory(tags="colores/rojos, tono/brillante"), self.factory(tags="lunares/azules", description="zapatos rojos"), self.factory(tags="lunares/rojos", description="zapatos"), self.factory(tags="lunares/verdes", company=company), @@ -568,7 +567,6 @@ class ProductSearchTest(TestCase): self.assertIsNotNone(payload.get('prices')) # check for object creation - # import ipdb; ipdb.set_trace() self.assertEquals(len(payload['products']), len(expected_instances)) # check for filters self.assertTrue(len(payload['filters']['tags']) >= 2 ) @@ -609,7 +607,7 @@ class ProductSearchTest(TestCase): def test_anon_user_can_paginate_search(self): expected_instances = [ self.factory(tags="lunares/rojos", category='zapatos', description="zapatos verdes"), - # self.factory(tags="colores/rojos, tono/brillante"), + self.factory(tags="colores/rojos, tono/brillante"), self.factory(tags="lunares/azules", description="zapatos rojos"), self.factory(tags="lunares/rojos", description="zapatos"), ] @@ -672,7 +670,7 @@ class ProductSearchTest(TestCase): def test_anon_user_can_filter_shipping_cost_true(self): expected_instances = [ - # self.factory(tags="colores/rojos, tono/brillante", shipping_cost=100.00), + self.factory(tags="colores/rojos, tono/brillante", shipping_cost=100.00), self.factory(tags="lunares/azules", description="zapatos rojos", shipping_cost=12.00), ] unexpected_instances = [ @@ -934,34 +932,6 @@ class ProductSearchTest(TestCase): # first instance should be most recent self.assertTrue(dates[i] < dates[i+1]) - def test_anon_user_can_search_geo(self): - """Restrict results by geographical location - """ - # create geo point - longitude = 1.0 - latitude = 1.0 - point = Point(longitude, latitude) - company = CompanyFactory(geo=point) - - expected_instances = [ - self.factory(tags="lunares/rojos", category='zapatos', description="zapatos verdes", company=company) for i in range(12) - ] - - unexpected_instances = [ - self.factory(description="chanclas"), - self.factory(tags="azules"), - ] - - q = quote("zapatos rojos") - - url = f"{self.endpoint}?q={q}&latitude=1.0&longitude=1.0" - # send in request - response = self.client.get(url) - # check response - self.assertEqual(response.status_code, 200) - payload = response.json() - self.assertIsNotNone(payload['georesult']) - self.assertEquals(payload['georesult'], '10k') class MyProductsViewTest(APITestCase): """my_products tests diff --git a/products/utils.py b/products/utils.py index fd9569b..c0f4c7e 100644 --- a/products/utils.py +++ b/products/utils.py @@ -7,8 +7,6 @@ from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector from django.db.models import Max, Min from django.conf import settings from django.utils import timezone -from django.contrib.gis.geos import Point -from django.contrib.gis.measure import D import requests @@ -126,7 +124,7 @@ def get_related_products(product): return total_results[:10] -def ranked_product_search(keywords, shipping_cost=None, discount=None, category=None, tags=None, price_min=None,price_max=None, coordinates=None): +def ranked_product_search(keyword, shipping_cost=None, discount=None, category=None, tags=None, price_min=None,price_max=None): """ Ranked product search @@ -136,37 +134,13 @@ def ranked_product_search(keywords, shipping_cost=None, discount=None, category= allow filtering by: - shipping cost - - Response includes: - - result_set - - min_price - - max_price - - georesult """ vector = SearchVector('name') + SearchVector('description') + SearchVector('tags__label') + SearchVector('attributes__label') + SearchVector('category__label') + SearchVector('company__company_name') - - query = SearchQuery(keywords, search_type='plain') + query = SearchQuery(keyword) products_qs = Product.objects.annotate( rank=SearchRank(vector, query) ).filter(rank__gt=0.05, active=True) - # geolocation filtering - if coordinates is not None: - point = Point(coordinates) - filtered_qs = products_qs.filter(company__geo__distance_lte=(point, D(km=10))) - georesult = '10k' - if filtered_qs.count() <= 10: - products_qs = products_qs.filter(company__geo__distance_lte=(point, D(km=50))) - georesult = '50k' - if filtered_qs.count() <= 10: - products_qs = products_qs.filter(company__geo__distance_lte=(point, D(km=200))) - georesult = '200k' - if filtered_qs.count() > 10: - products_qs = filtered_qs - else: - georesult = None - else: - georesult = None # filter by category if category is not None: @@ -209,7 +183,7 @@ def ranked_product_search(keywords, shipping_cost=None, discount=None, category= max_price = products_qs.aggregate(Max('price')) - return set(products_qs), min_price, max_price, georesult + return set(products_qs), min_price, max_price def product_loader(csv_reader, user, company=None): diff --git a/products/views.py b/products/views.py index 4b73d3d..8103f67 100644 --- a/products/views.py +++ b/products/views.py @@ -2,7 +2,6 @@ import logging import csv import datetime import json -from decimal import Decimal from django.db.models import Q from django.core import serializers @@ -20,7 +19,6 @@ from rest_framework.decorators import api_view, permission_classes, action from rest_framework.filters import OrderingFilter from django_filters.rest_framework import DjangoFilterBackend - import requests from history.models import HistorySync @@ -30,7 +28,7 @@ from products.models import Product, CategoryTag from products.serializers import ProductSerializer, TagFilterSerializer, SearchResultSerializer from companies.models import Company from stats.models import StatsLog -from back_latienda.permissions import IsCreator, IsSiteAdmin +from back_latienda.permissions import IsCreator, IsSiteAdmin, ReadOnly from .utils import extract_search_filters, ranked_product_search, product_loader, get_related_products from utils.tag_serializers import TaggitSerializer from utils.tag_filters import ProductTagFilter, ProductOrderFilter @@ -127,8 +125,8 @@ def product_search(request): Params: - q: used for search [MANDATORY] - - limit: max number of returned instances - - offset: where to start counting results + - limit: max number of returned instances [OPTIONAL] + - offset: where to start counting results [OPTIONAL] - shipping_cost: true/false - discount: true/false - category: string @@ -136,8 +134,6 @@ def product_search(request): - order: string (newest/oldest) - price_min: int - price_max: int - - longitude: 23.23 - - latitude: 22.234 In the response: - filters @@ -162,20 +158,11 @@ def product_search(request): discount = request.GET.get('discount', None) if discount is not None: if discount == 'true': - discount = True + discount = True elif discount == 'false': - discount = False + discount = False else: - discount = None - longitude = request.GET.get('longitude', None) - latitude = request.GET.get('latitude', None) - try: - if longitude and latitude: - coordinates = (Decimal(longitude), Decimal(latitude)) - else: - coordinates = None - except: - return Response({"error": "Improperly formated coordinates"}, status=406) + discount = None category = request.GET.get('category', None) tags = request.GET.get('tags', None) price_min = request.GET.get('price_min', None) @@ -207,16 +194,16 @@ def product_search(request): else: # split query string into single words chunks = q.split(' ') - # all-in-one search - product_set, min_price, max_price, georesult = ranked_product_search(q, shipping_cost, discount, category, tags, price_min, price_max, coordinates) - # update price values - if product_set: - if prices['min'] is None or min_price['price__min'] < prices['min']: - prices['min'] = min_price['price__min'] - if prices['max'] is None or max_price['price__max'] > prices['max']: - prices['max'] = max_price['price__max'] - # add to result set - result_set.update(product_set) + for chunk in chunks: + product_set, min_price, max_price = ranked_product_search(chunk, shipping_cost, discount, category, tags, price_min, price_max) + # update price values + if product_set: + if prices['min'] is None or min_price['price__min'] < prices['min']: + prices['min'] = min_price['price__min'] + if prices['max'] is None or max_price['price__max'] > prices['max']: + prices['max'] = max_price['price__max'] + # add to result set + result_set.update(product_set) # serialize and list data serializer = SearchResultSerializer(product_set, many=True) result_list = [dict(i) for i in serializer.data] @@ -243,9 +230,8 @@ def product_search(request): elif limit is not None: limit = int(limit) result_list = result_list[:limit] - return Response(data={"filters": filters, "count": total_results, "products": result_list, 'prices': prices, 'georesult': georesult}) + return Response(data={"filters": filters, "count": total_results, "products": result_list, 'prices': prices}) except Exception as e: - import ipdb; ipdb.set_trace() return Response({"errors": {"details": str(e)}}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/stats/views.py b/stats/views.py index 20ff805..a11193e 100644 --- a/stats/views.py +++ b/stats/views.py @@ -51,7 +51,6 @@ def track_user(request): """ try: data = json.loads(request.body) - # import ipdb; ipdb.set_trace() if data.get('geo'): coordinates = (data['geo'].get('latitude'), data['geo'].get('longitude'))