-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
310 lines (235 loc) · 8.79 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#----------------------------------------
# imports
#----------------------------------------
import datetime
import json
import functools
import os
import time
from flask import Flask, redirect, render_template, request, Response, url_for
from lib.helper import loadGuestList, mailRSVP, minify, todatetime, tolocaldt, nowAsSeconds, saveRSVP, rsvpAttendingText, getGuestStats, mailInvite, saveInvite, storeInvite
#----------------------------------------
# initialization
#----------------------------------------
app = Flask(__name__)
#----------------------------------------
# constants
#----------------------------------------
# guest list path
TZ = 'US/Pacific'
HERE = os.path.dirname(os.path.realpath(__file__))
LIST_PATH = os.path.join(HERE, 'config', 'list.json')
GUESTS_CONFIG = loadGuestList(LIST_PATH)
# May 5, 2016
WEDDING_PROPOSE_DT = tolocaldt(todatetime(5, 1, 2016, tz=TZ))
# June 3, 2017
WEDDING_DAY_DT = tolocaldt(todatetime(6, 3, 2017, hour=10, minutes=0, seconds=0, tz=TZ))
# Mail
MAIL_FROM = 'no-reply@sallyandmichael.com'
MAIL_TO = 'me@smaili.org'
WEDDING_RSVP_MAIL_SUBJECT = 'Wedding RSVP'
BABY_SHOWER_RSVP_MAIL_SUBJECT = 'Baby Shower RSVP'
WEDDING_INVITE_MAIL_SUBJECT = 'Subject'
BABY_SHOWER_INVITE_MAIL_SUBJECT = 'Sally Smaili\'s Baby Shower (Women Only)'
# RSVP
MAX_GUESTS = 5
# May 20, 2017
RSVP_BY_DT = tolocaldt(todatetime(5, 15, 2017, tz=TZ))
CONTACT_PHONE = '(408) 605-4636'
# Baby Shower
BABY_SHOWER_MAX_GUESTS = 5
# November 5, 2019
BABY_SHOWER_RSVP_BY_DT = tolocaldt(todatetime(11, 1, 2019, tz=TZ))
BABY_SHOWER_DAY_DT = tolocaldt(todatetime(11, 16, 2019, hour=10, minutes=0, seconds=0, tz=TZ))
BABY_SHOWER_CONTACT_PHONE = '(408) 605-4636'
# guest list path
BABY_SHOWER_LIST_PATH = os.path.join(HERE, 'config', 'list-babyshower.json')
BABY_SHOWER_GUESTS_CONFIG = loadGuestList(BABY_SHOWER_LIST_PATH)
#----------------------------------------
# helpers
#----------------------------------------
def check_auth(username, password, guest_config):
guests_login = guest_config['login']
return username == guests_login['username'] and password == guests_login['password']
def authenticate():
return Response(
'Please login', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
def requires_auth(guest_config):
def requires_auth_inner(f):
@functools.wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password, guest_config):
return authenticate()
return f(*args, **kwargs)
return decorated
return requires_auth_inner
#----------------------------------------
# routes
#----------------------------------------
@app.route('/')
def home():
weddingtimes = {
'start': int(time.mktime(WEDDING_PROPOSE_DT.timetuple())),
'end': int(time.mktime(WEDDING_DAY_DT.timetuple())),
'now': nowAsSeconds(tz=TZ),
}
return minify(render_template('layouts/default.pyhtml', page='home', weddingtimes=weddingtimes))
@app.route('/couple')
def couple():
return minify(render_template('layouts/default.pyhtml', page='couple'))
@app.route('/story')
def story():
return minify(render_template('layouts/default.pyhtml', page='story'))
@app.route('/wedding')
def wedding():
return minify(render_template('layouts/default.pyhtml', page='wedding'))
@app.route('/gifts')
def gifts():
return minify(render_template('layouts/default.pyhtml', page='gifts'))
@app.route('/rsvp', methods=['GET', 'POST'])
def rsvp():
targs = {
'fname': '',
'lname': '',
'phone': '',
'attending': '',
'errors': {},
'success': False,
'MAX_GUESTS': MAX_GUESTS,
'CONTACT_PHONE': CONTACT_PHONE,
'rsvpAttendingText': rsvpAttendingText,
'rsvpByDate': '{d:%A}, {d:%B} {d.day}'.format(d=RSVP_BY_DT),
'rsvpEnabled': tolocaldt(tz=TZ) <= RSVP_BY_DT,
'rsvpEventDate': '{d:%A}, {d:%b} {d.day}'.format(d=WEDDING_DAY_DT),
}
if targs['rsvpEnabled'] and request.method == 'POST':
targs['fname'] = request.form['fname']
targs['lname'] = request.form['lname']
targs['phone'] = request.form['phone']
targs['attending'] = request.form['attending']
targs.update(saveRSVP(targs))
if targs['success']:
targs['mailfailed'] = mailRSVP(MAIL_FROM, MAIL_TO, WEDDING_RSVP_MAIL_SUBJECT, targs)
targs['page'] = 'rsvp'
targs['rsvpFormHref'] = url_for('rsvp')
return minify(render_template('layouts/default.pyhtml', **targs))
@app.route('/guests')
@requires_auth(GUESTS_CONFIG)
def guests():
targs = {}
targs['guests'] = loadGuestList(LIST_PATH)
targs['stats'] = getGuestStats(targs['guests'])
targs['guestTitle'] = 'Sally & Michael'
return minify(render_template('layouts/guests.pyhtml', **targs))
@app.route('/babyshower')
def babyshower():
return redirect(url_for('babyshower_rsvp'))
@app.route('/babyshower/rsvp', methods=['GET', 'POST'])
def babyshower_rsvp():
targs = {
'fname': '',
'lname': '',
'phone': '',
'attending': '',
'errors': {},
'success': False,
'MAX_GUESTS': BABY_SHOWER_MAX_GUESTS,
'CONTACT_PHONE': BABY_SHOWER_CONTACT_PHONE,
'rsvpAttendingText': rsvpAttendingText,
'rsvpByDate': '{d:%A}, {d:%b} {d.day}'.format(d=BABY_SHOWER_RSVP_BY_DT),
'rsvpEnabled': tolocaldt(tz=TZ) <= BABY_SHOWER_RSVP_BY_DT,
'rsvpEventDate': '{d:%A}, {d:%b} {d.day}'.format(d=BABY_SHOWER_DAY_DT),
}
if targs['rsvpEnabled'] and request.method == 'POST':
targs['fname'] = request.form['fname']
targs['lname'] = request.form['lname']
targs['phone'] = request.form['phone']
targs['attending'] = request.form['attending']
targs.update(saveRSVP(targs))
if targs['success']:
targs['mailfailed'] = mailRSVP(MAIL_FROM, MAIL_TO, BABY_SHOWER_RSVP_MAIL_SUBJECT, targs)
targs['page'] = 'babyshower'
targs['section'] = 'rsvp'
targs['rsvpFormHref'] = url_for('babyshower_rsvp')
return minify(render_template('layouts/default.pyhtml', **targs))
@app.route('/babyshower/whenwhere')
def babyshower_whenwhere():
targs = {
'eventDate': '{d:%A}, {d:%b} {d.day}'.format(d=BABY_SHOWER_DAY_DT),
}
targs['page'] = 'babyshower'
targs['section'] = 'whenwhere'
return minify(render_template('layouts/default.pyhtml', **targs))
@app.route('/babyshower/registry')
def babyshower_registry():
targs = {}
targs['page'] = 'babyshower'
targs['section'] = 'registry'
return minify(render_template('layouts/default.pyhtml', **targs))
@app.route('/babyshower/guests', methods=['GET', 'POST'])
@requires_auth(BABY_SHOWER_GUESTS_CONFIG)
def babyshower_guests():
targs = {}
inviteargs = {
'date': '{d:%A}, {d:%b} {d.day}'.format(d=BABY_SHOWER_DAY_DT),
'website': 'http://www.sallyandmichael.com/babyshower',
}
targs['invitations'] = {
'name': '',
'email': '',
'cc': '',
'bcc': '',
'subject': BABY_SHOWER_INVITE_MAIL_SUBJECT,
'message': render_template('email/babyshower-invite.pyhtml', **inviteargs),
}
targs['invitations']['inviteFormHref'] = url_for('babyshower_guests')
if request.method == 'POST':
targs['invitations']['name'] = request.form['name']
targs['invitations']['email'] = request.form['email']
targs['invitations']['cc'] = request.form['cc']
targs['invitations']['bcc'] = request.form['bcc']
targs['invitations']['subject'] = request.form['subject']
targs['invitations']['message'] = request.form['message']
targs.update(saveInvite(targs['invitations']))
if targs['success']:
targs['mailfailed'] = mailInvite(MAIL_FROM, targs['invitations']['email'], targs['invitations'])
targs['status'] = 'success' if targs['success'] and not targs['mailfailed'] else 'error'
if targs['status'] == 'success':
data = loadGuestList(BABY_SHOWER_LIST_PATH)
data['invitations'].append({
'name': targs['invitations']['name'],
'email': targs['invitations']['email'],
})
success = storeInvite(BABY_SHOWER_LIST_PATH, data)
if not success:
targs['status'] = 'error'
targs['savefailed'] = 'Invitation sent but unable to save changes to %s' % BABY_SHOWER_LIST_PATH
else:
targs['success'] = 'Invitation has been sent and changes saved.'
return json.dumps(targs)
targs['guests'] = loadGuestList(BABY_SHOWER_LIST_PATH)
targs['stats'] = getGuestStats(targs['guests'])
targs['guestTitle'] = 'Baby Shower'
return minify(render_template('layouts/guests.pyhtml', **targs))
@app.errorhandler(404)
def error_404(e):
return error(404)
@app.errorhandler(Exception)
def error_500(e=None):
print e
return error(500)
@app.route('/nginxerror.html')
def nginx_error():
code = int(request.args.get('c'))
return error(code)
def error(code):
return redirect('/')
#----------------------------------------
# launch
#----------------------------------------
if __name__ == '__main__':
app.config.update(DEBUG=True)
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)