PyTaxonomies/website/__init__.py

72 lines
1.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask, request, render_template
from flask_bootstrap import Bootstrap
from flask_nav import Nav
from flask_nav.elements import Navbar, View
2016-10-12 16:35:54 +02:00
from flask_wtf import FlaskForm
2016-10-13 17:23:10 +02:00
from wtforms import StringField, SubmitField
2016-10-12 16:35:54 +02:00
from wtforms.validators import DataRequired
from pytaxonomies import Taxonomies
nav = Nav()
2016-10-12 14:22:19 +02:00
@nav.navigation()
def mynavbar():
return Navbar(
'MISP taxonomies viewer and editor',
2018-05-24 07:35:11 +02:00
View('Taxonomies', 'taxonomies', name=None),
View('Search', 'search')
)
2018-05-24 07:35:11 +02:00
app = Flask(__name__)
2016-10-12 16:35:54 +02:00
app.secret_key = '<changeme>'
Bootstrap(app)
app.config['BOOTSTRAP_SERVE_LOCAL'] = True
app.debug = True
nav.init_app(app)
#t = Taxonomies(manifest_path="../../misp-taxonomies/MANIFEST.json")
t = Taxonomies()
2016-10-12 16:35:54 +02:00
class SearchForm(FlaskForm):
query = StringField('Query', validators=[DataRequired()])
2016-10-13 17:23:10 +02:00
submit = SubmitField('Search')
2016-10-12 16:35:54 +02:00
@app.route('/', methods=['GET'])
def index():
return taxonomies()
2016-10-12 14:22:19 +02:00
@app.route('/taxonomies/', defaults={'name': None})
@app.route('/taxonomies/<name>', methods=['GET'])
def taxonomies(name=None):
if name and t.get(name):
return render_template('taxonomy.html', taxonomy=t.get(name))
else:
return render_template('taxonomies.html', all_taxonomies=t)
2018-05-24 07:35:11 +02:00
@app.route('/search', methods=['GET', 'POST'])
def search():
2016-10-12 16:35:54 +02:00
form = SearchForm()
if form.validate_on_submit():
q = request.form.get('query')
entries = t.search(q)
if entries:
to_display = {e: t.revert_machinetag(e) for e in entries}
2016-10-12 16:35:54 +02:00
return render_template('search.html', form=form, entries=to_display)
else:
2016-10-12 16:35:54 +02:00
return render_template('search.html', form=form, entries=None)
return render_template('search.html', form=form, entries=None)
def main():
app.run()