def test_uuidsToObjects(self): folder_uid = self.portal.folder.UID() folder2_uid = self.portal.folder2.UID() uuids = [folder_uid, folder2_uid, 'unknown_uid'] self.assertEqual(len(uuidsToObjects(uuids)), 2) self.assertEqual(uuidsToObjects(uuids, ordered=True), [self.portal.folder, self.portal.folder2]) uuids = [folder2_uid, folder_uid] self.assertEqual(uuidsToObjects(uuids, ordered=True), [self.portal.folder2, self.portal.folder])
def test_uuidsToObjects(self): folder_uid = self.portal.folder.UID() folder2_uid = self.portal.folder2.UID() uuids = [folder_uid, folder2_uid, 'unknown_uid'] self.assertEqual(len(uuidsToObjects(uuids)), 2) self.assertEqual( uuidsToObjects(uuids, ordered=True), [self.portal.folder, self.portal.folder2]) uuids = [folder2_uid, folder_uid] self.assertEqual( uuidsToObjects(uuids, ordered=True), [self.portal.folder2, self.portal.folder])
def _get_obj_from_uid(uid): """ """ # change self.context with element found with self.uid objs = uuidsToObjects(uuids=uid) if not objs: # try to get it unrestricted objs = uuidsToObjects(uuids=uid, unrestricted=True) if objs: raise BadRequest(UID_NOT_ACCESSIBLE_ERROR % (uid, api.user.get_current().getId())) else: raise BadRequest(UID_NOT_FOUND_ERROR % uid) return objs[0]
def __call__(self, uids): """ """ wfTool = api.portal.get_tool('portal_workflow') # defer call to Meeting.update_item_references self.request.set('defer_Meeting_update_item_references', True) lowest_itemNumber = 0 objs = uuidsToObjects(uids, ordered=True) for obj in objs: try: wfTool.doActionFor(obj, 'present') except WorkflowException: # sometimes an item may not be presented, # even if shown in presentable items api.portal.show_message(_( 'Item \"${item_title}\" could not be presented!', mapping={'item_title': safe_unicode(obj.Title())}), request=self.request, type='warning') if not lowest_itemNumber or obj.getItemNumber( ) < lowest_itemNumber: lowest_itemNumber = obj.getItemNumber() self.request.set('defer_Meeting_update_item_references', False) # now we may call update_item_references self.context.update_item_references(start_number=lowest_itemNumber) msg = translate('present_several_items_done', domain='PloneMeeting', context=self.request) plone_utils = api.portal.get_tool('plone_utils') plone_utils.addPortalMessage(msg)
def _apply(self, **data): """ """ if data['folders']: targets = uuidsToObjects(data['folders'], unrestricted=True) for brain in self.brains: obj = brain.getObject() for target in targets: api.content.copy(source=obj, target=target, safe_id=True)
def _set_query_base_search(self): """ """ query = {} form = self.request.form base_search_uid = form.get("base_search_uid", "").strip() if base_search_uid: element = uuidsToObjects(uuids=base_search_uid) if element and ICollection.providedBy(element[0]): collection = element[0] query = parseFormquery(collection, collection.query) elif element: self.context = element[0] return query
def __call__(self, uids): """ """ wfTool = api.portal.get_tool('portal_workflow') # make sure we have a list of uids, in some case, as it is called # by jQuery, we receive only one uid, as a string... if isinstance(uids, str): uids = [uids] # defer call to Meeting.update_item_references self.request.set('defer_Meeting_update_item_references', True) lowest_itemNumber = 0 objs = uuidsToObjects(uids, ordered=True, unrestricted=True) for obj in objs: # save lowest_itemNumber for call to Meeting.update_item_references here under if not lowest_itemNumber or obj.getItemNumber( ) < lowest_itemNumber: lowest_itemNumber = obj.getItemNumber() # execute every 'back' transitions until item is in state 'validated' changedState = True while not obj.query_state() == 'validated': availableTransitions = [ tr['id'] for tr in wfTool.getTransitionsFor(obj) ] if not availableTransitions or not changedState: break changedState = False # if several back transitions (like when WFAdaptation 'presented_item_back_to_xxx' # is selected), are available, give the priority to 'backToValidated' if 'backToValidated' in availableTransitions: availableTransitions = ['backToValidated'] for tr in availableTransitions: if tr.startswith('back'): wfTool.doActionFor(obj, tr) changedState = True break self.request.set('defer_Meeting_update_item_references', False) # now we may call update_item_references self.context.update_item_references(start_number=lowest_itemNumber) msg = translate('remove_several_items_done', domain='PloneMeeting', context=self.request) plone_utils = api.portal.get_tool('plone_utils') plone_utils.addPortalMessage(msg)
def __call__(self, object_uid, redirect=True, catch_before_delete_exception=True): """ """ # redirect can by passed by jQuery, in this case, we receive '0' or '1' if redirect == '0': redirect = False elif redirect == '1': redirect = True # Get the object to delete, if not found using UID index, # try with contained_uids index objs = uuidsToObjects(uuids=[object_uid], check_contained_uids=True) if not objs: # URBAN ovveride: check if the context is the UID to delete. if self.context.UID() == object_uid: objs = [self.context] else: raise KeyError('The given uid could not be found!') obj = objs[0] # we use an adapter to manage if we may delete the object # that checks if the user has the 'Delete objects' permission # on the content by default but that could be overrided if IContentDeletable(obj).mayDelete(): msg = {'message': _('object_deleted'), 'type': 'info'} # remove the object # just manage BeforeDeleteException because we rise it ourselves from OFS.ObjectManager import BeforeDeleteException try: unrestrictedRemoveGivenObject(obj) except BeforeDeleteException, exc: # abort because element was removed transaction.abort() msg = { 'message': u'{0} ({1})'.format(exc.message, exc.__class__.__name__), 'type': 'error' } if not catch_before_delete_exception: raise BeforeDeleteException(exc.message)
def get_organizations(only_selected=True, the_objects=True, not_empty_suffix=None, kept_org_uids=[], caching=True): """ Return organizations. If only_selected, check registry if org is selected. If the objects, return organization objects, either return UIDs. If not_empty_suffix, return organizations for which Plone group using given suffix is not empty. If kept_org_uids, return only organizations with these UIDs. If caching, use REQUEST caching. """ orgs = None if caching: request = getRequest() if request: # in some cases like in tests, request can not be retrieved key = "plonegroup-utils-get_organizations-{0}-{1}-{2}-{3}".format( not_empty_suffix or '', str(only_selected), str(the_objects), '_'.join(sorted(kept_org_uids))) cache = IAnnotations(getRequest()) orgs = cache.get(key, None) else: caching = False if orgs is None: if only_selected: org_uids = [org_uid for org_uid in api.portal.get_registry_record(ORGANIZATIONS_REGISTRY)] else: # use the vocabulary to get selectable organizations so if vocabulary # is overrided get_organizations is still consistent vocab = getUtility( IVocabularyFactory, name=u'collective.contact.plonegroup.organization_services') portal = api.portal.get() org_uids = [term.value for term in vocab(portal)._terms] # filter out regarding parameter kept_org_uids if kept_org_uids: org_uids = [org_uid for org_uid in org_uids if org_uid in kept_org_uids] # we only keep orgs for which Plone group with not_empty_suffix suffix contains members if not_empty_suffix: filtered_orgs = [] for org_uid in org_uids: plone_group_id = get_plone_group_id(org_uid, suffix=not_empty_suffix) plone_group = api.group.get(plone_group_id) if plone_group and plone_group.getMemberIds(): filtered_orgs.append(org_uid) org_uids = filtered_orgs # return org uids or org objects if the_objects: orgs = uuidsToObjects(org_uids, ordered=True) else: orgs = org_uids if caching: cache[key] = orgs return orgs
def get_organizations(only_selected=True, the_objects=True, not_empty_suffix=None, kept_org_uids=[], caching=True): """ Return organizations. If only_selected, check registry if org is selected. If the objects, return organization objects, either return UIDs. If not_empty_suffix, return organizations for which Plone group using given suffix is not empty. If kept_org_uids, return only organizations with these UIDs. If caching, use REQUEST caching. """ orgs = None if caching: request = getRequest() if request: # in some cases like in tests, request can not be retrieved key = "plonegroup-utils-get_organizations-{0}-{1}-{2}-{3}".format( not_empty_suffix or '', str(only_selected), str(the_objects), '_'.join(sorted(kept_org_uids))) cache = IAnnotations(request) orgs = cache.get(key, None) else: caching = False if orgs is None: if only_selected: org_uids = get_registry_organizations() else: # use the vocabulary to get selectable organizations so if vocabulary # is overrided get_organizations is still consistent vocab = getUtility( IVocabularyFactory, name=u'collective.contact.plonegroup.organization_services') portal = api.portal.get() org_uids = [term.value for term in vocab(portal)._terms] # filter out regarding parameter kept_org_uids if kept_org_uids: # make sure order defined by kept_org_uids is kept org_uids = [ kept_org_uid for kept_org_uid in kept_org_uids if kept_org_uid in org_uids ] # we only keep orgs for which Plone group with not_empty_suffix suffix contains members if not_empty_suffix: filtered_orgs = [] for org_uid in org_uids: plone_group_id = get_plone_group_id(org_uid, suffix=not_empty_suffix) plone_group = api.group.get(plone_group_id) if plone_group and plone_group.getMemberIds(): filtered_orgs.append(org_uid) org_uids = filtered_orgs # return org uids or org objects if the_objects: orgs = uuidsToObjects(org_uids, ordered=True) else: orgs = org_uids if caching: # store a new list in cache so it can not be modified cache[key] = list(orgs) return orgs
def _extra_include(self, result): """ """ extra_include = self._get_asked_extra_include() if "categories" in extra_include: categories = self.context.getCategories(onlySelectable=False) result["extra_include_categories"] = [] for category in categories: serializer = self._get_serializer(category, "categories") result["extra_include_categories"].append(serializer()) result["extra_include_categories_items_total"] = len(categories) if "classifiers" in extra_include: classifiers = self.context.getCategories(catType='classifiers', onlySelectable=False) result["extra_include_classifiers"] = [] for classifier in classifiers: serializer = self._get_serializer(classifier, "classifiers") result["extra_include_classifiers"].append(serializer()) result["extra_include_classifiers_items_total"] = len(classifiers) if "pod_templates" in extra_include: pod_templates = [ obj for obj in self.context.podtemplates.objectValues() if getattr(obj, 'enabled', False) ] result["extra_include_pod_templates"] = [] for pod_template in pod_templates: serializer = self._get_serializer(pod_template, "pod_templates") result["extra_include_pod_templates"].append(serializer()) result["extra_include_pod_templates_items_total"] = len( pod_templates) if "searches" in extra_include: collections = [ obj for obj in self.context.searches.searches_items.objectValues() if (obj.portal_type == 'DashboardCollection' and obj.enabled) ] result["extra_include_searches"] = [] for collection in collections: serializer = self._get_serializer(collection, "searches") result["extra_include_searches"].append(serializer()) result["extra_include_searches_items_total"] = len(collections) if "proposing_groups" in extra_include: orgs = self.context.getUsingGroups(theObjects=True) result["extra_include_proposing_groups"] = [] for org in orgs: serializer = self._get_serializer(org, "proposing_groups") result["extra_include_proposing_groups"].append(serializer()) result["extra_include_proposing_groups_items_total"] = len(orgs) if "associated_groups" in extra_include: vocab = get_vocab( self.context, 'Products.PloneMeeting.vocabularies.associatedgroupsvocabulary' ) org_uids = [term.value for term in vocab._terms] orgs = uuidsToObjects(org_uids, ordered=True) result["extra_include_associated_groups"] = [] for org in orgs: serializer = self._get_serializer(org, "associated_groups") result["extra_include_associated_groups"].append(serializer()) result["extra_include_associated_groups_items_total"] = len(orgs) if "groups_in_charge" in extra_include: vocab = get_vocab( self.context, 'Products.PloneMeeting.vocabularies.groupsinchargevocabulary') org_uids = [term.value for term in vocab._terms] orgs = uuidsToObjects(org_uids, ordered=True) result["extra_include_groups_in_charge"] = [] for org in orgs: serializer = self._get_serializer(org, "groups_in_charge") result["extra_include_groups_in_charge"].append(serializer()) result["extra_include_groups_in_charge_items_total"] = len(orgs) return result
def get_groups_in_charge(self, the_objects=False): """Accessor so it can be called in a TAL expression.""" res = self.groups_in_charge if res and the_objects: res = uuidsToObjects(res, ordered=True, unrestricted=True) return res
def get_using_groups(self, the_objects=False): """ """ res = self.using_groups if res and the_objects: res = uuidsToObjects(res, ordered=True) return res
def get_groups_in_charge(self, the_objects=False): """ """ res = self.groups_in_charge if res and the_objects: res = uuidsToObjects(res, ordered=True, unrestricted=True) return res