1
|
|
|
from flask import Blueprint, jsonify |
2
|
|
|
import markdown |
3
|
|
|
from sqlalchemy import func |
4
|
|
|
|
5
|
|
|
from sugarloaf.models import db, TrailStatus, Trail, Area, Lift, LiftStatus, DailyReport |
6
|
|
|
|
7
|
|
|
api = Blueprint('api', __name__) |
8
|
|
|
|
9
|
|
|
@api.route('/current') |
10
|
|
|
def current(): |
11
|
|
|
"""Return a json with most recent trail and lift conditions and daily report""" |
12
|
|
|
latest = db.session.scalar(func.MAX(TrailStatus.dt)) |
13
|
|
|
|
14
|
|
|
trails = db.session.query(Trail.name, |
15
|
|
|
Trail.difficulty, |
16
|
|
|
Area.name.label('area'), |
17
|
|
|
TrailStatus.open, |
18
|
|
|
TrailStatus.groomed, |
19
|
|
|
TrailStatus.snowmaking, |
20
|
|
|
)\ |
21
|
|
|
.filter(TrailStatus.trail_id == Trail.id, |
22
|
|
|
Area.id == Trail.area_id, |
23
|
|
|
TrailStatus.dt == latest) |
24
|
|
|
lifts = db.session.query(Lift.name, |
25
|
|
|
LiftStatus.running, |
26
|
|
|
LiftStatus.scheduled, |
27
|
|
|
LiftStatus.hold)\ |
28
|
|
|
.filter(LiftStatus.lift_id == Lift.id, |
29
|
|
|
LiftStatus.dt == latest) |
30
|
|
|
latest_report = DailyReport.query.order_by(DailyReport.dt.desc()).first() |
31
|
|
|
|
32
|
|
|
return jsonify( |
33
|
|
|
{'trails': [{'name': trail.name, |
34
|
|
|
'difficulty': trail.difficulty, |
35
|
|
|
'area': trail.area, |
36
|
|
|
'groomed': trail.groomed, |
37
|
|
|
'snowmaking': trail.snowmaking, |
38
|
|
|
'open': trail.open} for trail in trails], |
39
|
|
|
'datetime': latest, |
40
|
|
|
'report': markdown.markdown(latest_report.report), |
41
|
|
|
'lifts': [{'name': lift.name, |
42
|
|
|
'running': lift.running, |
43
|
|
|
'scheduled': lift.scheduled, |
44
|
|
|
'hold': lift.hold} for lift in lifts] |
45
|
|
|
}) |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
@api.route('/summary') |
49
|
|
|
def summary(): |
50
|
|
|
""" Return json with a summary of the trail status by update """ |
51
|
|
|
conditions = db.session.query(TrailStatus.dt, |
52
|
|
|
TrailStatus.open, |
53
|
|
|
TrailStatus.groomed, |
54
|
|
|
TrailStatus.snowmaking, |
55
|
|
|
Trail.difficulty, |
56
|
|
|
Area.name, |
57
|
|
|
func.count())\ |
58
|
|
|
.filter(TrailStatus.trail_id == Trail.id, Trail.area_id == Area.id)\ |
59
|
|
|
.group_by(TrailStatus.dt, |
60
|
|
|
TrailStatus.open, |
61
|
|
|
TrailStatus.groomed, |
62
|
|
|
TrailStatus.snowmaking, |
63
|
|
|
Trail.difficulty, |
64
|
|
|
Area.name) |
65
|
|
|
|
66
|
|
|
output = {'conditions': [{ |
67
|
|
|
'datetime': condition.dt.isoformat(), |
68
|
|
|
'open': condition.open, |
69
|
|
|
'groomed': condition.groomed, |
70
|
|
|
'snowmaking': condition.snowmaking, |
71
|
|
|
'difficulty': condition.difficulty, |
72
|
|
|
'trail_count': condition[6], |
73
|
|
|
'area': condition.name |
74
|
|
|
} for condition in conditions]} |
75
|
|
|
|
76
|
|
|
print(output) |
77
|
|
|
return jsonify(output) |
78
|
|
|
|