summaryrefslogtreecommitdiff
path: root/app.py
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2016-05-29 19:01:27 +0200
committerschneefux <schneefux+commit@schneefux.xyz>2016-05-29 19:01:27 +0200
commitcf94177eb06be4d7b9c2d677dea58ab3f0d7133e (patch)
treeabbda594b7aa2835e5a337baaa751a25358faf49 /app.py
parent40d78eecccd510041624877e3ff9efb900abc222 (diff)
downloadjokevote-cf94177eb06be4d7b9c2d677dea58ab3f0d7133e.tar.gz
jokevote-cf94177eb06be4d7b9c2d677dea58ab3f0d7133e.zip
prevent users from voting multiple times
Diffstat (limited to 'app.py')
-rw-r--r--app.py35
1 files changed, 27 insertions, 8 deletions
diff --git a/app.py b/app.py
index dc385dd..e24f619 100644
--- a/app.py
+++ b/app.py
@@ -20,6 +20,7 @@ class dbProxy(object):
def create(self):
self.c.execute("CREATE TABLE IF NOT EXISTS jokes(id INTEGER PRIMARY KEY NOT NULL, text TEXT, upvotes INTEGER, downvotes INTEGER, reports INTEGER)")
+ self.c.execute("CREATE TABLE IF NOT EXISTS votes(id INTEGER PRIMARY KEY NOT NULL, ip TEXT, jokeid INTEGER, type INTEGER)")
def close(self):
self.conn.close()
@@ -49,17 +50,24 @@ class dbProxy(object):
self.c.execute("INSERT INTO jokes(text, upvotes, downvotes, reports) VALUES (?, 0, 0, 0)", (text, ))
self.conn.commit()
- def voteJoke(self, objectId, down):
+ def voteJoke(self, objectId, down, ip):
if down:
self.c.execute("UPDATE jokes SET downvotes=downvotes+1 WHERE id=?", (objectId,))
else:
self.c.execute("UPDATE jokes SET upvotes=upvotes+1 WHERE id=?", (objectId,))
+ self.c.execute("INSERT INTO votes(ip, jokeid, type) VALUES (?, ?, ?)", (ip, objectId, -1 if down else +1))
self.conn.commit()
- def reportJoke(self, objectId):
+ def reportJoke(self, objectId, ip):
self.c.execute("UPDATE jokes SET reports=reports+1 WHERE id=?", (objectId,))
+ self.c.execute("INSERT INTO votes(ip, jokeid, type) VALUES (?, ?, ?)", (ip, objectId, 0))
self.conn.commit()
+ def getUserVotes(self, ip):
+ votes = self.c.execute("SELECT jokeid FROM votes WHERE ip=?", (ip, )).fetchall()
+ votes = [v['jokeid'] for v in votes]
+ return votes
+
def db():
db = getattr(g, "_database", None)
@@ -84,7 +92,14 @@ def root():
@app.route('/page/<int:num>')
def page(num):
numpages = db().getPages(PERPAGE)
- return render_template('index.html', currentpage=num, pages=[[]]*numpages, jokes=db().getJokes(PERPAGE, num))
+ jokes = db().getJokes(PERPAGE, num)
+ ip = request.remote_addr
+ votes = db().getUserVotes(ip)
+ for joke in jokes:
+ if joke['id'] in votes:
+ joke['locked'] = True
+
+ return render_template('index.html', currentpage=num, pages=[[]]*numpages, jokes=jokes)
@app.route('/submit', methods=['POST'])
def submit():
@@ -96,16 +111,20 @@ def submit():
@app.route('/vote', methods=['POST'])
def vote():
- objectId = request.form['id']
+ objectId = int(request.form['id'])
page = request.form['redirpage']
- db().voteJoke(objectId, request.form['vote'] == 'downvote')
+ ip = request.remote_addr
+ if objectId not in db().getUserVotes(ip):
+ db().voteJoke(objectId, request.form['vote'] == 'downvote', ip)
return redirect('/page/' + page)
@app.route('/report', methods=['POST'])
def report():
- objectId = request.form['id']
+ objectId = int(request.form['id'])
page = request.form['redirpage']
- db().reportJoke(objectId)
+ ip = request.remote_addr
+ if objectId not in db().getUserVotes(ip):
+ db().reportJoke(objectId, ip)
return redirect('/page/' + page)
@app.route('/static/<path:path>')
@@ -114,4 +133,4 @@ def get_static(path):
app.debug = True
if __name__ == '__main__':
- app.run()
+ app.run(host="0.0.0.0")