def client_contact_permissions_on_batches(portal, ut): """ Grants view permissions on batches folder in navtree :param portal: portal object :return: None """ mp = portal.batches.manage_permission mp(permissions.View, [ 'Manager', 'LabManager', 'LabClerk', 'Analyst', 'RegulatoryInspector', 'Client' ], 0) portal.batches.reindexObject() # Update batch object permissions workflow_tool = api.get_tool("portal_workflow") workflow = workflow_tool.getWorkflowById('bika_batch_workflow') catalog = api.get_tool('portal_catalog') # Update permissions programmatically query = Eq('portal_type', 'Batch') & ~Eq('allowedRolesAndUsers', 'Client') brains = catalog.evalAdvancedQuery(query) counter = 0 total = len(brains) logger.info("Changing permissions for Batch objects: {0}".format(total)) for brain in brains: obj = api.get_object(brain) workflow.updateRoleMappingsFor(obj) obj.reindexObject() counter += 1 if counter % 100 == 0: logger.info("Changing permissions for Batch objects: " + "{0}/{1}".format(counter, total)) logger.info("Changed permissions for Batch objects: " + "{0}/{1}".format(counter, total))
def get_uids(index_client, root="", types=()): start = 0 need_results = True query = [Eq("tx_state", 0)] if root: root = root.rstrip('/') query.append( Or(Eq("uid", "{}".format(root)), MatchGlob("uid", "{}/*".format(root)))) if not isinstance(types, (tuple, list)): types = (types, ) if types: query.append(In("objectImplements", [dottedname(t) for t in types])) while need_results: search_results = index_client.search( SearchParams(query=And(*query), start=start, limit=MODEL_INDEX_BATCH_SIZE, order_by="uid", fields=["uid"])) start += MODEL_INDEX_BATCH_SIZE for result in search_results.results: yield result.uid need_results = start < search_results.total_count
def _recover_failed_objects(self): """ Checks for non-updated objects (by filtering null Title) and re-updates them. :return: """ uc = api.get_tool('uid_catalog', self.portal) # Reference objects must be skipped query = Eq('Title', '') & ~ Eq('portal_type', 'Reference') & ~ \ Eq('portal_type', 'ARReport') brains = uc.evalAdvancedQuery(query) total = len(brains) logger.info('*** Recovering {} objects ***'.format(total)) for idx, brain in enumerate(brains): # Check if object has been created during migration uid = brain.UID existing = self.sh.find_unique(LOCAL_UID, uid) if existing is None: continue logger.info('Recovering {0}/{1} : {2} '.format( idx + 1, total, existing[REMOTE_PATH])) # Mark that update failed previously existing['updated'] = '0' self._handle_obj(existing, handle_dependencies=False) obj = brain.getObject() obj.reindexObject() return
def searchTablePage(self, tp, **kwargs): if 'path' not in kwargs.keys(): kwargs['path'] = '/'.join(tp.getPhysicalPath()) if 'is_label' not in kwargs.keys(): kwargs['is_label'] = False query = Eq('is_label', True) query &= Eq('path', kwargs['path']) sub_query = None for k, v in kwargs.items(): if k in SKIP_KEYS: continue if type(v) == dict: # Handle complex subqueries (range?) term = self._buildRangeQuery(k, v) elif type(v) == list: term = In(k, v) else: term = Eq(k, v) if sub_query: sub_query &= term else: sub_query = term query = query | sub_query return self.evalAdvancedQuery(query, sortSpecs=(kwargs.get( 'sort_on', 'getObjPositionInParent'), ))
def getProducts(self): """ """ accessories_uids = [a["uid"] for a in self.getAccessories()] product_title = self.request.get("product-title", "") if product_title == "": return [] catalog = getToolByName(self.context, "portal_catalog") # if easyshop.search ist installed we take that search try: view = getMultiAdapter((self.context, self.request), name="search-view") brains = view.getSearchResults(searchable_text=product_title) except ComponentLookupError: query = Eq("portal_type", "Product") & Eq("Title", product_title) brains = catalog.evalAdvancedQuery(query) result = [] for brain in brains: if brain.UID not in accessories_uids: result.append({"title": brain.Title, "uid": brain.UID}) return result
def _get_device_components(self, device, str_types, *args, **kwargs): types = set(["Products.ZenModel.DeviceComponent.DeviceComponent"]) if str_types: types = types | set(str_types) query = [ Eq("objectImplements", str_type) for str_type in types ] query.append(Eq("deviceId", "/".join(device.getPrimaryPath()))) kwargs["query"] = And(*query) return self.model_catalog.search(*args, **kwargs)
def getAppKpis(self): paths = [] devices = self.REQUEST.get('deviceId') if not devices: self.REQUEST.RESPONSE.setHeader('status', 404) paths.append(['ERROR', 'Device ids not specified']) self.REQUEST.RESPONSE.setHeader('content-type', 'application/json') return json.dumps(paths) if isinstance(devices, basestring): devices = [devices] applicationIds = self.REQUEST.get('applicationId') if not applicationIds: applicationIds = [] elif isinstance(applicationIds, basestring): applicationIds = [applicationIds] for devId in devices: dev = self.dmd.Devices.deviceSearch({'id': devId}) if len(dev): dev = dev[0].getObject() devPathL = len(dev.getPrimaryId()) + 1 for brain in dev.componentSearch({ 'getParentDeviceName': devId, 'monitored': True, 'meta_type': 'BBCApplication' }): # skip if not requested if len(applicationIds) and (brain.getPrimaryId.split('/') )[-1] not in applicationIds: continue # we got here so this is one of the wanted records app = brain.getObject() for kpi in app.ApplicationToKPI(): paths.append([kpi.getPrimaryId()[devPathL:], kpi.name()]) # fetch non-automated datasources if 'manual' in applicationIds: for brain in dev.componentSearch.evalAdvancedQuery( Eq('getParentDeviceName', devId) & Eq('monitored', True) & ~In('meta_type', [ 'BBCApplication', 'BBCApplicationKPI', 'BBCApplicationKPIDerive', 'BBCApplicationKPIAbsolute', 'BBCApplicationKPICounter', 'BBCApplicationKPIGauge' ])): dsId = (brain.getPrimaryId.split('/'))[-1] if dsId == '-': continue try: ds = brain.getObject() paths.append([ds.getPrimaryId()[devPathL:], ds.name()]) except: continue self.REQUEST.RESPONSE.setHeader('content-type', 'application/json') return json.dumps(paths)
def findDeviceByIdExact(self, devicename): """ Look up device in catalog and return it. devicename must match device id exactly """ if devicename: query = And( Eq("objectImplements", "Products.ZenModel.Device.Device"), Eq('id', devicename) ) search_results = IModelCatalogTool(self.dmd.Devices).search(query=query) for brain in search_results.results: dev = brain.getObject() if dev.id == devicename: return dev
def validate_device_catalog(self): # get all devices brains = self.device_catalog() self.assertEqual(len(brains), self.n_devices) # Grab a device and perform diferent searches for dev in self.devices.values(): uid = "/".join(dev.device.getPrimaryPath()) titleOrId = dev.device.id ip = dev.ip.id queries = [("titleOrId", titleOrId), ("getDeviceIp", ip), ("getPhysicalPath", uid)] for q_field, q_value in queries: brains = self.device_catalog.search(Eq(q_field, q_value)) self.assertEqual(len(brains), 1) self.assertEqual(brains[0].getPrimaryId, uid) self.assertEqual(brains[0].id, dev.device.id) self.assertEqual(sorted(brains[0].path), sorted(dev.device.path())) # Search with keywords as the query kw = {q_field: q_value} brains = self.device_catalog(**kw) self.assertEqual(len(brains), 1) self.assertEqual(brains[0].getPrimaryId, uid) self.assertEqual(brains[0].id, dev.device.id) self.assertEqual(sorted(brains[0].path), sorted(dev.device.path())) # Search with name=value format brains = self.device_catalog(getPhysicalPath=uid) self.assertEqual(len(brains), 1) self.assertEqual(brains[0].getPrimaryId, uid) self.assertEqual(brains[0].id, dev.device.id) self.assertEqual(sorted(brains[0].path), sorted(dev.device.path())) # Search with both (query is AdvancedQuery) query = Eq("titleOrId", titleOrId) brains = self.device_catalog(query=query, getDeviceIp=ip) self.assertEqual(len(brains), 1) self.assertEqual(brains[0].getPrimaryId, uid) self.assertEqual(brains[0].id, dev.device.id) self.assertEqual(sorted(brains[0].path), sorted(dev.device.path())) # Search with both (query is dict) query = {"getDeviceIp": ip} brains = self.device_catalog(query=query, titleOrId=titleOrId, getPhysicalPath=uid) self.assertEqual(len(brains), 1) self.assertEqual(brains[0].getPrimaryId, uid) self.assertEqual(brains[0].id, dev.device.id) self.assertEqual(sorted(brains[0].path), sorted(dev.device.path()))
def getObjectBrains(self, uid=None, start=0, limit=50, sort='name', dir='ASC', params=None, hashcheck=None, types=(), fields=[]): cat = IModelCatalogTool(self._getObject(uid)) reverse = bool(dir == 'DESC') qs = [] query = None globFilters = {} prodStates = None params = params if params else {} for key, value in params.iteritems(): if key == 'ipAddress': qs.append(MatchGlob('text_ipAddress', '{}*'.format(value))) elif key == 'productionState': qs.append( Or(*[Eq('productionState', str(state)) for state in value])) # ZEN-30949 - stringify values from the 'priority' list if it's passed in for query criteria elif key == 'priority': qs.append( Or(*[Eq('priority', str(priority)) for priority in value])) # ZEN-10057 - move filtering on indexed groups/systems/location from post-filter to query elif key in organizersToClass: organizerQuery = self.findMatchingOrganizers( organizersToClass[key], organizersToPath[key], value) if not organizerQuery: return [] qs.append(organizerQuery) else: globFilters[key] = value if qs: query = And(*qs) return cat.search(types, start=start, limit=limit, orderby=sort, reverse=reverse, query=query, globFilters=globFilters, hashcheck=hashcheck, fields=fields)
def _get_component_types_from_model_catalog(self, uid): """ """ componentTypes = {} uuidMap = {} model_catalog = IModelCatalogTool(self.context.dmd) model_query = Eq('objectImplements', "Products.ZenModel.DeviceComponent.DeviceComponent") model_query = And(model_query, Eq("deviceId", uid)) model_query_results = model_catalog.search(query=model_query, fields=["uuid", "meta_type"]) for brain in model_query_results.results: uuidMap[brain.uuid] = brain.meta_type compType = componentTypes.setdefault(brain.meta_type, { 'count' : 0, 'severity' : 0 }) compType['count'] += 1 return (componentTypes, uuidMap)
def get_items(self): catalog = getToolByName(self.context, 'portal_catalog') today = DateTime() query = In('review_state', ('published', )) & \ Eq('chimpfeeds', self.name) & \ Le('feedSchedule', today) settings = IFeedSettings(self.context) if settings.use_moderation: query = query & Eq('feedModerate', True) brains = catalog.evalAdvancedQuery(query, (('Date', 'desc'), )) objects = tuple(brain.getObject() for brain in brains) return tuple(ItemProxy(obj).__of__(obj) for obj in objects)
def getSubComponents(self): cat = ICatalogTool(self) query = And( Not( Eq('objectImplements', 'Products.ZenModel.ComponentGroup.ComponentGroup')), Not(Eq('objectImplements', 'Products.ZenModel.Device.Device'))) brains = cat.search(query=query) children = [] for brain in brains: try: children.append(brain.getObject()) except: pass return children
def validate_layer3_catalog(self): # get all ip interfaces brains = self.layer3_catalog() self.assertEqual(len(brains), self.n_devices) # perform diferent searches for each of the devices for dev in self.devices.values(): device_id = dev.device.id ip_address_uid = "/".join(dev.ip.getPrimaryPath()) interface_id = dev.interface.id network_uid = "/".join(dev.ip.network().getPrimaryPath()) queries = [("networkId", network_uid), ("interfaceId", interface_id), ("ipAddressId", ip_address_uid), ("deviceId", device_id)] for q_field, q_value in queries: brains = self.layer3_catalog.search(Eq(q_field, q_value)) self.assertEqual(len(brains), 1) self.assertEqual(brains[0].networkId, network_uid) self.assertEqual(brains[0].interfaceId, interface_id) self.assertEqual(brains[0].ipAddressId, ip_address_uid) self.assertEqual(brains[0].deviceId, device_id) # Search with keywords as the query kw = {q_field: q_value} brains = self.layer3_catalog(**kw) self.assertEqual(len(brains), 1) self.assertEqual(brains[0].networkId, network_uid) self.assertEqual(brains[0].interfaceId, interface_id) self.assertEqual(brains[0].ipAddressId, ip_address_uid) self.assertEqual(brains[0].deviceId, device_id) # Search with both (query is AdvancedQuery) query = Eq("networkId", network_uid) brains = self.layer3_catalog(query, interfaceId=interface_id) self.assertEqual(len(brains), 1) self.assertEqual(brains[0].networkId, network_uid) self.assertEqual(brains[0].interfaceId, interface_id) self.assertEqual(brains[0].ipAddressId, ip_address_uid) self.assertEqual(brains[0].deviceId, device_id) # Search with both (query is dict) query = {"networkId": network_uid} brains = self.layer3_catalog(query, interfaceId=interface_id) self.assertEqual(len(brains), 1) self.assertEqual(brains[0].networkId, network_uid) self.assertEqual(brains[0].interfaceId, interface_id) self.assertEqual(brains[0].ipAddressId, ip_address_uid) self.assertEqual(brains[0].deviceId, device_id)
def searchForMembers(self, REQUEST=None, **kw): """Search for users (not groups, not groups-as-users) of a site """ if REQUEST: dict = REQUEST else: dict = kw name = dict.get('name', '') # Split up search terms but leave quoted ones together try: names = shlex.split(name) except ValueError: try: names = shlex.split(name.replace("'", "\\'")) except ValueError: names = shlex.split(name.replace("'", "\\'") + '"') # Short circuit: if all that was asked for was '*', just return everyone if names == ['*']: query = And() else: queries = [] for name in names: queries.extend([ MatchGlob('fullname', name), MatchGlob('email', name), Eq('getUserName', name) ]) query = Or(*queries) zLOG.LOG('MembershipTool', zLOG.BLATHER, 'Querying: %s' % query) catalog = getToolByName(self, 'member_catalog') return catalog.evalAdvancedQuery(query, ('surname', 'firstname'))
def getDevProdStateJSON(self, prodStates=['Maintenance']): """ Return a map of device to production state in a format suitable for a YUI data table. @return: A JSON representation of a dictionary describing devices @rtype: "{ 'columns':['Device', 'Prod State'], 'data':[ {'Device':'<a href=/>', 'Prod State':'Production'}, {'Device':'<a href=/>', 'Prod State':'Maintenance'}, ]}" """ devroot = self.context.dmd.Devices if isinstance(prodStates, basestring): prodStates = [prodStates] orderby, orderdir = 'id', 'asc' catalog = getattr(devroot, devroot.default_catalog) queries = [] for state in prodStates: queries.append(Eq('getProdState', state)) query = Or(*queries) objects = catalog.evalAdvancedQuery(query, ((orderby, orderdir),)) devs = (x.getObject() for x in objects) mydict = {'columns':['Device', 'Prod State'], 'data':[]} for dev in devs: if not self.context.checkRemotePerm(ZEN_VIEW, dev): continue mydict['data'].append({ 'Device' : dev.getPrettyLink(), 'Prod State' : dev.getProdState() }) if len(mydict['data'])>=100: break return mydict
def getDeviceBrains(self, uid=None, start=0, limit=50, sort='name', dir='ASC', params=None, hashcheck=None): cat = ICatalogTool(self._getObject(uid)) reverse = dir=='DESC' qs = [] query = None globFilters = {} if params is None: params = {} for key, value in params.iteritems(): if key == 'ipAddress': ip = ensureIp(value) try: checkip(ip) except IpAddressError: pass else: if numbip(ip): minip, maxip = getSubnetBounds(ip) qs.append(Between('ipAddress', str(minip), str(maxip))) elif key == 'deviceClass': qs.append(MatchRegexp('uid', '(?i).*%s.*' % value)) elif key == 'productionState': qs.append(Or(*[Eq('productionState', str(state)) for state in value])) else: globFilters[key] = value if qs: query = And(*qs) brains = cat.search('Products.ZenModel.Device.Device', start=start, limit=limit, orderby=sort, reverse=reverse, query=query, globFilters=globFilters, hashcheck=hashcheck) return brains
def search(self, *args, **kwargs): if self.objectImplements: dict_query = { "objectImplements" : self.objectImplements } current_query = kwargs.get("query") if not current_query: kwargs["query"] = dict_query else: if isinstance(current_query, dict): values = current_query.get("objectImplements", []) if isinstance(values, basestring): values = [ values ] for value in self.objectImplements: if value not in values: values.append(value) kwargs["query"]["objectImplements"] = values else: # it is advanced query advanced_query = [ Eq("objectImplements", value) for value in self.objectImplements ] if len(advanced_query) > 1: advanced_query = And(*advanced_query) else: advanced_query = advanced_query[0] kwargs["query"] = And(current_query, advanced_query) if self.fields: current_fields = kwargs.get("fields") if not current_fields: kwargs["fields"] = self.fields else: if isinstance(current_fields, basestring): current_fields = [ current_fields ] current_fields.extend(self.fields) kwargs["fields"] = current_fields return self.model_catalog(*args, **kwargs)
def _getDeviceBatch(self, selectstatus='none', goodevids=[], badevids=[], offset=0, count=50, filter='', orderby='titleOrId', orderdir='asc'): unused(count, offset, orderby, orderdir) if not isinstance(goodevids, (list, tuple)): goodevids = [goodevids] if not isinstance(badevids, (list, tuple)): badevids = [badevids] if selectstatus == 'all': idquery = ~In('id', badevids) else: idquery = In('id', goodevids) devfilter = '(?is).*%s.*' % filter filterquery = Or(MatchRegexp('id', devfilter), MatchRegexp('name', devfilter), MatchRegexp('text_ipAddress', devfilter), MatchRegexp('deviceClassPath', devfilter)) query = Eq('uid', self.context.absolute_url_path()) & idquery query = query & filterquery catalog = IModelCatalogTool(self.context) objects = catalog.search(query=query) return [x['id'] for x in objects]
def to_advanced_query(query): """ convert a dictionary to an advanced query """ # nothing to do if not query: return Eq("Title", "") a_query = None def get_query_expression_for(value): # return the Advanced Query Expression if type(value) in (tuple, list): return In if type(value) is dict: return Generic return Eq for k, v in query.iteritems(): exp = get_query_expression_for(v) # first loop, build the initial query expression if a_query is None: a_query = exp(k, v) else: a_query = a_query & exp(k, v) return a_query
def _getAdvancedQueryDeviceList(self, offset=0, count=50, filter='', orderby='name', orderdir='asc'): """ Ask the catalog for devices matching the criteria specified. """ context = self.context if not isinstance(context, DeviceOrganizer): context = self.context.dmd.Devices catalog = IModelCatalogTool(context).devices devfilter = '(?is).*%s.*' % filter filterquery = Or(MatchRegexp('id', devfilter), MatchRegexp('name', devfilter), MatchRegexp('text_ipAddress', devfilter), MatchRegexp('deviceClassPath', devfilter)) query = Eq('uid', context.absolute_url_path()) & filterquery objects = catalog.search(query=query, start=int(offset), limit=int(count), order_by=orderby, reverse=orderdir != 'asc') objects = list(objects) totalCount = len(objects) return totalCount, objects
def _buildQuery(self, types, paths, depth, query, filterPermissions): qs = [] if query is not None: qs.append(query) # Build the path query if not paths: paths = ('/'.join(self.context.getPhysicalPath()), ) q = {'query': paths} if depth is not None: q['depth'] = depth pathq = Generic('path', q) qs.append(pathq) # Build the type query if not isinstance(types, (tuple, list)): types = (types, ) subqs = [Eq('objectImplements', dottedname(t)) for t in types] if subqs: # Don't unnecessarily nest in an Or if there is only one type query typeq = subqs[0] if len(subqs) == 1 else Or(*subqs) qs.append(typeq) # filter based on permissions if filterPermissions: qs.append( In('allowedRolesAndUsers', allowedRolesAndGroups(self.context))) # Consolidate into one query return And(*qs)
def results(self, start, until=None): today = DateTime() today = DateTime(today.year(), today.month(), today.day()) start = DateTime(start) start = DateTime(start.year(), start.month(), start.day()) query = Indexed('chimpfeeds') & \ In('review_state', ('published', )) & \ Ge('feedSchedule', start) if until: try: until = DateTime(until) except DateTime.SyntaxError: pass else: query = query & Le('feedSchedule', until) site = getToolByName(self.context, "portal_url").getPortalObject() settings = IFeedSettings(site) if settings.use_moderation: query = query & Eq('feedModerate', True) catalog = getToolByName(self.context, "portal_catalog") extras = [] utilities = getUtilitiesFor(IGroupExtras) groups = InterestGroupVocabulary()(self.context) for name, util in utilities: for group in groups: extras.extend(util.items(group.title, start, until)) return list(catalog.evalAdvancedQuery( query, (('feedSchedule', 'desc'), ))) + extras
def getSubComponents(self, dmd): i = 0 catalog = ICatalogTool(dmd.Devices) COMPONENT = 'Products.ZenModel.DeviceComponent.DeviceComponent' query = Eq('monitored', '1') for brain in catalog.search(COMPONENT, query=query): i += 1 obj = None try: obj = brain.getObject() except KeyError: continue dev = obj.device() status = obj.getStatus() row = (dict(getParentDeviceTitle=obj.getParentDeviceTitle(), hostname=obj.getParentDeviceTitle(), name=obj.name(), meta_type=obj.meta_type, getInstDescription=obj.getInstDescription(), getStatusString=obj.convertStatus(status), getDeviceLink=obj.getDeviceLink(), getPrimaryUrlPath=obj.getPrimaryUrlPath(), cssclass=obj.getStatusCssClass(status), status=status)) obj._p_invalidate() dev._p_invalidate() if i % 100 == 0: transaction.abort() yield Utils.Record(**row)
def keyword_search(obj, keyword, types=(), meta_type=None): """Generate objects with a matching serial number.""" keyword_query = Eq('searchKeywords', keyword) query = None if meta_type: query = And(Eq('meta_type', meta_type), keyword_query) else: query = keyword_query for brain in ICatalogTool(obj.dmd).search(types, query=query): try: yield brain.getObject() except Exception: # ignore a stale entry pass
def _findDevices(self, identifier, ipAddress, limit=None): """ Returns a tuple ([device brains], [devices]) searching manage IP and interface IPs. limit is the maximum total number in both lists. """ dev_cat = IModelCatalogTool(self._devices) try: ip_address = next(i for i in (ipAddress, identifier) if isip(i)) ip_decimal = ipToDecimal(ip_address) except Exception: ip_address = None ip_decimal = None quoted_id = quote_and_escape(identifier) query_set = Or(Eq('id', quoted_id), Eq('name', quoted_id)) if ip_decimal is not None: query_set.addSubquery(Eq('decimal_ipAddress', str(ip_decimal))) device_brains = list( dev_cat.search(types=Device, query=query_set, limit=limit, filterPermissions=False, fields=["uid", "uuid"])) if device_brains: return device_brains, [] if ip_decimal is not None: # don't search interfaces for 127.x.x.x IPv4 addresses if ipToDecimal('126.255.255.255') < ip_decimal < ipToDecimal( '128.0.0.0'): ip_decimal = None # don't search interfaces for the ::1 IPv6 address elif ipToDecimal('::1') == ip_decimal: ip_decimal = None if ip_decimal is None: return [], [] net_cat = IModelCatalogTool(self._networks) results = net_cat.search(types=IpAddress, query=(Eq('name', ip_address)), limit=limit, filterPermissions=False) devices = [brain.getObject().device() for brain in results] return device_brains, devices
def get_possible_faqs(self): log.info('get_possible_faqs') queries = [] title = In('Title', ["*frequently*", "*faq*", "FAQ*", "Frequently*"]) portal_type = In("portal_type", ["Document", "RichDocument", "Folder"]) ids = ["faq", "faq.php", "faq.stm", "faqs"] for i in range(0, 10): ids.append('faq%d.stm' % i) ids.append('faq0%d.php' % i) id = In('getId', ids) body = Eq('SearchableText', "FAQ") fop = Eq('path', '/osha/portal/fop') advanced_query = And(Or(id, title, body), portal_type, Not(fop)) ls = self.portal_catalog.evalAdvancedQuery(advanced_query, (('Date', 'desc'), )) # XXX: Didn't work :( # ls = self.portal_catalog( # getId='faq.php', # path='/osha/portal/en/good_practice/priority_groups/disability/') ls = self.portal_catalog( getId='faq2.stm', path='osha/en/good_practice/topics/dangerous_substances/faq2.stm') # ls = self.portal_catalog( # getId='faq.php', # path='osha/en/good_practice/topics/accident_prevention/') log.info("Processing FAQs: %s" % "\n".join([i.getURL() for i in ls])) odict = {} for l in ls: o = l.getObject() odict[o.absolute_url()] = o ts = o.getTranslations().values() for t in ts: odict[t[0].absolute_url()] = t[0] objects = odict.values() return objects k = ['/'.join(o.getPhysicalPath()) for o in objects] k.sort() display_str = '\n'.join(k) or 'none' return display_str
def __call__(self, context): terms = [] today = DateTime() today = DateTime(today.year(), today.month(), today.day()) query = (Indexed('chimpfeeds') & Eq('allowedRolesAndUsers', 'Anonymous') & (Ge('feedSchedule', today) | Eq('feedModerate', False))) brains = context.portal_catalog.evalAdvancedQuery( query, (('feedSchedule', 'desc'), )) for brain in brains: rid = brain.getRID() terms.append(SimpleTerm(rid, rid, brain.Title)) return SimpleVocabulary(terms)
def findMatchingOrganizers(self, organizerClass, organizerPath, userFilter): filterRegex = '(?i)^%s.*%s.*' % (organizerPath, userFilter) if self.validRegex(filterRegex): orgquery = (Eq('objectImplements','Products.ZenModel.%s.%s' % (organizerClass, organizerClass)) & MatchRegexp('uid', filterRegex)) paths = [b.getPath() for b in ICatalogTool(self._dmd).search(query=orgquery)] if paths: return Generic('path', {'query':paths})
def search_brain(self, uid, context, fields=None, commit_dirty=False): """ """ tx_state = self._get_tx_state() if commit_dirty: self.do_mid_transaction_commit() query = Eq(OBJECT_UID_FIELD, uid) search_params = SearchParams(query, fields=fields) search_params = self._add_tx_state_query(search_params, tx_state) return self._do_search(search_params, context)
def validate_global_catalog(self): # Get Devices using AdvancedQuery query = Eq("objectImplements", "Products.ZenModel.Device.Device") brains = self.global_catalog.search(query) self.assertEqual(len(brains), self.n_devices) # Get Devices using dict query query = {"objectImplements": "Products.ZenModel.Device.Device"} brains = self.global_catalog.search(query) self.assertEqual(len(brains), self.n_devices) # Get Locations query = Eq("objectImplements", "Products.ZenModel.Location.Location") brains = self.global_catalog.search(query) self.assertEqual(len(brains), self.n_devices + 1) # n_devices plus root node # Get Devices query = [] query.append(Eq("objectImplements", "Products.ZenModel.Device.Device")) query.append(Eq("objectImplements", "Products.ZenModel.IpAddress.IpAddress")) brains = self.global_catalog.search(Or(*query)) self.assertEqual(len(brains), 2*self.n_devices) # Get a device by uid d = self.devices[0] uid = "/".join(d.device.getPrimaryPath()) # global_catalog strips /zport/dmd/ uid = uid[10:] # using AdvancedQuery brains = self.global_catalog.search(Eq("uid", uid)) self.assertEqual(len(brains), 1) # using dict query brains = self.global_catalog.search({"uid": uid}) self.assertEqual(len(brains), 1)