diff options
Diffstat (limited to 'meetingtools/apps/room/views.py')
-rw-r--r-- | meetingtools/apps/room/views.py | 273 |
1 files changed, 125 insertions, 148 deletions
diff --git a/meetingtools/apps/room/views.py b/meetingtools/apps/room/views.py index 048fc05..bf9bc61 100644 --- a/meetingtools/apps/room/views.py +++ b/meetingtools/apps/room/views.py @@ -3,14 +3,15 @@ Created on Jan 31, 2011 @author: leifj """ -from meetingtools.apps.room.models import Room, ACCluster +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 meetingtools.apps import room from django.contrib.auth.decorators import login_required import logging from pprint import pformat @@ -20,7 +21,7 @@ from django.conf import settings from django.utils.datetime_safe import datetime from django.http import HttpResponseRedirect from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned -from django_co_acls.models import allow, deny, acl, clear_acl +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 @@ -44,45 +45,21 @@ def _user_meeting_folder(request,acc): return session(request,'my_meetings_sco_id') -def _shared_templates_folder(request,acc): - if not session(request,'shared_templates_sco_id'): - with ac_api_client(acc) as api: - shared = api.request('sco-shortcuts').et.xpath('.//sco[@type="shared-meeting-templates"]') - logging.debug("shared templates folder: "+pformat(shared)) - #folder = next((f for f in folders if f.findtext('.//folder-name') == 'User Meetings'), None) - if shared and len(shared) > 0: - session(request,'shared_templates_sco_id',shared[0].get('sco-id')) - return session(request,'shared_templates_sco_id') - -def _user_rooms(request,acc,my_meetings_sco_id): - rooms = [] - if my_meetings_sco_id: - with ac_api_client(acc) as api: - meetings = api.request('sco-expanded-contents',{'sco-id': my_meetings_sco_id,'filter-type': 'meeting'}) - if meetings: - rooms = [{'sco_id': r.get('sco-id'), - 'name': r.findtext('name'), - 'source_sco_id': r.get('source-sco-id'), - 'urlpath': r.findtext('url-path'), - 'description': r.findtext('description')} for r in meetings.et.findall('.//sco')] - return rooms - -def _user_templates(request,acc,my_meetings_sco_id): +def _user_templates(request,acc,folder_sco): templates = [] with ac_api_client(acc) as api: - if my_meetings_sco_id: - my_templates = api.request('sco-contents',{'sco-id': my_meetings_sco_id,'filter-type': 'folder'}).et.xpath('.//sco[folder-name="My Templates"][0]') + 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 = templates + [(r.get('sco-id'),r.findtext('name')) for r in meetings.et.findall('.//sco')] + templates += [(get_sco(acc,r.get('sco-id')),r.findtext('name')) for r in meetings.et.findall('.//sco')] - shared_templates_sco_id = _shared_templates_folder(request, acc) - if shared_templates_sco_id: - shared_templates = api.request('sco-contents',{'sco-id': shared_templates_sco_id,'filter-type': 'meeting'}) - if shared_templates: - templates = templates + [(r.get('sco-id'),r.findtext('name')) for r in shared_templates.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 @@ -113,45 +90,54 @@ def view(request,id): 'active': True, }) -def _init_update_form(request,form,acc,my_meetings_sco_id): +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_id'): - form.fields['source_sco_id'].widget.choices = [('','-- select template --')]+[r for r in _user_templates(request,acc,my_meetings_sco_id)] + 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()): +def _update_room(request, room, data=dict(), acc=None): params = {'type':'meeting'} - - for attr,param in (('sco_id','sco-id'),('folder_sco_id','folder-id'),('source_sco_id','source-sco-id'),('urlpath','url-path'),('name','name'),('description','description')): + + 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,v)) + 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(room.acc) as api: + with ac_api_client(acc) as api: r = api.request('sco-update', params, True) - sco = r.et.find(".//sco") - if sco: - sco_id = sco.get('sco-id') + sco_elt = r.et.find(".//sco") + if sco_elt: + sco_id = sco_elt.get('sco-id') if sco_id: - data['sco_id'] = sco_id - data['source_sco_id'] = r.et.find(".//sco").get('sco-source-id') - room.sco_id = sco_id - room.save() + data['sco'] = get_sco(acc,sco_id) - sco_id = room.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) @@ -185,7 +171,7 @@ def _update_room(request, room, data=dict()): if principal_id: api.request('permissions-update',{'acl-id': room.sco_id, 'principal-id': principal_id, 'permission-id': ace.permission},True) - room.deleted_sco_id = 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.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 @@ -193,47 +179,52 @@ def _update_room(request, room, data=dict()): @login_required def create(request): acc = acc_for_user(request.user) - my_meetings_sco_id = _user_meeting_folder(request,acc) + 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,acc=acc,folder_sco_id=my_meetings_sco_id,source_sco_id=template_sco_id) + 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, room.folder_sco_id) + _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, room.folder_sco_id) + _init_update_form(request, form, acc, my_meetings_sco) - return respond_to(request,{'text/html':'apps/room/create.html'},{'form':form,'formtitle': title,'cancelname':'Cancel','submitname':'%s Room' % what}) + 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_id = _user_meeting_folder(request,acc) + 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.objects.get(acc=acc,name=request.user.username) + 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, acc=acc, - folder_sco_id=my_meetings_sco_id, + folder_sco=my_meetings_sco, name=request.user.username, - source_sco_id=template_sco_id) + source_sco=template_sco) _update_room(request,room,dict(access='public')) if not room: @@ -245,23 +236,29 @@ def myroom(request): @login_required def update(request,id): room = get_object_or_404(Room,pk=id) - acc = room.acc + 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_id) + _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_id) + _init_update_form(request, form, acc, room.folder_sco) - return respond_to(request,{'text/html':'apps/room/update.html'},{'form':form,'formtitle': title,'cancelname': 'Cancel','submitname':'%s Room' % what}) - + 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 @@ -273,8 +270,8 @@ def _import_room(request,acc,r): r['urlpath'] = r['urlpath'].strip('/') try: - room = Room.objects.get(sco_id=r['sco_id'],acc=acc) - for key in ('sco_id','name','source_sco_id','urlpath','description','user_count','host_count'): + 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]: @@ -286,16 +283,15 @@ def _import_room(request,acc,r): room.save() except ObjectDoesNotExist: - if r['folder_sco_id']: + if r['folder_sco']: try: - room = Room.objects.create(sco_id=r['sco_id'], - source_sco_id=r['source_sco_id'], - acc=acc, + 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_id=r['folder_sco_id']) + folder_sco=r['folder_sco']) except Exception,e: room = None pass @@ -305,7 +301,7 @@ def _import_room(request,acc,r): logging.debug("+++ looking at user counts") with ac_api_client(acc) as api: - userlist = api.request('meeting-usermanager-user-list',{'sco-id': room.sco_id},False) + 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'])")) @@ -331,35 +327,6 @@ def list_rooms(request,username=None): {'title':'Your Rooms','edit':True,'active':len(rooms) == 1,'rooms':rooms}) @login_required -def user_rooms(request,user=None): - if user is None: - user = request.user - - acc = acc_for_user(user) - my_meetings_sco_id = _user_meeting_folder(request,acc) - user_rooms = _user_rooms(request,acc,my_meetings_sco_id) - - ar = [] - for r in user_rooms: - logging.debug(pformat(r)) - ar.append(int(r['sco_id'])) - - for r in Room.objects.filter(creator=user).all(): - if not r.sco_id in ar: # and (not r.self_cleaning): #XXX this logic isn't right! - for t in Tag.objects.get_for_object(r): - t.delete() - r.delete() - - for r in user_rooms: - r['folder_sco_id'] = my_meetings_sco_id - room = _import_room(request,acc,r) - - rooms = Room.objects.filter(creator=user).order_by('name').all() - 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() @@ -371,28 +338,37 @@ def delete(request,id): if request.method == 'POST': form = DeleteRoomForm(request.POST) if form.is_valid(): - with ac_api_client(room.acc) as api: - api.request('sco-delete',{'sco-id':room.sco_id},raise_error=True) + with ac_api_client(room.sco.acc) as api: + api.request('sco-delete',{'sco-id':room.sco.sco_id},raise_error=True) clear_acl(room) + del room.sco + if room.folder_sco is not None: + del room.folder_sco + if room.deleted_sco is not None: + del room.deleted_sco room.delete() - return redirect_to("/rooms") else: form = DeleteRoomForm() - return respond_to(request,{'text/html':'edit.html'},{'form':form,'formtitle': 'Delete %s' % room.name,'cancelname':'Cancel','submitname':'Delete Room'}) + 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.acc) as api: - room.deleted_sco_id = room.sco_id + with ac_api_client(room.sco.acc) as api: + room.deleted_sco = room.sco room.save() - api.request('sco-delete',{'sco-id':room.sco_id},raise_error=False) - room.sco_id = None + 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.acc) as api: + 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, @@ -433,7 +409,7 @@ def _goto(request,room,clean=True,promote=False): lastvisit = room.lastvisit() room.lastvisited = datetime.now() - with ac_api_client(room.acc) as api: + 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 @@ -455,22 +431,22 @@ def _goto(request,room,clean=True,promote=False): key = _random_key(20) user_principal = api.find_user(request.user.username) principal_id = user_principal.get('principal-id') - with ac_api_client(room.acc) as api: + 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_id,'principal-id': user_principal.get('principal-id'),'permission-id':'host'},True) + 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_id},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.acc.api_url, request.user.username, key, cache=False) - return user_client.redirect_to(room.acc.url+urlpath) + 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.acc.url+urlpath) + return HttpResponseRedirect(room.sco.acc.url+urlpath) ## Tagging @@ -499,23 +475,6 @@ def list_by_tag(request,tn): 'tagstring': tn, 'rooms':rooms}) -# should not require login -def list_and_import_by_tag(request,tn): - tags = tn.split('+') - rooms = TaggedItem.objects.get_by_model(Room, tags).order_by('name').all() - for room in rooms: - _import_room(request,room.acc,{'sco_id': room.sco_id}) - title = 'Rooms tagged with %s' % " and ".join(tags) - return respond_to(request, - {'text/html':'apps/room/list.html', - 'application/json': json_response([_room2dict(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' @@ -560,19 +519,37 @@ def tag(request,rid): tn = "+".join([t.name for t in tags]) return respond_to(request, {'text/html': "apps/room/tag.html"}, - {'form': form,'formtitle': 'Add Tag','cancelname':'Done','submitname': 'Add Tag','room': room, 'tagstring': tn,'tags': tags}) + {'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): - with ac_api_client(room.acc) as api: - r = api.request('sco-expanded-contents',{'sco-id': room.sco_id,'filter-icon':'archive'},True) - return [{'name': sco.findtext('name'), - 'sco_id': sco.get('sco-id'), - 'url': room.acc.make_url(sco.findtext('url-path')), - 'dl': room.acc.make_dl_url(sco.findtext('url-path')), - 'description': sco.findtext('description'), - 'date_created': iso8601.parse_date(sco.findtext('date-created')), - 'date_modified': iso8601.parse_date(sco.findtext('date-modified'))} for sco in r.et.findall(".//sco")] - + 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() + ] @login_required def recordings(request,rid): room = get_object_or_404(Room,pk=rid) |