summaryrefslogtreecommitdiff
path: root/app.py
diff options
context:
space:
mode:
Diffstat (limited to 'app.py')
-rw-r--r--app.py19
1 files changed, 14 insertions, 5 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)