2019-03-30 16:50:29 +00:00
|
|
|
import datetime
|
|
|
|
|
2019-02-10 13:00:35 +00:00
|
|
|
from flask import request, render_template
|
2019-03-19 14:19:11 +00:00
|
|
|
from sqlalchemy import func, and_, or_
|
2019-02-10 13:00:35 +00:00
|
|
|
|
2019-02-10 17:39:06 +00:00
|
|
|
from ogn_python import app
|
|
|
|
from ogn_python import db
|
2019-04-02 21:48:24 +00:00
|
|
|
from ogn_python import cache
|
2019-02-10 12:10:19 +00:00
|
|
|
|
2019-02-10 17:39:06 +00:00
|
|
|
from ogn_python.model import *
|
2019-02-10 12:10:19 +00:00
|
|
|
|
2019-02-10 13:00:35 +00:00
|
|
|
|
2019-04-02 21:48:24 +00:00
|
|
|
@cache.cached(key_prefix='countries_in_receivers')
|
|
|
|
def get_countries_in_receivers():
|
|
|
|
query = db.session.query(Country) \
|
|
|
|
.filter(Country.gid == Receiver.country_id) \
|
|
|
|
.order_by(Country.iso2) \
|
2019-04-03 08:00:46 +00:00
|
|
|
.distinct(Country.iso2)
|
2019-04-02 21:48:24 +00:00
|
|
|
|
|
|
|
return [country for country in query.all()]
|
|
|
|
|
|
|
|
|
|
|
|
@cache.cached(key_prefix='countries_in_logbook')
|
|
|
|
def get_countries_in_logbook():
|
|
|
|
query = db.session.query(Country) \
|
|
|
|
.filter(Country.iso2 == Airport.country_code) \
|
|
|
|
.order_by(Country.iso2) \
|
2019-04-03 08:00:46 +00:00
|
|
|
.distinct(Country.iso2)
|
2019-04-02 21:48:24 +00:00
|
|
|
|
|
|
|
return [country for country in query.all()]
|
|
|
|
|
|
|
|
|
2019-04-03 08:00:46 +00:00
|
|
|
@cache.memoize()
|
2019-04-02 21:48:24 +00:00
|
|
|
def get_airports_in_country(sel_country):
|
|
|
|
query = db.session.query(Airport) \
|
|
|
|
.filter(Airport.country_code == sel_country) \
|
|
|
|
.filter(Logbook.takeoff_airport_id == Airport.id) \
|
|
|
|
.order_by(Airport.name) \
|
2019-04-03 08:00:46 +00:00
|
|
|
.distinct(Airport.name)
|
2019-04-02 21:48:24 +00:00
|
|
|
|
|
|
|
return [airport for airport in query.all()]
|
|
|
|
|
|
|
|
|
2019-04-03 08:00:46 +00:00
|
|
|
@cache.memoize()
|
2019-04-02 21:48:24 +00:00
|
|
|
def get_dates_for_airport(sel_airport):
|
|
|
|
query = db.session.query(func.date(Logbook.reftime), func.count(Logbook.id).label('logbook_count')) \
|
|
|
|
.filter(Airport.id == sel_airport) \
|
|
|
|
.filter(or_(Airport.id == Logbook.takeoff_airport_id, Airport.id == Logbook.landing_airport_id)) \
|
|
|
|
.group_by(func.date(Logbook.reftime)) \
|
|
|
|
.order_by(func.date(Logbook.reftime).desc())
|
|
|
|
|
|
|
|
return [{'date': date, 'logbook_count': logbook_count} for (date, logbook_count) in query.all()]
|
|
|
|
|
|
|
|
|
2019-02-10 12:10:19 +00:00
|
|
|
@app.route('/')
|
2019-03-23 11:25:21 +00:00
|
|
|
@app.route('/index.html')
|
2019-02-10 12:10:19 +00:00
|
|
|
def index():
|
2019-02-10 13:00:35 +00:00
|
|
|
return render_template('base.html')
|
|
|
|
|
|
|
|
|
2019-03-23 11:25:21 +00:00
|
|
|
@app.route('/devices.html', methods=['GET', 'POST'])
|
2019-02-10 13:00:35 +00:00
|
|
|
def devices():
|
2019-03-23 11:25:21 +00:00
|
|
|
devices = db.session.query(Device) \
|
2019-04-02 21:48:24 +00:00
|
|
|
.order_by(Device.address) \
|
2019-03-23 11:25:21 +00:00
|
|
|
.limit(100)
|
|
|
|
return render_template('devices.html', devices=devices)
|
|
|
|
|
|
|
|
|
2019-03-23 12:57:44 +00:00
|
|
|
@app.route('/device_detail.html', methods=['GET', 'POST'])
|
|
|
|
def device_detail():
|
2019-02-10 13:00:35 +00:00
|
|
|
device_id = request.args.get('id')
|
2019-03-23 11:25:21 +00:00
|
|
|
device = db.session.query(Device) \
|
|
|
|
.filter(Device.id == device_id) \
|
|
|
|
.one()
|
2019-02-10 13:00:35 +00:00
|
|
|
|
2019-03-23 11:25:21 +00:00
|
|
|
return render_template('device_detail.html',
|
|
|
|
title='Device',
|
|
|
|
device=device)
|
2019-02-10 13:00:35 +00:00
|
|
|
|
|
|
|
|
2019-03-23 11:25:21 +00:00
|
|
|
@app.route('/receivers.html')
|
2019-02-10 13:00:35 +00:00
|
|
|
def receivers():
|
2019-03-19 14:19:11 +00:00
|
|
|
sel_country = request.args.get('country')
|
|
|
|
|
2019-04-02 21:48:24 +00:00
|
|
|
countries = get_countries_in_receivers()
|
2019-03-19 14:19:11 +00:00
|
|
|
|
|
|
|
# Get receiver selection list
|
|
|
|
if sel_country:
|
|
|
|
receivers = db.session.query(Receiver) \
|
|
|
|
.filter(and_(Receiver.country_id == Country.gid, Country.iso2 == sel_country)) \
|
|
|
|
.order_by(Receiver.name)
|
|
|
|
else:
|
|
|
|
receivers = db.session.query(Receiver) \
|
|
|
|
.order_by(Receiver.name)
|
|
|
|
|
2019-03-23 11:25:21 +00:00
|
|
|
return render_template('receivers.html',
|
|
|
|
title='Receivers',
|
|
|
|
sel_country=sel_country,
|
2019-04-02 21:48:24 +00:00
|
|
|
countries=countries,
|
2019-03-23 11:25:21 +00:00
|
|
|
receivers=receivers)
|
2019-02-10 13:00:35 +00:00
|
|
|
|
|
|
|
|
2019-03-23 11:25:21 +00:00
|
|
|
@app.route('/receiver_detail.html')
|
|
|
|
def receiver_detail():
|
2019-03-19 14:19:11 +00:00
|
|
|
sel_receiver_id = request.args.get('receiver_id')
|
|
|
|
|
|
|
|
receiver = db.session.query(Receiver) \
|
2019-03-23 11:25:21 +00:00
|
|
|
.filter(Receiver.id == sel_receiver_id) \
|
|
|
|
.one()
|
2019-03-19 14:19:11 +00:00
|
|
|
|
2019-03-23 19:46:27 +00:00
|
|
|
airport = db.session.query(Airport) \
|
|
|
|
.filter(and_(Receiver.id == sel_receiver_id,
|
|
|
|
func.st_contains(func.st_buffer(Receiver.location_wkt, 0.5), Airport.location_wkt),
|
|
|
|
func.st_distance_sphere(Airport.location_wkt, Receiver.location_wkt) < 1000)) \
|
2019-03-19 14:19:11 +00:00
|
|
|
.filter(Airport.style.in_((2,4,5))) \
|
|
|
|
|
2019-03-23 11:25:21 +00:00
|
|
|
return render_template('receiver_detail.html',
|
|
|
|
title='Receiver Detail',
|
|
|
|
receiver=receiver,
|
2019-03-23 19:46:27 +00:00
|
|
|
airport=airport.first())
|
2019-03-19 14:19:11 +00:00
|
|
|
|
|
|
|
|
2019-03-23 11:25:21 +00:00
|
|
|
@app.route('/airports.html', methods=['GET', 'POST'])
|
2019-02-10 13:00:35 +00:00
|
|
|
def airports():
|
2019-03-19 14:19:11 +00:00
|
|
|
sel_country = request.args.get('country')
|
|
|
|
|
2019-04-02 21:48:24 +00:00
|
|
|
countries = get_countries_in_logbook()
|
2019-03-19 14:19:11 +00:00
|
|
|
|
|
|
|
if sel_country:
|
2019-04-02 21:48:24 +00:00
|
|
|
airports = db.session.query(Airport) \
|
|
|
|
.filter(Airport.country_code == sel_country) \
|
2019-03-19 14:19:11 +00:00
|
|
|
.order_by(Airport.name)
|
|
|
|
else:
|
2019-03-23 19:46:27 +00:00
|
|
|
airports = []
|
2019-03-19 14:19:11 +00:00
|
|
|
|
2019-02-10 13:00:35 +00:00
|
|
|
page = request.args.get('page', 1, type=int)
|
|
|
|
|
2019-03-23 19:46:27 +00:00
|
|
|
return render_template('airports.html',
|
|
|
|
sel_country=sel_country,
|
2019-04-02 21:48:24 +00:00
|
|
|
countries=countries,
|
2019-03-23 19:46:27 +00:00
|
|
|
airports=airports)
|
2019-02-10 13:00:35 +00:00
|
|
|
|
2019-03-23 11:25:21 +00:00
|
|
|
|
2019-03-23 19:46:27 +00:00
|
|
|
@app.route('/airport_detail.html')
|
|
|
|
def airport_detail():
|
|
|
|
sel_airport = request.args.get('airport')
|
|
|
|
|
|
|
|
airport = db.session.query(Airport) \
|
|
|
|
.filter(Airport.id == sel_airport)
|
|
|
|
|
|
|
|
devices = db.session.query(Device).join(Logbook) \
|
|
|
|
.filter(Logbook.takeoff_airport_id == sel_airport) \
|
|
|
|
.order_by(Device.address)
|
|
|
|
|
|
|
|
return render_template('airport_detail.html',
|
|
|
|
title='Airport Detail',
|
|
|
|
airport=airport.one(),
|
|
|
|
devices=devices)
|
2019-02-10 13:00:35 +00:00
|
|
|
|
2019-03-23 11:25:21 +00:00
|
|
|
|
|
|
|
@app.route('/logbook.html', methods=['GET', 'POST'])
|
2019-02-10 13:00:35 +00:00
|
|
|
def logbook():
|
|
|
|
sel_country = request.args.get('country')
|
|
|
|
sel_airport = request.args.get('airport')
|
|
|
|
sel_date = request.args.get('date')
|
|
|
|
|
|
|
|
sel_device_id = request.args.get('device_id')
|
|
|
|
|
2019-04-02 21:48:24 +00:00
|
|
|
countries = get_countries_in_logbook()
|
2019-02-10 13:00:35 +00:00
|
|
|
|
|
|
|
if sel_country:
|
2019-04-02 21:48:24 +00:00
|
|
|
airports = get_airports_in_country(sel_country)
|
2019-02-10 13:00:35 +00:00
|
|
|
else:
|
2019-04-02 21:48:24 +00:00
|
|
|
airports = []
|
|
|
|
|
|
|
|
if sel_airport:
|
|
|
|
sel_airport = int(sel_airport)
|
|
|
|
if sel_airport not in [airport.id for airport in airports]:
|
|
|
|
sel_airport = None
|
|
|
|
sel_date = None
|
|
|
|
dates = get_dates_for_airport(sel_airport)
|
2019-02-10 13:00:35 +00:00
|
|
|
else:
|
2019-04-02 21:48:24 +00:00
|
|
|
dates = []
|
|
|
|
|
|
|
|
if sel_date:
|
|
|
|
sel_date = datetime.datetime.strptime(sel_date, '%Y-%m-%d').date()
|
|
|
|
if sel_date not in [entry['date'] for entry in dates]:
|
|
|
|
sel_date = dates[0]['date']
|
|
|
|
elif len(dates) > 0:
|
|
|
|
sel_date = dates[0]['date']
|
2019-02-10 13:00:35 +00:00
|
|
|
|
|
|
|
# Get Logbook
|
|
|
|
filters = []
|
2019-04-02 21:48:24 +00:00
|
|
|
if sel_airport:
|
2019-02-10 13:00:35 +00:00
|
|
|
filters.append(db.or_(Logbook.takeoff_airport_id == sel_airport, Logbook.landing_airport_id == sel_airport))
|
|
|
|
|
2019-03-23 17:08:00 +00:00
|
|
|
if sel_date:
|
|
|
|
filters.append(db.func.date(Logbook.reftime) == sel_date)
|
|
|
|
|
2019-02-10 13:00:35 +00:00
|
|
|
if sel_device_id:
|
|
|
|
filters.append(Logbook.device_id == sel_device_id)
|
|
|
|
|
|
|
|
if len(filters) > 0:
|
2019-03-23 12:57:44 +00:00
|
|
|
logbook = db.session.query(Logbook) \
|
2019-02-10 13:00:35 +00:00
|
|
|
.filter(*filters) \
|
|
|
|
.order_by(Logbook.reftime)
|
|
|
|
else:
|
2019-03-23 12:57:44 +00:00
|
|
|
logbook = None
|
2019-03-23 11:25:21 +00:00
|
|
|
|
|
|
|
return render_template('logbook.html',
|
|
|
|
title='Logbook',
|
|
|
|
sel_country=sel_country,
|
2019-04-02 21:48:24 +00:00
|
|
|
countries=countries,
|
2019-03-23 11:25:21 +00:00
|
|
|
sel_airport=sel_airport,
|
|
|
|
airports=airports,
|
|
|
|
sel_date=sel_date,
|
|
|
|
dates=dates,
|
2019-03-23 12:57:44 +00:00
|
|
|
logbook=logbook)
|
2019-03-23 11:25:21 +00:00
|
|
|
|
2019-03-23 19:46:27 +00:00
|
|
|
|
2019-03-23 11:25:21 +00:00
|
|
|
@app.route('/statistics.html')
|
|
|
|
def statistics():
|
2019-03-30 16:50:29 +00:00
|
|
|
|
|
|
|
today = datetime.date.today()
|
|
|
|
today = datetime.date(2018, 7, 31)
|
|
|
|
|
2019-03-23 11:25:21 +00:00
|
|
|
receiverstats = db.session.query(ReceiverStats) \
|
2019-03-30 16:50:29 +00:00
|
|
|
.filter(ReceiverStats.date == today)
|
2019-02-10 13:00:35 +00:00
|
|
|
|
2019-03-30 16:50:29 +00:00
|
|
|
return render_template('statistics.html',
|
|
|
|
title='Receiver Statistics',
|
|
|
|
receiverstats=receiverstats)
|
2019-02-10 13:00:35 +00:00
|
|
|
|
2019-03-23 11:25:21 +00:00
|
|
|
# Backend routes for other sites
|
2019-02-10 13:00:35 +00:00
|
|
|
|
2019-03-23 19:46:27 +00:00
|
|
|
|
2019-03-23 11:25:21 +00:00
|
|
|
@app.route('/live.html')
|
2019-02-10 13:00:35 +00:00
|
|
|
def live():
|
2019-03-23 11:25:21 +00:00
|
|
|
return render_template('ogn_live.jinja')
|