You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
4.5 KiB
116 lines
4.5 KiB
from django.shortcuts import render, get_list_or_404, get_object_or_404
|
|
from django.contrib import auth
|
|
from django.http import Http404
|
|
import json
|
|
import decimal
|
|
|
|
from django.views.generic import ListView
|
|
|
|
from cart.forms import CartAddProductForm
|
|
from .utils import *
|
|
from cart.cart import Cart
|
|
from .models import *
|
|
|
|
|
|
class ProductSearchView(ListView):
|
|
model = Product
|
|
template_name = 'products/search.html'
|
|
|
|
def get_queryset(self):
|
|
queryset = super().get_queryset()
|
|
return queryset.filter(name__icontains=self.request)
|
|
|
|
# Uncomment for elasticsearch
|
|
|
|
# from .layout import FacetedProductSearchForm
|
|
# from haystack.generic_views import FacetedSearchView as BaseFacetedSearchView
|
|
# from haystack.query import SearchQuerySet
|
|
|
|
def serialize_decimal(obj):
|
|
if isinstance(obj, decimal.Decimal):
|
|
return str(obj)
|
|
return json.JSONEncoder.default(obj)
|
|
|
|
def producerslist(request):
|
|
username = auth.get_user(request).username
|
|
# category = None
|
|
# categories = ProductCategory.objects.filter(level__lte=0)
|
|
# products = Product.objects.filter(is_active=True)
|
|
producers = Producer.objects.filter(is_active=True)
|
|
# if category_slug:
|
|
# category = get_object_or_404(ProductCategory, slug=category_slug)
|
|
# products = products.filter(category__in=category.get_descendants(include_self=True))
|
|
return render(request, 'products/list.html', locals())
|
|
|
|
# def categorieslist(request, producer_slug, category_slug=None):
|
|
# username = accounts_ext.get_user(request).username
|
|
# producer = Producer.objects.get(slug=producer_slug)
|
|
# if category_slug:
|
|
# _categories = ProductCategory.objects.filter(is_active=True, parent=category_slug)
|
|
# else:
|
|
# _categories = ProductCategory.objects.filter(is_active=True, producer=producer, level__lte=0)
|
|
# categories, products = expand_categories(_categories)
|
|
# return render(request, 'products/categorieslist.html', {'username': username, 'categories':categories,
|
|
# 'products': products})
|
|
|
|
def categorieslist(request, path, instance, producer_slug):
|
|
username = auth.get_user(request).username
|
|
if instance:
|
|
_categories = instance.get_children()
|
|
else:
|
|
_categories = get_list_or_404(ProductCategory, producer__slug=producer_slug, level__lte=0)
|
|
if _categories:
|
|
categories, products = expand_categories(_categories)
|
|
else:
|
|
return productslist(request, producer_slug, instance.slug)
|
|
return render(
|
|
request,
|
|
'products/categorieslist.html',
|
|
{
|
|
'username': username,
|
|
'instance': instance,
|
|
'categories': categories,
|
|
'producer_slug': producer_slug,
|
|
'products': products
|
|
}
|
|
)
|
|
|
|
def productslist(request, producer_slug, category_slug):
|
|
username = auth.get_user(request).username
|
|
category = ProductCategory.objects.get(slug=category_slug)
|
|
products = Product.objects.filter(is_active=True, category=category)
|
|
return render(request, 'products/productslist.html', locals())
|
|
|
|
def product(request, product_slug):
|
|
username = auth.get_user(request).username
|
|
product = get_object_or_404(Product, slug=product_slug, is_active=True)
|
|
cart_product_form = CartAddProductForm()
|
|
variant_picker_data = get_variant_picker_data(product)
|
|
show_variant_picker = all([v.attributes for v in product.variants.all()])
|
|
# session_key = request.session.session_key
|
|
# if not session_key:
|
|
# request.session.cycle_key()
|
|
|
|
return render(request, 'products/product.html', {'username': username, 'products': product, 'form': cart_product_form,
|
|
'show_variant_picker': show_variant_picker,
|
|
'variant_picker_data': variant_picker_data,
|
|
})
|
|
|
|
# Uncomment for elasticsearch
|
|
|
|
# def autocomplete(request):
|
|
# sqs = SearchQuerySet().autocomplete(content_auto=request.GET.get('query', ''))[:5]
|
|
# s = []
|
|
# for result in sqs:
|
|
# print(result)
|
|
# d = {"value": result.name, "data": result.object.slug}
|
|
# s.append(d)
|
|
# output = {'suggestions': s}
|
|
# return JsonResponse(output)
|
|
#
|
|
# class FacetedSearchView(BaseFacetedSearchView):
|
|
# form_class = FacetedProductSearchForm
|
|
# facet_fields = ['category', 'producer']
|
|
# template_name = 'search/search.html'
|
|
# paginate_by = 3
|
|
# context_object_name = 'object_list'
|
|
|