# Path: votes/models.py
# class Vote(models.Model):
#     isVoteUp = models.BooleanField(default=False)
#     createdAt = models.DateTimeField(auto_now_add=True)
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="votes")
# 
#     content_type = models.ForeignKey(ContentType)
#     object_id = models.PositiveIntegerField()
#     content = GenericForeignKey()
# 
#     objects = VoteManager()
# 
#     def save(self, *args, **kwargs):
#         super(Vote, self).save(*args, **kwargs)
#         self.content.upVotes = Vote.objects.get_votes_count(self.content, True)
#         self.content.downVotes = Vote.objects.get_votes_count(self.content,
#                                                               False)
#         self.content.save()
# 
#     def delete(self, *args, **kwargs):
#         if self.isVoteUp:
#             self.content.upVotes = Vote.objects.get_votes_count(self.content,
#                                                                 True) - 1
#         else:
#             self.content.downVotes = Vote.objects.get_votes_count(self.content,
#                                                                   False) - 1
#         self.content.save()
#         super(Vote, self).delete(*args, **kwargs)
# 
#     def get_content_type():
#         if not hasattr(Vote, '_content_type'):
#             Vote._content_type = ContentType.objects.get(
#                 app_label="votes", model="vote")
#         return Vote._content_type


from django.contrib.contenttypes.models import ContentType
from votes.models import Vote


# Doesn't render anything


def do_vote(user, content, vote_request):
    try:
        content_type = ContentType.objects.get_for_model(content)
        old_vote = Vote.objects.get(user=user,
                                    object_id=content.id,
                                    content_type=content_type)
        vote_was_up = old_vote.isVoteUp
        old_vote.delete()
        new_vote = False
    except:
        new_vote = True


# Path: votes/models.py
# class Vote(models.Model):
#     isVoteUp = models.BooleanField(default=False)
#     createdAt = models.DateTimeField(auto_now_add=True)
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="votes")
# 
#     content_type = models.ForeignKey(ContentType)
#     object_id = models.PositiveIntegerField()
#     content = GenericForeignKey()
# 
#     objects = VoteManager()
# 
#     def save(self, *args, **kwargs):
#         super(Vote, self).save(*args, **kwargs)
#         self.content.upVotes = Vote.objects.get_votes_count(self.content, True)
#         self.content.downVotes = Vote.objects.get_votes_count(self.content,
#                                                               False)
#         self.content.save()
# 
#     def delete(self, *args, **kwargs):
#         if self.isVoteUp:
#             self.content.upVotes = Vote.objects.get_votes_count(self.content,
#                                                                 True) - 1
#         else:
#             self.content.downVotes = Vote.objects.get_votes_count(self.content,
#                                                                   False) - 1
#         self.content.save()
#         super(Vote, self).delete(*args, **kwargs)
# 
#     def get_content_type():
#         if not hasattr(Vote, '_content_type'):
#             Vote._content_type = ContentType.objects.get(
#                 app_label="votes", model="vote")
#         return Vote._content_type


# Path: votes/views.py
from django.contrib.contenttypes.models import ContentType
from votes.models import Vote



# Doesn't render anything


def do_vote(user, content, vote_request):
    try:
        content_type = ContentType.objects.get_for_model(content)
        old_vote = Vote.objects.get(user=user,
                                    object_id=content.id,
                                    content_type=content_type)
        vote_was_up = old_vote.isVoteUp
        old_vote.delete()
        new_vote = False
    except:
        new_vote = True


    vote = Vote(user=user, content=content)