示例#1
0
class Calendar(Persistent, CalendarMixin):
    """A persistent calendar."""

    # CalendarMixin is only used for the expand() method

    implements(ISchoolToolCalendar, IAttributeAnnotatable)

    __name__ = "calendar"

    title = property(lambda self: self.__parent__.title)

    def __init__(self, owner):
        self.events = PersistentDict()
        self.__parent__ = owner

    def __iter__(self):
        return self.events.itervalues()

    def __len__(self):
        return len(self.events)

    def addEvent(self, event):
        assert ISchoolToolCalendarEvent.providedBy(event)
        if event.unique_id in self.events:
            raise ValueError("an event with this unique_id already exists")
        if event.__parent__ is None:
            for resource in event.resources:
                if ISchoolToolCalendar(resource) is self:
                    raise ValueError("cannot book itself")
            event.__parent__ = self
            for resource in event.resources:
                ISchoolToolCalendar(resource).addEvent(event)
        elif self.__parent__ not in event.resources:
            raise ValueError("Event already belongs to a calendar")
        self.events[event.unique_id] = event

    def removeEvent(self, event):
        if self.__parent__ in event.resources:
            event.unbookResource(self.__parent__)
        else:
            del self.events[event.unique_id]
            parent_calendar = event.__parent__
            if self is parent_calendar:
                for resource in event.resources:
                    event.unbookResource(resource)
                event.__parent__ = None

    def clear(self):
        # clear is not actually used anywhere in schooltool.app (except tests),
        # so it doesn't have to be efficient.
        for e in list(self):
            self.removeEvent(e)

    def find(self, unique_id):
        return self.events[unique_id]
示例#2
0
class Calendar(Persistent, CalendarMixin):
    """A persistent calendar."""

    # CalendarMixin is only used for the expand() method

    implements(ISchoolToolCalendar, IAttributeAnnotatable)

    __name__ = 'calendar'

    title = property(lambda self: self.__parent__.title)

    def __init__(self, owner):
        self.events = PersistentDict()
        self.__parent__ = owner

    def __iter__(self):
        return self.events.itervalues()

    def __len__(self):
        return len(self.events)

    def addEvent(self, event):
        assert ISchoolToolCalendarEvent.providedBy(event)
        if event.unique_id in self.events:
            raise ValueError('an event with this unique_id already exists')
        if event.__parent__ is None:
            for resource in event.resources:
                if ISchoolToolCalendar(resource) is self:
                    raise ValueError('cannot book itself')
            event.__parent__ = self
            for resource in event.resources:
                ISchoolToolCalendar(resource).addEvent(event)
        elif self.__parent__ not in event.resources:
            raise ValueError("Event already belongs to a calendar")
        self.events[event.unique_id] = event

    def removeEvent(self, event):
        if self.__parent__ in event.resources:
            event.unbookResource(self.__parent__)
        else:
            del self.events[event.unique_id]
            parent_calendar = event.__parent__
            if self is parent_calendar:
                for resource in event.resources:
                    event.unbookResource(resource)
                event.__parent__ = None

    def clear(self):
        # clear is not actually used anywhere in schooltool.app (except tests),
        # so it doesn't have to be efficient.
        for e in list(self):
            self.removeEvent(e)

    def find(self, unique_id):
        return self.events[unique_id]
示例#3
0
class SubscriptionContainer(Persistent):
    interface.implements(ISubscriptionContainer)
    component.adapts(ISubscriptionTarget)

    def __init__(self):
        self.subscriptions = PersistentDict()
        self._next_id = 1

    def add(self, subscription):
        self.subscriptions[self._next_id] = subscription
        self._next_id += 1

    def remove(self, n):
        del self.subscriptions[n]

    def list_with_keys(self):
        for n, subscription in self.subscriptions.iteritems():
            yield n, subscription

    def __iter__(self):
        for subscription in self.subscriptions.itervalues():
            yield subscription
示例#4
0
class SubscriptionContainer(Persistent):
    """ Holds anonymous and authenticated notifications """
    interface.implements(ISubscriptionContainer)
    component.adapts(ISubscriptionTarget)

    def __init__(self):
        self.subscriptions = PersistentDict()
        self._next_id = 1

    def add(self, subscription):
        self.subscriptions[self._next_id] = subscription
        self._next_id += 1

    def remove(self, n):
        del self.subscriptions[n]

    def list_with_keys(self):
        for n, subscription in self.subscriptions.iteritems():
            yield n, subscription

    def __iter__(self):
        for subscription in self.subscriptions.itervalues():
            yield subscription