def test_get_number_of_votes(self): ''' Testing the method get_number_of_votes ''' # Exising idea and user number_of_votes = IdeaService.get_number_of_votes( self.user.id, self.idea.id ) self.assertEqual(number_of_votes, 1) # User does not exist number_of_votes = IdeaService.get_number_of_votes(0, self.idea.id) self.assertEqual(number_of_votes, 0) # Idea does not exist number_of_votes = IdeaService.get_number_of_votes(self.user.id, 0) self.assertEqual(number_of_votes, 0) # Both idea and user do not exist number_of_votes = IdeaService.get_number_of_votes(0, 0) self.assertEqual(number_of_votes, 0) # Using None number_of_votes = IdeaService.get_number_of_votes(None, None) self.assertEqual(number_of_votes, 0)
def test_get_number_of_votes(self): ''' Testing the method get_number_of_votes ''' # Exising idea and user number_of_votes = IdeaService.get_number_of_votes( self.user.id, self.idea.id) self.assertEqual(number_of_votes, 1) # User does not exist number_of_votes = IdeaService.get_number_of_votes(0, self.idea.id) self.assertEqual(number_of_votes, 0) # Idea does not exist number_of_votes = IdeaService.get_number_of_votes(self.user.id, 0) self.assertEqual(number_of_votes, 0) # Both idea and user do not exist number_of_votes = IdeaService.get_number_of_votes(0, 0) self.assertEqual(number_of_votes, 0) # Using None number_of_votes = IdeaService.get_number_of_votes(None, None) self.assertEqual(number_of_votes, 0)
def validate(self, value): ''' Validate the vote for an idea from the current user. At this point, the idea is valid, and we check whereas the user already voted for this idea or not. ''' super(IdeaIdField, self).validate(value) if not self.user.id: raise forms.ValidationError('You must be logged in to vote.'); number_of_votes = IdeaService.get_number_of_votes( self.user.id, value.id ) if number_of_votes > 0: raise forms.ValidationError('You already voted for this idea.');
def get_context_data(self, **kwargs): ''' This is how we can add extra data to the context when using a generic view. We are just adding a flag to check whether the user already voted for the idea or not. ''' context = super(DetailView, self).get_context_data(**kwargs) #Cannot vote if not logged in has_not_voted = False #Checking if the user already voted if self.request.user.id: number_of_votes = IdeaService.get_number_of_votes( self.request.user.id, self.kwargs['pk']) has_not_voted = (number_of_votes == 0) context['has_not_voted'] = has_not_voted return context
def get_context_data(self, **kwargs): ''' This is how we can add extra data to the context when using a generic view. We are just adding a flag to check whether the user already voted for the idea or not. ''' context = super(DetailView, self).get_context_data(**kwargs) #Cannot vote if not logged in has_not_voted = False #Checking if the user already voted if self.request.user.id: number_of_votes = IdeaService.get_number_of_votes( self.request.user.id, self.kwargs['pk'] ) has_not_voted = (number_of_votes == 0) context['has_not_voted'] = has_not_voted return context