コード例 #1
0
ファイル: tests.py プロジェクト: GwamakaCharles/MyMDB
 def testUserManipulationOnCreateFails(self):
     self.assertEqual(0,
                      Vote.objects.filter(user=self.correct_user).count())
     self.assertEqual(0, Vote.objects.filter(user=self.wrong_user).count())
     form = VoteForm(initial={'user': self.correct_user},
                     data={
                         'user': self.wrong_user.id,
                         'movie': self.movie.id,
                         'value': Vote.UP
                     })
     self.assertFalse(form.is_valid())
コード例 #2
0
ファイル: tests.py プロジェクト: GwamakaCharles/MyMDB
 def testVoteCreatedWithGoodData(self):
     self.assertEqual(0,
                      Vote.objects.filter(user=self.correct_user).count())
     self.assertEqual(0, Vote.objects.filter(user=self.wrong_user).count())
     form = VoteForm(initial={
         'user': self.correct_user.id,
         'movie': self.movie.id,
     },
                     data={'value': Vote.UP})
     self.assertTrue(form.is_valid(), form.errors)
     form.save()
     self.assertEqual(1,
                      Vote.objects.filter(user=self.correct_user).count())
     self.assertEqual(0, Vote.objects.filter(user=self.wrong_user).count())
コード例 #3
0
ファイル: views.py プロジェクト: inlearn/MyMDB
 def get_context_data(self, **kwargs):
     ctx = super().get_context_data(**kwargs)
     ctx['image_form'] = self.movie_image_form()
     if self.request.user.is_authenticated:
         #  这边看
         vote = Vote.objects.get_vote_or_unsaved_blank_vote(
             movie=self.object, user=self.request.user)
         #  如果存在投票记录,和如果不存在投票记录,
         #  通过vote_form_url 决定,是要更新还是新投票
         #  这里,我也可以预判,这题做没做,如果做过就更新,如果没做就
         #  新做
         if vote.id:
             vote_form_url = reverse('core:UpdateVote',
                                     kwargs={
                                         'movie_id': vote.movie.id,
                                         'pk': vote.id
                                     })
         else:
             vote_form_url = reverse('core:CreateVote',
                                     kwargs={'movie_id': self.object.id})
         vote_form = VoteForm(instance=vote)
         ctx['vote_form'] = vote_form
         ctx['vote_form_url'] = vote_form_url
         print(ctx)
     return ctx
コード例 #4
0
ファイル: views.py プロジェクト: chootingstar/myMovieDataBase
	def get_context_data(self, **kwargs):
		ctx = super().get_context_data(**kwargs)
		if self.request.user.is_authenticated:
			vote = Vote.objects.get_vote_or_unsaved_blank_vote(
				movie=self.object,
				user=self.request.user
			)
			if vote.id:
				vote_form_url = reverse(
					'core:UpdateVote',
					kwargs={
						'movie_id': vote.movie.id,
						'pk': vote.id})
			else:
				vote_form_url = (
					reverse(
						'core:CreateVote',
						kwargs={
							'movie_id': self.object.id}
					)
				)
			vote_form = VoteForm(instance=vote)
			ctx['vote_form'] = vote_form
			ctx['vote_form_url'] = \
				vote_form_url
		return ctx
コード例 #5
0
ファイル: views.py プロジェクト: Gabkings/MyMDB
class UpdateVote(LoginRequiredMixin, UpdateView):
    form_class = VoteForm()
    queryset = Vote.objects.all()

    def get_object(self, queryset=None):
        vote = super().get_object(queryset)
        user = self.request.user
        if vote.user != user:
            raise PermissionDenied('cannot change another' 'users vote')
        return vote

    def get_success_url(self):
        movie_id = self.objects.movie.id
        return reverse('core:MovieDetail', kwargs={'pk': movie_id})

    def render_to_response(self, context, **response_kwargs):
        movie_id = context['object'].id
        movie_detail_url = reverse('core:MovieDetail', kwargs={'pk': movie_id})
        return redirect(to=movie_detail_url)
コード例 #6
0
 def get_context_data(
     self, **kwargs
 ):  # get_context_data Obtiene datos del contexto variables, objectos, etc, que le vas a pasar al template
     ctx = super().get_context_data(**kwargs)
     ctx["image_form"] = self.movie_image_form()
     if self.request.user.is_authenticated:
         vote = Vote.objects.get_vote_or_unsaved_blank_vote(
             movie=self.object,
             user=self.request.user)  #Obtiene movie y usuario
         if vote.id:
             vote_form_url = reverse('core:UpdateVote',
                                     kwargs={
                                         'movie_id': vote.movie.id,
                                         "pk": vote.id
                                     })
         else:
             vote_form_url = (reverse("core:CreateVote",
                                      kwargs={"movie_id": self.object.id}))
         vote_form = VoteForm(instance=vote)
         ctx["vote_form"] = vote_form
         ctx["vote_form_url"] = vote_form_url
     return ctx
コード例 #7
0
 def get_context_data(self, **kwargs):
     ctx = super().get_context_data(**kwargs)
     ctx['image_form'] = self.movie_image_form()
     if self.request.user.is_authenticated:
         vote = Vote.objects.get_vote_or_unsaved_blank_vote(
             movie=self.object,
             user=self.request.user
         )
         if vote.id:
             vote_form_url = reverse(
                 'core:update-vote',
                 kwargs=dict(movie_id=vote.movie.id, pk=vote.id)
             )
         else:
             vote_form_url = reverse(
                  'core:create-vote',
                  kwargs=dict(movie_id=self.object.id)
              )
         vote_form = VoteForm(instance=vote)
         ctx['vote_form'] = vote_form
         ctx['vote_form_url'] = vote_form_url
     return ctx