Merge branch 'development' into diego

This commit is contained in:
Diego Calvo
2021-03-11 08:37:33 +01:00
11 changed files with 187 additions and 113 deletions

View File

@@ -14,7 +14,6 @@ from rest_framework import status
from companies.factories import CompanyFactory
from products.factories import ProductFactory, ActiveProductFactory
from products.models import Product
from products.utils import find_related_products_v3
from core.factories import CustomUserFactory
from core.utils import get_tokens_for_user
@@ -196,19 +195,23 @@ class ProductViewSetTest(APITestCase):
self.assertEquals(len(expected_instance), len(payload))
def test_anon_can_get_related_products(self):
tag = 'cosa'
company = CompanyFactory()
# Create instances
instance = self.factory()
# make our user the creator
instance.creator = self.user
instance.save()
url = f"{self.endpoint}{instance.id}/related/"
instances = [self.factory(tags=tag, company=company) for i in range(10)]
url = f"{self.endpoint}{instances[0].id}/related/"
response = self.client.get(url)
self.assertEquals(response.status_code, 200)
payload= response.json()
self.assertTrue(len(payload) <= 6)
self.assertTrue(len(payload) <= 10)
# authenticated user
def test_auth_user_can_paginate_instances(self):
@@ -1159,44 +1162,6 @@ class AdminProductViewSetTest(APITestCase):
self.assertEquals(response.status_code, 204)
class FindRelatedProductsTest(APITestCase):
def setUp(self):
"""Tests setup
"""
self.factory = ActiveProductFactory
self.model = Product
# clear table
self.model.objects.all().delete()
def test_v3_find_by_tags(self):
# create tagged product
tag = 'cool'
expected_instances = [
self.factory(tags=tag),
self.factory(tags=f'{tag} hat'),
self.factory(tags=f'temperatures/{tag}'),
self.factory(tags=f'temperatures/{tag}, body/hot'),
self.factory(tags=f'temperatures/{tag}, hats/{tag}'),
# multiple hits
self.factory(tags=tag, attributes=tag),
self.factory(tags=tag, attributes=tag, category=tag),
self.factory(tags=tag, attributes=tag, category=tag, name=tag),
self.factory(tags=tag, attributes=tag, category=tag, name=tag, description=tag),
]
unexpected_instances = [
self.factory(tags="notcool"), # shouldn't catch it
self.factory(tags="azules"),
]
# searh for it
results = find_related_products_v3(tag)
# assert result
self.assertTrue(len(results) == len(expected_instances))
class PurchaseEmailTest(APITestCase):
def setUp(self):
@@ -1233,9 +1198,6 @@ class PurchaseEmailTest(APITestCase):
def test_auth_user_can_use(self):
company = CompanyFactory()
self.user.role = 'COOP_MANAGER'
self.user.company = company
self.user.save()
product = ProductFactory(company=company)
data = {
@@ -1253,3 +1215,21 @@ class PurchaseEmailTest(APITestCase):
self.assertEquals(response.status_code, 200)
self.assertEquals(2, len(mail.outbox))
def test_anon_user_bad_email(self):
company = CompanyFactory()
product = ProductFactory(company=company)
data = {
'email': '324r@qwer',
'telephone': '123123123',
'company': company.id,
'product': product.id,
'comment': '',
}
response = self.client.post(self.endpoint, data=data, format='json')
# assertions
self.assertEquals(response.status_code, 406)
payload = response.json()
self.assertTrue( 'email' in payload['error'])

View File

@@ -85,35 +85,45 @@ def extract_search_filters(result_set):
return filter_dict
def find_related_products_v7(description, tags, attributes, category):
products_qs = Product.objects.filter(
description=description,
tags__in=tags,
attributes__in=attributes,
category=category
)[:6]
return products_qs
def find_related_products_v3(keyword):
def get_related_products(product):
"""Make different db searches until you get 10 instances to return
"""
Ranked product search
total_results = []
SearchVectors for the fields
SearchQuery for the value
SearchRank for relevancy scoring and ranking
"""
vector = SearchVector('name') + SearchVector('description') + SearchVector('tags__label') + SearchVector('attributes__label') + SearchVector('category__name')
query = SearchQuery(keyword)
# search by category
category_qs = Product.objects.filter(category=product.category)[:10]
# add to results
for item in category_qs:
total_results.append(item)
products_qs = Product.objects.annotate(
rank=SearchRank(vector, query)
).filter(rank__gt=0.05) # removed order_by because its lost in casting
# check size
if len(total_results) < 10:
# search by tags
tags_qs = Product.objects.filter(tags__in=product.tags.all())[:10]
# add to results
for item in tags_qs:
total_results.append(item)
return set(products_qs)
# check size
if len(total_results) < 10:
# search by coop
coop_qs = Product.objects.filter(company=product.company)[:10]
# add to results
for item in coop_qs:
total_results.append(item)
# check size
if len(total_results) < 10:
# search by latest
latest_qs = Product.objects.order_by('-created')[:10]
# add to results
for item in coop_qs:
total_results.append(item)
return total_results[:10]
def find_related_products_v6(keyword, shipping_cost=None, discount=None, category=None, tags=None, price_min=None,price_max=None):
def ranked_product_search(keyword, shipping_cost=None, discount=None, category=None, tags=None, price_min=None,price_max=None):
"""
Ranked product search

View File

@@ -5,6 +5,7 @@ import json
from django.db.models import Q
from django.core import serializers
from django.core.validators import validate_email
from django.contrib.auth import get_user_model
from django.template.loader import render_to_string
from django.core.mail import EmailMessage
@@ -28,7 +29,7 @@ from products.serializers import ProductSerializer, TagFilterSerializer, SearchR
from companies.models import Company
from stats.models import StatsLog
from back_latienda.permissions import IsCreator, IsSiteAdmin, ReadOnly
from .utils import extract_search_filters, find_related_products_v6, product_loader, find_related_products_v7
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
@@ -58,7 +59,7 @@ class ProductViewSet(viewsets.ModelViewSet):
"""
# TODO: find the most similar products
product = self.get_object()
qs = find_related_products_v7(product.description, product.tags.all(), product.attributes.all(), product.category)
qs = get_related_products(product)
serializer = self.serializer_class(qs, many=True)
return Response(data=serializer.data)
@@ -194,7 +195,7 @@ def product_search(request):
# split query string into single words
chunks = q.split(' ')
for chunk in chunks:
product_set, min_price, max_price = find_related_products_v6(chunk, shipping_cost, discount, category, tags, price_min, price_max)
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']:
@@ -259,7 +260,6 @@ def purchase_email(request):
# check data
if request.user.is_anonymous and 'email' not in data:
return Response({"error": "Anonymous users must include an email parameter value"}, status=status.HTTP_406_NOT_ACCEPTABLE)
try:
for param in ('telephone', 'company', 'product', 'comment'):
assert(param in data.keys())
@@ -267,44 +267,50 @@ def purchase_email(request):
return Response({"error": "Required parameters for anonymous user: telephone, company, product, comment"}, status=status.HTTP_406_NOT_ACCEPTABLE)
if request.user.is_anonymous:
email = data.get('email')
user_email = data.get('email')
else:
email = request.user.email
user_email = request.user.email
telephone = data.get('telephone')
# validate email
try:
validate_email(user_email)
except:
return Response({"error": "Value for email is not valid"}, status=status.HTTP_406_NOT_ACCEPTABLE)
# get company
company = Company.objects.filter(id=data['company']).first()
if not company:
return Response({"error": "Invalid value for company"}, status=status.HTTP_406_NOT_ACCEPTABLE)
# get company manager
manager = User.objects.filter(company=company).first()
if not manager or manager.role != 'COOP_MANAGER':
return Response({"error": "Company has no managing user"}, status=status.HTTP_406_NOT_ACCEPTABLE)
# get product
product = Product.objects.filter(id=data['product'], company=company).first()
if not product:
return Response({"error": "Invalid value for product"}, status=status.HTTP_406_NOT_ACCEPTABLE)
# send email to company manager
manager_message = render_to_string('purchase_notification.html', {
'company': company,
'user': request.user,
'product': product,
'telephone': data['telephone'],
})
subject = "Contacto de usuario sobre venta"
email = EmailMessage(subject, manager_message, to=[manager.email])
email.send()
logging.info(f"Email sent to {manager.email} as manager of {company}")
# send confirmation email to user
user_message = render_to_string('purchase_contact_confirmation.html', {
'company': company,
'product': product,
})
subject = 'Confirmación de contacto con vendedor'
email = EmailMessage(subject, user_message, to=[email])
email.send()
logging.info(f"Purchase Contact confirmation email sent to {email}")
# check company.email
if company.email is None:
return Response({"error": "Related compay has no contact email address"}, status=status.HTTP_406_NOT_ACCEPTABLE)
try:
# send email to company
company_message = render_to_string('purchase_notification.html', {
'company': company,
'user': request.user,
'product': product,
'telephone': data['telephone'],
})
subject = "[latienda.coop] Solicitud de compra"
email = EmailMessage(subject, company_message, to=[company.email])
email.send()
logging.info(f"Email sent to {company}")
# send confirmation email to user
user_message = render_to_string('purchase_contact_confirmation.html', {
'company': company,
'product': product,
'company_message': company_message,
})
subject = 'Confirmación de petición de compra'
email = EmailMessage(subject, user_message, to=[user_email])
email.send()
logging.info(f"Purchase Contact confirmation email sent to {user_email}")
except Exception as e:
return Response({'error': f"Could not send emails [{str(type(e))}]: {str(e)}"}, status=500)
# create statslog instance to register interaction
stats_data = {