BGP-Ranking/website/web/__init__.py

63 lines
1.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
2018-04-12 18:09:04 +02:00
from flask import Flask, render_template, request, session
from flask_bootstrap import Bootstrap
from bgpranking.querying import Querying
2018-04-12 18:09:04 +02:00
from pathlib import Path
from datetime import date, timedelta
app = Flask(__name__)
2018-04-12 18:09:04 +02:00
app.secret_key = '\xeb\xfd\x1b\xee\xed<\xa5~\xd5H\x85\x00\xa5r\xae\x80t5@\xa2&>\x03S'
Bootstrap(app)
app.config['BOOTSTRAP_SERVE_LOCAL'] = True
2018-04-12 18:09:04 +02:00
jquery_js = Path('static', 'jquery-ui.js')
jquery_css = Path('static', 'jquery-ui.css')
def load_session():
if request.method == 'POST':
d = request.form
elif request.method == 'GET':
d = request.args
2018-04-12 18:09:04 +02:00
if 'date' in d:
session['date'] = d['date']
if 'ipversion' in d:
session['ipversion'] = d['ipversion']
if 'source' in d:
session['source'] = d['source']
if 'asn' in d:
session['asn'] = d['asn']
set_default_date_session()
2018-04-12 18:09:04 +02:00
def set_default_date_session():
if 'date' not in session:
session['date'] = (date.today() - timedelta(days=1)).isoformat()
@app.route('/', methods=['GET', 'POST'])
def index():
2018-04-12 18:09:04 +02:00
load_session()
q = Querying()
2018-04-12 18:09:04 +02:00
sources = q.get_sources(date=session['date'])
session.pop('asn', None)
ranks = q.asns_global_ranking(limit=-1, **session)
2018-04-13 18:02:44 +02:00
descriptions = [q.get_asn_descriptions(int(asn)) for asn, rank in ranks]
r = zip(ranks, descriptions)
return render_template('index.html', ranks=r, sources=sources, **session)
2018-04-11 15:31:34 +02:00
2018-04-11 16:16:19 +02:00
@app.route('/asn', methods=['GET', 'POST'])
def asn_details():
2018-04-12 18:09:04 +02:00
load_session()
2018-04-11 15:31:34 +02:00
q = Querying()
2018-04-12 18:09:04 +02:00
ranks = q.asn_details(**session)
return render_template('asn.html', ranks=ranks, **session)