-
Notifications
You must be signed in to change notification settings - Fork 7
/
pointout.py
176 lines (121 loc) · 5.15 KB
/
pointout.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
import os
import base64
import re
import logging
import datetime
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext import blobstore
from google.appengine.api import channel
from google.appengine.api import memcache
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import simplejson as json
class Project(db.Model):
image = db.BlobProperty()
image_content_type = db.StringProperty(indexed = False)
creator = db.IntegerProperty()
data = db.TextProperty()
def channel_key(self, user_id):
return str(self.key().id())+str(user_id)
def connected_users(self):
project_users = memcache.get("%s_users" % self.key())
if project_users is None:
project_users = []
return project_users
def connect_user(self, user_id):
self.broadcast_message({'action':'user_connected', 'user':str(user_id)})
project_users = memcache.get("%s_users" % self.key())
if project_users is not None:
if project_users.count(user_id) == 0:
project_users.append(user_id)
else:
project_users = [user_id]
memcache.set("%s_users" % self.key(), project_users, 3600)
return project_users
def broadcast_message(self, message, sender = None):
users = self.connected_users()
message = json.dumps(message)
for user_id in users:
if user_id != sender:
logging.info("Sending message '%s' to '%s'" % (message, user_id))
channel.send_message(self.channel_key(user_id), message)
class PointOutHandler(webapp.RequestHandler):
def render_json(self, data):
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(data))
class RedirectToIndex(PointOutHandler):
def get(self, project_id):
self.redirect("/#%s" % project_id)
class LoadProject(PointOutHandler):
def post(self, project_id):
project = Project.get_by_id(int(project_id))
user_id = self.request.get('user_id')
if self.request.get('updating') == '':
token = channel.create_channel(project.channel_key(user_id))
project.connect_user(int(user_id))
else:
token = None
if project:
self.response.headers['Content-Type'] = 'application/json'
self.render_json({
'points':json.loads(project.data),
'token':token,
'connected_users':project.connected_users()})
class SaveProject(PointOutHandler):
def post(self, project_id):
project = Project.get_by_id(int(project_id))
user = int(self.request.get('user'))
if project:
project.data = self.request.get('points')
project.put()
self.render_json({'status':'ok'})
class AddComment(PointOutHandler):
def post(self, project_id):
project = Project.get_by_id(int(project_id))
if project:
user = int(self.request.get('user'));
message = {
'action': 'comment_added',
'user': user,
'user_name': self.request.get('user_name'),
'point': self.request.get('point'),
'text': self.request.get('comment'),
'image': self.request.get('image')
}
project.broadcast_message(message, user)
self.render_json({'status':'ok'})
class CreateProject(PointOutHandler):
dataUrlPattern = re.compile('data:(image/(png|jpeg));base64,(.*)$')
def post(self):
project = Project()
imgdata = self.request.get('image')
match = self.dataUrlPattern.match(imgdata)
imgb64 = match.group(3)
content_type = match.group(1)
if imgb64 is not None and len(imgb64) > 0:
project.image = db.Blob(base64.b64decode(imgb64))
project.image_content_type = content_type
project.creator = int(self.request.get('user_id'))
project.data = '[]';
project.put()
project.connect_user(project.creator)
token = channel.create_channel(project.channel_key(project.creator))
self.render_json({'id':project.key().id(), 'token': token})
class GetImage(webapp.RequestHandler):
def get(self, project_id):
project = Project.get_by_id(int(project_id))
self.response.headers['Content-Type'] = project.image_content_type
self.response.out.write(project.image)
application = webapp.WSGIApplication(
[('/create', CreateProject),
('/save/([^/]+)?', SaveProject),
('/image/([^/]+)?', GetImage),
('/load/([^/]+)?', LoadProject),
('/comment/([^/]+)?', AddComment),
('/([^/]+)?', RedirectToIndex)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()