-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
234 lines (185 loc) · 7.02 KB
/
main.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import csv
import io
import logging
import os
import markdown2
import googlemaps
from geojson import Point, dumps, FeatureCollection, Feature
from flask import (
Flask,
request,
redirect,
render_template,
render_template_string,
url_for,
)
import firebase_admin
from firebase_admin import firestore
app = Flask(__name__)
fb_app = firebase_admin.initialize_app()
db = firestore.client()
googlemaps_config = db.collection('configs').document('googlemaps').get().to_dict()
gmaps = googlemaps.Client(key=googlemaps_config.get('api_key'))
_COMMITTEE_CHAIR = "Committee Chair"
_COR = "Chartered Organization Rep."
_UNIT_LEADER = frozenset(
["Cubmaster", "Scoutmaster", "Skipper", "Venturing Crew Advisor"]
)
_COMMITTEE_CHAIR_KEY = "cc"
_COR_KEY = "cor"
_UNIT_LEADER_KEY = "leader"
@app.route("/")
def root():
return render_template("main.html", googlemaps_api_key=googlemaps_config.get('api_key'))
def UnitNameToUnitType(name):
return name.split(" ")[0].upper()
def UnitNameToUnitLetter(name):
return UnitNameToUnitType(name)[0]
def UnitNameToNumber(name):
num = name.split(" ")[1]
return str(int(num))
@app.route("/upload_key3", methods=["POST"])
def upload_key3():
logging.warning("%s", request.files)
if "file" not in request.files:
return "No file part"
fp = request.files["file"].stream
# Skip the first 8 header lines in this report.
for _ in range(8):
fp.readline()
with io.StringIO(fp.read().decode()) as f:
for row in csv.DictReader(f):
unit_type = UnitNameToUnitLetter(row["Unit_Name"])
unit_num = UnitNameToNumber(row["Unit_Name"])
position = row["Position"]
key = f"{unit_type} {unit_num}"
leader = dict(name=row["Name_"], email=row["Email"])
unit_entry = dict(
key=key,
unit_type=UnitNameToUnitType(row["Unit_Name"]),
unit_num=unit_num,
)
if position == _COR:
unit_entry[_COR_KEY] = leader
elif position == _COMMITTEE_CHAIR:
unit_entry[_COMMITTEE_CHAIR_KEY] = leader
elif position in _UNIT_LEADER:
unit_entry[_UNIT_LEADER_KEY] = leader
else:
logging.error("%s is an unknown leader type", position)
logging.warning("Storing unit %s", key)
db.collection("units").document(key).set(unit_entry, merge=True)
return redirect("/")
@app.route("/upload_pin", methods=["POST"])
def upload_pin():
logging.warning("%s", request.files)
if "file" not in request.files:
return "No file part"
fp = request.files["file"].stream
with io.StringIO(fp.read().decode()) as f:
for row in csv.DictReader(f):
full_unit_name = row["Unit_Name"]
unit_name = full_unit_name.split(", ")[0]
unit_type = UnitNameToUnitLetter(unit_name)
unit_num = UnitNameToNumber(unit_name)
key = f"{unit_type} {unit_num}"
address_line = row["Unit_BeAScout_Address"]
city = row["City"]
state = row["State"]
zipcode = row["ZIPCODE"]
last_modified_date = row["Last_Modified_Date"]
# Geocoding an address
geocode_result = gmaps.geocode(f'{address_line}, {city}, {state}')[0]
location = geocode_result.get('geometry').get('location')
unit_entry = dict(
key=key,
unit_type=UnitNameToUnitType(row["Unit_Name"]),
unit_num=unit_num,
pin_info=dict(
address_line=address_line,
city=city,
state=state,
zipcode=zipcode,
latitude=location.get('lat'),
longitude=location.get('lng'),
last_modified_date=last_modified_date,
),
website=row["Unit_Website"],
)
db.collection("units").document(key).set(unit_entry, merge=True)
logging.error(unit_name)
return redirect("/")
def get_contacts_from_unit(unit):
return [
contact
for contact in [unit.get("leader"), unit.get("cor"), unit.get("cc")]
if contact is not None
]
@app.route("/units")
def list_units():
units = [u.to_dict() for u in db.collection("units").stream()]
all_emails = []
for unit in units:
all_emails.extend([c.get("email") for c in get_contacts_from_unit(unit)])
return render_template("units.html", units=units, all_emails=all_emails)
@app.route("/units/<unit_key>")
def list_single_unit(unit_key: str):
logging.warning(unit_key)
unit = db.collection("units").document(unit_key).get().to_dict()
return render_template(
"single_unit.html",
unit=unit,
emails=[c.get("email") for c in get_contacts_from_unit(unit)],
)
@app.route("/send_email", methods=["POST", "GET"])
def send_email():
unit_type = request.form.get("unit_type", request.args.get("unit_type", "PACK"))
code = request.form.get("msg", "")
subject = request.form.get("subject", "")
units = [
u.to_dict()
for u in db.collection("units").where("unit_type", "==", unit_type).stream()
]
markdown = markdown2.markdown(render_template_string(code, unit=units[0]))
if request.method == "GET" or request.form.get("preview"):
return render_template(
"send_email.html",
markdown=markdown,
code=code,
unit_type=unit_type,
subject=subject,
units=units,
)
elif request.form.get("send"):
send_to_units(code=code, subject=subject, units=units)
redirect(url_for("root"))
else:
send_to_units(code=code, subject=subject, units=units, send_test=True)
return render_template(
"send_email.html",
markdown=markdown,
code=code,
unit_type=unit_type,
subject=subject,
units=units,
)
def _unit_to_geo_feature(unit):
pin_info=unit.get('pin_info')
return Feature(geometry=Point((pin_info.get('longitude'), pin_info.get('latitude'))),
properties=dict(name=unit.get('key'),
address_line=f'{pin_info["address_line"]}',
city=f'{pin_info["city"]}',
state=f'{pin_info["state"]}',
zip=f'{pin_info["zipcode"]}',
))
@app.route("/unit_geojson")
def unit_geojson():
units = [_unit_to_geo_feature(u.to_dict()) for u in db.collection("units").stream()]
return dumps(FeatureCollection(units))
def send_to_units(code, subject, units, send_test=False):
for unit in units:
db.collection("mailqueue").add(
dict(subject=subject, code=code, unit=unit, send_test=send_test)
)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))