-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
124 lines (103 loc) · 4.24 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
import datetime
import time
import logging
import os
import random
import re
import model
import cgi
import calendar
from django.utils import simplejson as json
#channel api
from google.appengine.api import channel
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
class InvalidRequestException( Exception ):
def __init__( self, msg ):
self.message = msg
class MainPage(webapp.RequestHandler):
"""Main UI Page, renders the front page"""
def get(self):
token = channel.create_channel("rhokds")
template_values = {"token":token}
path = os.path.join(os.path.dirname(__file__), "templates/mapstream.html")
self.response.out.write(template.render(path, template_values))
class FeedHandler(webapp.RequestHandler):
"""Feeds"""
def get(self):
query = model.Message.all()
retlist = []
out=""
for q in query:
k = q.__dict__.keys()
print q.__dict__
r = { "handler": q.handler }
r["text"] = q.text
r["timestamp"] = calendar.timegm(q.timestamp.timetuple())
if hasattr(q, "imgpath"):
r["imgpath"] = q.imgpath
if hasattr(q, "timestamp"):
r["timestamp"] = calendar.timegm(q.timestamp.timetuple())
if hasattr(q, "lat") and hasattr(q, "lon"):
r["lat"] = q.lat
r["lon"] = q.qlon
if hasattr(q, "category"):
r["category"] = q.category
retlist.append( r )
self.response.out.write({"status":"ok", "data": retlist})
class PostHandler(webapp.RequestHandler):
"""A Poster Handler"""
def post(self):
args = self.request.arguments()
msg = None
jsu = {}
try:
if "handler" not in args:
raise InvalidRequestException("`handler` field not found")
else:
msg = model.Message( handler = self.request.get("handler"));
jsu["handler"] = msg.handler
if ( "text" not in args ) and ("imgpath" not in args):
raise InvalidRequestException("`text` and `imgpath` not found")
if "text" in args:
msg.text = self.request.get("text")
jsu["text"] = msg.text
if "imgpath" in args:
msg.imgpath = self.request.get("imgpath")
jsu["imgpath"] = msg.imgpath
try:
msg.timestamp = datetime.datetime.fromtimestamp( long( self.request.get( "timestamp")))
jsu["timestamp"] = long( self.request.get( "timestamp" ) )
except ValueError:
msg.timestamp = datetime.datetime.utcnow()
jsu["timestamp"] = calendar.timegm(datetime.datetime.utcnow().timetuple())
if ("lat" in args) and ("lon" in args):
msg.lat = float( self.request.get("lat"))
msg.lon = float( self.request.get("lon"))
jsu["lat"] = float( self.request.get("lat") )
jsu["lon"] = float( self.request.get("lon") )
if "category" in args:
msg.category = db.Category( self.request.get("category") )
jsu["category"] = msg.category
msg.put()
channel.send_message("rhokds", json.dumps(jsu))
print msg.handler,": ", msg.text
self.response.headers["Content-Type"]="application/json"
self.response.out.write(json.dumps({"status":"ok"}))
except InvalidRequestException as ex:
self.response.headers["Content-Type"]="application/json"
self.response.out.write(json.dumps({"status":"failed", "message":ex.message}))
#except:
# self.response.headers["Content-Type"]="application/json"
# self.response.out.write(json.dumps({"status":"failed", "message":"invalid names"}))
application=webapp.WSGIApplication([
('/', MainPage),
('/post', PostHandler),
('/feed', FeedHandler)
])
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()