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.

329 lines
7.0 KiB

import os.path
import time
from django.contrib import messages
from django.db.models import Max,Count
from django.shortcuts import render,redirect,reverse
from django.views.generic import View,ListView,DetailView
from.forms import RegisterForm,LoginForm,CommentForm
from.model import User, Movie, Movie_rating, Movie_hot
BASE=os.path.dirname(os.path.abspath(__file__))
class IndexView(ListView):
model=Movie
template_name='movie/index.html'
paginatee_by=15
context_objject_name='movies'
ordering='imdb_id'
page_kwarg='p'
def get_queryset(self):
return Movie.object.filter(imdb_id__lte=1000)
def get_context_data(self,*,object_list=None,**kwargs):
context=super(IndexView,self).get_context_data(*kwargs)
paginator=context.get('paginator')
page_obj=context.get('page_obj')
pagination_data=self.get_pagination_data(paginator,page_obj)
context.updata(pagination_data)
return context
def get_pagination_data(self,paginator,page_obj,around_count=2):
current_page=page_obj.number
if current_page<=around_count+2:
left_pages=range(1,current_page)
left_has_more=False
else:
left_pages=range(current_page-around_count,current_page)
left_has_more=True
if current_page>=paginator.num_pages-around_count-1:
right_pages=range(current_page+1,paginator.num_pages+1)
right_has_more=False
else:
right_pages=range(current_page+1,current_page+around_count+1)
right_has_more=True
return{
'left_pages':left_pages,
'right_pages':right_pages,
'current_page':current_page,
'left_has__more':left_has_more,
'right_has__more':right_has_more
}
class PopularMovieView(ListView):
model=Movie_hot
template_name='movie/hot.html'
paginate_by=15
context_object_name='movies'
page_kwarg='p'
def get_queryset(self):
movies=Movie.objects.annotate(nums=Count('movie_rating__score')).order_by('-nums')[:100]
for movie in movies:
record=Movie_hot(movie=movie,rating_number=movie.nums)
record.save()
hot_movies=Movie_hot.objects.all().values("movie_id")
movies=Movie.objects.filter(id__in=hot_movies).ammotate(nums=Max('movie_hot__num')).order_by('-nums')
return movies
def get_context_data(self,*,object_list=None,**kwargs):
context=super(PopularMovieView,self).get_context_data(*kwargs)
paginator=context.get('paginator')
page_obj=context.get('page_obj')
pagination_data=self.get_pagination_data(paginator,page_obj)
context.updata(pagination_data)
return context
def get_pagination_data(self,paginator,page_obj,around_count=2):
current_page=page_obj.number
if current_page<=around_count+2:
left_pages=range(1,current_page)
left_has_more=False
else:
left_pages=range(current_page-around_count,current_page)
left_has_more=True
if current_page>=paginator.num_pages-around_count-1:
right_pages=range(current_page+1,paginator.num_pages+1)
right_has_more=False
else:
right_pages=range(current_page+1,current_page+around_count+1)
right_has_more=True
return{
'left_pages':left_pages,
'right_pages':right_pages,
'current_page':current_page,
'left_has__more':left_has_more,
'right_has__more':right_has_more
}
class TagView(ListView):
model=Movie
template_name='movie/tag.html'
paginate_by=15
context_object_name='movies'
page_kwarg='p'
def get_queryset(self):
if 'genre' not in self.request.GET.dict().keys():
movies=Movie.objects.all()
return movies[100:200]
else:
movies=movie.objects.filter(genre__name=self.request.GET.dict()['genre'])
print(movies)
return movies[:100]
def get_context_data(self,*,object_list=None,**kwargs):
context=super(TagView,self).get_context_data(*kwargs)
if "genre" in self.request.GET.dict().keys():
genre=self.request.GET.dict()['genre']
context.update({'genre':genre})
paginator=context.get('paginator')
page_obj=context.get('page_obj')
pagination_data=self.get_pagination_data(paginator,page_obj)
context.updata(pagination_data)
return context
def get_pagination_data(self,paginator,page_obj,around_count=2):
current_page=page_obj.number
if current_page<=around_count+2:
left_pages=range(1,current_page)
left_has_more=False
else:
left_pages=range(current_page-around_count,current_page)
left_has_more=True
if current_page>=paginator.num_pages-around_count-1:
right_pages=range(current_page+1,paginator.num_pages+1)
right_has_more=False
else:
right_pages=range(current_page+1,current_page+around_count+1)
right_has_more=True
return {
'left_pages':left_pages,
'right_pages':right_pages,
'current_page':current_page,
'left_has__more':left_has_more,
'right_has__more':right_has_more
}
class SearchView(ListView):
model=Movie
template_name='movie/search.html'
paginate_by=15
context_object_name='movies'
page_kwarg='p'
def get_queryset(self):
movies=Movie.objects.filter(name__icontains=self.request.GET.dict()['keyword'])
return movies
def get_context_data(self,*,object_list=None,**kwargs):
context=super(SearchView,self).get_context_data(*kwargs)
paginator=context.get('paginator')
page_obj=context.get('page_obj')
pagination_data=self.get_pagination_data(paginator,page_obj)
context.updata(pagination_data)
context.updata({'keyword':self.request.GET.dict()['keyword']})
return context
def get_pagination_data(self,paginator,page_obj,around_count=2):
current_page=page_obj.number
if current_page<=around_count+2:
left_pages=range(1,current_page)
left_has_more=False
else:
left_pages=range(current_page-around_count,current_page)
left_has_more=True
if current_page>=paginator.num_pages-around_count-1:
right_pages=range(current_page+1,paginator.num_pages+1)
right_has_more=False
else:
right_pages=range(current_page+1,current_page+around_count+1)
right_has_more=True
return {
'left_pages':left_pages,
'right_pages':right_pages,
'current_page':current_page,
'left_has__more':left_has_more,
'right_has__more':right_has_more
}