diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2016-06-07 20:28:57 +0200 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2016-06-07 20:28:57 +0200 |
| commit | 5bd75654d252e3e1a4eef515493bfc300fe5ab16 (patch) | |
| tree | c4862b3a4d79adf04b055a3c819ab992ed7c2120 | |
| parent | effcf34153ffd0a40e4604c740a9321f57317156 (diff) | |
| download | jokevote-5bd75654d252e3e1a4eef515493bfc300fe5ab16.tar.gz jokevote-5bd75654d252e3e1a4eef515493bfc300fe5ab16.zip | |
add export as txt
| -rw-r--r-- | app.py | 20 | ||||
| -rw-r--r-- | templates/index.html | 1 |
2 files changed, 19 insertions, 2 deletions
@@ -142,7 +142,10 @@ class dbProxy(object): l = len(self.getJokes(user=user))-1 return int(l/perpage)+1 - def getJokes(self, perpage=None, page=None, user=None): + def getJokes(self, perpage=None, page=None, user=None, sortby='rank'): + # perpage, page: only return subset for pagination + # user: return with user-specific attributes, also return deleted jokes + # sortby: rank - calculated by score and freshness, score - only score ret_jokes = [] jokes = self.c.execute("SELECT * FROM " + self.prefix + "_jokes ORDER BY id ASC").fetchall() votewhere = "SELECT COUNT(*) FROM " + self.prefix + "_votes WHERE " @@ -194,7 +197,12 @@ class dbProxy(object): ret_jokes.append(ret_joke) - ret_jokes = sorted(ret_jokes, key=lambda j: (j['score']+1)/pow(j['freshness']+1, 2), reverse=True) + if sortby == 'rank': + sorter = lambda j: (j['score']+1)/pow(j['freshness']+1, 2) + if sortby == 'score': + sorter = lambda j: j['score'] + + ret_jokes = sorted(ret_jokes, key=sorter, reverse=True) if page != None and perpage != None: return ret_jokes[page*perpage:(page+1)*perpage] return ret_jokes @@ -408,6 +416,14 @@ def logout(): page = request.form['redirpage'] return redirect('/page/' + page) +@app.route('/export') +def export(): + jokes = db().getJokes(sortby='score') + texts = [j['text'] for j in jokes] + r = make_response("\n\r\n\r".join(texts)) + r.headers['Content-Type'] = 'text/plain; charset=utf-8'; + return r + # TODO use nginx for this @app.route('/static/<path:path>') def get_static(path): diff --git a/templates/index.html b/templates/index.html index cdbb1b5..8ea3722 100644 --- a/templates/index.html +++ b/templates/index.html @@ -15,6 +15,7 @@ <div class="row"> <h3>Lehrersprüche</h3> <div class="section"> + <a class="btn-floating btn-large right" href="/export"><i class="material-icons">file_download</i></a> {% if user.loggedin %} <form action="/logout" method="post"> <input type="hidden" name="redirpage" value="{{ currentpage }}"> |
