Changeset 119b7d in indico


Ignore:
Timestamp:
10/18/09 23:31:20 (4 years ago)
Author:
David Martín Clavo <david.martin.clavo@…>
Branches:
master, burotel, hello-world-walkthrough, ipv6, new-webex, prov-dual-interface, v0.97-series, v0.98-series, v0.98.2, v0.98.3, v0.98b1, v0.98b2, v0.99, 051b2622c51afb171a1dedb46a0df4fbb0cbd02e, d9941f8582b36b24821a11ea5ba16fda6a457fb1
Children:
f4ad5a
Parents:
498147
git-author:
David Martín Clavo <david.martin.clavo@…> (10/07/09 09:49:13)
git-committer:
David Martín Clavo <david.martin.clavo@…> (10/18/09 23:31:20)
Message:

[REFACTOR] New observer mechanism, simpler and more escalable

Location:
indico/MaKaC
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • indico/MaKaC/conference.py

    re83ea5 r119b7d  
    20082008        self._sortUrlTag = "" 
    20092009 
    2010         self._titleChangeObservers = [] 
    2011         self._dateChangeObservers = [] 
    2012         self._deleteObservers = [] 
     2010        self._observers = [] 
    20132011 
    20142012        if PluginsHolder().hasPluginType("Collaboration"): 
     
    26762674        """deletes the conference from the system. 
    26772675        """ 
    2678         #we notify the observers that the conference 
    2679         for observer in self.getDeleteObservers(): 
     2676        #we notify the observers that the conference has been deleted 
     2677        for observer in self.getObservers(): 
    26802678            try: 
    26812679                observer.notifyDeletion() 
     
    27072705        indexes.IndexesHolder().getById("OAIPrivateConferenceModificationDate").unindexConference(self) 
    27082706 
    2709     def getDeleteObservers(self): 
    2710         if not hasattr(self, "_deleteObservers"): 
    2711             self._deleteObservers = [] 
    2712         return self._deleteObservers 
    2713  
    2714     def addDeleteObserver(self, observer): 
    2715         self.getDeleteObservers().append(observer) 
    2716         self._p_changed=1 
    2717  
    2718     def removeDeleteObserver(self, observer): 
    2719         self.getDeleteObservers().remove(observer) 
    2720         self._p_changed=1 
    2721      
    2722      
    2723  
    27242707    def getConference( self ): 
    27252708        return self 
     2709     
     2710    def getObservers(self): 
     2711        if not hasattr(self, "_observers"): 
     2712            self._observers = [] 
     2713        return self._observers 
     2714 
     2715    def addObserver(self, observer): 
     2716        self.getObservers().append(observer) 
     2717        self._p_changed=1 
     2718 
     2719    def removeObserver(self, observer): 
     2720        self.getObservers().remove(observer) 
     2721        self._p_changed=1 
    27262722 
    27272723    def setDates( self, sDate, eDate=None, check=1, moveEntries=1 ): 
     
    27412737        self.cleanCategoryCache() 
    27422738         
    2743         for observer in self.getDateChangeObservers(): 
     2739        for observer in self.getObservers(): 
    27442740            try: 
    27452741                observer.notifyEventDateChanges(oldStartDate, self.getStartDate(), oldEndDate, self.getEndDate()) 
     
    27972793        #if everything went well, we notify the observers that the start date has changed 
    27982794        if notifyObservers: 
    2799             for observer in self.getDateChangeObservers(): 
     2795            for observer in self.getObservers(): 
    28002796                try: 
    28012797                    observer.notifyEventDateChanges(oldSdate, sDate, None, None) 
     
    28242820        #if everything went well, we notify the observers that the start date has changed 
    28252821        if notifyObservers: 
    2826             for observer in self.getDateChangeObservers(): 
     2822            for observer in self.getObservers(): 
    28272823                try: 
    28282824                    observer.notifyEventDateChanges(sdate, self.startDate, None, None) 
     
    29122908        #if everything went well, we notify the observers that the start date has changed 
    29132909        if notifyObservers: 
    2914             for observer in self.getDateChangeObservers(): 
     2910            for observer in self.getObservers(): 
    29152911                try: 
    29162912                    observer.notifyEventDateChanges(None, None, oldEdate, eDate) 
     
    29322928        #if everything went well, we notify the observers that the start date has changed 
    29332929        if notifyObservers: 
    2934             for observer in self.getDateChangeObservers(): 
     2930            for observer in self.getObservers(): 
    29352931                try: 
    29362932                    observer.notifyEventDateChanges(None, None, edate, self.endDate) 
     
    29622958    ################################## 
    29632959 
    2964     def getDateChangeObservers(self): 
    2965         if not hasattr(self, "_dateChangeObservers"): 
    2966             self._dateChangeObservers = [] 
    2967         return self._dateChangeObservers 
    2968  
    2969     def addDateChangeObserver(self, observer): 
    2970         self.getDateChangeObservers().append(observer) 
    2971         self._p_changed=1 
    2972  
    2973     def removeDateChangeObserver(self, observer): 
    2974         self.getDateChangeObservers().remove(observer) 
    2975         self._p_changed=1 
    2976  
    29772960    def setScreenEndDate(self, date): 
    29782961        if date == self.getEndDate(): 
     
    30082991        oldTimezone = self.timezone 
    30092992        self.timezone = tz 
    3010         for observer in self.getDateChangeObservers(): 
    3011             observer.notifyTimezoneChange(oldTimezone, tz) 
     2993         
     2994        for observer in self.getObservers(): 
     2995            try: 
     2996                observer.notifyTimezoneChange(oldTimezone, tz) 
     2997            except Exception, e: 
     2998                try: 
     2999                    Logger.get('Conference').error("Exception while notifying the observer %s of a timezone change from %s to %s for conference %s: "% 
     3000                                                   (observer.getObserverName(), str(oldTimezone), str(tz), self.getId(), str(e))) 
     3001                except Exception, e2: 
     3002                    Logger.get('Conference').error("Exception while notifying a timezone change: %s (origin: %s)"%(str(e2), str(e))) 
    30123003 
    30133004    def getTimezone(self): 
     
    30523043    def setTitle(self, title): 
    30533044        """changes the current title of the conference to the one specified""" 
     3045        oldTitle = self.title 
    30543046         
     3047        self.title = title 
     3048        self.cleanCategoryCache() 
     3049        self.notifyModification() 
    30553050         
    30563051        #we notify the observers that the conference's title has changed 
    3057         for observer in self.getTitleChangeObservers(): 
     3052        for observer in self.getObservers(): 
    30583053            try: 
    3059                 observer.notifyTitleChange(self.title, title) 
     3054                observer.notifyTitleChange(oldTitle, title) 
    30603055            except Exception, e: 
    30613056                try: 
     
    30643059                except Exception, e2: 
    30653060                    Logger.get('Conference').error("Exception while notifying a conference title change: %s (origin: %s)"%(str(e2), str(e))) 
    3066          
    3067         self.title = title 
    3068         self.cleanCategoryCache() 
    3069         self.notifyModification() 
    3070  
    3071     def getTitleChangeObservers(self): 
    3072         if not hasattr(self, "_titleChangeObservers"): 
    3073             self._titleChangeObservers = [] 
    3074         return self._titleChangeObservers 
    3075  
    3076     def addTitleChangeObserver(self, observer): 
    3077         self.getTitleChangeObservers().append(observer) 
    3078         self._p_changed=1 
    3079  
    3080     def removeTitleChangeObserver(self, observer): 
    3081         self.getTitleChangeObservers().remove(observer) 
    3082         self._p_changed=1 
    30833061 
    30843062    def getDescription(self): 
     
    49314909class Observer(object): 
    49324910    """ Base class for Observer objects. 
    4933         Provides the getObserverName method (used when writing info to the Indico log) 
     4911        Inheriting classes should overload the following boolean class attributes: 
     4912            _shouldBeTitleNotified 
     4913            _shouldBeDateChangeNotified 
     4914            _shouldBeDeletionNotified 
     4915        And set them to True if they want to be notified of the corresponding event. 
     4916        In that case, they also have to implement the corresponding methods: 
     4917            _notifyTitleChange (for title notification) 
     4918            _notifyEventDateChanges and _notifyTimezoneChange (for date / timezone notification) 
     4919            _notifyDeletion (for deletion notification). 
     4920        The interface for those methods is also specified in this class. If the corresponding 
     4921        class attribute is set to False but the method is not implemented, an exception will be thrown. 
    49344922    """ 
     4923    _shouldBeTitleNotified = False 
     4924    _shouldBeDateChangeNotified = False 
     4925    _shouldBeDeletionNotified = False 
     4926     
    49354927    def getObserverName(self): 
    49364928        name = "'Observer of class" + self.__class__.__name__ 
     
    49414933            pass 
    49424934        return name 
     4935     
     4936    def notifyTitleChange(self, oldTitle, newTitle): 
     4937        if self._shouldBeTitleNotified: 
     4938            self._notifyTitleChange(oldTitle, newTitle) 
     4939             
     4940    def notifyEventDateChanges(self, oldStartDate = None, newStartDate = None, oldEndDate = None, newEndDate = None): 
     4941        if self._shouldBeDateChangeNotified: 
     4942            self._notifyEventDateChanges(oldStartDate, newStartDate, oldEndDate, newEndDate) 
     4943             
     4944    def notifyTimezoneChange(self, oldTimezone, newTimezone): 
     4945        if self._shouldBeDateChangeNotified: 
     4946            self._notifyTimezoneChange(oldTimezone, newTimezone) 
     4947             
     4948    def notifyDeletion(self): 
     4949        if self._shouldBeDeletionNotified: 
     4950            self._notifyDeletion() 
     4951             
     4952    def _notifyTitleChange(self, oldTitle, newTitle):  
     4953        """ To be implemented by inheriting classes 
     4954            Notifies the observer that the Conference object's title has changed 
     4955        """ 
     4956        raise MaKaCError("Class " + str(self.__class__.__name__) + " did not implement method _notifyTitleChange") 
     4957     
     4958    def _notifyEventDateChanges(self, oldStartDate, newStartDate, oldEndDate, newEndDate): 
     4959        """ To be implemented by inheriting classes 
     4960            Notifies the observer that the start and / or end dates of the object it is attached to has changed. 
     4961            If the observer finds any problems during whatever he needs to do as a consequence of 
     4962            the event dates changing, he should write strings describing the problems 
     4963            in the 'dateChangeNotificationProblems' context variable (which is a list of strings). 
     4964        """ 
     4965        raise MaKaCError("Class " + str(self.__class__.__name__) + " did not implement method notifyStartDateChange") 
     4966     
     4967    def _notifyTimezoneChange(self, oldTimezone, newTimezone): 
     4968        """ To be implemented by inheriting classes. 
     4969            Notifies the observer that the end date of the object it is attached to has changed. 
     4970            This method has to return a list of strings describing problems encountered during 
     4971            whatever the DateChangeObserver object does as a consequence of the notification. 
     4972            If there are no problems, the DateChangeObserver should return an empty list. 
     4973        """ 
     4974        raise MaKaCError("Class " + str(self.__class__.__name__) + " did not implement method notifyTimezoneChange") 
     4975     
     4976    def _notifyDeletion(self): 
     4977        """ To be implemented by inheriting classes 
     4978            Notifies the observer that the Conference object it is attached to has been deleted 
     4979        """ 
     4980        raise MaKaCError("Class " + str(self.__class__.__name__) + " did not implement method notifyDeletion") 
    49434981 
    49444982class TitleChangeObserver(Observer): 
  • indico/MaKaC/plugins/Collaboration/base.py

    r34a768 r119b7d  
    4747    """ 
    4848     
     49    _shouldBeTitleNotified = True 
     50    _shouldBeDateChangeNotified = True 
     51    _shouldBeDeletionNotified = True 
     52     
    4953    def __init__(self, conf): 
    5054        """ Constructor for the CSBookingManager class. 
     
    6569        self._managers = {} 
    6670         
    67         #we register as a date change observer of the conference 
    68         self._conf.addTitleChangeObserver(self) 
    69          
    70         #we register as a date change observer of the conference 
    71         self._conf.addDateChangeObserver(self) 
    72          
    73         #we register as a delete observer of the conference 
    74         self._conf.addDeleteObserver(self) 
     71        #we register as an observer of the conference 
     72        self._conf.addObserver(self) 
    7573 
    7674    def getOwner(self): 
     
    8078     
    8179    def restoreAsObserver(self): 
    82         if not self in self._conf.getTitleChangeObservers(): 
    83             self._conf.addTitleChangeObserver(self) 
    84         if not self in self._conf.getDateChangeObservers(): 
    85             self._conf.addDateChangeObserver(self) 
    86         if not self in self._conf.getDeleteObservers(): 
    87             self._conf.addDeleteObserver(self) 
     80        if not self in self._conf.getObservers(): 
     81            self._conf.addObserver(self) 
    8882     
    8983    def isCSAllowed(self): 
Note: See TracChangeset for help on using the changeset viewer.