summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2016-06-14 10:59:13 +0200
committerschneefux <schneefux+commit@schneefux.xyz>2016-06-14 10:59:13 +0200
commit60e3ab8f5edd4513581d674e2b18acdf2d32af10 (patch)
tree5ee260917f262af4fd3430e4279a8e2fc9573687
parentc9ffbf6e180b713d6601b54ecde98ed9ef370d41 (diff)
downloadjokevote-60e3ab8f5edd4513581d674e2b18acdf2d32af10.tar.gz
jokevote-60e3ab8f5edd4513581d674e2b18acdf2d32af10.zip
add search
-rw-r--r--app.py20
-rw-r--r--templates/index.html17
2 files changed, 28 insertions, 9 deletions
diff --git a/app.py b/app.py
index 39d4b1f..8944a49 100644
--- a/app.py
+++ b/app.py
@@ -21,6 +21,7 @@ from config import config
class Markup(object):
tagmark = "_"
+ spacemark = " "
def prettify_text(self, text):
html = re.sub('<[^<]+?>', '', text)
@@ -244,7 +245,7 @@ class DBProxy(object):
def get_jokes(self, user=None, search=None, sortby='rank'):
# user: return with user-specific attributes, also return deleted jokes
- # search: return jokes including the specified word
+ # search: return jokes including all the specified words
# sortby: rank - calculated by score and freshness, score - only score
ret_jokes = []
jokes = self.c.execute("SELECT * FROM " + self.prefix +
@@ -275,8 +276,12 @@ class DBProxy(object):
joke['text'])
if search is not None: # simple search
- search = re.sub(r"^" + Markup.tagmark, "#", search)
- if search.lower() not in ret_joke['text'].lower():
+ match = True
+ for word in search:
+ if word.lower() not in ret_joke['text'].lower():
+ match = False
+ break
+ if not match:
continue
ret_joke['score'] = self.score(joke['id'])
@@ -439,21 +444,22 @@ def root():
@app.route('/page/<int:num>')
def page(num):
search = request.args.get('filter')
+ if search:
+ # TODO find a cleaner way
+ search = re.sub(r"^" + Markup.tagmark, "#", search)
+ search = search.split(Markup.spacemark)
perpage = abs(int(request.args.get('perpage') or 10))
jokes = db().get_jokes(user=userid(), search=search)
user = {'loggedin': False}
if 'userlogin' in session:
user['loggedin'] = True
user['name'] = session['userlogin']
- if search:
- # TODO find a cleaner way
- search = re.sub(r"^" + Markup.tagmark, "#", search)
resp = make_response(
render_template(
'index.html',
currentpage=num,
query=str(request.query_string, 'utf-8'),
- tag=search,
+ tags=search,
perpage=perpage,
jokes=jokes,
user=user))
diff --git a/templates/index.html b/templates/index.html
index f8d9bcf..861f259 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -34,6 +34,15 @@
</div>
</form>
{% endif %}
+ <form>
+ <div class="col s9 input-field">
+ <label for="search">Stichworte</label>
+ <input type="text" name="filter">
+ </div>
+ <div class="col s3">
+ <button class="btn" type="submit">Suchen</button>
+ </div>
+ </form>
<div class="col s2 m12 l2 right">
<a id="export" target="_blank" class="btn-floating btn-large right" href="/export{% if query %}?{{ query }}{% endif %}"><i class="material-icons">file_download</i></a>
</div>
@@ -49,11 +58,13 @@
</div>
{% endif %}
{% endwith %}
- {% if tag %}
+ {% if tags %}
+ {% for tag in tags %}
<div class="chip">
{{ tag }}
- <a href="/"><i class="material-icons">close</i></a>
+ <a href="/?filter={{ tags|reject('sameas', tag)|join('+') }}"><i class="material-icons">close</i></a>
</div>
+ {% endfor %}
{% endif %}
<ul class="collection">
{% for joke in jokes[currentpage*perpage:(currentpage+1)*perpage] %}
@@ -109,6 +120,7 @@
</li>
{% endfor %}
</ul>
+ {% if jokes|length > 0 %}
<div class="section center">
<ul class="pagination">
<li{% if currentpage==0 %} class="disabled"{% endif %}><a href="/page/{{ currentpage-1 }}{% if query %}?{{ query }}{% endif %}"><i class="material-icons">chevron_left</i></a></li>
@@ -119,6 +131,7 @@
</ul>
</div>
<div class="divider"></div>
+ {% endif %}
<div class="section">
<form action="/submit" method="post">
<div class="row">