Created
November 17, 2010 18:30
-
-
Save csytan/703780 to your computer and use it in GitHub Desktop.
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
class Votable(BaseModel): | |
points = db.IntegerProperty(default=1) | |
score = db.FloatProperty() | |
up_votes = db.StringListProperty() # contains user key_names | |
down_votes = db.StringListProperty() # contains user key_names | |
def update_score(self): | |
"""Adapted from reddit's algorithm | |
http://code.reddit.com/browser/r2/r2/lib/db/sorts.py?rev=4778b17e939e119417cc5ec25b82c4e9a65621b2 | |
""" | |
td = self.created - datetime.datetime(1970, 1, 1) | |
epoch_seconds = td.days * 86400 + td.seconds + (float(td.microseconds) / 1000000) | |
order = math.log(max(abs(self.points), 1), 10) | |
sign = 1 if self.points > 0 else -1 if self.points < 0 else 0 | |
seconds = epoch_seconds - 1134028003 | |
self.score = round(order + sign * seconds / 45000, 7) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment