Feature: add search route with pagination and template
This commit is contained in:
parent
29151553cc
commit
5326efcdf7
3 changed files with 372 additions and 283 deletions
|
@ -38,9 +38,9 @@
|
|||
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
<li>
|
||||
<form action="/search" method="post" class="navbar-form navbar-left" role="search">
|
||||
<form onSubmit="location.href='/search/' + this.search.value;return false" class="navbar-form navbar-left" role="search">
|
||||
<div class="form-group">
|
||||
<input type="search" class="form-control" placeholder="Search">
|
||||
<input id="search" type="search" class="form-control" placeholder="Search">
|
||||
</div>
|
||||
</form>
|
||||
</li>
|
||||
|
|
53
smash/templates/search.html
Normal file
53
smash/templates/search.html
Normal file
|
@ -0,0 +1,53 @@
|
|||
{% extends "base.html" %}
|
||||
{% block content %}
|
||||
|
||||
|
||||
|
||||
{% if numpages>1 %}
|
||||
<ul class="pagination pagination-sm">
|
||||
{% for page in range(numpages) %}
|
||||
<li {% if curpage==page %} class="active" {% endif %} >
|
||||
<a {% if curpage!=page %}href="/{{page_type}}/{{search_query}}/{{page+1}}"{% endif %}>{{page+1}}</a>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
|
||||
{% if quotes %}
|
||||
{% for quote in quotes %}
|
||||
{% if quote.approved %}
|
||||
<div class="quote-header">
|
||||
<a class="quote-link" href="/quote/{{ quote.id }}">#{{ quote.id }}</a>
|
||||
<a class="rate-positive">+</a> ({{quote.rating}}) <a class="rate-negative">-</a>
|
||||
<div class="pull-right quote-date">{{ quote.time }}</div>
|
||||
|
||||
{% if session.authorized %}
|
||||
<form class="mod-form" action="/moderate" name="moderate" method="post">
|
||||
<input type="hidden" name="quoteid" value={{quote.id}} />
|
||||
<button type="submit" name="submit" class="btn btn-danger btn-sm btn-mod" value="Delete">Delete</button>
|
||||
</form>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
|
||||
<div class="quote">
|
||||
<p>{{ quote.content|safe }}</p>
|
||||
</div>
|
||||
|
||||
<div class="tags">
|
||||
|
||||
Tags:
|
||||
{% if quote.tags|length > 0 and quote.tags[0].name|length>0%}
|
||||
{% for tag in quote.tags %}
|
||||
<a href="/tag/{{tag.name}}" class="badge"> {{tag.name}} </a>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<strong> No tags </strong>
|
||||
{% endif %}
|
||||
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
|
@ -218,16 +218,52 @@ def tags():
|
|||
)
|
||||
|
||||
|
||||
@app.route('/search', methods=['POST'])
|
||||
def search():
|
||||
if request.method == 'POST':
|
||||
@app.route('/search/<query>')
|
||||
def search(query):
|
||||
quotes = Quote.query.filter_by(approved=True).filter(Quote.content.ilike('%{}%'.format(query))).order_by(Quote.id.desc()).all()
|
||||
|
||||
allquotes = len(quotes)
|
||||
quotes = quotes[:10]
|
||||
|
||||
if len(quotes)>0:
|
||||
# Replace line breaks with html breaks and escape special characters
|
||||
for quote in quotes:
|
||||
quote.content = str(Markup.escape(quote.content)).replace('\n', '</br>')
|
||||
|
||||
return render_template(
|
||||
"message.html",
|
||||
alertclass="alert-warning",
|
||||
message="Not implemented yet. "
|
||||
"search.html",
|
||||
title="Search for: {}".format(query),
|
||||
quotes=quotes,
|
||||
numpages=1 + allquotes//10,
|
||||
curpage=0,
|
||||
page_type="search",
|
||||
search_query=query
|
||||
)
|
||||
else:
|
||||
return 'Invalid request.'
|
||||
return message("alert-warning", "No quotes in the database.")
|
||||
|
||||
|
||||
@app.route('/search/<query>/<int:page>')
|
||||
def search_page(query, page):
|
||||
allquotes = len(Quote.query.filter_by(approved=True).\
|
||||
filter(Quote.content.ilike('%{}%'.format(query))).\
|
||||
order_by(Quote.id.desc()).all())
|
||||
quotes = Quote.query.filter_by(approved=True).\
|
||||
filter(Quote.content.ilike('%{}%'.format(query))).\
|
||||
order_by(Quote.id.desc()).all()[(page-1)*10:page*10]
|
||||
|
||||
for quote in quotes:
|
||||
quote.content = str(Markup.escape(quote.content)).replace('\n', '</br>')
|
||||
|
||||
return render_template(
|
||||
"search.html",
|
||||
title="Search for: {} - page {}".format(query, page),
|
||||
quotes=quotes,
|
||||
numpages=1 + allquotes//10,
|
||||
curpage=page-1,
|
||||
page_type="search",
|
||||
search_query=query
|
||||
)
|
||||
|
||||
|
||||
@app.route('/add', methods=['GET', 'POST'])
|
||||
|
|
Loading…
Add table
Reference in a new issue