|
|
|
@ -23,11 +23,11 @@ class IndexView(ListView):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
return Movie.object.filter(imdb_id__lte=1000)
|
|
|
|
|
return Movie.objects.filter(imdb_id__lte=1000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_context_data(self,*,object_list=None,**kwargs):
|
|
|
|
|
context=super(IndexView,self).get_context_data(*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)
|
|
|
|
@ -82,7 +82,7 @@ class PopularMovieView(ListView):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_context_data(self,*,object_list=None,**kwargs):
|
|
|
|
|
context=super(PopularMovieView,self).get_context_data(*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)
|
|
|
|
@ -132,8 +132,7 @@ class TagView(ListView):
|
|
|
|
|
print(movies)
|
|
|
|
|
return movies[:100]
|
|
|
|
|
def get_context_data(self,*,object_list=None,**kwargs):
|
|
|
|
|
|
|
|
|
|
context=super(TagView,self).get_context_data(*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})
|
|
|
|
@ -177,7 +176,7 @@ class SearchView(ListView):
|
|
|
|
|
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)
|
|
|
|
|
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)
|
|
|
|
@ -252,7 +251,7 @@ class LoginView(View):
|
|
|
|
|
for error in errors:
|
|
|
|
|
messages.info(request,error)
|
|
|
|
|
return redirect(reverse('movie:login'))
|
|
|
|
|
class UserLogout(request):
|
|
|
|
|
def UserLogout(request):
|
|
|
|
|
request.session.set_expiry(-1)
|
|
|
|
|
return redirect(reverse('movie:index'))
|
|
|
|
|
|
|
|
|
@ -269,7 +268,7 @@ class MovieDetailView(DetailView):
|
|
|
|
|
login=False
|
|
|
|
|
|
|
|
|
|
pk=self.kwargs['pk']
|
|
|
|
|
movie=Movie.object.get(pk=pk)
|
|
|
|
|
movie=Movie.objects.get(pk=pk)
|
|
|
|
|
if login:
|
|
|
|
|
user=User.objects.get(pk=user_id)
|
|
|
|
|
rating=Movie_rating.objects.filter(user=user,movie=movie).first()
|
|
|
|
@ -291,8 +290,8 @@ class MovieDetailView(DetailView):
|
|
|
|
|
comment=form.cleaned_data.get('comment')
|
|
|
|
|
user_id=request.session['user_id']
|
|
|
|
|
user=User.objects.get(pk=user_id)
|
|
|
|
|
movie=Movie.object.get(pk=pk)
|
|
|
|
|
rating=Movie_rating.object.filter(user=user,movie=movie).first()
|
|
|
|
|
movie=Movie.objects.get(pk=pk)
|
|
|
|
|
rating=Movie_rating.objects.filter(user=user,movie=movie).first()
|
|
|
|
|
if rating:
|
|
|
|
|
rating.score=score
|
|
|
|
|
rating.comment=comment
|
|
|
|
@ -327,7 +326,7 @@ def delete_recode(request,pk):
|
|
|
|
|
messages.info(request,f'删除{movie.name}评分记录成功!')
|
|
|
|
|
return redirect(reverse('movie:history',args=(user_id,)))
|
|
|
|
|
|
|
|
|
|
class recommendMovieView(ListView):
|
|
|
|
|
class RecommendMovieView(ListView):
|
|
|
|
|
model=Movie
|
|
|
|
|
template_name='movie/recommend.html'
|
|
|
|
|
paginate_by=15
|
|
|
|
@ -348,10 +347,56 @@ class recommendMovieView(ListView):
|
|
|
|
|
other_users=User.objects.exclude(pk=cur_user_id)
|
|
|
|
|
self.cur_user_movie_qs=Movie.objects.filter(user=cur_user)
|
|
|
|
|
for user in other_users:
|
|
|
|
|
user_sim_dct[user.id]=len(Movie.object,filter(user=user)&self.cur_user_movie_qs)
|
|
|
|
|
user_sim_dct[user.id]=len(Movie.objects.filter(user=user)&self.cur_user_movie_qs)
|
|
|
|
|
print("user similarity calculated!")
|
|
|
|
|
return sorted(user_sim_dct.items(),key=lambda x:-x[1][:self.K])
|
|
|
|
|
def get_recommend_movie(self,user_lst):
|
|
|
|
|
movie_val_dct=dict()
|
|
|
|
|
for user,_in user_lst:
|
|
|
|
|
movie_set=Movie.objects.filter(user=user).exclude(id__in=self.cur_user_movie_qs).annotate(score=F('movie_rating__score')*F('movie_rating__score'))
|
|
|
|
|
for user in user_lst:
|
|
|
|
|
movie_set=Movie.objects.filter(user=user).exclude(id__in=self.cur_user_movie_qs).annotate(score=Movie('movie_rating__score')*Movie('movie_rating__score'))
|
|
|
|
|
for movie in movie_set:
|
|
|
|
|
movie_val_dct.setdefault(movie,0)
|
|
|
|
|
movie_val_dct[movie]+=movie.score
|
|
|
|
|
return sorted(movie_val_dct.items(),key=lambda x:-x[1])[:self.N]
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
s=time.time()
|
|
|
|
|
user_lst=self.get_user_sim()
|
|
|
|
|
movie_lst=self.get_recommend_movie(user_lst)
|
|
|
|
|
result_lst=[]
|
|
|
|
|
for movie, in movie_lst:
|
|
|
|
|
result_lst.append(movie)
|
|
|
|
|
e=time.time()
|
|
|
|
|
print(f"算法推荐用时:[e-s]秒")
|
|
|
|
|
return result_lst
|
|
|
|
|
def get_context_data(self,*,object_list=None,**kwargs):
|
|
|
|
|
context=super(RecommendMovieView,self).get_context_data(**kwargs)
|
|
|
|
|
print(context)
|
|
|
|
|
paginator=context.get('pagginator')
|
|
|
|
|
page_obj=context.get('page_obj')
|
|
|
|
|
pagination_data=self.get_pagination_data(paginator,page_obj)
|
|
|
|
|
context.update(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
|
|
|
|
|
}
|