-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathja.py
More file actions
203 lines (145 loc) · 5.76 KB
/
ja.py
File metadata and controls
203 lines (145 loc) · 5.76 KB
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
import logging
import os
import urllib
from google.appengine.ext import ndb
import jinja2
import webapp2
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
DEFAULT_RSVP_NAME = 'default_rsvp'
def rsvp_key(rsvp_name=DEFAULT_RSVP_NAME):
"""Constructs a Datastore key for a Guestbook entity.
We use guestbook_name as the key.
"""
return ndb.Key('RSVPModel', rsvp_name)
class RSVPModel(ndb.Model):
"""Sub model for representing an author."""
date = ndb.DateTimeProperty(auto_now=True)
email = ndb.StringProperty(indexed=False)
firstName = ndb.StringProperty(indexed=True)
group = ndb.IntegerProperty(indexed=True)
lastName = ndb.StringProperty(indexed=True)
message = ndb.StringProperty(indexed=False)
rsvp = ndb.IntegerProperty(indexed=True)
transportation = ndb.IntegerProperty(indexed=True)
class MainPage(webapp2.RequestHandler):
def get(self):
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render())
class Invite(webapp2.RequestHandler):
def get(self):
template = JINJA_ENVIRONMENT.get_template('invite.html')
self.response.write(template.render())
class RSVPAdd(webapp2.RequestHandler):
def get(self):
template = JINJA_ENVIRONMENT.get_template('rsvp_add.html')
self.response.write(template.render())
def post(self):
rsvp_first_name = self.request.get('first_name').lower()
rsvp_last_name = self.request.get('last_name').lower()
rsvp_group = int(self.request.get('group'))
rsvp = RSVPModel()
rsvp.firstName = rsvp_first_name
rsvp.lastName = rsvp_last_name
rsvp.group = rsvp_group
rsvp.put()
template = JINJA_ENVIRONMENT.get_template('rsvp_add.html')
self.response.write(template.render())
class RSVPFind(webapp2.RequestHandler):
def get(self):
template = JINJA_ENVIRONMENT.get_template('rsvp_find.html')
self.response.write(template.render())
def post(self):
rsvp_first_name = self.request.get('first_name').lower()
rsvp_last_name = self.request.get('last_name').lower()
query = RSVPModel.query(
RSVPModel.firstName == rsvp_first_name,
RSVPModel.lastName == rsvp_last_name
)
result = query.get()
if result is None:
logging.error(
rsvp_first_name + ' ' + rsvp_last_name + ' was not found'
)
template_values = {
'error': 'NOT_FOUND',
'message': 'Name not found, please try again or try another name in your party'
}
template = JINJA_ENVIRONMENT.get_template('rsvp_find.html')
self.response.write(template.render(template_values))
else:
query_params = {'group': result.group}
self.redirect('/rsvp/found?' + urllib.urlencode(query_params))
class RSVP(webapp2.RequestHandler):
def get(self):
rsvp_group = int(self.request.get('group'))
rsvp_query = RSVPModel.query(RSVPModel.group == rsvp_group)
rsvps = rsvp_query.fetch()
template_values = {
'rsvps': rsvps,
'group': rsvp_group
}
template = JINJA_ENVIRONMENT.get_template('rsvp.html')
self.response.write(template.render(template_values))
def post(self):
rsvp_yes = 0
rsvp_group = int(self.request.get('group'))
rsvp_query = RSVPModel.query(RSVPModel.group == rsvp_group)
rsvps = rsvp_query.fetch()
for rsvp in rsvps:
rsvp.rsvp = int(self.request.get('rsvp_'+rsvp.firstName))
if rsvp.rsvp and rsvp_yes == 0:
rsvp_yes = 1
else:
rsvp_yes = 2
rsvp.email = self.request.get('email')
if self.request.get('transportation'):
rsvp.transportation = 1
else:
rsvp.transportation = 0
rsvp.message = self.request.get('message')
rsvp.put()
query_params = {'rsvp': rsvp_yes}
self.redirect('/thanks?' + urllib.urlencode(query_params))
class Guestlist(webapp2.RequestHandler):
def get(self):
rsvp_query = RSVPModel.query().order(RSVPModel.group)
rsvps = rsvp_query.fetch(250)
rsvp_query_yes = RSVPModel.query(RSVPModel.rsvp == 1)
rsvps_yes = rsvp_query_yes.fetch(250)
rsvp_query_no = RSVPModel.query(RSVPModel.rsvp == 0)
rsvps_no = rsvp_query_no.fetch(250)
rsvp_query_none = RSVPModel.query(RSVPModel.rsvp == None)
rsvps_none = rsvp_query_none.fetch(250)
rsvp_query_transportation = RSVPModel.query(RSVPModel.transportation == 1)
rsvps_transportation = rsvp_query_transportation.fetch(250)
rsvp_none = len(rsvps)-len(rsvps_yes)-len(rsvps_no)
template_values = {
'rsvps': rsvps,
'total': len(rsvps),
'yes': len(rsvps_yes),
'no': len(rsvps_no),
'rsvps_none': len(rsvps_none),
'transportation': len(rsvps_transportation)
}
template = JINJA_ENVIRONMENT.get_template('guestlist.html')
self.response.write(template.render(template_values))
class Thanks(webapp2.RequestHandler):
def get(self):
rsvp = self.request.get("rsvp")
template_values = {
'rsvp': rsvp
}
template = JINJA_ENVIRONMENT.get_template('thanks.html')
self.response.write(template.render(template_values))
application = webapp2.WSGIApplication([
('/', MainPage),
('/invite', Invite),
('/rsvp', RSVPFind),
('/rsvp/found', RSVP),
('/rsvp/guestlist', Guestlist),
('/rsvp/add', RSVPAdd),
('/thanks', Thanks),
], debug=True)