summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2016-05-28 12:03:56 +0200
committerschneefux <schneefux+commit@schneefux.xyz>2016-05-28 12:03:56 +0200
commit7231d6dd3b16d9454593ad75fdd26ea6b81fd449 (patch)
tree99a0c0437376ef5e988647f057ca38b965f973eb
parent446163be0f346ce8a91d4201ec67e56cf31efa6f (diff)
downloadjokevote-7231d6dd3b16d9454593ad75fdd26ea6b81fd449.tar.gz
jokevote-7231d6dd3b16d9454593ad75fdd26ea6b81fd449.zip
add report button
-rw-r--r--app.py19
-rw-r--r--templates/index.html15
2 files changed, 25 insertions, 9 deletions
diff --git a/app.py b/app.py
index d4d850d..eadd1e6 100644
--- a/app.py
+++ b/app.py
@@ -18,18 +18,18 @@ class dbProxy(object):
self.c = self.conn.cursor()
def create(self):
- self.c.execute("CREATE TABLE IF NOT EXISTS jokes(id INTEGER PRIMARY KEY NOT NULL, text TEXT, upvotes INTEGER, downvotes INTEGER)")
+ self.c.execute("CREATE TABLE IF NOT EXISTS jokes(id INTEGER PRIMARY KEY NOT NULL, text TEXT, upvotes INTEGER, downvotes INTEGER, reports INTEGER)")
def close(self):
self.conn.close()
def getJokes(self):
- jokes = self.c.execute("SELECT * FROM jokes").fetchall()
- jokes = [{"id": id, "text": text, "upvotes": upvotes, "downvotes": downvotes} for id, text, upvotes, downvotes in jokes]
+ 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
return jokes
def addJoke(self, text):
- self.c.execute("INSERT INTO jokes(text, upvotes, downvotes) VALUES (?, 0, 0)", (text, ))
+ self.c.execute("INSERT INTO jokes(text, upvotes, downvotes, reports) VALUES (?, 0, 0, 0)", (text, ))
self.conn.commit()
def voteJoke(self, objectId, down):
@@ -39,6 +39,10 @@ class dbProxy(object):
self.c.execute("UPDATE jokes SET upvotes=upvotes+1 WHERE id=?", (objectId))
self.conn.commit()
+ def reportJoke(self, objectId):
+ self.c.execute("UPDATE jokes SET reports=reports+1 WHERE id=?", (objectId))
+ self.conn.commit()
+
def db():
db = getattr(g, "_database", None)
@@ -63,7 +67,6 @@ def root():
joke['text'] = Markup(markdown.markdown(joke['text'], extensions=['markdown.extensions.nl2br'], output_format="html5", safe_mode="remove")) # TODO cache TODO safe_mode deprecated
return render_template('index.html', jokes=jokes)
-
@app.route('/submit', methods=['POST'])
def submit():
text = request.form['text']
@@ -76,6 +79,12 @@ def vote():
db().voteJoke(objectId, request.form['vote'] == 'downvote')
return redirect('/')
+@app.route('/report', methods=['POST'])
+def report():
+ objectId = request.form['id']
+ db().reportJoke(objectId)
+ return redirect('/')
+
@app.route('/static/<path:path>')
def get_static(path):
return send_from_directory('static', path)
diff --git a/templates/index.html b/templates/index.html
index 33a5126..ef7559d 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -16,12 +16,19 @@
<div class="section row valign-wrapper">
<form class="col s2 row" action="vote" method="post">
<input type="hidden" name="id" value="{{ joke.id }}">
- <button class="btn col s12" type="submit" name="vote" value="upvote"><i class="material-icons">thumb_up</i> {{ joke.upvotes }}</button>
- <button class="btn 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 %} 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>
</form>
- <div class="col s10 row">
- <p class="valign s12">{{ joke.text }}</p>
+ <div class="col s8 {% if joke.reports>0 %}grey-text{% endif %}">
+ <p>{{ joke.text }}</p>
</div>
+ <form class="col s2" action="report" method="post">
+ <input type="hidden" name="id" value="{{ joke.id }}">
+ <button class="btn-flat row" type="submit">
+ <i class="material-icons col s6">report_problem</i>
+ <span class="red-text col s6">{% if joke.reports>0 %}{{ joke.reports }}{% endif %}</span>
+ </button>
+ </form>
</div>
<div class="divider"></div>
{% endfor %}