removed geo search code, moved it to geosearch branch
This commit is contained in:
@@ -627,7 +627,6 @@ class CreateCompanyUserTest(APITestCase):
|
|||||||
}
|
}
|
||||||
|
|
||||||
response = self.client.post(self.endpoint, data=data, format='json')
|
response = self.client.post(self.endpoint, data=data, format='json')
|
||||||
import ipdb; ipdb.set_trace()
|
|
||||||
|
|
||||||
self.assertEquals(response.status_code, 201)
|
self.assertEquals(response.status_code, 201)
|
||||||
self.assertEquals(len(mail.outbox), 1)
|
self.assertEquals(len(mail.outbox), 1)
|
||||||
|
|||||||
@@ -122,7 +122,6 @@ def create_company_user(request):
|
|||||||
|
|
||||||
# create company
|
# create company
|
||||||
company_data = request.data['company']
|
company_data = request.data['company']
|
||||||
|
|
||||||
company_serializer = CompanySerializer(
|
company_serializer = CompanySerializer(
|
||||||
data=company_data,
|
data=company_data,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ from urllib.parse import quote
|
|||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.core import mail
|
from django.core import mail
|
||||||
from django.contrib.gis.geos import Point
|
|
||||||
|
|
||||||
from rest_framework.test import APITestCase
|
from rest_framework.test import APITestCase
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
@@ -541,7 +540,7 @@ class ProductSearchTest(TestCase):
|
|||||||
company = CompanyFactory(company_name='Zapatos Rojos')
|
company = CompanyFactory(company_name='Zapatos Rojos')
|
||||||
expected_instances = [
|
expected_instances = [
|
||||||
self.factory(tags="lunares/rojos", category='zapatos', description="zapatos verdes"),
|
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/azules", description="zapatos rojos"),
|
||||||
self.factory(tags="lunares/rojos", description="zapatos"),
|
self.factory(tags="lunares/rojos", description="zapatos"),
|
||||||
self.factory(tags="lunares/verdes", company=company),
|
self.factory(tags="lunares/verdes", company=company),
|
||||||
@@ -568,7 +567,6 @@ class ProductSearchTest(TestCase):
|
|||||||
self.assertIsNotNone(payload.get('prices'))
|
self.assertIsNotNone(payload.get('prices'))
|
||||||
|
|
||||||
# check for object creation
|
# check for object creation
|
||||||
# import ipdb; ipdb.set_trace()
|
|
||||||
self.assertEquals(len(payload['products']), len(expected_instances))
|
self.assertEquals(len(payload['products']), len(expected_instances))
|
||||||
# check for filters
|
# check for filters
|
||||||
self.assertTrue(len(payload['filters']['tags']) >= 2 )
|
self.assertTrue(len(payload['filters']['tags']) >= 2 )
|
||||||
@@ -609,7 +607,7 @@ class ProductSearchTest(TestCase):
|
|||||||
def test_anon_user_can_paginate_search(self):
|
def test_anon_user_can_paginate_search(self):
|
||||||
expected_instances = [
|
expected_instances = [
|
||||||
self.factory(tags="lunares/rojos", category='zapatos', description="zapatos verdes"),
|
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/azules", description="zapatos rojos"),
|
||||||
self.factory(tags="lunares/rojos", description="zapatos"),
|
self.factory(tags="lunares/rojos", description="zapatos"),
|
||||||
]
|
]
|
||||||
@@ -672,7 +670,7 @@ class ProductSearchTest(TestCase):
|
|||||||
|
|
||||||
def test_anon_user_can_filter_shipping_cost_true(self):
|
def test_anon_user_can_filter_shipping_cost_true(self):
|
||||||
expected_instances = [
|
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),
|
self.factory(tags="lunares/azules", description="zapatos rojos", shipping_cost=12.00),
|
||||||
]
|
]
|
||||||
unexpected_instances = [
|
unexpected_instances = [
|
||||||
@@ -934,34 +932,6 @@ class ProductSearchTest(TestCase):
|
|||||||
# first instance should be most recent
|
# first instance should be most recent
|
||||||
self.assertTrue(dates[i] < dates[i+1])
|
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):
|
class MyProductsViewTest(APITestCase):
|
||||||
"""my_products tests
|
"""my_products tests
|
||||||
|
|||||||
@@ -7,8 +7,6 @@ from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
|
|||||||
from django.db.models import Max, Min
|
from django.db.models import Max, Min
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.contrib.gis.geos import Point
|
|
||||||
from django.contrib.gis.measure import D
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
@@ -126,7 +124,7 @@ def get_related_products(product):
|
|||||||
return total_results[:10]
|
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
|
Ranked product search
|
||||||
|
|
||||||
@@ -136,37 +134,13 @@ def ranked_product_search(keywords, shipping_cost=None, discount=None, category=
|
|||||||
|
|
||||||
allow filtering by:
|
allow filtering by:
|
||||||
- shipping cost
|
- 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')
|
vector = SearchVector('name') + SearchVector('description') + SearchVector('tags__label') + SearchVector('attributes__label') + SearchVector('category__label') + SearchVector('company__company_name')
|
||||||
|
query = SearchQuery(keyword)
|
||||||
query = SearchQuery(keywords, search_type='plain')
|
|
||||||
|
|
||||||
products_qs = Product.objects.annotate(
|
products_qs = Product.objects.annotate(
|
||||||
rank=SearchRank(vector, query)
|
rank=SearchRank(vector, query)
|
||||||
).filter(rank__gt=0.05, active=True)
|
).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
|
# filter by category
|
||||||
if category is not None:
|
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'))
|
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):
|
def product_loader(csv_reader, user, company=None):
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import logging
|
|||||||
import csv
|
import csv
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
from decimal import Decimal
|
|
||||||
|
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.core import serializers
|
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 rest_framework.filters import OrderingFilter
|
||||||
|
|
||||||
from django_filters.rest_framework import DjangoFilterBackend
|
from django_filters.rest_framework import DjangoFilterBackend
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from history.models import HistorySync
|
from history.models import HistorySync
|
||||||
@@ -30,7 +28,7 @@ from products.models import Product, CategoryTag
|
|||||||
from products.serializers import ProductSerializer, TagFilterSerializer, SearchResultSerializer
|
from products.serializers import ProductSerializer, TagFilterSerializer, SearchResultSerializer
|
||||||
from companies.models import Company
|
from companies.models import Company
|
||||||
from stats.models import StatsLog
|
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 import extract_search_filters, ranked_product_search, product_loader, get_related_products
|
||||||
from utils.tag_serializers import TaggitSerializer
|
from utils.tag_serializers import TaggitSerializer
|
||||||
from utils.tag_filters import ProductTagFilter, ProductOrderFilter
|
from utils.tag_filters import ProductTagFilter, ProductOrderFilter
|
||||||
@@ -127,8 +125,8 @@ def product_search(request):
|
|||||||
|
|
||||||
Params:
|
Params:
|
||||||
- q: used for search [MANDATORY]
|
- q: used for search [MANDATORY]
|
||||||
- limit: max number of returned instances
|
- limit: max number of returned instances [OPTIONAL]
|
||||||
- offset: where to start counting results
|
- offset: where to start counting results [OPTIONAL]
|
||||||
- shipping_cost: true/false
|
- shipping_cost: true/false
|
||||||
- discount: true/false
|
- discount: true/false
|
||||||
- category: string
|
- category: string
|
||||||
@@ -136,8 +134,6 @@ def product_search(request):
|
|||||||
- order: string (newest/oldest)
|
- order: string (newest/oldest)
|
||||||
- price_min: int
|
- price_min: int
|
||||||
- price_max: int
|
- price_max: int
|
||||||
- longitude: 23.23
|
|
||||||
- latitude: 22.234
|
|
||||||
|
|
||||||
In the response:
|
In the response:
|
||||||
- filters
|
- filters
|
||||||
@@ -167,15 +163,6 @@ def product_search(request):
|
|||||||
discount = False
|
discount = False
|
||||||
else:
|
else:
|
||||||
discount = None
|
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)
|
|
||||||
category = request.GET.get('category', None)
|
category = request.GET.get('category', None)
|
||||||
tags = request.GET.get('tags', None)
|
tags = request.GET.get('tags', None)
|
||||||
price_min = request.GET.get('price_min', None)
|
price_min = request.GET.get('price_min', None)
|
||||||
@@ -207,8 +194,8 @@ def product_search(request):
|
|||||||
else:
|
else:
|
||||||
# split query string into single words
|
# split query string into single words
|
||||||
chunks = q.split(' ')
|
chunks = q.split(' ')
|
||||||
# all-in-one search
|
for chunk in chunks:
|
||||||
product_set, min_price, max_price, georesult = ranked_product_search(q, shipping_cost, discount, category, tags, price_min, price_max, coordinates)
|
product_set, min_price, max_price = ranked_product_search(chunk, shipping_cost, discount, category, tags, price_min, price_max)
|
||||||
# update price values
|
# update price values
|
||||||
if product_set:
|
if product_set:
|
||||||
if prices['min'] is None or min_price['price__min'] < prices['min']:
|
if prices['min'] is None or min_price['price__min'] < prices['min']:
|
||||||
@@ -243,9 +230,8 @@ def product_search(request):
|
|||||||
elif limit is not None:
|
elif limit is not None:
|
||||||
limit = int(limit)
|
limit = int(limit)
|
||||||
result_list = result_list[: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:
|
except Exception as e:
|
||||||
import ipdb; ipdb.set_trace()
|
|
||||||
return Response({"errors": {"details": str(e)}}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
return Response({"errors": {"details": str(e)}}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,6 @@ def track_user(request):
|
|||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
data = json.loads(request.body)
|
data = json.loads(request.body)
|
||||||
# import ipdb; ipdb.set_trace()
|
|
||||||
|
|
||||||
if data.get('geo'):
|
if data.get('geo'):
|
||||||
coordinates = (data['geo'].get('latitude'), data['geo'].get('longitude'))
|
coordinates = (data['geo'].get('latitude'), data['geo'].get('longitude'))
|
||||||
|
|||||||
Reference in New Issue
Block a user