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 # Web thing
flask flask
flask-bootstrap flask-bootstrap
# Other
pycountry

View File

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

View File

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