def record_vote(self, obj, user, vote):
        """
        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 not in (+1, 0, -1):
            raise ValueError('Invalid vote (must be +1/0/-1)')
        ctype = ContentType.objects.get_for_model(obj)
        try:
            v = self.get(user=user, content_type=ctype,
                         object_id=obj._get_pk_val())
            
            # previous vote exists, so remove the old one from the object
            if v.vote == 1:
                obj.yeas -= 1
            if v.vote == -1:
                obj.nays -= 1
            
            if vote == 0:
                v.delete()
                # the user wants to clear their vote so
                obj.votes -= 1
            else:
                v.vote = vote
                v.save()
        except models.ObjectDoesNotExist:
            if vote != 0:
                self.create(user=user, content_type=ctype,
                            object_id=obj._get_pk_val(), vote=vote)
                # we just created a new vote so 
                obj.votes += 1
        
        # update the denorm fields
        if vote == 1:
            obj.yeas += 1
        if vote == -1:
            obj.nays += 1
        
        obj.save()
dataset/ETHPy150Open caseywstark/colab/colab/apps/voting/managers.py/VoteManager.record_vote/original