def getSourceObj(source):
    if source is None:
        return None

    items = source.split('_')
    scid = items[0]
    ws_hash = items[-2]
    act_hash = items[-1]
    sid = '_'.join(items[1:-2])

    app = ISchoolToolApplication(None)
    sectionContainer = app['schooltool.course.section'].get(scid, None)
    if sectionContainer is None:
        return None

    section = sectionContainer.get(sid, None)
    if section is None:
        return None

    for worksheet in interfaces.IActivities(section).values():
        if ws_hash == unicode(hash(IKeyReference(worksheet))):
            break
    else:
        return None

    if act_hash == 'ave':
        return worksheet
    for key, activity in worksheet.items():
        if act_hash == unicode(hash(IKeyReference(activity))):
            return activity
    return None
def createSourceString(sourceObj):
    if interfaces.IActivity.providedBy(sourceObj):
        act_hash = unicode(hash(IKeyReference(sourceObj)))
        worksheet = sourceObj.__parent__
    else:
        act_hash = 'ave'
        worksheet = sourceObj
    section = worksheet.__parent__.__parent__
    sectionContainer = section.__parent__
    return '%s_%s_%s_%s' % (sectionContainer.__name__, section.__name__,
        unicode(hash(IKeyReference(worksheet))), act_hash)
Exemple #3
0
    def migrate_intid(self, old_object, new_object):
        if '_intids' not in dir(self):
            self._intids = getUtility(IIntIds)

        old_key = IKeyReference(old_object)
        new_key = IKeyReference(new_object)
        uid = self._intids.ids[old_key]

        self._intids.refs[uid] = new_key
        self._intids.ids[new_key] = uid
        del self._intids.ids[old_key]
Exemple #4
0
def moveIntIdSubscriber(ob, event):
    """A subscriber to ObjectMovedEvent

    Updates the stored path for the object in all the unique
    id utilities.
    """
    utilities = tuple(getAllUtilitiesRegisteredFor(IIntIds))
    if not utilities:
        return
    try:
        key = IKeyReference(ob, None)
    except NotYet:
        key = None

    # Register only objects that adapt to key reference
    if key is None:
        return

    # Update objects that adapt to key reference
    for utility in utilities:
        try:
            uid = utility.getId(ob)
            utility.refs[uid] = key
            utility.ids[key] = uid
        except KeyError:
            pass
 def appendToHistory(self, requirement, evaluation):
     if self._history is None:
         self._history = OOBTree()
     key = IKeyReference(requirement)
     if key not in self._history:
         self._history[key] = persistent.list.PersistentList()
     self._history[key].append(evaluation)
Exemple #6
0
def handle_removed_object(event):
    """Notify IntId utility for removed objects

    This subscriber is used for all persistent objects to be unregistered
    from all locally registered IIntIds utilities.
    """
    registry = get_current_registry()
    locations = ISublocations(event.object, None)
    if locations is not None:
        for location in locations.sublocations():  # pylint: disable=too-many-function-args
            registry.notify(ObjectRemovedEvent(location))
    utilities = tuple(get_all_utilities_registered_for(IIntIds))
    if utilities:
        try:
            key = IKeyReference(event.object, None)
        except NotYet:
            pass
        else:
            # Register only objects that adapt to key reference
            if key is not None:
                # Notify the catalogs that this object is about to be removed.
                registry.notify(IntIdRemovedEvent(event.object, event))
                for utility in utilities:
                    try:
                        utility.unregister(key)
                    except KeyError:
                        pass
Exemple #7
0
def moveIntIdSubscriber(ob, event):
    """A subscriber to ObjectMovedEvent

    Updates the stored path for the object in all the unique
    id utilities.
    """
    if IObjectRemovedEvent.providedBy(event) or \
           IObjectAddedEvent.providedBy(event):
        return
    utilities = tuple(getAllUtilitiesRegisteredFor(IIntIds))
    if utilities:
        key = None
        try:
            key = IKeyReference(ob, None)
        except NotYet:  # @@ temporary fix
            pass

        # Update objects that adapt to key reference
        if key is not None:
            for utility in utilities:
                try:
                    uid = utility.getId(ob)
                    utility.refs[uid] = key
                    utility.ids[key] = uid
                except KeyError:
                    pass
Exemple #8
0
def addIntIdSubscriber(ob, event):
    """A subscriber to ObjectAddedEvent

    Registers the object added in all unique id utilities and fires
    an event for the catalogs.
    """
    factorytool = getToolByName(ob, 'portal_factory', None)
    if factorytool is not None and factorytool.isTemporary(ob):
        # Ignore objects marked as temporary in the CMFPlone portal_factory
        # tool
        return

    utilities = tuple(getAllUtilitiesRegisteredFor(IIntIds))
    if utilities:  # assert that there are any utilites
        key = None
        try:
            key = IKeyReference(ob, None)
        except NotYet:
            pass

        # Register only objects that adapt to key reference
        if key is not None:
            for utility in utilities:
                utility.register(key)
            # Notify the catalogs that this object was added.
            notify(IntIdAddedEvent(ob, event))
 def getSortKey(self, person):
     person = proxy.removeSecurityProxy(person)
     ann = annotation.interfaces.IAnnotations(person)
     if GRADEBOOK_SORTING_KEY not in ann:
         ann[GRADEBOOK_SORTING_KEY] = PersistentDict()
     section_id = hash(IKeyReference(self.section))
     return ann[GRADEBOOK_SORTING_KEY].get(section_id, ('student', False))
 def setSortKey(self, person, value):
     person = proxy.removeSecurityProxy(person)
     ann = annotation.interfaces.IAnnotations(person)
     if GRADEBOOK_SORTING_KEY not in ann:
         ann[GRADEBOOK_SORTING_KEY] = PersistentDict()
     section_id = hash(IKeyReference(self.section))
     ann[GRADEBOOK_SORTING_KEY][section_id] = value
Exemple #11
0
def removeIntIdSubscriber(ob, event):
    """A subscriber to ObjectRemovedEvent

    Removes the unique ids registered for the object in all the unique
    id utilities.
    """
    utilities = tuple(getAllUtilitiesRegisteredFor(IIntIds))
    if not utilities:
        return
    try:
        key = IKeyReference(ob, None)
    except NotYet:
        key = None

    # Register only objects that adapt to key reference
    if key is None:
        return

    # Notify the catalogs that this object is about to be removed.
    notify(IntIdRemovedEvent(ob, event))
    for utility in utilities:
        try:
            utility.unregister(key)
        except KeyError:
            pass
 def __delitem__(self, key):
     """See zope.interface.common.mapping.IWriteMapping"""
     value = self[key]
     del self._btree[IKeyReference(key)]
     self.appendToHistory(key, value)
     event = ObjectRemovedEvent(value, self)
     zope.event.notify(event)
Exemple #13
0
def evolveLinks(connection, oids):
    int_ids = getSite()._sm.getUtility(IIntIds)

    states = {}
    for n, oid in enumerate(oids):
        try:
            link = connection.get(oid)
        except KeyError:
            continue
        links = link.__parent__._links
        if (link.__name__ not in links or links[link.__name__] is not link):
            # this is a record of a replaced link, skip.
            continue
        key = IKeyReference(link)
        idmap = {int_ids: int_ids.register(key)}

        this = link.__parent__.__parent__
        thishash = (
            hash(IKeyReference(this)),
            hash(link.my_role),
            hash(IKeyReference(link.target)),
            hash(link.role),
            hash(link.rel_type),
        )
        backhash = (
            hash(IKeyReference(link.target)),
            hash(link.role),
            hash(IKeyReference(this)),
            hash(link.my_role),
            hash(link.rel_type),
        )

        if backhash in states:
            link.shared = states[thishash] = states[backhash]
        else:
            assert thishash not in states
            states[thishash] = link.shared = OOBTree()
            link.shared['X'] = link.__dict__.get('extra_info')
        if 'extra_info' in link.__dict__:
            del link.__dict__['extra_info']
        if isinstance(link.rel_type, TemporalURIObject):
            link.shared['tmp'] = ()

        notify(IntIdAddedEvent(link, ObjectAddedEvent(link), idmap))

        if n % 10000 == 9999:
            transaction.savepoint(optimistic=True)
Exemple #14
0
    def unregister(self, ob):
        key = IKeyReference(ob, None)
        if key is None:
            return

        uid = self.ids[key]
        del self.refs[uid]
        del self.ids[key]
Exemple #15
0
 def register(self, ob):
     key = IKeyReference(ob)
     res = self.ids.get(key, None)
     if res is not None:
         return res
     uid = self._generateId()
     self.refs[uid] = key
     self.ids[key] = uid
     return uid
 def __setitem__(self, requirement, value):
     """See zope.interface.common.mapping.IWriteMapping"""
     key = IKeyReference(requirement)
     if (key in self._btree or
         self.getHistory(requirement)):
         current = self._btree.get(key, None)
         self.appendToHistory(requirement, current)
     self._btree[key] = value
     value, event = containedEvent(value, self)
     zope.event.notify(event)
Exemple #17
0
    def getId(self, ob):
        try:
            key = IKeyReference(ob)
        except (NotYet, TypeError):
            raise KeyError(ob)

        try:
            return self.ids[key]
        except KeyError:
            raise KeyError(ob)
 def getCurrentWorksheet(self, person):
     default = self.getDefaultWorksheet()
     current = self.getCurrentSectionWorksheets(person)
     if not current:
         return default
     section_id = hash(IKeyReference(self.__parent__))
     worksheet = current.get(section_id, default)
     if worksheet is not None and worksheet.hidden:
         return default
     return worksheet
 def unlinkContext(self, context):
     id = hash(IKeyReference(context))
     keys_removed = set()
     for key, ids in self._items.items():
         ids.discard(id)
         if not ids:
             keys_removed.add(key)
     for key in keys_removed:
         del self._items[key]
     return keys_removed
Exemple #20
0
    def unregister(self, ob):
        # Note that we'll still need to keep this proxy removal.
        ob = removeSecurityProxy(ob)
        key = IKeyReference(ob, None)
        if key is None:
            return

        uid = self.ids[key]
        del self.refs[uid]
        del self.ids[key]
Exemple #21
0
    def migrate_intid(self, old_object, new_object):
        if '_intids' not in dir(self):
            self._intids = getUtility(IIntIds)

        old_key = IKeyReference(old_object)
        new_key = IKeyReference(new_object)
        try:
            uid = self._intids.ids[old_key]
        except KeyError:
            # Key was missing in catalog
            uid = self._intids.register(new_object)
            LOG.info(
                'Add previously unregistered object to IIntIds catalog %s',
                new_object)
        else:
            del self._intids.ids[old_key]
        finally:
            self._intids.refs[uid] = new_key
            self._intids.ids[new_key] = uid
Exemple #22
0
    def register(self, ob):
        # Note that we'll still need to keep this proxy removal.
        ob = removeSecurityProxy(ob)
        key = IKeyReference(ob)

        if key in self.ids:
            return self.ids[key]
        uid = self._generateId()
        self.refs[uid] = key
        self.ids[key] = uid
        return uid
Exemple #23
0
def addIntIdSubscriber(ob, event):
    """A subscriber to ObjectAddedEvent

    Registers the object added in all unique id utilities and fires
    an event for the catalogs.
    """
    utilities = tuple(getAllUtilitiesRegisteredFor(IIntIds))
    if utilities:  # assert that there are any utilites
        key = IKeyReference(ob, None)
        # Register only objects that adapt to key reference
        if key is not None:
            idmap = {}
            for utility in utilities:
                idmap[utility] = utility.register(key)
            # Notify the catalogs that this object was added.
            notify(IntIdAddedEvent(ob, event, idmap))
 def query(self,
           my_role=None,
           target=None,
           role=None,
           rel_type=None,
           catalog=None):
     if catalog is None:
         catalog = self.catalog
     empty = IFBTree.TreeSet()
     this_hash = hash_persistent(self.__parent__)
     result = None
     if my_role is not None:
         ids = catalog['my_role_hash'].values_to_documents.get(
             (hash(my_role), this_hash), empty)
         if result is None:
             result = ids
         else:
             result = IFBTree.intersection(result, ids)
         if not result:
             return result
     if target is not None:
         ids = catalog['target'].values_to_documents.get(
             (IKeyReference(target), this_hash), empty)
         if result is None:
             result = ids
         else:
             result = IFBTree.intersection(result, ids)
         if not result:
             return result
     if role is not None:
         ids = catalog['role_hash'].values_to_documents.get(
             (hash(role), this_hash), empty)
         if result is None:
             result = ids
         else:
             result = IFBTree.intersection(result, ids)
         if not result:
             return result
     if rel_type is not None:
         ids = catalog['rel_type_hash'].values_to_documents.get(
             (hash(rel_type), this_hash), empty)
         if result is None:
             result = ids
         else:
             result = IFBTree.intersection(result, ids)
     return result
Exemple #25
0
def handle_added_object(event):
    """Notify IntId utility for added objects

    This subscriber is used for all persistent objects to be registered
    in all locally registered IIntIds utilities.
    """
    utilities = tuple(get_all_utilities_registered_for(IIntIds))
    if utilities:
        # assert that there are any utilities
        try:
            key = IKeyReference(event.object, None)
        except NotYet:
            pass
        else:
            # Register only objects that adapt to key reference
            if key is not None:
                idmap = {}
                for utility in utilities:
                    idmap[utility] = utility.register(key)
                # Notify the catalogs that this object was added.
                get_current_registry().notify(
                    IntIdAddedEvent(event.object, event, idmap))
def getUniqueId(page):
    return 'page' + str(hash(IKeyReference(page)))
def getRequirementKey(requirement):
    """Get the reference key for any requirement."""
    return IKeyReference(requirement)
Exemple #28
0
def get_link_shared_uid(link):
    uid = tuple([hash(link.rel_type)] + sorted([
        (hash(link.role), hash(IKeyReference(link.target))),
        (hash(link.my_role), hash(IKeyReference(link.__parent__.__parent__))),
    ]))
    return uid
Exemple #29
0
def link_this_keyref(link):
    linkset = link.__parent__
    return IKeyReference(linkset.__parent__)
Exemple #30
0
def link_target_keyref(link):
    return IKeyReference(link.target)