Skip to content

Instantly share code, notes, and snippets.

@DJWOMS
Created January 26, 2020 10:59
Show Gist options
  • Save DJWOMS/8ddebe70fd3a6da525c2d5c84f5328a0 to your computer and use it in GitHub Desktop.
Save DJWOMS/8ddebe70fd3a6da525c2d5c84f5328a0 to your computer and use it in GitHub Desktop.
SortProduct
class CategoryProductVue(View):
"""Список товаров из категории для vue"""
def get(self, request):
return render(request, "shop/vue/list-product-vue.html")
def post(self, request):
slug = request.POST.get("slug")
node = Category.objects.get(slug=slug)
if Product.objects.filter(category__slug=slug).exists():
products = Product.objects.filter(category__slug=slug)
else:
products = Product.objects.filter(category__slug__in=[x.slug for x in node.get_family()])
category_ser = CatSer(Category.objects.filter(parent__isnull=True), many=True)
serializers = ProductSer(products, many=True)
return JsonResponse(
{
"products": serializers.data,
"category": category_ser.data
},
safe=False)
class SortProducts(View):
"""Фильтр товаров"""
def get(self, request):
return render(request, "shop/vue/list-product-vue.html")
def post(self, request):
category = request.POST.get("category", None)
price_1 = request.POST.get("price1", 1)
price_2 = request.POST.get("price2", 1000000000)
availability = request.POST.get("availability", None)
filt = []
if category:
cat = Q()
cat &= Q(category__name__icontains=category)
filt.append(cat)
if price_1 or price_2:
price = Q()
price &= Q(price__gte=int(price_1)) & Q(price__lte=int(price_2))
filt.append(price)
if availability:
if availability == "False":
avail = False
elif availability == "True":
avail = True
availability = Q()
availability &= Q(availability=avail)
filt.append(availability)
sort = Product.objects.filter(*filt)
category_ser = CatSer(Category.objects.filter(parent__isnull=True), many=True)
serializers = ProductSer(sort, many=True)
return JsonResponse(
{
"products": serializers.data,
"category": category_ser.data
},
safe=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment