diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2016-05-28 17:56:25 +0200 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2016-05-28 17:56:25 +0200 |
| commit | 28fc7d8068ee6b88fed55328f0bf88a37c522020 (patch) | |
| tree | 289521f889f39f8e4efdc9604dfaf565f21e16f0 /app.py | |
| parent | 7231d6dd3b16d9454593ad75fdd26ea6b81fd449 (diff) | |
| download | jokevote-28fc7d8068ee6b88fed55328f0bf88a37c522020.tar.gz jokevote-28fc7d8068ee6b88fed55328f0bf88a37c522020.zip | |
implement simple scoring system
Diffstat (limited to 'app.py')
| -rw-r--r-- | app.py | 18 |
1 files changed, 14 insertions, 4 deletions
@@ -15,6 +15,7 @@ import sqlite3 class dbProxy(object): def __init__(self, db): self.conn = sqlite3.connect(db) + self.conn.row_factory = self.dict_factory self.c = self.conn.cursor() def create(self): @@ -23,9 +24,20 @@ class dbProxy(object): def close(self): self.conn.close() + def dict_factory(self, cursor, row): + d = {} + for idx, col in enumerate(cursor.description): + d[col[0]] = row[idx] + return d + + def sort(self, joke): + interactions = joke["reports"]*5 + joke["upvotes"] + joke["downvotes"] + 1 + score = 1/interactions * (joke["upvotes"]+1)/interactions + return score + def getJokes(self): - jokes = self.c.execute("SELECT * FROM jokes ORDER BY reports ASC").fetchall() - jokes = [{"id": int(id), "text": text, "upvotes": int(upvotes), "downvotes": int(downvotes), "reports": int(reports)} for id, text, upvotes, downvotes, reports in jokes] # TODO use factory + jokes = self.c.execute("SELECT * FROM jokes").fetchall() + jokes = sorted(jokes, key=self.sort, reverse=True) return jokes def addJoke(self, text): @@ -89,7 +101,5 @@ def report(): def get_static(path): return send_from_directory('static', path) -# TODO sort by votes -# TODO buttons floating weird if __name__ == '__main__': app.run(debug=True) |
