-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
98 lines (80 loc) · 3.1 KB
/
application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from datetime import datetime
from flask import Flask, render_template, send_from_directory
from flask_cors import CORS
from config import SITE_OFFLINE
from marshmallow import Schema, fields, validate
import re
from routes import routes, request
# Elastic Beanstalk wants `application` to be present.
application = app = Flask(__name__)
CORS(app)
app.register_blueprint(routes)
def get_service_categories():
"""
This is the new location for the service_categories on the main page.
This will function will be called on the default route and the list
will be passed to the index.html template.
"""
return [
("Climate Indicators", "/indicators"),
("Climate Protection from Spruce Beetles", "/beetles"),
("Degree Days", "/degree_days"),
("Digital Elevation Models (DEMs)", "/elevation"),
("Flammability and Vegetation Type (ALFRESCO)", "/alfresco"),
("Hydrology", "/hydrology"),
("Landfast Sea Ice", "/landfastice"),
("Permafrost", "/permafrost"),
("Physical and Administrative Boundary Polygons", "/boundary"),
("Physiography", "/physiography"),
("Sea Ice Concentration", "/seaice"),
("Snowfall Equivalent", "/snow"),
("Temperature and Precipitation", "/taspr"),
("Wet Days Per Year", "/wet_days_per_year"),
("Wildfire", "/fire"),
("Demographics", "/demographics"),
]
@app.context_processor
def inject_date():
"""
Inject date so it can be used in the footer easily.
"""
year = datetime.now().year
return dict(year=year)
@app.before_request
def validate_get_params():
class QueryParamsSchema(Schema):
format = fields.Str(validate=validate.OneOf(["csv"]), required=False)
summarize = fields.Str(validate=validate.OneOf(["mmm"]), required=False)
# Make sure "community" parameter is only uppercase letters and
# numbers, and less than or equal to 10 characters long.
community = fields.Str(
validate=lambda str: bool(re.match(r"^[A-Z0-9]{0,10}$", str)),
required=False,
)
# Make sure "vars" parameter is only letters and commas, and less than
# or equal to 100 characters long.
vars = fields.Str(
validate=lambda str: bool(re.match(r"^[A-Za-z,]{0,100}$", str))
and len(str) < 100,
required=False,
)
schema = QueryParamsSchema()
errors = schema.validate(request.args)
if errors:
return render_template("422/invalid_get_parameter.html"), 422
@app.after_request
def add_cache_control(response):
# Set cache control headers here
response.cache_control.max_age = 7776000
return response
@app.route("/")
def index():
"""Render index page"""
# Sort the service categories by category name
service_categories = sorted(get_service_categories(), key=lambda x: x[0])
return render_template(
"index.html", service_categories=service_categories, SITE_OFFLINE=SITE_OFFLINE
)
@app.route("/robots.txt")
def static_from_root():
return send_from_directory(app.static_folder, request.path[1:])