def _addObjects(self, obj): """ Adds any objects that are specified by the featurelet's info. First checks for a previous installation of this featurelet, skips anything that was previously installed. Raises an error if content of the same id that was NOT a part of a previous installation of this featurelet exists. """ info = self._info supporter = IFeatureletSupporter(obj) objmgr = IObjectManager(obj) prior_ids = tuple() prior_info = supporter.getFeatureletDescriptor(self.id) if prior_info is not None: prior_content = prior_info.get('objects', tuple()) prior_ids = [item['id'] for item in prior_content] for item in info.get('objects', tuple()): iid = item['id'] if iid not in prior_ids: if objmgr.hasObject(iid): msg = "Object with id '%s' already exists." % iid raise zExceptions.BadRequest, msg try: newobj = item['class']() except TypeError: # might need an id passed to the constructor newobj = item['class'](iid) objmgr._setOb(iid, newobj)
def _removeObjects(self, obj, prior_info, info_key): """ Removes any objects that were added to the supporter as a result of a prior installation of this featurelet. Will not touch any objects that were not installed by this featurelet. """ prior_objects = prior_info.get(info_key) if prior_objects is None: return objmgr = IObjectManager(obj) prior_ids = [item['id'] for item in prior_objects] del_ids = [id_ for id_ in prior_ids if objmgr.hasObject(id_)] if del_ids: objmgr.manage_delObjects(ids=del_ids)
def walk_folder(folder): for idx, ob in folder.ZopeFind(folder, obj_metatypes=REPORTEK_META_TYPES, search_sub=0): yield ob if IObjectManager.providedBy(ob): for sub_ob in walk_folder(ob): yield sub_ob
def isDirectory(self, path): try: obj = self.context.unrestrictedTraverse(path) except: obj = None return IObjectManager.providedBy(obj)
def zope_find_user(container, username): while IObjectManager.providedBy(container): if container._getOb('acl_users', None) is not None: user = container.acl_users.getUser(username) if user is not None and user.getUserName() is not None: return user.__of__(container.acl_users) container = aq_parent(container) fail("%s is not a valid user." % username)
def create_subobjects(root, context, data, total=0): amount = int(data.get('amount', 3)) types = data.get('portal_type') if types is None: base = aq_base(context) if IBaseContent.providedBy(base): types = [] if hasattr(base, 'constrainTypesMode') and base.constrainTypesMode: types = context.locallyAllowedTypes elif IDexterityContent.providedBy(base): fti = component.getUtility(IDexterityFTI, name=context.portal_type) types = fti.filter_content_types and fti.allowed_content_types if not types: msg = _('Either restrict the addable types in this folder or ' \ 'provide a type argument.') addStatusMessage(context.request, msg) return total else: msg = _("The context doesn't provide IBaseContent or " "IDexterityContent. It might be a Plone Site object, " "but either way, I haven't gotten around to dealing with " "it. Why don't you jump in and help?") addStatusMessage(context.request, msg) return total recurse = False if data.get('recurse', None) not in [None, '0', 'False', False]: depth = 0 node = context while IUUID(node) != IUUID(root): depth += 1 node = node.aq_parent if depth < data.get('recursion_depth'): recurse = True for portal_type in types: for n in range(0, amount): obj = create_object(context, portal_type, data) total += 1 if not IObjectManager.providedBy(obj): continue if recurse: if shasattr(obj, 'getLocallyAllowedTypes'): data['portal_type'] = \ list(obj.getLocallyAllowedTypes()) elif shasattr(obj, 'allowedContentTypes'): data['portal_type'] = \ [t.id for t in obj.allowedContentTypes()] total = create_subobjects(root, obj, data, total) return total
def copyroles(source, dest): """ recursive copy of local roles for a copied content item or tree thereof. """ local_roles = source.get_local_roles() for (userid, roles) in local_roles: dest.manage_setLocalRoles(userid, list(roles)) if IObjectManager.providedBy(source): for name, contained in source.objectItems(): if name in dest.objectIds(): copyroles(contained, dest[name])
def traverse(path, content, request=None): """Simple helper to traverse path from content, following only Zope 2 containers API. """ for piece in path: if not IObjectManager.providedBy(content): raise BadRequest( u'Invalid path, not a container /%s.' % '/'.join(path)) child = content._getOb(piece, None) if child is None: raise BadRequest( u'Invalid path, content not found /%s' % '/'.join(path)) if request is not None: hook = getattr(child, '__before_publishing_traverse__', None) if hook is not None: hook(child, request) request['PARENTS'].append(child) content = child return content
def create_subobjects(root, context, data, total=0): amount = int(data.get('amount', 3)) types = data.get('portal_type') request = getRequest() depth = 0 node = context if not IPloneSiteRoot.providedBy(root): while IUUID(node) != IUUID(root): depth += 1 node = node.aq_parent else: while not IPloneSiteRoot.providedBy(node): depth += 1 node = node.aq_parent if types is None or depth > 0: base = aq_base(context) if IBaseContent.providedBy(base): types = [] if hasattr(base, 'constrainTypesMode') and base.constrainTypesMode: types = context.locallyAllowedTypes elif IDexterityContent.providedBy(base): fti = component.getUtility(IDexterityFTI, name=context.portal_type) types = fti.filter_content_types and fti.allowed_content_types if not types: msg = _('Either restrict the addable types in this folder ' 'or provide a type argument.') addStatusMessage(request, msg) return total else: msg = _("The context doesn't provide IBaseContent or " "IDexterityContent. It might be a Plone Site object, " "but either way, I haven't gotten around to dealing with " "it. Why don't you jump in and help?") addStatusMessage(request, msg) return total recurse = False if data.get('recurse', None) not in [None, '0', 'False', False] and \ depth < data.get('recursion_depth'): recurse = True for portal_type in types: for n in range(0, amount): obj = create_object(context, portal_type, data) total += 1 if not IObjectManager.providedBy(obj): continue if recurse: if not data.get('recurse_same_ptypes', False): if shasattr(obj, 'getLocallyAllowedTypes'): data['portal_type'] = \ list(obj.getLocallyAllowedTypes()) elif shasattr(obj, 'allowedContentTypes'): data['portal_type'] = \ [t.id for t in obj.allowedContentTypes()] total = create_subobjects(root, obj, data, total) return total