-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
237 lines (199 loc) · 7.7 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
235
236
237
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import webapp2
import urllib2
import json
import datetime
import pprint
import cgi
import codecs
import logging
#from twilio import twiml
from google.appengine.api import memcache
from google.appengine.ext import ndb
import map
import aliases
from models import Character, Message, Location
import os
import jinja2
jinja_env = jinja2.Environment(
autoescape=True,
loader=jinja2.FileSystemLoader(
os.path.join(os.path.dirname(__file__), 'templates')
)
)
def cache(key, update = False):
msgs = memcache.get(key)
if msgs is None or update:
print "DB CACHE QUERY %s" % key
if key is 'top_msgs':
msgs = Message.query().order(-Message.createDate).fetch(20)
elif key is 'get_chars':
msgs = Character.all()
msgs = list(msgs)
memcache.set(key, msgs)
return msgs
def top_msgs(update = False):
key = 'top_msgs'
msgs = cache(key, update)
print msgs
return msgs
def get_chars(update = False):
key = 'get_chars'
msgs = cache(key, update)
print msgs
return msgs
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
class MainHandler(Handler):
def writeHTML(self, **kw):
# Get messages
messages = top_msgs()
# Get characters
characters = get_chars()
self.render('form.html', messages=messages, characters=characters, **kw)
# Initial page load
def get(self):
character = Character(name="")
location = Location()
location.latlng=ndb.GeoPt(37.7749295, -122.4194155)
# Write
self.writeHTML(error="", character=character, location=location)
# User form submission
def post(self):
messageid = self.request.get("messageid")
if messageid == "":
rawCharacter = cgi.escape(self.request.get("character"), quote = True)
rawLocation = cgi.escape(self.request.get("address"), quote = True)
# Validate character
(character, charError) = map.validateCharacter(rawCharacter)
# Validate location
location = map.validateLocation(rawLocation)
error, msgError = "", ""
# Check validation errors and format error message
if character == None:
msgChar = rawCharacter
else:
msgChar = str(character.name)
if charError != "":
error = charError
msgError = error
if location.status != "OK":
error = (error + " " + location.status.encode('utf_8')).decode('utf_8')
msgError = error
if (charError == "") and (location.status == "OK"):
error = ("Move %s to %s. Got it!" % (msgChar, location.address.encode('utf_8'))).decode('utf_8')
msgError = ""
print datetime.datetime.utcnow()
print "error: " + error.encode('utf_8')
print type(error)
print "msgError: " + msgError.encode('utf_8')
print type(msgError)
# Store in Message store
if recordMessage("WebForm", None, self.request.remote_addr, msgChar, location, rawCharacter+" "+rawLocation, msgError):
print "IN APP:"
top_msgs(True)
self.writeHTML(error=error, character=character, location=location)
else:
error = "App Error: Failed to insert message."
self.writeHTML(error=error, character=character, location=location)
else:
# Validate messageid and get message
messagekey = ndb.Key(urlsafe=messageid)
message = Message()
message = messagekey.get()
character = Character.query(Character.name == message.character).get()
location = Location()
location.address = message.address
location.latlng = message.latlng
# If message found
if not message:
error = "App Error: Cannot get message."
self.writeHTML(error=error, character=None, location=None)
# If message not found
else:
# Write
self.writeHTML(error="", character=character, location=location)
'''
class SMSHandler(webapp2.RequestHandler):
def post(self):
messageSid = self.request.get("MessageSid")
sourcePhone = self.request.get("From")
rawMessage = self.request.get("Body")
print ("Debug", messageSid, sourcePhone, rawMessage)
rawCharacter = rawMesssage.partition(' ')[0] #first word of message
rawLocation = rawMesssage.partition(' ')[1] #rest of the message (except first word)
error = ""
# Validate character
character = validateCharacter(rawCharacter)
if character == None:
error = "Invalid character. [Character: %s]" % rawCharacter
responseMsg = error
# Validate location
location = validateLocation(rawLocation)
if location.error != "OK":
error += "Invalid location. [Error: %s] [Location: %s]" % (str(location.error), rawLocation)
responseMsg = error
# If both character and location are validated
if error == "":
responseMsg = "Move %s to %s. Got it!" % (character, location)
# Store SMS in Message store
recordMessage("SMS", sourcePhone, None, character, location, (("messageSid: %s" % messageSid) + rawMessage), error)
if recordMessage != True:
print ("Unable to record message.") # for debugging
response = twilio.twiml.Response()
response.message(responseMsg)
return str(response)
'''
def recordMessage(sourceType, sourcePhone, sourceIP, character, location, rawMessage, errorstr):
message = Message()
message.sourceType = sourceType
message.sourcePhone = sourcePhone
message.sourceIP = sourceIP
message.character = character
message.address = location.address
message.latlng = location.latlng
message.rawMessage = rawMessage
message.error = errorstr
if message.put():
return True
else:
return False
class LoadDB(webapp2.RequestHandler):
def get(self):
# Loading character DB
char = Character()
for x in aliases.charlist:
print x[0]
print x[1]
char = Character.get_or_insert(str(x[0]), name=str(x[0]), avatarFile=str(x[1]))
print char
char.put()
for ch in Character.query().order(Character.name).fetch():
print ch
get_chars(True)
self.response.write('Loaded DB')
app = webapp2.WSGIApplication([
('/', MainHandler), ('/submitForm', MainHandler), ('/load', LoadDB)
], debug=True)