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