Cocktail-DB/cocktail_list/views.py

79 lines
3.2 KiB
Python
Raw Normal View History

2020-05-25 09:49:36 +00:00
from django.shortcuts import render,get_object_or_404,redirect
2020-06-03 13:45:22 +00:00
from .models import Cocktail,Ingredienti,Strumenti,Bicchieri
import random
2020-05-27 20:19:33 +00:00
import time
2020-05-25 09:49:36 +00:00
# Create your views here.
def homepage(request):
lista_cocktail = Cocktail.objects.all()
context = {"lista_cocktail":lista_cocktail}
return render(request,"homepage.html",context)
def cocktail(request,nome_cocktail):
cocktail = get_object_or_404(Cocktail,nome_cocktail=nome_cocktail)
context = {"cocktail":cocktail}
return render(request,"cocktail.html",context)
def random_cocktail(request):
random_cocktail = Cocktail.objects.all()
number_of_cocktail = Cocktail.objects.all().count()
random_cocktail_list = random.randint(1,number_of_cocktail)
rand_cocktail = random.sample(list(random_cocktail),random_cocktail_list)
context = {"random_cocktail":rand_cocktail}
return render(request,"random_cocktail.html",context)
2020-06-03 13:45:22 +00:00
def strumenti_cocktail(request):
lista_strumenti = Strumenti.objects.all()
context = {"lista_strumenti":lista_strumenti}
return render(request,"strumenti.html",context)
# def strumento_views(request,nome_strumento):
# strumento = get_object_or_404(Strumenti,nome_strumento=nome_strumento)
# context = {"strumento":strumento}
# return render(request,"strumento_detail.html",context)
def bicchieri_cocktail(request):
lista_bicchieri = Bicchieri.objects.all()
context = {"lista_bicchieri":lista_bicchieri}
return render(request,"bicchieri.html",context)
# def bicchiere_views(request,nome_bicchiere):
# bicchiere = get_object_or_404(Strumenti,nome_bicchiere=nome_bicchiere)
# context = {"bicchiere":bicchiere}
# return render(request,"bicchiere_detail.html",context)
# def cerca(request):
# if "q" in request.GET:
# querystring = request.GET.get("q")
# if len(querystring) == 0:
# return redirect("cerca")
# ingrediente = Cocktail.objects.filter(ingredienti__ingrediente__icontains=querystring).order_by("-nome_cocktail")
# cocktail = Cocktail.objects.filter(nome_cocktail__icontains=querystring).order_by("-nome_cocktail")
# context = {"querystring":querystring,"cocktail":cocktail,"ingrediente":ingrediente}
# return render(request,"cerca.html",context)
# else:
# return render(request,"cerca.html")
2020-05-25 09:49:36 +00:00
def cerca(request):
if "q" in request.GET:
querystring = request.GET.get("q")
if len(querystring) == 0:
2020-06-03 13:45:22 +00:00
return redirect("cerca")
all_ingredients = Cocktail.objects.all()
searching_ingred = Cocktail.objects.none()
for one_ingredient in querystring.split(","):
print(one_ingredient)
searching_ingred = searching_ingred.union(searching_ingred, all_ingredients.filter(ingredienti__ingrediente__icontains=one_ingredient))
print(searching_ingred)
# ingrediente = Cocktail.objects.filter(ingredienti__ingrediente__icontains=querystring).order_by("-nome_cocktail")
2020-05-25 09:49:36 +00:00
cocktail = Cocktail.objects.filter(nome_cocktail__icontains=querystring).order_by("-nome_cocktail")
2020-06-03 13:45:22 +00:00
context = {"querystring":querystring,"cocktail":cocktail,"ingrediente":searching_ingred}
2020-05-25 09:49:36 +00:00
return render(request,"cerca.html",context)
else:
2020-06-03 13:45:22 +00:00
return render(request,"cerca.html")