diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2016-05-29 19:01:27 +0200 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2016-05-29 19:01:27 +0200 |
| commit | cf94177eb06be4d7b9c2d677dea58ab3f0d7133e (patch) | |
| tree | abbda594b7aa2835e5a337baaa751a25358faf49 | |
| parent | 40d78eecccd510041624877e3ff9efb900abc222 (diff) | |
| download | jokevote-cf94177eb06be4d7b9c2d677dea58ab3f0d7133e.tar.gz jokevote-cf94177eb06be4d7b9c2d677dea58ab3f0d7133e.zip | |
prevent users from voting multiple times
| -rw-r--r-- | app.py | 35 | ||||
| -rw-r--r-- | templates/index.html | 6 |
2 files changed, 30 insertions, 11 deletions
@@ -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") diff --git a/templates/index.html b/templates/index.html index c4e17b8..1a6edf3 100644 --- a/templates/index.html +++ b/templates/index.html @@ -19,13 +19,13 @@ <form action="/vote" method="post"> <input type="hidden" name="id" value="{{ joke.id }}"> <input type="hidden" name="redirpage" value="{{ currentpage }}"> - <button class="btn{% if joke.reports>0 %}-flat{% endif %} col s12" type="submit" name="vote" value="upvote"><i class="material-icons">thumb_up</i> {{ joke.upvotes }}</button> - <button class="btn{% if joke.reports>0 %}-flat{% endif %} col s12" type="submit" name="vote" value="downvote"><i class="material-icons">thumb_down</i> {{ joke.downvotes }}</button> + <button class="btn{% if joke.reports>0 %}-flat{% endif %}{% if joke.locked %} disabled{% endif %} col s12" type="submit" name="vote" value="upvote"><i class="material-icons">thumb_up</i> {{ joke.upvotes }}</button> + <button class="btn{% if joke.reports>0 %}-flat{% endif %}{% if joke.locked %} disabled{% endif %} col s12" type="submit" name="vote" value="downvote"><i class="material-icons">thumb_down</i> {{ joke.downvotes }}</button> </form> <form action="/report" method="post"> <input type="hidden" name="id" value="{{ joke.id }}"> <input type="hidden" name="redirpage" value="{{ currentpage }}"> - <button class="btn-flat col s12" type="submit"> + <button class="btn-flat{% if joke.locked %} disabled{% endif %} col s12" type="submit"> {% if joke.reports>0 %} <div class="row"> <i class="material-icons col s6">report_problem</i> |
