diff --git a/back_latienda/urls.py b/back_latienda/urls.py index 96a509f..b7b27b1 100644 --- a/back_latienda/urls.py +++ b/back_latienda/urls.py @@ -34,6 +34,7 @@ def trigger_error(request): urlpatterns = [ path('admin/', admin.site.urls), + path('api/v1/initial/', product_views.initial, name='initial'), path('activate///',core_views.activate_user, name='activate_user'), path('api/v1/social-auth/', include('rest_framework_social_oauth2.urls')), path('api/v1/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'), diff --git a/products/views.py b/products/views.py index 357503b..bfe260f 100644 --- a/products/views.py +++ b/products/views.py @@ -2,6 +2,7 @@ import logging import csv import datetime import json +import random from django.db.models import Q from django.core import serializers @@ -22,6 +23,7 @@ from rest_framework.filters import OrderingFilter from django_filters.rest_framework import DjangoFilterBackend import requests +from companies.serializers import CompanySerializer from history.models import HistorySync from dal import autocomplete @@ -89,6 +91,53 @@ class AdminProductsViewSet(viewsets.ModelViewSet): def perform_create(self, serializer): serializer.save(creator=self.request.user) +def get_decent_products(): + return Product.objects.filter( + Q(active=True) + ).exclude( + Q(image__isnull=True) | Q(image='') + ) + + +def get_random_sample_list_of_decent_products_in_category(category_name, sample_size): + random_ids = [] + cats = get_category_and_descendants(category_name) + products = get_decent_products() + ids = list(products.filter(Q(category__in=cats)).values_list('id', flat=True)) + size = len(ids) if len(ids) < sample_size else sample_size + random_ids = random.sample(ids, size) + products_qs = products.filter(id__in=random_ids) + result = ProductSerializer(products_qs, many=True).data + return result + +def get_latest_products(number): + products_qs = get_decent_products().order_by('-created')[:number] + result = ProductSerializer(products_qs, many=True).data + return result + +def get_latest_companies(number): + companies_qs = Company.objects.filter(is_validated=True).order_by('-created')[:number] + result = CompanySerializer(companies_qs, many=True).data + return result + + +@api_view(['GET',]) # include allowed methods +def initial(request): + """ + Set landing page items + + """ + category_cards = dict() + cards_size = 4 + categories = ['Libros', 'Cosméticos', 'Plantas'] + for c in categories: + category_cards[c] = get_random_sample_list_of_decent_products_in_category(c, cards_size) + + products = get_latest_products(12) + companies = get_latest_companies(12) + + return Response(data={"cards": category_cards, "products": products, "companies": companies}) + @api_view(['POST',]) @permission_classes([IsAuthenticated,]) @@ -127,6 +176,15 @@ def load_coop_products(request): return Response({"errors": {"details": str(e)}}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) +def get_category_and_descendants(category_name): + categories=[] + cat = CategoryTag.objects.filter(label__iexact=category_name).first() + # append category tag, and children + categories.append(cat) + categories.extend(cat.get_descendants()) + return categories + + @api_view(['GET',]) # include allowed methods def product_search(request): """ @@ -197,10 +255,7 @@ def product_search(request): if categories is not None: descendants = [] for entry in categories: - cat = CategoryTag.objects.filter(label__iexact=entry).first() - # append category tag, and children - descendants.append(cat) - descendants.extend(cat.get_descendants()) + descendants.extend(get_category_and_descendants(entry)) products_qs = products_qs.filter(category__in=descendants) # filter by tags