summaryrefslogtreecommitdiff
path: root/src/meetingtools/apps/room/views.py
blob: 65e085b96f61b8bc3f311583e5a5c42429240f41 (plain)
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
'''
Created on Jan 31, 2011

@author: leifj
'''
from meetingtools.apps.room.models import Room, ACCluster
from meetingtools.multiresponse import respond_to, redirect_to
from meetingtools.apps.room.forms import UpdateRoomForm, DeleteRoomForm
from django.shortcuts import get_object_or_404
from meetingtools.ac import ac_api_client, api
import re
from meetingtools.apps import room
from django.contrib.auth.decorators import login_required
from meetingtools.apps.auth.utils import groups
import logging
from pprint import pformat
from meetingtools.utils import session
import time
from meetingtools.settings import GRACE
from django.utils.datetime_safe import datetime
from django.http import HttpResponseRedirect
from django.core.exceptions import ObjectDoesNotExist

def _acc_for_user(user):
    (local,domain) = user.username.split('@')
    if not domain:
        #raise Exception,"Improperly formatted user: %s" % user.username
        domain = "nordu.net" # testing with local accts only
    for acc in ACCluster.objects.all():
        for regex in acc.domain_match.split():
            if re.match(regex,domain):
                return acc
    raise Exception,"I don't know which cluster you belong to... (%s)" % user.username

def _user_meeting_folder(request,acc):
    if not session(request,'my_meetings_sco_id'):
        connect_api = ac_api_client(request, acc)
        userid = request.user.username
        folders = connect_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 _shared_templates_folder(request,acc):
    if not session(request,'shared_templates_sco_id'):
        connect_api = ac_api_client(request, acc)
        shared = connect_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:
        connect_api = ac_api_client(request, acc)
        meetings = connect_api.request('sco-expanded-contents',{'sco-id': my_meetings_sco_id,'filter-type': 'meeting'})
        if meetings:
            rooms = [(r.get('sco-id'),r.findtext('name'),r.get('source-sco-id'),r.findtext('url-path')) for r in meetings.et.findall('.//sco')]
    return rooms

def _user_templates(request,acc,my_meetings_sco_id):
    templates = []
    connect_api = ac_api_client(request, acc)
    if my_meetings_sco_id:
        my_templates = connect_api.request('sco-contents',{'sco-id': my_meetings_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 = connect_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')]
    
    shared_templates_sco_id = _shared_templates_folder(request, acc)
    if shared_templates_sco_id:
        shared_templates = connect_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')]
            
    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)
    api = ac_api_client(request,room.acc)
    room_info = api.request('sco-info',{'sco-id':room.sco_id},raise_error=True)
    perm_info = api.request('permissions-info',{'acl-id':room.sco_id,'filter-principal-id': 'public-access'},raise_error=True)
    session_info = api.request('report-meeting-sessions',{'sco-id':room.sco_id},raise_error=True)
    
    room.name = room_info.et.findtext('.//sco/name')
    room.save()
    return respond_to(request,
                      {'text/html':'apps/room/view.html'},
                      {'user':request.user,
                       'room':room,
                       'permission':  perm_info.et.find('.//principal').get('permission-id'),       
                       'nusers': _nusers(session_info)
                       })

def _init_update_form(request,form,acc,my_meetings_sco_id):
    form.fields['participants'].widget.choices = [('','-- anyone --')]+[(g,g) for g in groups(request)]
    form.fields['presenters'].widget.choices = [('','-- nobody --')]+[(g,g) for g in groups(request)]
    form.fields['hosts'].widget.choices = [('','-- nobody --')]+[(g,g) for g in groups(request)]
    form.fields['source_sco_id'].widget.choices = [('','-- select template --')]+[r for r in _user_templates(request,acc,my_meetings_sco_id)]

@login_required
def _update_room(request, room):
    api = ac_api_client(request, room.acc)
    params = {'type':'meeting', 
              'name':room.name, 
              'folder-id': room.folder_sco_id, 
              'source-sco-id': room.source_sco_id, 
              'url-path':room.urlpath}
    
    if room.sco_id:
        params['sco-id'] = room.sco_id
    r = api.request('sco-update', params, raise_error=True)
    
    room.sco_id = r.et.find(".//sco").get('sco-id')
    room.source_sco_id = r.et.find(".//sco").get('sco-source-id')
    room.save()
    #room = _import_room(params['sco-id'], params['name'], params['source-sco-id'], params['url-path'], request.user, acc)
    return room

@login_required
def update(request,id=None):
    if id:
        room = get_object_or_404(Room,pk=id)
        acc = room.acc
        what = "Update"
        title = "Modify %s" % room.name
        formname = "update"
        update = True
    else:
        acc = _acc_for_user(request.user)
        my_meetings_sco_id = _user_meeting_folder(request,acc)
        room = Room(creator=request.user,acc=acc,folder_sco_id=my_meetings_sco_id)
        what = "Create"
        formname = "create"
        title = "Create a new room"
        update = False
    
    if request.method == 'POST':
        form = UpdateRoomForm(request.POST,instance=room)
        _init_update_form(request, form, acc, room.folder_sco_id)
        if form.is_valid():
            room = _update_room(request, room)
            room = form.save()
            return redirect_to("/rooms#%d" % room.id)
    else:
        form = UpdateRoomForm(instance=room)
        _init_update_form(request, form, acc, room.folder_sco_id)
        if update:
            form.fields['urlpath'].widget.attrs['readonly'] = True
        
    return respond_to(request,{'text/html':'apps/room/%s.html' % formname},{'form':form,'formtitle': title,'submitname':'%s Room' % what})

def _import_room(request,acc,sco_id,source_sco_id,folder_sco_id,name,urlpath):
    modified = False
    try:
        room = Room.objects.get(sco_id=sco_id,acc=acc)
    except ObjectDoesNotExist:
        room = Room.objects.create(sco_id=sco_id,acc=acc,creator=request.user,folder_sco_id=folder_sco_id)
        
    logging.debug(pformat(room))
    
    if room.name != name and name:
        room.name = name
        modified = True
    
    if room.sco_id != sco_id and sco_id:
        room.sco_id = sco_id
        modified = True
    
    if not room.source_sco_id and source_sco_id:
        room.source_sco_id = source_sco_id
        modified = True
        
    if urlpath:
        urlpath = urlpath.strip('/')
        
    if room.urlpath != urlpath and urlpath:
        room.urlpath = urlpath
        modified = True
        
    #if '/' in room.urlpath:
    #    room.urlpath = urlpath.strip('/')
    #    modified = True
    
    logging.debug(pformat(room))
        
    if modified:
        logging.debug("saving ... %s" % pformat(room))
        room.save()
    
    return room

@login_required
def list(request):
    acc = _acc_for_user(request.user)
    my_meetings_sco_id = _user_meeting_folder(request,acc)
    user_rooms = _user_rooms(request,acc,my_meetings_sco_id)
    
    ar = []
    for (sco_id,name,source_sco_id,urlpath) in user_rooms:
        logging.debug("%s %s %s %s" % (sco_id,name,source_sco_id,urlpath))
        room = _import_room(request,acc,sco_id,source_sco_id,my_meetings_sco_id,name,urlpath)
        ar.append(int(sco_id))
    
    #logging.debug(pformat(ar))
        
    for r in Room.objects.filter(creator=request.user).all():
        #logging.debug(pformat(r))
        if (not r.sco_id in ar) and (not r.self_cleaning):
            r.delete() 
    return respond_to(request,{'text/html':'apps/room/list.html'},{'user':request.user,'rooms':Room.objects.filter(creator=request.user).all()})

def rooms_by_group(request,group):
    for room in Room.objects.filter(participants=group):
        pass

@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():
            api = ac_api_client(request,room.acc)
            api.request('sco-delete',{'sco-id':room.sco_id},raise_error=True)
            room.delete()
            return redirect_to("/rooms")
    else:
        form = DeleteRoomForm()
        
    return respond_to(request,{'text/html':'edit.html'},{'form':form,'formtitle': 'Delete %s' % room.name,'submitname':'Delete Room'})      

def _clean(request,room):
    api = ac_api_client(request, room.acc)
    api.request('sco-delete',{'sco-id':room.sco_id},raise_error=True)
    room.sco_id = None
    return _update_room(request, room)

def go_by_id(request,id):
    room = get_object_or_404(Room,pk=id)
    return goto(request,room)

def go_by_path(request,path):
    room = get_object_or_404(Room,urlpath=path)
    return goto(request,room)
        
def goto(request,room):
    api = ac_api_client(request, room.acc)
    session_info = api.request('report-meeting-sessions',{'sco-id':room.sco_id})
    
    now = time.time()
    if room.self_cleaning:
        if (_nusers(session_info) == 0) and (abs(room.lastvisit() - now) > GRACE):
            room = _clean(request,room)
    
    room.lastvisited = datetime.now()
    room.save()
    
    r = api.request('sco-info',{'sco-id':room.sco_id})
    urlpath = r.et.findtext('.//sco/url-path')
    return HttpResponseRedirect(room.acc.url+urlpath) #in this case we want the absolute URL