ogn-python/ogn_python/routes.py

238 wiersze
7.1 KiB
Python
Czysty Zwykły widok Historia

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
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
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.iso2) \
2019-04-02 21:48:24 +00:00
.filter(Country.gid == Receiver.country_id) \
.order_by(Country.iso2) \
.distinct(Country.iso2)
2019-04-02 21:48:24 +00:00
return [{'iso2': country[0]} for country in query.all()]
2019-04-02 21:48:24 +00:00
@cache.cached(key_prefix='countries_in_logbook')
def get_countries_in_logbook():
query = db.session.query(Country.iso2) \
2019-04-02 21:48:24 +00:00
.filter(Country.iso2 == Airport.country_code) \
.order_by(Country.iso2) \
.distinct(Country.iso2)
2019-04-02 21:48:24 +00:00
return [{'iso2': country[0]} for country in query.all()]
2019-04-02 21:48:24 +00:00
@cache.memoize()
2019-04-02 21:48:24 +00:00
def get_airports_in_country(sel_country):
query = db.session.query(Airport.id, Airport.name) \
2019-04-02 21:48:24 +00:00
.filter(Airport.country_code == sel_country) \
.filter(Logbook.takeoff_airport_id == Airport.id) \
.order_by(Airport.name) \
.distinct(Airport.name)
2019-04-02 21:48:24 +00:00
return [{'id': airport[0], 'name': airport[1]} for airport in query.all()]
2019-04-02 21:48:24 +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-03 18:59:25 +00:00
airports = get_airports_in_country(sel_country)
2019-03-19 14:19:11 +00:00
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]:
2019-04-02 21:48:24 +00:00
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))
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():
return render_template('ogn_live.jinja')