Improve search form

pull/2/head
Raphaël Vinot 2016-10-12 16:35:54 +02:00
parent 1668b87df8
commit aec809fac8
4 changed files with 23 additions and 12 deletions

View File

@ -1,3 +1,4 @@
flask flask
flask_bootstrap flask_bootstrap
flask_nav flask_nav
flask_wtf

View File

@ -5,6 +5,9 @@ from flask import Flask, request, render_template
from flask_bootstrap import Bootstrap from flask_bootstrap import Bootstrap
from flask_nav import Nav from flask_nav import Nav
from flask_nav.elements import Navbar, View from flask_nav.elements import Navbar, View
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import DataRequired
from pytaxonomies import Taxonomies from pytaxonomies import Taxonomies
@ -20,6 +23,7 @@ def mynavbar():
) )
app = Flask(__name__) app = Flask(__name__)
app.secret_key = '<changeme>'
Bootstrap(app) Bootstrap(app)
app.config['BOOTSTRAP_SERVE_LOCAL'] = True app.config['BOOTSTRAP_SERVE_LOCAL'] = True
app.debug = True app.debug = True
@ -28,6 +32,11 @@ nav.init_app(app)
#t = Taxonomies(manifest_path="../../misp-taxonomies/MANIFEST.json") #t = Taxonomies(manifest_path="../../misp-taxonomies/MANIFEST.json")
t = Taxonomies() t = Taxonomies()
class SearchForm(FlaskForm):
query = StringField('Query', validators=[DataRequired()])
@app.route('/', methods=['GET']) @app.route('/', methods=['GET'])
def index(): def index():
return taxonomies() return taxonomies()
@ -43,15 +52,16 @@ def taxonomies(name=None):
@app.route('/search', methods=['GET', 'POST']) @app.route('/search', methods=['GET', 'POST'])
def search(): def search():
if request.form.get('query'): form = SearchForm()
if form.validate_on_submit():
q = request.form.get('query') q = request.form.get('query')
entries = t.search(q) entries = t.search(q)
if entries: if entries:
to_display = {e: t.revert_machinetag(e) for e in entries} to_display = {e: t.revert_machinetag(e) for e in entries}
return render_template('search.html', query=q, entries=to_display) return render_template('search.html', form=form, entries=to_display)
else: else:
return render_template('search.html', query=q, entries=None) return render_template('search.html', form=form, entries=None)
return render_template('search.html', query=None, entries=None) return render_template('search.html', form=form, entries=None)
def main(): def main():

View File

@ -1,4 +1,5 @@
{% extends "bootstrap/base.html" %} {% extends "bootstrap/base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block scripts %} {% block scripts %}
{{ super() }} {{ super() }}

View File

@ -5,11 +5,10 @@
<div class="container"> <div class="container">
<h1>Search {% if query %} - {{query}} {%endif%}</h1> <h1>Search {% if query %} - {{query}} {%endif%}</h1>
<form class="form-inline" role="form" action="search" method=post> <form class="form-inline" action="search" method=post>
<div class="form-group"> {{ form.csrf_token }}
<input type="text" class="form-control" name="query" id=query placeholder="Search a thing "> {{ form.query(size=20) }}
</div> <input type="submit" value="Search">
<button type="submit" class="btn btn-default">Search</button>
</form> </form>
</br> </br>
<div> <div>
@ -39,12 +38,12 @@
<td> <td>
{% if val|length == 3 %} {% if val|length == 3 %}
{% if val[2].description %} {% if val[2].description %}
{{ val[2].description.decode() }} {{ val[2].description }}
{% elif val[2].expanded %} {% elif val[2].expanded %}
{{ val[2].expanded.decode() }} {{ val[2].expanded }}
{%endif%} {%endif%}
{% elif val[1].description %} {% elif val[1].description %}
{{ val[1].description.decode() }} {{ val[1].description }}
{% else %} {% else %}
{{ val[1].predicate }} {{ val[1].predicate }}
{%endif%} {%endif%}