summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2016-09-11 15:12:19 +0200
committerschneefux <schneefux+commit@schneefux.xyz>2016-09-11 15:12:19 +0200
commit9a983203c4c22f6e6494f7596433e6c857f9a591 (patch)
treeac7e3906412d3470ed92642a25a9aa2aa61a151e
parentf1892179491d68ad0b72913bc7c1b682e81afa2a (diff)
downloadjokevote-9a983203c4c22f6e6494f7596433e6c857f9a591.tar.gz
jokevote-9a983203c4c22f6e6494f7596433e6c857f9a591.zip
do not count a user's own votes; prevents jumping jokes
-rw-r--r--app.py23
-rw-r--r--templates/index.html15
2 files changed, 30 insertions, 8 deletions
diff --git a/app.py b/app.py
index 4e386c8..c35c130 100644
--- a/app.py
+++ b/app.py
@@ -289,28 +289,39 @@ class DBProxy(object):
def close(self):
self.conn.close()
- def score(self, jokeid):
+ def score(self, jokeid, exclude_voter=None):
+ # exclude_voter (optional): do not count a user's votes
+ if exclude_voter is None:
+ exclude_voter = ""
+
users = self.c.execute("SELECT id FROM " + self.prefix +
"_users WHERE role='user' " +
"OR role='super'").fetchall()
users = [u['id'] for u in users]
+
votewhere = "SELECT COUNT(*) FROM " + self.prefix + "_votes WHERE "
+
# TODO optimize queries
+ # count guest votes
score = 0
score += self.c.execute(
- votewhere + "joke=? AND type='up'",
- (jokeid,)).fetchone()['COUNT(*)']
+ votewhere + "joke=? AND NOT user=? AND type='up'",
+ (jokeid, exclude_voter)).fetchone()['COUNT(*)']
score -= self.c.execute(
- votewhere + "joke=? AND type='down'",
- (jokeid,)).fetchone()['COUNT(*)']
+ votewhere + "joke=? AND NOT user=? AND type='down'",
+ (jokeid, exclude_voter)).fetchone()['COUNT(*)']
+
for user in users:
# user's scores count 10 times more
+ if user == exclude_voter:
+ continue
score += self.c.execute(
votewhere + "joke=? AND type='up' AND user=?",
(jokeid, user)).fetchone()['COUNT(*)'] * 9
score -= self.c.execute(
votewhere + "joke=? AND type='down' AND user=?",
(jokeid, user)).fetchone()['COUNT(*)'] * 9
+
return score
def get_jokes(self, user=None, search=None, sortby='rank'):
@@ -359,7 +370,7 @@ class DBProxy(object):
if not match:
continue
- ret_joke['score'] = self.score(joke['id'])
+ ret_joke['score'] = self.score(joke['id'], user)
ret_joke['freshness'] = (now - joke['created']).days
# skip other's jokes marked as deleted
diff --git a/templates/index.html b/templates/index.html
index d176cdc..b884c57 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -112,8 +112,19 @@
<li class="collection-item row valign-wrapper">
<div class="col s4 m2 valign">
{% if not joke.deleted %}
- <div>
- <i class="material-icons">whatshot</i> <span class="right">{{ joke.score }} Punkte</span>
+ <div class="center">
+ <i class="material-icons">whatshot</i>
+ <span>{{ joke.score }}</span>
+ {% if joke.downvoted %}
+ <small class="red-text">
+ {% if user.loggedin %}-10{% else %}-1{% endif %}
+ </small>
+ {% endif %}
+ {% if joke.upvoted %}
+ <small class="green-text">
+ {% if user.loggedin %}+10{% else %}+1{% endif %}
+ </small>
+ {% endif %}
</div>
<form action="/upvote{{ query() }}" method="post">
<input type="hidden" name="id" value="{{ joke.id }}">