def view_search(request): """:synopsis: Permet de générer la page de resultat d'une recherche si request = GET. :param request: La requete du client de type GET :type request: djangoRequest :return: le template + le context -> générer au final la page html """ context = {'product': object(), 'form': PriceRange(), "basketCount": tools.basketCount(request)} context = tools.mergeDict(context, tools.displayCategory()) context = tools.mergeDict(context, {"emailNewsLetter": request.session.get('emailNewsLetter')}) request.session['emailNewsLetter'] = "" # on verifie que la form est valide form = Search(request.GET) print("ok est a search") if form.is_valid(): terms = form.cleaned_data['search'] context['terms'] = terms product = Product.objects.filter(name__icontains=terms, quantity__gte=1) context["productName"] = terms paginator = Paginator(product, 1) page = request.GET.get('page') try: context["product"] = paginator.get_page(page) except PageNotAnInteger: context["product"] = paginator.page(1) except EmptyPage: context["product"] = paginator.page(1) return render(request, 'view_search.html', context) else: redirect("index")
def home(request): """:synopsis: Permet de générer la page index du site si request = GET :param request: La requete du client de type GET :type request: djangoRequest :return: le template + le context -> générer au final la page html """ context = {"product": object, "basketCount": tools.basketCount(request), 'form': PriceRange()} # on recupere tous les produits product = Product.objects.filter(quantity__gte=1).order_by("date") pub = IndexPub.objects.all() context["indexPub"] = pub paginator = Paginator(product, 25) page = request.GET.get('page') try: context["product"] = paginator.get_page(page) except PageNotAnInteger: context["product"] = paginator.page(1) except EmptyPage: context["product"] = paginator.page(1) context = tools.mergeDict(context, {"emailNewsLetter": request.session.get('emailNewsLetter')}) context = tools.mergeDict(context, tools.displayCategory()) request.session['emailNewsLetter'] = "" return render(request, 'index.html', context)
def index_price_range(request): """:synopsis: Permet de Trier les produits de l'index en fonction d'un prix min et max si request = GET :param request: La requete du client de type GET :type request: djangoRequest :return: le template + le context -> générer au final la page index.html avec les produits trier """ context = {"product": object(), "basketCount": tools.basketCount(request)} context = tools.mergeDict(context, tools.displayCategory()) # on verifie que la requete est de type get pub = IndexPub.objects.all() context["indexPub"] = pub if request.method == "GET": form = PriceRange(request.GET) context['form'] = form if form.is_valid(): product = Product.objects.filter(price__range=(form.cleaned_data['priceMin'], form.cleaned_data['priceMax']), quantity__gte=1) paginator = Paginator(product, 25) page = request.GET.get('page') try: context["product"] = paginator.get_page(page) except PageNotAnInteger: context["product"] = paginator.page(1) except EmptyPage: context["product"] = paginator.page(1) finally: get = dict(request.GET) context["getParam"] = "priceMin={0}&priceMax={1}".format(get["priceMin"][0], get["priceMax"][0]) context = tools.mergeDict(context, {"emailNewsLetter": request.session.get('emailNewsLetter')}) request.session['emailNewsLetter'] = "" return render(request, 'index.html', context)
def view_product(request, id_product): """:synopsis: Permet de générer la page details produit si request = GET. Si request = POST permet de poster un avis sur la page produit details :param request: La requete du client de type GET ou POST :type request: djangoRequest :param id_product: L'id du produit a afficher sur la page :type id_product: int :return: le template + le context -> générer au final la page html """ context = {"product": get_object_or_404(Product, id=id_product), "image": Images.objects.filter(product_id=id_product), "reviews": ProductComment.objects.filter(product_id=id_product).order_by('-date'), "form": ReviewsForm(), "basketCount": tools.basketCount(request), 'basket': Basket()} context = tools.mergeDict(context, tools.displayCategory()) context = tools.mergeDict(context, {"emailNewsLetter": request.session.get('emailNewsLetter')}) request.session['emailNewsLetter'] = "" if request.method == "POST": form = ReviewsForm(request.POST) if form.is_valid(): # on insert le commentaire dans la bd review = ProductComment(name=form.cleaned_data['name'], email=form.cleaned_data['email'], comment=form.cleaned_data['comment'], product=Product.objects.get(pk=id_product), star=4) review.save() return render(request, 'view_details.html', context)
def category_sorted(request, id_category, sorted_by): """:synopsis: Permet de Trier les produits de la page view Category en fonction d'une action passer en paramètre (request = GET) :param request: La requete du client de type GET :type request: djangoRequest :param id_category: L'id de la catégorie a afficher :type id_category: int :param sorted_by: Action a effectué sur le trie des produits :type sorted_by: str :return: le template + le context -> générer au final la page index.html avec les produits trier """ # on recupere tous les article lier a cette catégorie context = {"product": object(), 'id_category': id_category, 'form': PriceRange(), "basketCount": tools.basketCount(request)} sorted_values = {"priceASC": lambda: (Product.objects.filter(category_id=id_category, quantity__gte=1).order_by('price')), "priceDESC": lambda: (Product.objects.filter(category_id=id_category, quantity__gte=1).order_by('-price')), "date": lambda: (Product.objects.filter(category_id=id_category, quantity__gte=1).order_by('date')), "star": lambda: (Product.objects.filter(category=id_category, quantity__gte=1).annotate( review=Count('productcomment__comment')).order_by('-review')), "error": lambda: (Product.objects.filter(category_id=id_category, quantity__gte=1))} try: product = sorted_values[sorted_by]() except KeyError: product = sorted_values["error"]() paginator = Paginator(product, 25) page = request.GET.get('page') try: context["product"] = paginator.get_page(page) except PageNotAnInteger: context["product"] = paginator.page(1) except EmptyPage: context["product"] = paginator.page(1) context = tools.mergeDict(context, tools.displayCategory()) context = tools.mergeDict(context, {"emailNewsLetter": request.session.get('emailNewsLetter')}) request.session['emailNewsLetter'] = "" return render(request, 'view_category.html', context)
def view_category(request, id_category): """:synopsis: Permet de générer la page pour afficher tous les produit contenu dans une catégorie si request = GET. :param request: La requete du client de type GET :type request: djangoRequest :param id_category: L'id de la catégorie a afficher :type id_category: int :return: le template + le context -> générer au final la page html """ # on recupere tous les article lier a cette catégorie product = Product.objects.filter(category_id=id_category, quantity__gte=1) paginator = Paginator(product, 25) page = request.GET.get('page') # get_list_or_404(Product, category_id=id_category) context = {"product": paginator.get_page(page), 'id_category': id_category, 'form': PriceRange(), "basketCount": tools.basketCount(request), 'categoryName': SubCategory.objects.get(id=id_category)} context = tools.mergeDict(context, tools.displayCategory()) context = tools.mergeDict(context, {"emailNewsLetter": request.session.get('emailNewsLetter')}) request.session['emailNewsLetter'] = "" return render(request, 'view_category.html', context)
def view_price_by_name(request, product_name): """:synopsis: Permet de Trier les produits de la page view Search en fonction d'un prix min et max si request = GET :param request: La requete du client de type GET :type request: djangoRequest :param product_name: le terms rechercher :type product_name: str :return: Le template + le context -> générer au final la page view_search.html avec les produits trier par prix """ context = {"product": object(), 'form': PriceRange(), 'productName': product_name, "basketCount": tools.basketCount(request)} context = tools.mergeDict(context, tools.displayCategory()) context = tools.mergeDict(context, {"emailNewsLetter": request.session.get('emailNewsLetter')}) request.session['emailNewsLetter'] = "" # on verifie que la requete est de type get if request.method == "GET": form = PriceRange(request.GET) if form.is_valid(): product = Product.objects.filter(price__range=(form.cleaned_data['priceMin'], form.cleaned_data['priceMax']), name__contains=product_name, quantity__gte=1) paginator = Paginator(product, 25) page = request.GET.get('page') try: context["product"] = paginator.get_page(page) except PageNotAnInteger: context["product"] = paginator.page(1) except EmptyPage: context["product"] = paginator.page(1) finally: get = dict(request.GET) get = "priceMin={0}&priceMax={1}".format(get["priceMin"][0], get["priceMax"][0]) context["getParam"] = get return render(request, 'view_search.html', context)