Created
February 19, 2012 21:34
-
-
Save santiagobasulto/1865924 to your computer and use it in GitHub Desktop.
Functional programing with python and django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __vote(self,user,up_or_down): | |
if self.user is not user: | |
votes = VoteQuestion.objects.filter(question=self,user=user) | |
if len(votes)>0: | |
vote = votes[0] | |
if vote.score is up_or_down: | |
# el mismo voto | |
return -1 | |
else: | |
vote.score += up_or_down | |
vote.save() | |
#smell | |
if up_or_down is 1: | |
self.votes_up += 1 | |
elif up_or_down is -1: | |
self.votes_down += 1 | |
else: | |
raise ValueError("Bad argument, up_or_down must be 1 or -1") | |
self.save() | |
return 1 | |
else: | |
VoteQuestion(question=self,user=user,score=up_or_down).save() | |
#smell | |
if up_or_down is 1: | |
self.votes_up += 1 | |
elif up_or_down is -1: | |
self.votes_down += 1 | |
else: | |
raise ValueError("Bad argument, up_or_down must be 1 or -1") | |
self.save() | |
return 1 | |
return -1 | |
vote_up = lambda self,user: self.__vote(user,1) | |
vote_down = lambda self,user: self.__vote(user,-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment