Skip to content

Instantly share code, notes, and snippets.

@santiagobasulto
Created February 19, 2012 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santiagobasulto/1865924 to your computer and use it in GitHub Desktop.
Save santiagobasulto/1865924 to your computer and use it in GitHub Desktop.
Functional programing with python and django
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