コード例 #1
0
ファイル: tests.py プロジェクト: thumbimigwe/pi
 def test_vote_model(self):
     vote = Vote()
     vote.object_id = self.article._get_pk_val()
     vote.content_type = ContentType.objects.get_for_model(self.article)
     #vote.content_object = ContentType.objects.get_for_model(self.article)
     
     vote.voter = self.user1
     vote.vote = UPVOTE
     
     vote.save()
     
     vote_obj = Vote.objects.all()[0]
     self.assertEqual(vote_obj._get_pk_val(), vote._get_pk_val(), "Primary Keys do not match")
     self.assertEqual(vote.vote, vote_obj.vote, "Vote value does not match")
コード例 #2
0
ファイル: tests.py プロジェクト: thumbimigwe/pi
    def test_vote_model(self):
        vote = Vote()
        vote.object_id = self.article._get_pk_val()
        vote.content_type = ContentType.objects.get_for_model(self.article)
        #vote.content_object = ContentType.objects.get_for_model(self.article)

        vote.voter = self.user1
        vote.vote = UPVOTE

        vote.save()

        vote_obj = Vote.objects.all()[0]
        self.assertEqual(vote_obj._get_pk_val(), vote._get_pk_val(),
                         "Primary Keys do not match")
        self.assertEqual(vote.vote, vote_obj.vote, "Vote value does not match")
コード例 #3
0
ファイル: serializers.py プロジェクト: thumbimigwe/pi
 def create(self, validated_data):
     print 'In create'        
     vote = Vote()
     vote.voter = validated_data.get('voter')
     vote.vote = validated_data.get('vote')
     vote.content_type = validated_data.get('content_type')
     vote.object_id = validated_data.get('object_id')
     
     #Get row from contentType which has content_type
     content_object = ContentType.objects.get_for_id(vote.content_type.id)
     
     vote.content_object = content_object.model_class().objects.get(id=vote.object_id)
                     
     """
     Record a user's vote on a given object. Only allows a given user
     to vote once, though that vote may be changed.
     
     A zero vote indicates that any existing vote should be removed.
     """
     if vote.vote not in (+1, 0, -1):
         raise ValueError('Invalid vote (must be +1/0/-1)')
     
     # First, try to fetch the instance of this row from DB
     # If that does not exist, then it is the first time we're creating it
     # If it does, then just update the previous one
     try:
         vote_obj = Vote.objects.get(voter=vote.voter, content_type=vote.content_type, object_id=vote.object_id)
         if vote == 0 and not ZERO_VOTES_ALLOWED:
             vote_obj.delete()
         else:
             vote_obj.vote = vote
             vote_obj.save()
             
     except ObjectDoesNotExist:
         #This is the first time we're creating it
         try:
             if not ZERO_VOTES_ALLOWED and vote == 0:
                 # This shouldn't be happening actually
                 return
             vote_obj = Vote.objects.create(voter=vote.voter, content_type=vote.content_type, object_id=vote.object_id, vote=vote.vote)                        
         except:
             print '{file}: something went wrong in creating a vote object at {line}'.format(file=str('__FILE__'), line=str('__LINE__'))
             raise ObjectDoesNotExist    
     
     return vote_obj