Merge branch 'development' of https://bitbucket.org/enreda/back-latienda into development
This commit is contained in:
@@ -1,11 +1,14 @@
|
|||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from companies.models import Company
|
from companies.models import Company
|
||||||
|
|
||||||
|
from drf_extra_fields.geo_fields import PointField
|
||||||
|
|
||||||
from utils.tag_serializers import TagListSerializerField, TaggitSerializer
|
from utils.tag_serializers import TagListSerializerField, TaggitSerializer
|
||||||
|
|
||||||
class CompanySerializer(TaggitSerializer, serializers.ModelSerializer):
|
class CompanySerializer(TaggitSerializer, serializers.ModelSerializer):
|
||||||
|
|
||||||
tags = TagListSerializerField(required=False)
|
tags = TagListSerializerField(required=False)
|
||||||
|
geo = PointField(required=False, allow_null=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Company
|
model = Company
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.core import serializers
|
|
||||||
from django.core.mail import EmailMessage
|
from django.core.mail import EmailMessage
|
||||||
from django.template.loader import render_to_string
|
from django.template.loader import render_to_string
|
||||||
|
|
||||||
@@ -61,6 +60,5 @@ class CompanyViewSet(viewsets.ModelViewSet):
|
|||||||
@permission_classes([IsAuthenticated,])
|
@permission_classes([IsAuthenticated,])
|
||||||
def my_company(request):
|
def my_company(request):
|
||||||
qs = Company.objects.filter(creator=request.user)
|
qs = Company.objects.filter(creator=request.user)
|
||||||
data = serializers.serialize('json', qs)
|
company_serializer = CompanySerializer(qs)
|
||||||
return Response(data=data)
|
return Response(data=company_serializer.data)
|
||||||
|
|
||||||
|
|||||||
@@ -1,19 +1,21 @@
|
|||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
from . import models
|
from . import models
|
||||||
|
|
||||||
|
User = get_user_model()
|
||||||
|
|
||||||
|
|
||||||
class CustomUserSerializer(serializers.ModelSerializer):
|
class CustomUserSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.CustomUser
|
model = User
|
||||||
fields = ('email', 'full_name', 'role', 'is_active')
|
fields = ('email', 'full_name', 'role', 'is_active')
|
||||||
|
|
||||||
|
|
||||||
class CustomUserReadSerializer(serializers.ModelSerializer):
|
class CustomUserReadSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.CustomUser
|
model = User
|
||||||
fields = ('id', 'email', 'full_name', 'role', 'is_active', 'provider', 'notify')
|
fields = ('id', 'email', 'full_name', 'role', 'is_active', 'provider', 'notify')
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import datetime
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.core import serializers
|
|
||||||
from django.utils.http import urlsafe_base64_decode
|
from django.utils.http import urlsafe_base64_decode
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
from django.db import IntegrityError
|
from django.db import IntegrityError
|
||||||
@@ -161,8 +160,9 @@ def create_company_user(request):
|
|||||||
@permission_classes([IsAuthenticated,])
|
@permission_classes([IsAuthenticated,])
|
||||||
def my_user(request):
|
def my_user(request):
|
||||||
qs = User.objects.filter(email=request.user.email)
|
qs = User.objects.filter(email=request.user.email)
|
||||||
data = serializers.serialize('json', qs)
|
user_serializer = core_serializers.CustomUserReadSerializer(qs, many=True)
|
||||||
return Response(data=data)
|
return Response(data=user_serializer.data)
|
||||||
|
|
||||||
|
|
||||||
@api_view(['POST',])
|
@api_view(['POST',])
|
||||||
@permission_classes([IsAdminUser,])
|
@permission_classes([IsAdminUser,])
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.core import serializers
|
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets
|
||||||
|
|||||||
@@ -2,8 +2,7 @@ from rest_framework import serializers
|
|||||||
|
|
||||||
from products.models import Product
|
from products.models import Product
|
||||||
|
|
||||||
from utils.tag_serializers import TagListSerializerField, TaggitSerializer, SingleTagSerializerField
|
from utils.tag_serializers import TagListSerializerField, TaggitSerializer, SingleTagSerializerField, SingleTag, TagList
|
||||||
|
|
||||||
|
|
||||||
class ProductSerializer(TaggitSerializer, serializers.ModelSerializer):
|
class ProductSerializer(TaggitSerializer, serializers.ModelSerializer):
|
||||||
|
|
||||||
@@ -14,3 +13,11 @@ class ProductSerializer(TaggitSerializer, serializers.ModelSerializer):
|
|||||||
class Meta:
|
class Meta:
|
||||||
model = Product
|
model = Product
|
||||||
exclude = ['created', 'updated', 'creator']
|
exclude = ['created', 'updated', 'creator']
|
||||||
|
|
||||||
|
|
||||||
|
class TagFilterSerializer(TaggitSerializer, serializers.ModelSerializer):
|
||||||
|
tags = TagListSerializerField(required=False)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = TagList
|
||||||
|
fields = '__all__'
|
||||||
|
|||||||
@@ -390,12 +390,10 @@ class ProductSearchTest(TestCase):
|
|||||||
url = f"{self.endpoint}?query_string={query_string}"
|
url = f"{self.endpoint}?query_string={query_string}"
|
||||||
# send in request
|
# send in request
|
||||||
response = self.client.get(url)
|
response = self.client.get(url)
|
||||||
|
|
||||||
# check re sponse
|
# check re sponse
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
# check for object creation
|
# check for object creation
|
||||||
data = json.loads(response.data)
|
self.assertEquals(len(response.data['products']), len(expected_instances))
|
||||||
self.assertEquals(len(data), len(expected_instances))
|
|
||||||
|
|
||||||
|
|
||||||
class MyProductsViewTest(APITestCase):
|
class MyProductsViewTest(APITestCase):
|
||||||
|
|||||||
9
products/utils.py
Normal file
9
products/utils.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
def extract_search_filters(result_set):
|
||||||
|
filters = set()
|
||||||
|
|
||||||
|
for item in result_set:
|
||||||
|
tags = item.tags.all()
|
||||||
|
for tag in tags:
|
||||||
|
filters.add(tag.name)
|
||||||
|
return list(filters)
|
||||||
@@ -6,7 +6,6 @@ from functools import reduce
|
|||||||
|
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core import serializers
|
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
@@ -18,12 +17,13 @@ from rest_framework.decorators import api_view, permission_classes
|
|||||||
import requests
|
import requests
|
||||||
|
|
||||||
from products.models import Product
|
from products.models import Product
|
||||||
from products.serializers import ProductSerializer
|
from products.serializers import ProductSerializer, TagFilterSerializer
|
||||||
from companies.models import Company
|
from companies.models import Company
|
||||||
from history.models import HistorySync
|
from history.models import HistorySync
|
||||||
|
|
||||||
from back_latienda.permissions import IsCreator
|
from back_latienda.permissions import IsCreator
|
||||||
|
from .utils import extract_search_filters
|
||||||
|
from utils.tag_serializers import TaggitSerializer
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
filename='logs/product-load.log',
|
filename='logs/product-load.log',
|
||||||
@@ -46,8 +46,8 @@ class ProductViewSet(viewsets.ModelViewSet):
|
|||||||
@permission_classes([IsAuthenticated,])
|
@permission_classes([IsAuthenticated,])
|
||||||
def my_products(request):
|
def my_products(request):
|
||||||
qs = Product.objects.filter(creator=request.user)
|
qs = Product.objects.filter(creator=request.user)
|
||||||
data = serializers.serialize('json', qs)
|
product_serializer = ProductSerializer(qs)
|
||||||
return Response(data=data)
|
return Response(data=product_serializer.data)
|
||||||
|
|
||||||
|
|
||||||
@api_view(['POST',])
|
@api_view(['POST',])
|
||||||
@@ -142,17 +142,14 @@ def product_search(request):
|
|||||||
# save results
|
# save results
|
||||||
result_set = set()
|
result_set = set()
|
||||||
# split query string into single words
|
# split query string into single words
|
||||||
chunks = query_string.split('+')
|
chunks = query_string.split(' ')
|
||||||
|
|
||||||
for chunk in chunks:
|
for chunk in chunks:
|
||||||
# search in name
|
# search inside name and description
|
||||||
products = Product.objects.filter(name=chunk)
|
products = Product.objects.filter(Q(name__icontains=chunk) | Q(description__icontains=chunk))
|
||||||
for item in products:
|
|
||||||
result_set.add(item)
|
|
||||||
# search in description
|
|
||||||
products = Product.objects.filter(description=chunk)
|
|
||||||
for item in products:
|
for item in products:
|
||||||
result_set.add(item)
|
result_set.add(item)
|
||||||
|
|
||||||
# search in tags
|
# search in tags
|
||||||
products = Product.objects.filter(tags=chunk)
|
products = Product.objects.filter(tags=chunk)
|
||||||
for item in products:
|
for item in products:
|
||||||
@@ -165,9 +162,10 @@ def product_search(request):
|
|||||||
products = Product.objects.filter(attributes=chunk)
|
products = Product.objects.filter(attributes=chunk)
|
||||||
for item in products:
|
for item in products:
|
||||||
result_set.add(item)
|
result_set.add(item)
|
||||||
|
# extract filters from result_set
|
||||||
serializer = ProductSerializer(result_set, many=True)
|
filters = extract_search_filters(result_set)
|
||||||
|
# serialize and respond
|
||||||
return Response(data=serializer.data)
|
product_serializer = ProductSerializer(result_set, many=True)
|
||||||
|
return Response(data={"filters": filters, "products": product_serializer.data})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return Response({"errors": {"details": str(type(e))}})
|
return Response({"errors": {"details": str(type(e))}})
|
||||||
|
|||||||
@@ -10,3 +10,4 @@ django-cors-headers==3.5.0
|
|||||||
django-taggit-serializer==0.1.7
|
django-taggit-serializer==0.1.7
|
||||||
django-tagulous==1.1.0
|
django-tagulous==1.1.0
|
||||||
Pillow==8.1.0
|
Pillow==8.1.0
|
||||||
|
drf-extra-fields==3.0.4
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.core import serializers
|
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets
|
||||||
|
|||||||
Reference in New Issue
Block a user