new: Country selector

pull/12/head
Raphaël Vinot 2018-07-30 14:40:42 +02:00
parent 8ccaae8194
commit c278f83517
3 changed files with 30 additions and 13 deletions

View File

@ -1,3 +1,6 @@
# Web thing
flask
flask-bootstrap
# Other
pycountry

View File

@ -3,11 +3,12 @@
import json
from flask import Flask, render_template, request, session
from flask import Flask, render_template, request, session, Response
from flask_bootstrap import Bootstrap
from bgpranking.querying import Querying
from datetime import date, timedelta
import pycountry
app = Flask(__name__)
@ -40,8 +41,10 @@ def load_session():
session['source'] = d['source']
if 'asn' in d:
session['asn'] = d['asn']
if 'country' in d:
session.pop('country', None)
elif 'country' in d:
session['country'] = d['country']
session.pop('asn', None)
set_default_date_session()
@ -50,6 +53,11 @@ def set_default_date_session():
session['date'] = (date.today() - timedelta(days=1)).isoformat()
def get_country_codes():
for c in pycountry.countries:
yield c.alpha_2, c.name
@app.route('/', methods=['GET', 'POST'])
def index():
load_session()
@ -60,13 +68,12 @@ def index():
ranks = q.asns_global_ranking(limit=100, **session)
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)
return render_template('index.html', ranks=r, sources=sources, countries=get_country_codes(), **session)
@app.route('/asn', methods=['GET', 'POST'])
def asn_details():
load_session()
session.pop('country', None)
q = Querying()
asn_descriptions = q.get_asn_descriptions(asn=session['asn'], all_descriptions=True)
sources = q.get_sources(date=session['date'])
@ -84,16 +91,15 @@ def asn_details():
@app.route('/asn_history', methods=['GET', 'POST'])
def asn_history():
load_session()
session.pop('country', None)
q = Querying()
return json.dumps(q.get_asn_history(**session))
return Response(json.dumps(q.get_asn_history(**session)), mimetype='application/json')
@app.route('/country_history', methods=['GET', 'POST'])
def country_history():
load_session()
q = Querying()
return json.dumps(q.country_history(**session))
return Response(json.dumps(q.country_history(**session)), mimetype='application/json')
@app.route('/country', methods=['GET', 'POST'])
@ -101,5 +107,4 @@ def country():
load_session()
q = Querying()
sources = q.get_sources(date=session['date'])
session.pop('asn', None)
return render_template('country.html', sources=sources, **session)
return render_template('country.html', sources=sources, countries=get_country_codes(), **session)

View File

@ -1,16 +1,16 @@
<p>
<form style="width:300px; display:inline-block;" action="" method=post>
<form style="width:250px; display:inline-block;" action="" method=post>
<input name="date" type="date" value="{{ date }}">
<input type="submit" value="Set date">
</form>
<form style="width:300px; display:inline-block;" action="" method=post>
<form style="width:250px; display:inline-block;" action="" method=post>
<select name="ipversion">
<option value="v4" {% if ipversion == 'v4' %} selected="selected"{% endif %}>v4</option>
<option value="v6" {% if ipversion == 'v6' %} selected="selected"{% endif %}>v6</option>
</select>
<input type="submit" value="Set IP version">
</form>
<form style="width:500px; display:inline-block;" action="" method=post>
<form style="width:300px; display:inline-block;" action="" method=post>
<select name="source">
<option value="" {% if not source %} selected="selected"{% endif %}>all</option>
{% for s in sources %}
@ -19,9 +19,18 @@
</select>
<input type="submit" value="Set source">
</form>
<form style="width:500px; display:inline-block;" action="{{ url_for('asn_details') }}" method=post>
<form style="width:400px; display:inline-block;" action="{{ url_for('asn_details') }}" method=post>
ASN to search: <input type=number name=asn>
<input type="submit" value="Search">
</form>
<form style="width:500px; display:inline-block;" action="{{ url_for('country') }}" method=post>
<select name="country">
<option value="" {% if not country %} selected="selected"{% endif %}>all</option>
{% for cc, name in countries %}
<option value="{{ cc }}" {% if country == cc %} selected="selected"{% endif %}>{{ name }}</option>
{% endfor %}
</select>
<input type="submit" value="Set country">
</form>
<p>
<br/>