-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.py
182 lines (152 loc) · 6.24 KB
/
event.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
#!/usr/bin/env python
import os
import jinja2
import webapp2
import datetime
import uuid
from createevent import Event
from google.appengine.api import channel
from google.appengine.api import users
from google.appengine.ext import ndb
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
import formcomment
client_id = 0
template_env = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.getcwd()))
class Message(ndb.Model):
"""Models an individual message entry for posting to nosql database."""
content = ndb.StringProperty()
event = ndb.StringProperty()
image = ndb.BlobProperty()
class ChatUser(ndb.Model):
"""Models an individual chatuser entry for posting to nosql database."""
event = ndb.StringProperty()
username = ndb.StringProperty()
uuid = ndb.StringProperty()
class EventHandler(webapp2.RequestHandler):
"""
Handler for main event streaming page and event.html
event key (read at beginning) defines the event attributes to be
loaded to the page.
client id is made global in place of cookies to ensure posting to
database. This would be resolved in a production release.
each client that connects to the page is given a unique identifier
for use with googles channel api. This ensures no message crossover.
"""
def get(self):
eventkey = self.request.get('key')
actualkey = ndb.Key(urlsafe=eventkey)
event = Event.query(Event.key == actualkey).fetch(1)[0]
user=users.get_current_user()
#assigning users for client list in datastore
global client_id
#creating unique id
client_id = str(uuid.uuid1())
if user:
#if user is logged in
chatuser = ChatUser()
chatuser.event = eventkey
chatuser.username = user.nickname()
chatuser.uuid = str(client_id)
chatuser.put()
print 'put'
else:
#else we don't have a user name for the datastore
chatuser = ChatUser()
chatuser.event = eventkey
chatuser.uuid = str(client_id)
chatuser.put()
print 'put'
#instantiate users access to channel for pushed data (messages and comments)
channel_token = channel.create_channel(client_id)
template = template_env.get_template('templates/event.html')
#login and out urls for the users api, passed to html
login_url = users.create_login_url(self.request.url)
logout_url = users.create_logout_url(self.request.url)
#load the messages and comments prestored in the datastore so that they are
#up to date with the current event feed
messages = Message.query(Message.event == eventkey).fetch(500)
comments = formcomment.Comment.query(formcomment.Comment.event == eventkey).fetch(500)
context = {
'user': user,
'eventkey': eventkey,
'videokey': event.video,
'login_url': login_url,
'logout_url': logout_url,
'token': channel_token,
'client_id': client_id,
'eventdescription': event.description,
'messages': messages,
'comments': comments}
self.response.write(template.render(context))
class MessageHandler(webapp2.RequestHandler):
"""
Post handler for new messages in the event chat room, recieves the message,
queries datastore for client ids and uses these with channel to distribute
to all connected clients.
Message is also put into datastore for preloading and analyses post video
stream
"""
def post(self):
message = Message()
eventkey = self.request.get('eventkey')
message_content = self.request.get('message')
user = users.get_current_user()
if not user:
user = 'Anon'
time=datetime.datetime.now()
#preformatting for ease of posting
message.content = "<span>" + str(user) + ':' + self.request.get('message') + ' - ' + time.strftime('%H:%M') + "</span>"
message.event = eventkey
message.put()
#pushing through channel
clientquery = ChatUser.query(ChatUser.event == eventkey)
clients = clientquery.fetch()
for client in clients:
channel.send_message(client.uuid, "<span>" + str(user) + ':' + self.request.get('message') + ' - ' + time.strftime('%H:%M') + "</span>")
class VideoHandler(blobstore_handlers.BlobstoreDownloadHandler):
"""
Blobstore handler for putting the video into <video> in event.html
retrieves the key using the key applied in jinja
"""
def get(self):
blob_info = blobstore.BlobInfo.get(self.request.get('id'))
self.send_blob(blob_info)
class ChannelConnectHandler(webapp2.RequestHandler):
def post(self):
"""
Channel connection handler. Will be called when a client connects.
"""
#using channel id to avoid crossover with global client_id
channel_id = self.request.get('from')
print "Channel Connect. Id: %s" % channel_id
CHANNEL_DISCONNECT_URL_PATTERN = '/_ah/channel/disconnected/'
class ChannelDisconnectHandler(webapp2.RequestHandler):
@classmethod
def mapping(cls):
return CHANNEL_DISCONNECT_URL_PATTERN, cls
def post(self):
"""
Channel disconnection handler. Will be called when a client disconnects.
"""
channel_id = self.request.get('from')
print "Channel Disconnect. Id: %s" % channel_id
userlist=ChatUser.query(ChatUser.uuid == channel_id).fetch(None, keys_only=True)
ndb.delete_multi(userlist)
class VideoDownloadHandler(blobstore_handlers.BlobstoreDownloadHandler):
"""
Blobstore handler for downloading the video of the event called on click
of the relevant link in event.html
"""
def get(self):
blob_info = blobstore.BlobInfo.get(self.request.get('id'))
self.send_blob(blob_info, save_as=blob_info.filename)
app = webapp2.WSGIApplication([
('/event', EventHandler),
('/message', MessageHandler),
('/_ah/channel/connected/', ChannelConnectHandler),
('/_ah/channel/disconnected/', ChannelDisconnectHandler),
('/video', VideoHandler),
('/save', VideoDownloadHandler)
], debug=True)