PyTaxonomies/website/__init__.py

72 lines
1.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask, request, render_template
2020-02-17 19:15:39 +01:00
from flask_bootstrap import Bootstrap # type: ignore
from flask_nav import Nav # type: ignore
from flask_nav.elements import Navbar, View # type: ignore
from flask_wtf import FlaskForm # type: ignore
from wtforms import StringField, SubmitField # type: ignore
from wtforms.validators import DataRequired # type: ignore
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)
2020-02-17 19:15:39 +01:00
# 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()