LORDCHANNEL2.0/core/views.py

77 lines
3.9 KiB
Python

from django.shortcuts import render,get_object_or_404,redirect,HttpResponseRedirect
from .models import Profilo
from django.contrib.auth.models import User, UserManager
from django.contrib.auth import REDIRECT_FIELD_NAME, authenticate, login
from Film.models import Film,Commento
from Anime.models import Anime,CommentoAnime
from SerieTV.models import CommentoSerieTV,SerieTV
from django.db.models import Q
from itertools import chain
from operator import attrgetter
from utils.random_media import random_media
from .forms import FormRegistrazione,ProfiloForm
def accountUtente(request,username):
utente = get_object_or_404(User,username=username)
profilo = get_object_or_404(Profilo,user=utente)
commenti_film = Commento.objects.filter(utente=profilo).order_by('-data_commento').distinct()
commenti_anime = CommentoAnime.objects.filter(utente=profilo).order_by('-data_commento').distinct()
commenti_serietv = CommentoSerieTV.objects.filter(utente=profilo).order_by('-data_commento').distinct()
commenti = list(sorted(chain(commenti_film, commenti_anime,commenti_serietv), key=attrgetter('data_commento'), reverse=True))[:3]
film_lista_preferiti = profilo.preferiti.all()
film_watch_later = profilo.watch_later.all()
serietv_lista_preferiti = profilo.serietv_preferite.all()
serietv_watch_later = profilo.serietv_watch_later.all()
anime_lista_preferiti = profilo.anime_preferiti.all()
anime_watch_later = profilo.anime_watch_later.all()
if request.method == "POST":
form = ProfiloForm(request.POST or None,request.FILES or None,instance=profilo)
if form.is_valid():
form.save()
return redirect('profilo-utente',username=profilo)
else:
form = ProfiloForm(instance=profilo)
context = {"utente":profilo,"commenti":commenti,"film_lista_preferiti":film_lista_preferiti,"film_watch_later":film_watch_later,"serietv_lista_preferiti": serietv_lista_preferiti,"serietv_watch_later":serietv_watch_later,"anime_lista_preferiti":anime_lista_preferiti,"anime_watch_later":anime_watch_later,"form":form}
return render(request,'profilo.html',context)
def homepage(request):
film = Film.objects.all()
anime = Anime.objects.all()
serietv = SerieTV.objects.all()
random_film_serietv_anime = random_media()
ultimi_caricamenti_media = list(sorted(chain(film, anime,serietv), key=attrgetter('creato_in_data'), reverse=True))[:6]
media_popolari = list(sorted(chain(film, anime,serietv), key=attrgetter('visualizzazioni'),reverse=True))[:6]
context = {"ultimi_caricamenti_media":ultimi_caricamenti_media,"random_film_serietv_anime":random_film_serietv_anime,"media_popolari":media_popolari}
return render(request,"homepage.html",context)
def cerca(request):
if "q" in request.GET:
querystring = request.GET.get("q")
if len(querystring) == 0:
return redirect("/cerca/")
anime = Anime.objects.filter(titolo__icontains=querystring)
serietv = SerieTV.objects.filter(titolo__icontains=querystring)
film = Film.objects.filter(titolo__icontains=querystring)
media = list(chain(film,serietv,anime))
context = {"media":media}
return render(request,'cerca.html',context)
else:
return render(request, 'cerca.html')
def registrazione(request):
if request.method == "POST":
form = FormRegistrazione(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
email = form.cleaned_data['email']
password = form.cleaned_data['password1']
User.objects.create_user(username=username,password=password,email=email)
user = authenticate(username=username,password=password)
login(request,user)
return HttpResponseRedirect("/")
else:
form = FormRegistrazione()
context = {"form": form}
print(context)
return render(request,'registrazione.html',context)