[FIX] Method naming
[cds-indico.git] / indico / MaKaC / plugins / Collaboration / components.py
blob4cf35f21e33b55b3b948af8f90adc8a58cdeb913
1 # -*- coding: utf-8 -*-
2 ##
3 ##
4 ## This file is part of CDS Indico.
5 ## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 CERN.
6 ##
7 ## CDS Indico is free software; you can redistribute it and/or
8 ## modify it under the terms of the GNU General Public License as
9 ## published by the Free Software Foundation; either version 2 of the
10 ## License, or (at your option) any later version.
12 ## CDS Indico is distributed in the hope that it will be useful, but
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ## General Public License for more details.
17 ## You should have received a copy of the GNU General Public License
19 ## along with CDS Indico; if not, write to the Free Software Foundation, Inc.,
20 ## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 import zope.interface
24 from MaKaC.common.utils import *
25 from MaKaC.conference import Conference, Contribution
26 from MaKaC.rb_location import IIndexableByManagerIds
27 from MaKaC.plugins.Collaboration.collaborationTools import CollaborationTools
28 from MaKaC.plugins.Collaboration.indexes import CSBookingInstanceWrapper
29 from MaKaC.common.logger import Logger
30 from MaKaC.common.indexes import IndexesHolder
33 from indico.util.event import uid_to_obj
34 from indico.core.index import OOIndex, Index
35 from indico.core.index.adapter import IIndexableByStartDateTime
36 from indico.core.extpoint import Component, IListener
37 from indico.core.extpoint.events import IObjectLifeCycleListener, ITimeActionListener, \
38 IMetadataChangeListener
39 from indico.core.extpoint.location import ILocationActionListener
40 from indico.core.extpoint.index import ICatalogIndexProvider
41 from indico.core.index import Catalog
43 class CSBookingInstanceIndexCatalog(Index):
45 def __init__(self):
46 self._container = {}
48 def __getitem__(self, key):
49 return self._container[key]
51 def __setitem__(self, key, value):
52 self._container[key] = value
54 def initialize(self, dbi=None):
55 for index in ['WebcastRequest', 'RecordingRequest', 'All Requests']:
56 idx = CSBookingInstanceIndex(index)
57 idx.initialize()
58 self._container[index] = idx
60 def getIndexes(self):
61 return self._container.values()
64 class CSBookingInstanceIndex(OOIndex):
66 def __init__(self, index_name):
67 super(CSBookingInstanceIndex, self).__init__(IIndexableByStartDateTime)
68 self.index_name = index_name
70 def initialize(self, dbi=None):
71 # empty tree
72 self.clear()
74 idx = IndexesHolder().getById('collaboration')
76 for conf, bks in idx.getBookings(self.index_name, 'conferenceStartDate', None, None, None).getResults():
77 for bk in bks:
78 self.index_booking(bk)
80 def index_booking(self, bk):
81 conf = bk.getConference()
82 contribs = bk.getTalkSelectionList()
83 choose = bk.isChooseTalkSelected()
84 if choose:
85 if contribs:
86 for contrib_id in contribs:
87 if CollaborationTools.isAbleToBeWebcastOrRecorded(conf.getContributionById(contrib_id), bk.getType()):
88 self.index_obj(CSBookingInstanceWrapper(bk, conf.getContributionById(contrib_id)))
89 # We need to check if it is a lecture with a correct room
90 elif CollaborationTools.isAbleToBeWebcastOrRecorded(conf, bk.getType()) or conf.getType() !="simple_event":
91 for day in daysBetween(conf.getStartDate(), conf.getEndDate()):
92 bkw = CSBookingInstanceWrapper(bk, conf,
93 day.replace(hour=0, minute=0, second=0),
94 day.replace(hour=23, minute=59, second=59))
95 self.index_obj(bkw)
97 def iter_bookings(self, fromDT, toDT):
99 added_whole_events = set()
101 for dt, bkw in self.iteritems(fromDT, toDT):
102 evt = bkw.getOriginalBooking().getConference()
103 entries = evt.getSchedule().getEntriesOnDay(dt)
105 if bkw.getObject() == evt:
106 # this means the booking relates to an event
107 if evt in added_whole_events:
108 continue
110 if not evt.getSchedule().getEntries():
111 yield dt, CSBookingInstanceWrapper(bkw.getOriginalBooking(),
112 evt)
113 # mark whole event as "added"
114 added_whole_events.add(evt)
116 if entries:
117 # what a mess...
118 if self.index_name == 'All Requests':
119 talks = set(CollaborationTools.getCommonTalkInformation(evt, 'RecordingRequest', 'recordingCapableRooms')[3]) | \
120 set(CollaborationTools.getCommonTalkInformation(evt, 'WebcastRequest', 'webcastCapableRooms')[3])
121 else:
122 var = 'recordingCapableRooms' if self.index_name == 'RecordingRequest' else 'webcastCapableRooms'
123 talks = CollaborationTools.getCommonTalkInformation(evt, self.index_name, var)[3]
125 # add contribs that concern this day
126 for contrib in talks:
127 if contrib.isScheduled() and contrib.getStartDate().date() == dt.date():
128 yield dt, CSBookingInstanceWrapper(bkw.getOriginalBooking(),
129 contrib)
130 else:
131 yield dt, bkw
133 def unindex_booking(self, bk, fromDT=None, toDT=None):
134 to_unindex = set()
135 # go over possible wrappers
136 conf = bk.getConference()
138 for dt, bkw in self.iteritems((fromDT or conf.getStartDate()).replace(hour=0, minute=0, second=0),
139 (toDT or conf.getEndDate()).replace(hour=23, minute=59, second=59)):
140 if bkw.getOriginalBooking() == bk:
141 to_unindex.add(bkw)
143 for bkw in to_unindex:
144 self.unindex_obj(bkw)
146 def unindex_talk(self, bk, talk):
147 to_unindex = set()
148 bookingList = self.get(talk.getStartDate(), [])
149 for bkw in bookingList:
150 if bkw.getObject() == talk:
151 to_unindex.add(bkw)
152 for bkw in to_unindex:
153 self.unindex_obj(bkw)
155 def index_talk(self, bk, talk):
156 self.index_obj(CSBookingInstanceWrapper(bk, talk))
158 class CatalogIndexProvider(Component):
159 zope.interface.implements(ICatalogIndexProvider)
161 def catalogIndexProvider(self, obj):
162 return [('cs_booking_instance', CSBookingInstanceIndexCatalog)]
165 class EventCollaborationListener(Component):
167 zope.interface.implements(IObjectLifeCycleListener,
168 ITimeActionListener,
169 ILocationActionListener,
170 IMetadataChangeListener)
172 """In this case, obj is a conference object. Since all we
173 need is already programmed in CSBookingManager, we get the
174 object of this class and we call the appropiate method"""
175 @classmethod
176 def dateChanged(cls, obj, params={}):
177 obj = obj.getCSBookingManager()
178 try:
179 obj.notifyEventDateChanges(params['oldStartDate'], params['newStartDate'], params['oldEndDate'], params['newEndDate'])
180 except Exception, e:
181 Logger.get('PluginNotifier').error("Exception while trying to access to the date parameters when changing an event date" + str(e))
183 @classmethod
184 def startDateChanged(cls, obj, params={}):
185 obj = obj.getCSBookingManager()
186 try:
187 obj.notifyEventDateChanges(params['oldDate'], params['newDate'], None, None)
188 except Exception, e:
189 Logger.get('PluginNotifier').error("Exception while trying to access to the date parameters when changing an event date" + str(e))
191 @classmethod
192 def startTimeChanged(self, obj, sdate):
193 """ No one is calling this method in the class Conference. Probably this is completely unnecessary"""
194 bookingManager = obj.getCSBookingManager()
195 bookingManager.notifyEventDateChanges(sdate, obj.startDate, None, None)
197 @classmethod
198 def endTimeChanged(self, obj, edate):
199 """ No one is calling this method in the class Conference. Probably this is completely unnecessary"""
200 bookingManager = obj.getCSBookingManager()
201 bookingManager.notifyEventDateChanges(None, None, edate, obj.endDate)
203 @classmethod
204 def timezoneChanged(self, obj, oldTimezone):
205 bookingManager = obj.getCSBookingManager()
206 #ATTENTION! At this moment this method notifyTimezoneChange in the class CSBookingManager
207 #just returns [], so if you're expecting it to do something just implement whatever you
208 #want to do with it
209 bookingManager.notifyTimezoneChange(oldTimezone, obj.timezone)
211 @classmethod
212 def endDateChanged(cls, obj, params={}):
213 obj = obj.getCSBookingManager()
214 try:
215 obj.notifyEventDateChanges(None, None, params['oldDate'], params['newDate'])
216 except Exception, e:
217 Logger.get('PluginNotifier').error("Exception while trying to access to the date parameters when changing an event date" + str(e))
219 @classmethod
220 def eventDatesChanged(cls, obj, oldStartDate, oldEndDate,
221 newStartDate, newEndDate):
222 obj = obj.getCSBookingManager()
223 try:
224 obj.notifyEventDateChanges(oldStartDate, newStartDate,
225 oldEndDate, newEndDate)
226 except Exception, e:
227 Logger.get('PluginNotifier').error("Exception while trying to access to the date parameters when changing an event date" + str(e))
229 @classmethod
230 def contributionUnscheduled(self, contrib):
231 if contrib.getStartDate() is not None:
232 csBookingManager = contrib.getCSBookingManager()
233 for booking in csBookingManager.getBookingList():
234 booking.unindex_talk(contrib)
236 @classmethod
237 def contributionScheduled(self, contrib):
238 csBookingManager = contrib.getCSBookingManager()
239 for booking in csBookingManager.getBookingList():
240 booking.index_talk(contrib)
242 @classmethod
243 def eventTitleChanged(cls, obj, oldTitle, newTitle):
245 obj = obj.getCSBookingManager()
246 try:
247 obj.notifyTitleChange(oldTitle, newTitle)
248 except Exception, e:
249 Logger.get('PluginNotifier').error("Exception while trying to access to the title parameters when changing an event title" + str(e))
251 @classmethod
252 def infoChanged(cls, obj):
253 #Update Speaker Wrapper only if obj is a Conference
254 if isinstance(obj, Conference) or isinstance(obj, Contribution):
255 bookingManager = obj.getCSBookingManager()
257 try:
258 bookingManager.notifyInfoChange()
259 except Exception, e:
260 Logger.get('PluginNotifier').error("Exception while trying to access the info changes " + str(e))
263 @classmethod
264 def deleted(cls, obj, params={}):
265 if obj.__class__ == Conference:
266 obj = obj.getCSBookingManager()
267 obj.notifyDeletion()
268 elif obj.__class__ == Contribution:
269 csBookingManager = obj.getCSBookingManager()
270 for booking in csBookingManager.getBookingList():
271 booking.unindex_talk(obj)
273 # ILocationActionListener
274 @classmethod
275 def placeChanged(cls, obj):
276 obj = obj.getCSBookingManager()
277 obj.notifyLocationChange()
279 @classmethod
280 def locationChanged(cls, obj, oldLocation, newLocation):
281 csBookingManager = obj.getCSBookingManager()
282 for booking in csBookingManager.getBookingList():
283 booking.unindex_talk(obj)
284 booking.index_talk(obj)
286 @classmethod
287 def roomChanged(cls, obj, oldLocation, newLocation):
288 csBookingManager = obj.getCSBookingManager()
289 for booking in csBookingManager.getBookingList():
290 booking.unindex_talk(obj)
291 booking.index_talk(obj)