diff --git a/djangoProject2/movie/__pycache__/forms.cpython-312.pyc b/djangoProject2/movie/__pycache__/forms.cpython-312.pyc index 6ed2a462..c30a24f7 100644 Binary files a/djangoProject2/movie/__pycache__/forms.cpython-312.pyc and b/djangoProject2/movie/__pycache__/forms.cpython-312.pyc differ diff --git a/djangoProject2/movie/__pycache__/model.cpython-312.pyc b/djangoProject2/movie/__pycache__/model.cpython-312.pyc index e11479d6..c6950242 100644 Binary files a/djangoProject2/movie/__pycache__/model.cpython-312.pyc and b/djangoProject2/movie/__pycache__/model.cpython-312.pyc differ diff --git a/djangoProject2/movie/__pycache__/urls.cpython-312.pyc b/djangoProject2/movie/__pycache__/urls.cpython-312.pyc index 5e4505c6..bcc71e78 100644 Binary files a/djangoProject2/movie/__pycache__/urls.cpython-312.pyc and b/djangoProject2/movie/__pycache__/urls.cpython-312.pyc differ diff --git a/djangoProject2/movie/__pycache__/views.cpython-312.pyc b/djangoProject2/movie/__pycache__/views.cpython-312.pyc index c18f1a22..51b96cf3 100644 Binary files a/djangoProject2/movie/__pycache__/views.cpython-312.pyc and b/djangoProject2/movie/__pycache__/views.cpython-312.pyc differ diff --git a/djangoProject2/movie/urls.py b/djangoProject2/movie/urls.py index 35f6ed4e..a8b0279b 100644 --- a/djangoProject2/movie/urls.py +++ b/djangoProject2/movie/urls.py @@ -6,14 +6,14 @@ app_name='movie' urlpattterns=[ - path('',views.indexView.as_view(),name='index'), + path('',views.IndexView.as_view(),name='index'), path('hot',views.PopularMovieView.as_view(),name='hot'), - path('login',views.loginView.as_view(),name='login'), + path('login',views.LoginView.as_view(),name='login'), path('logout',views.UserLogout.as_view(),name='logout'), path('register',views.RegisterView.as_view(),name='register'), path('tag',views.TagView.as_view(),name='tag'), path('search',views.SearchView.as_view(),name='search'), path('detail/',views.MovieDetailView.as_view(),name='detail'), path('history/',views.RatingHistoryView.as_view(),name='history'), - path('del_rec/',views.delete_recode,name='delete_recode') + path('del_rec/',views.delete_recode,name='delete_recode'), ] \ No newline at end of file diff --git a/djangoProject2/movie/views.py b/djangoProject2/movie/views.py index 9a911759..cf0313e0 100644 --- a/djangoProject2/movie/views.py +++ b/djangoProject2/movie/views.py @@ -128,7 +128,7 @@ class TagView(ListView): return movies[100:200] else: - movies=movie.objects.filter(genre__name=self.request.GET.dict()['genre']) + 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): @@ -206,123 +206,152 @@ class SearchView(ListView): 'current_page':current_page, 'left_has__more':left_has_more, 'right_has__more':right_has_more - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + } +class RegisterView(View): + def get(self,request): + return render(request,'movie/register.html') + def post(self,request): + form=RegisterForm(request.POST) + if form.is_valid(): + form.save() + return redirect(reverse('movie.index')) + else: + errors=form.get_errors() + for error in errors: + messages.info(request,error) + print(form.errors.get_json_data()) + return redirect(reverse('movie:register')) + +class LoginView(View): + def get(self,request): + return render(request,'movie/login.html') + + def post(self,request): + form=LoginForm(request.POST) + if form.is_valid(): + name=form.cleaned_data.get('name') + pwd=form.cleaned_data.get('password') + remember=form.cleaned_data.get('remember') + user=User.objects.filter(name=name,password=pwd).first() + if user: + if remember: + request.session.set_expiry(None) + else: + request.session.set_expiry(0) + request.session['user_id']=user.id + return redirect(reverse('movie.index')) + else: + messages.info(request,'用户名或密码错误') + return redirect(reverse('movie:login')) + else: + errors=form.get_errors() + for error in errors: + messages.info(request,error) + return redirect(reverse('movie:login')) +class UserLogout(request): + request.session.set_expiry(-1) + return redirect(reverse('movie:index')) + +class MovieDetailView(DetailView): + model=Movie + template_name='movie/detail.html' + context_object_name = 'movie' + def get_context_data(self,**kwargs): + context=super().get_context_data(**kwargs) + login=True + try: + user_id=self.request.session['user_id'] + except KeyError as e: + login=False + + pk=self.kwargs['pk'] + movie=Movie.object.get(pk=pk) + if login: + user=User.objects.get(pk=user_id) + rating=Movie_rating.objects.filter(user=user,movie=movie).first() + score=0 + comment='' + if rating: + score=rating.score + comment=rating.comment + context.update({'score':score,'comment':comment}) + similary_movies=movie.get_similarity() + context.update({'similary_movies':similary_movies}) + context.update({'login':login}) + return context + def post(self,request,pk): + form=CommentForm(request.POST) + if form.is_valid(): + score=form.cleaned_data.get('score') + 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() + if rating: + rating.score=score + rating.comment=comment + rating.save() + else: + rating=Movie_rating(user=user,movie=movie,score=score,comment=comment) + rating.save() + messages.info(request,"评论成功!") + else: + messages.info(request,"评论不能为空!") + return redirect(reverse('movie:detail',args=(pk,))) + +class RatingHistoryView(DetailView): + model=User + template_name='movie/history.html' + qcontext_object_name='user' + + def get_context_data(self,**kwargs): + context=super().get_context_data(**kwargs) + user_id=self.request.session['user_id'] + user=User.objects.get(pk=user_id) + ratings=Movie_rating.objects.filter(user=user).order_by('-score') + context.update({'ratings':ratings}) + return context +def delete_recode(request,pk): + movie=Movie.objects.get(pk=pk) + user_id=request.session['user_id'] + user=User.objects.get(pk=user_id) + rating=Movie_rating.objects.get(user=user,movie=movie) + rating.delete() + messages.info(request,f'删除{movie.name}评分记录成功!') + return redirect(reverse('movie:history',args=(user_id,))) +class recommendMovieView(ListView): + model=Movie + template_name='movie/recommend.html' + paginate_by=15 + context_object_name='movies' + ordering='movie_rating__score' + page_kwarg='p' + def __int__(self): + super().__init__() + self.K=20 + self.N=10 + self.cur_user_movie_qs=None + + def get_user_sim(self): + user_sim_dct=dict() + cur_user_id=self.request.session['user_id'] + cur_user=User.objects.get(pk=cur_user_id) + 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) + 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')) \ No newline at end of file