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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
|
"""
Created on Jan 31, 2011
@author: leifj
"""
from celery.utils import deprecated
from meetingtools.apps.sco.models import get_sco, get_sco_shortcuts
from meetingtools.apps.room.models import Room
from meetingtools.multiresponse import respond_to, redirect_to, json_response
from meetingtools.apps.room.forms import DeleteRoomForm,\
CreateRoomForm, ModifyRoomForm, TagRoomForm
from django.shortcuts import get_object_or_404
from meetingtools.ac import ac_api_client
import re
from django.contrib.auth.decorators import login_required
import logging
from pprint import pformat
from meetingtools.utils import session, base_url
import time
from django.conf import settings
from django.utils.datetime_safe import datetime
from django.http import HttpResponseRedirect, HttpResponseNotFound
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from django_co_acls.models import allow, acl, clear_acl
from meetingtools.ac.api import ACPClient
from tagging.models import Tag, TaggedItem
import random, string
from django.utils.feedgenerator import rfc3339_date
from django.views.decorators.cache import never_cache
from meetingtools.apps.cluster.models import acc_for_user
from django.contrib.auth.models import User
import iso8601
from celery.execute import send_task
from meetingtools.apps.room.tasks import start_user_counts_poll
def _user_meeting_folder(request,acc):
if not session(request,'my_meetings_sco_id'):
with ac_api_client(acc) as api:
userid = request.user.username
folders = api.request('sco-search-by-field',{'filter-type': 'folder','field':'name','query':userid}).et.xpath('//sco[folder-name="User Meetings"]')
logging.debug("user meetings folder: "+pformat(folders))
#folder = next((f for f in folders if f.findtext('.//folder-name') == 'User Meetings'), None)
if folders and len(folders) > 0:
session(request,'my_meetings_sco_id',folders[0].get('sco-id'))
return session(request,'my_meetings_sco_id')
def user_meeting_folder(user,api):
userid = user.username
folders = api.request('sco-search-by-field',
{'filter-type': 'folder', 'field': 'name', 'query': userid}).et.xpath('//sco[folder-name="User Meetings"]')
logging.debug("user meetings folder: "+pformat(folders))
#folder = next((f for f in folders if f.findtext('.//folder-name') == 'User Meetings'), None)
return folders[0].get('sco-id')
def _user_templates(request,acc,folder_sco):
templates = []
with ac_api_client(acc) as api:
if folder_sco:
my_templates = api.request('sco-contents',{'sco-id': folder_sco.sco_id,'filter-type': 'folder'}).et.xpath('.//sco[folder-name="My Templates"][0]')
if my_templates and len(my_templates) > 0:
my_templates_sco_id = my_templates[0].get('sco_id')
meetings = api.request('sco-contents',{'sco-id': my_templates_sco_id,'filter-type': 'meeting'})
if meetings:
templates += [(get_sco(acc,r.get('sco-id')),r.findtext('name')) for r in meetings.et.findall('.//sco')]
shared_templates_sco = get_sco_shortcuts(acc,'shared-meeting-templates')
shared_templates = api.request('sco-contents',{'sco-id': shared_templates_sco.sco_id,'filter-type': 'meeting'})
if shared_templates:
templates += [(get_sco(acc,r.get('sco-id')).id,r.findtext('name')) for r in shared_templates.et.findall('.//sco')]
return templates
def _find_current_session(session_info):
for r in session_info.et.xpath('//row'):
#logging.debug(pformat(etree.tostring(r)))
end = r.findtext('date-end')
if end is None:
return r
return None
def _nusers(session_info):
cur = _find_current_session(session_info)
if cur is not None:
return cur.get('num-participants')
else:
return 0
@login_required
def view(request,id):
room = get_object_or_404(Room,pk=id)
return respond_to(request,
{'text/html':'apps/room/list.html'},
{'user':request.user,
'rooms':[room],
'title': room.name,
'baseurl': base_url(request),
'active': True,
})
def _init_update_form(request,form,acc,folder_sco):
if form.fields.has_key('urlpath'):
url = base_url(request)
form.fields['urlpath'].widget.prefix = url
if form.fields.has_key('source_sco'):
form.fields['source_sco'].widget.choices = [('','-- select template --')]+[r for r in _user_templates(request,acc,folder_sco)]
def _update_room(request, room, data=dict(), acc=None):
params = {'type':'meeting'}
if acc is None:
acc = acc_for_user(request.user)
for attr,param in (('sco','sco-id'),('folder_sco','folder-id'),('source_sco','source-sco-id'),('urlpath','url-path'),('name','name'),('description','description')):
v = None
if hasattr(room,attr):
v = getattr(room,attr)
logging.debug("%s,%s = %s" % (attr,param,repr(v)))
if data.has_key(attr) and data[attr]:
v = data[attr]
if v:
if isinstance(v,(str,unicode)):
params[param] = v
elif hasattr(v,'sco_id'):
params[param] = v.sco_id # support ACObject instances
elif hasattr(v,'__getitem__'):
params[param] = v[0]
else:
params[param] = repr(v)
logging.debug(pformat(params))
with ac_api_client(acc) as api:
r = api.request('sco-update', params, True)
sco_elt = r.et.find(".//sco")
if sco_elt:
sco_id = sco_elt.get('sco-id')
if sco_id:
data['sco'] = get_sco(acc,sco_id)
source_sco_id = r.et.find(".//sco").get('sco-source-id')
if source_sco_id:
data['source_sco'] = get_sco(acc,source_sco_id)
room.sco = data['sco']
room.save()
sco_id = room.sco.sco_id
assert(sco_id is not None and sco_id > 0)
user_principal = api.find_user(room.creator.username)
#api.request('permissions-reset',{'acl-id': sco_id},True)
api.request('permissions-update',{'acl-id': sco_id,
'principal-id': user_principal.get('principal-id'),
'permission-id':'host'},True) # owner is always host
if data.has_key('access'):
access = data['access']
if access == 'public':
allow(room,'anyone','view-hidden')
elif access == 'private':
allow(room,'anyone','remove')
# XXX figure out how to keep the room permissions in sync with the AC permissions
for ace in acl(room):
principal_id = None
if ace.group:
principal = api.find_group(ace.group.name)
if principal:
principal_id = principal.get('principal-id')
elif ace.user:
principal = api.find_user(ace.user.username)
if principal:
principal_id = principal.get('principal-id')
else:
principal_id = "public-access"
if principal_id:
api.request('permissions-update',{'acl-id': room.sco_id, 'principal-id': principal_id, 'permission-id': ace.permission},True)
room.deleted_sco = None # if we just cleaned a room we zero out the deleted_sco_id field to indicate the room is now ready for use
room.save() # a second save here to avoid races
return room
@never_cache
@login_required
def create(request):
acc = acc_for_user(request.user)
my_meetings_sco = get_sco(acc,_user_meeting_folder(request,acc))
template_sco_id = acc.default_template_sco_id
if not template_sco_id:
template_sco_id = settings.DEFAULT_TEMPLATE_SCO
room = Room(creator=request.user,folder_sco=my_meetings_sco,source_sco=get_sco(acc,template_sco_id))
what = "Create"
title = "Create a new room"
if request.method == 'POST':
form = CreateRoomForm(request.POST,instance=room)
_init_update_form(request, form, acc, my_meetings_sco)
if form.is_valid():
_update_room(request, room, form.cleaned_data)
room = form.save()
return redirect_to("/rooms#%d" % room.id)
else:
form = CreateRoomForm(instance=room)
_init_update_form(request, form, acc, my_meetings_sco)
return respond_to(request,{'text/html':'apps/room/create.html'},
{'form':form,
'formtitle': title,
'cancelurl': '/rooms',
'cancelname':'Cancel',
'submitname':'%s Room' % what})
@never_cache
@login_required
def myroom(request):
acc = acc_for_user(request.user)
my_meetings_sco = get_sco(acc,_user_meeting_folder(request,acc))
template_sco_id = acc.default_template_sco_id
if not template_sco_id:
template_sco_id = settings.DEFAULT_TEMPLATE_SCO
template_sco = get_sco(acc,template_sco_id)
room = None
try:
room = Room.by_name(acc,name=request.user.username)
except MultipleObjectsReturned:
raise ValueError("Oops - there seem to be multiple rooms with name '%s'" % request.user.username)
except ObjectDoesNotExist:
room = Room(creator=request.user,
folder_sco=my_meetings_sco,
name=request.user.username,
source_sco=template_sco)
_update_room(request,room,dict(access='public'))
if not room:
raise ValueError("Opps - can't find your room")
return _goto(request,room)
@never_cache
@login_required
def update(request,id):
room = get_object_or_404(Room,pk=id)
acc = room.sco.acc
what = "Update"
title = "Modify %s" % room.name
if request.method == 'POST':
form = ModifyRoomForm(request.POST,instance=room)
_init_update_form(request, form, acc, room.folder_sco)
if form.is_valid():
_update_room(request, room, form.cleaned_data)
room = form.save()
return redirect_to("/rooms#%d" % room.id)
else:
form = ModifyRoomForm(instance=room)
_init_update_form(request, form, acc, room.folder_sco)
return respond_to(request,{'text/html':'apps/room/update.html'},
{'form':form,
'formtitle': title,
'cancelurl': '/rooms#%d' % room.id,
'cancelname': 'Cancel',
'submitname':'%s Room' % what})
@deprecated
def _import_room(request,acc,r):
modified = False
room = None
if room and (abs(room.lastupdate() - time.time()) < settings.IMPORT_TTL):
return room
if r.has_key('urlpath'):
r['urlpath'] = r['urlpath'].strip('/')
try:
room = Room.by_sco(r['sco'])
for key in ('sco','name','source_sco','urlpath','description','user_count','host_count'):
if r.has_key(key) and hasattr(room,key):
rv = getattr(room,key)
if rv != r[key] and r[key] is not None and r[key]:
setattr(room,key,r[key])
modified = True
if modified:
logging.debug("+++ saving room ... %s" % pformat(room))
room.save()
except ObjectDoesNotExist:
if r['folder_sco']:
try:
room = Room.objects.create(sco=r['sco'],
source_sco=r['source_sco'],
name=r['name'],
urlpath=r['urlpath'],
description=r['description'],
creator=request.user,
folder_sco=r['folder_sco'])
except Exception,e:
room = None
pass
if not room:
return None
logging.debug("+++ looking at user counts")
with ac_api_client(acc) as api:
userlist = api.request('meeting-usermanager-user-list',{'sco-id': room.sco.sco_id},False)
if userlist.status_code() == 'ok':
room.user_count = int(userlist.et.xpath("count(.//userdetails)"))
room.host_count = int(userlist.et.xpath("count(.//userdetails/role[text() = 'host'])"))
room.save()
return room
@login_required
def list_rooms(request,username=None):
user = request.user
if username:
try:
user = User.objects.get(username=username)
except ObjectDoesNotExist:
user = None
rooms = []
if user:
rooms = Room.objects.filter(creator=user).order_by('name').all().prefetch_related("creator","sco","folder_sco","source_sco","deleted_sco")
return respond_to(request,
{'text/html':'apps/room/list.html'},
{'title':'Your Rooms','edit':True,'active':len(rooms) == 1,'rooms':rooms})
@login_required
def unlock(request,id):
room = get_object_or_404(Room,pk=id)
room.unlock()
return redirect_to("/rooms#%d" % room.id)
@login_required
def delete(request,id):
room = get_object_or_404(Room,pk=id)
if request.method == 'POST':
form = DeleteRoomForm(request.POST)
if form.is_valid():
with ac_api_client(room.sco.acc) as api:
api.request('sco-delete',{'sco-id':room.sco.sco_id},raise_error=False)
#clear_acl(room)
#room.sco.delete()
#if room.folder_sco is not None:
#room.folder_sco.delete()
#if room.deleted_sco is not None:
#room.deleted_sco.delete()
room.delete()
return redirect_to("/rooms")
else:
form = DeleteRoomForm()
return respond_to(request,{'text/html':'edit.html'},
{'form':form,
'formtitle': 'Delete %s' % room.name,
'cancelurl': '/rooms',
'cancelname':'Cancel',
'submitname':'Delete Room'})
def _clean(request,room):
with ac_api_client(room.sco.acc) as api:
room.deleted_sco = room.sco
room.save()
api.request('sco-delete',{'sco-id':room.sco.sco_id},raise_error=False)
room.sco = None
return _update_room(request, room)
def occupation(request,rid):
room = get_object_or_404(Room,pk=rid)
with ac_api_client(room.sco.acc) as api:
api.poll_user_counts(room)
d = {'nusers': room.user_count, 'nhosts': room.host_count}
return respond_to(request,
{'text/html': 'apps/room/fragments/occupation.txt',
'application/json': json_response(d, request)},
d)
def go_by_id(request,id):
room = get_object_or_404(Room,pk=id)
return goto(request,room)
def go_by_path(request,path):
rooms = Room.objects.filter(urlpath=path)
if len(rooms) == 1:
return goto(request,rooms[0])
if len(rooms) == 0:
return HttpResponseNotFound()
return respond_to(request,
{'text/html': 'apps/room/choose.html',
'application/json': json_response([base_url(request,room.go_url()) for room in rooms])},
{'rooms': rooms})
@login_required
def promote_and_launch(request,rid):
room = get_object_or_404(Room,pk=rid)
return _goto(request,room,clean=False,promote=True)
def launch(request,rid):
room = get_object_or_404(Room,pk=rid)
return _goto(request,room,clean=False)
def goto(request,room):
return _goto(request,room,clean=True)
def _random_key(length=20):
rg = random.SystemRandom()
alphabet = string.letters + string.digits
return str().join(rg.choice(alphabet) for _ in range(length))
def _goto(request,room,clean=True,promote=False):
if room.is_locked():
return respond_to(request, {"text/html": "apps/room/retry.html"}, {'room': room, 'wait': 10})
now = time.time()
lastvisit = room.lastvisit()
room.lastvisited = datetime.now()
with ac_api_client(room.sco.acc) as api:
api.poll_user_counts(room)
if clean:
# don't clean the room unless you get a good status code from the call to the usermanager api above
if room.self_cleaning and room.user_count == 0:
if (room.user_count == 0) and (abs(lastvisit - now) > settings.GRACE):
room.lock("Locked for cleaning")
try:
room = _clean(request,room)
finally:
room.unlock()
if room.host_count == 0 and room.allow_host:
return respond_to(request, {"text/html": "apps/room/launch.html"}, {'room': room})
else:
room.save()
key = None
if request.user.is_authenticated() and room.sco.acc.cross_domain_sso:
key = _random_key(20)
user_principal = api.find_user(request.user.username)
principal_id = user_principal.get('principal-id')
with ac_api_client(room.sco.acc) as api:
api.request("user-update-pwd",{"user-id": principal_id, 'password': key,'password-verify': key},True)
if promote and room.self_cleaning:
if user_principal:
api.request('permissions-update',{'acl-id': room.sco.sco_id,'principal-id': user_principal.get('principal-id'),'permission-id':'host'},True)
r = api.request('sco-info',{'sco-id':room.sco.sco_id},True)
urlpath = r.et.findtext('.//sco/url-path')
start_user_counts_poll(room,10)
if key:
try:
user_client = ACPClient(room.sco.acc.api_url, request.user.username, key, cache=False)
return user_client.redirect_to(room.sco.acc.url+urlpath)
except Exception,e:
pass
return HttpResponseRedirect(room.sco.acc.url+urlpath)
## Tagging
def _room2dict(request,room):
return {'name':room.name,
'description':room.description,
'user_count':room.nusers(),
'host_count':room.nhosts(),
'updated': rfc3339_date(room.lastupdated),
'self_cleaning': room.self_cleaning,
'url': base_url(request,room.go_url())}
# should not require login
def list_by_tag(request,tn):
tags = tn.split('+')
rooms = TaggedItem.objects.get_by_model(Room, tags).order_by('name').all().prefetch_related("creator","sco","folder_sco","source_sco","deleted_sco")
title = 'Rooms tagged with %s' % " and ".join(tags)
return respond_to(request,
{'text/html':'apps/room/list.html',
'application/json': json_response([_room2dict(request,room) for room in rooms],request)},
{'title':title,
'description':title ,
'edit':False,
'active':len(rooms) == 1,
'baseurl': base_url(request),
'tagstring': tn,
'rooms':rooms})
def widget(request,tags=None):
title = 'Meetingtools jQuery widget'
return respond_to(request,{'text/html':'apps/room/widget.html'},{'title': title,'tags':tags})
def _can_tag(request,tag):
if tag in ('selfcleaning','cleaning','public','private'):
return False,"'%s' is reserved" % tag
# XXX implement access model for tags here soon
return True,""
@never_cache
@login_required
def untag(request,rid,tag):
room = get_object_or_404(Room,pk=rid)
new_tags = []
for t in Tag.objects.get_for_object(room):
if t.name != tag:
new_tags.append(t.name)
Tag.objects.update_tags(room, ' '.join(new_tags))
return redirect_to("/room/%d/tag" % room.id)
@never_cache
@login_required
def tag(request,rid):
room = get_object_or_404(Room,pk=rid)
if request.method == 'POST':
form = TagRoomForm(request.POST)
if form.is_valid():
for tag in re.split('[,\s]+',form.cleaned_data['tag']):
tag = tag.strip()
ok,reason = _can_tag(request,tag)
if ok:
Tag.objects.add_tag(room, tag)
else:
form._errors['tag'] = form.error_class([u'%s ... please choose another tag!' % reason])
else:
form = TagRoomForm()
tags = Tag.objects.get_for_object(room)
tn = "+".join([t.name for t in tags])
return respond_to(request,
{'text/html': "apps/room/tag.html"},
{'form': form,
'formtitle': 'Add Tag',
'cancelurl': '/rooms#%d' % room.id,
'cancelname':'Done',
'submitname': 'Add Tag',
'room': room,
'tagstring': tn,
'tags': tags})
def room_recordings(request,room):
acc = room.sco.acc
with ac_api_client(acc) as api:
r = api.request('sco-expanded-contents',{'sco-id': room.sco.sco_id,'filter-icon':'archive'},True)
return [{'published': False,
'name': sco_elt.findtext('name'),
'sco': get_sco(acc,sco_elt.get('sco-id')),
'url': room.sco.acc.make_url(sco_elt.findtext('url-path')),
'dl': room.sco.acc.make_dl_url(sco_elt.findtext('url-path')),
'description': sco_elt.findtext('description'),
'date_created': iso8601.parse_date(sco_elt.findtext('date-created')),
'date_modified': iso8601.parse_date(sco_elt.findtext('date-modified'))} for sco_elt in r.et.findall(".//sco")] + [
{'published': True,
'ar': ar,
'name': ar.name,
'description': ar.description,
'sco': ar.sco,
'url': room.sco.acc.make_url(ar.urlpath),
'dl': room.sco.acc.make_url(ar.urlpath),
'date_created': ar.timecreated,
'date_modified': ar.lastupdated} for ar in room.archives.all().prefetch_related("creator","sco","folder_sco","source_sco","deleted_sco")
]
@login_required
def recordings(request,rid):
room = get_object_or_404(Room,pk=rid)
return respond_to(request,
{'text/html': 'apps/room/recordings.html'},
{'recordings': room_recordings(request,room),'room':room})
|