def test_map_operations_missing(self): """tests if nonexisting referenced operations are properly skipped""" registry = self.createRegistry(td.test_missing_operator_xml) reader = IQuerystringRegistryReader(registry) result = reader.parseRegistry() result = reader.mapOperations(result) operators = result.get("plone.app.querystring.field.created.operators").keys() self.assertTrue("plone.app.querystring.operation.date.lessThan" in operators) self.assertFalse("plone.app.querystring.operation.date.largerThan" in operators)
def test_get_vocabularies(self): """tests if getVocabularyValues is returning the correct vocabulary values in the correct format """ registry = self.createRegistry(td.test_vocabulary_xml) reader = IQuerystringRegistryReader(registry) result = reader.parseRegistry() result = reader.getVocabularyValues(result) vocabulary_result = result.get("plone.app.querystring.field.reviewState.values") self.assertEqual(vocabulary_result, {"foo": {"title": u"bar"}})
def test_map_operations_clean(self): """tests if mapOperations is getting all operators correctly""" registry = self.createRegistry(td.minimal_correct_xml) reader = IQuerystringRegistryReader(registry) result = reader.parseRegistry() result = reader.mapOperations(result) operations = result.get("plone.app.querystring.field.created.operations") operators = result.get("plone.app.querystring.field.created.operators") for operation in operations: self.assertTrue(operation in operators)
def test_get_vocabularies(self): """tests if getVocabularyValues is returning the correct vocabulary values in the correct format """ registry = self.createRegistry(td.test_vocabulary_xml) reader = IQuerystringRegistryReader(registry) result = reader.parseRegistry() result = reader.getVocabularyValues(result) vocabulary_result = result.get( 'plone.app.querystring.field.reviewState.values') self.assertEqual(vocabulary_result, {'foo': {'title': u'bar'}})
def _labels(self): """ Get indexes labels from portal_atct settings """ registry = getUtility(IRegistry) config = IQuerystringRegistryReader(registry)() indexes = config.get('indexes', {}) res = {} for index, ob in indexes.items(): res[index] = ob.get('title', index) return res
def listSortFields(self): """Return a list of available fields for sorting.""" registry = getUtility(IRegistry) config = IQuerystringRegistryReader(registry)() indexes = config.get('sortable_indexes', {}) for name, index in indexes.items(): title = index.get('title', name) description = index.get('description', title) yield (name, title, description)
def test_map_operations_clean(self): """tests if mapOperations is getting all operators correctly""" registry = self.createRegistry(td.minimal_correct_xml) reader = IQuerystringRegistryReader(registry) result = reader.parseRegistry() result = reader.mapOperations(result) operations = result.get( 'plone.app.querystring.field.created.operations') operators = result.get('plone.app.querystring.field.created.operators') for operation in operations: self.assertTrue(operation in operators)
def listSortFields(self): """Return a list of available fields for sorting.""" registry = getUtility(IRegistry) config = IQuerystringRegistryReader(registry)() indexes = config.get("sortable_indexes", {}) for name, index in indexes.items(): title = index.get("title", name) description = index.get("description", title) yield (name, title, description)
def test_map_operations_missing(self): """tests if nonexisting referenced operations are properly skipped""" registry = self.createRegistry(td.test_missing_operator_xml) reader = IQuerystringRegistryReader(registry) result = reader.parseRegistry() result = reader.mapOperations(result) operators = result.get( 'plone.app.querystring.field.created.operators').keys() self.assertTrue( 'plone.app.querystring.operation.date.lessThan' in operators) self.assertFalse( 'plone.app.querystring.operation.date.largerThan' in operators)
def listSortFields(self): """Return a list of available fields for sorting.""" registry = getUtility(IRegistry) config = IQuerystringRegistryReader(registry) indexes = config.parseRegistry() sortable = config.mapSortableIndexes(indexes)['sortable'] for name, index in sortable.items(): title = index.get('title', name) description = index.get('description', title) yield (name, title, description)
def test_sortable_indexes(self): """tests if sortable indexes from the registry will be available in the parsed registry """ registry = self.createRegistry(td.minimal_correct_xml) reader = IQuerystringRegistryReader(registry) result = reader.parseRegistry() result = reader.mapOperations(result) result = reader.mapSortableIndexes(result) sortables = result['sortable'] # there should be one sortable index self.assertEqual(len(sortables), 1) # confirm that every sortable really is sortable for field in sortables.values(): self.assertEqual(field['sortable'], True)
def test_registry_adapter(self): """tests the __call__ method of the IQuerystringRegistryReader adapter """ registry = self.createRegistry(td.minimal_correct_xml) reader = IQuerystringRegistryReader(registry) result = reader() self.assertEqual(result.keys(), ['sortable_indexes', 'indexes'])
def migrate_topics(portal): """Migrate ATContentTypes Topics to plone.app.contenttypes Collections. This could also be used as upgrade step. """ # Parse the registry to get allowed operations and pass it to the # migrator. reg = getUtility(IRegistry) reader = IQuerystringRegistryReader(reg) registry = reader.parseRegistry() # select migrator based on the base-class of collections fti = portal.portal_types['Collection'] if fti.content_meta_type == 'Dexterity Item': migrator = TopicMigrator elif fti.content_meta_type == 'Dexterity Container': migrator = FolderishTopicMigrator walker = CustomQueryWalker(portal, migrator)(registry=registry) return walker
def migrate_topics(portal): """Migrate ATContentTypes Topics to plone.app.contenttypes Collections. This could also be used as upgrade step. """ # Parse the registry to get allowed operations and pass it to the # migrator. reg = getUtility(IRegistry) reader = IQuerystringRegistryReader(reg) registry = reader.parseRegistry() # select migrator based on the base-class of collections fti = portal.portal_types['Collection'] if fti.content_meta_type == "Dexterity Item": migrator = TopicMigrator elif fti.content_meta_type == "Dexterity Container": migrator = FolderishTopicMigrator walker = CustomQueryWalker(portal, migrator)(registry=registry) return walker
def __call__(self, old_topic): """Store the criteria of the old Topic. Store the info on the migrator and restore the values in the migrate_criteria method. """ reg = getUtility(IRegistry) reader = IQuerystringRegistryReader(reg) self.registry = reader.parseRegistry() self.old = old_topic self._collection_sort_reversed = None self._collection_sort_on = None self._collection_query = None path = "/".join(self.old.getPhysicalPath()) # Get the old criteria. # See also Products.ATContentTypes.content.topic.buildQuery criteria = self.old.listCriteria() logger.debug( "Old criteria for %s: %r", path, [(crit, crit.getCriteriaItems()) for crit in criteria], ) formquery = [] for criterion in criteria: type_ = criterion.__class__.__name__ if type_ in ["ATSortCriterion", "FormSortCriterion"]: # Sort order and direction are now stored in the Collection. self._collection_sort_reversed = criterion.getReversed() self._collection_sort_on = criterion.Field() continue converter = CONVERTERS.get(type_) if converter is None: msg = "[SKIPPED] - Unsupported criterion {0}".format(type_) logger.warning(msg) continue converter(formquery, criterion, self.registry) logger.debug("New query for %s: %r", path, formquery) return formquery
def _widget_args(self): args = super(QueryStringWidget, self)._widget_args() registry = getUtility(IRegistry) config = IQuerystringRegistryReader(registry)() if 'pattern_options' not in args: args['pattern_options'] = {} args['pattern_options'].update(config) return args
def __call__(self, version=None, include_items=False): topic_metadata = super(SerializeTopicToJson, self).__call__(version=version) # migrate criteria formquery = [] reg = getUtility(IRegistry) reader = IQuerystringRegistryReader(reg) self.registry = reader.parseRegistry() criteria = self.context.listCriteria() for criterion in criteria: type_ = criterion.__class__.__name__ if type_ == "ATSortCriterion": # Sort order and direction are now stored in the Collection. self._collection_sort_reversed = criterion.getReversed() self._collection_sort_on = criterion.Field() logger.debug( "Sort on %r, reverse: %s.", self._collection_sort_on, self._collection_sort_reversed, ) continue converter = CONVERTERS.get(type_) if converter is None: msg = "Unsupported criterion {0}".format(type_) logger.error(msg) raise ValueError(msg) try: converter(formquery, criterion, self.registry) except Exception as e: logger.info(e) topic_metadata["query"] = json_compatible(formquery) # migrate batch size if self.context.itemCount: topic_metadata["b_size"] = self.context.itemCount return topic_metadata
def test_querystring_widget(self): registry = queryUtility(IRegistry) config = IQuerystringRegistryReader(registry)() self.assertEqual( sorted(config['indexes']['taxonomy_test']['values'].items()), [('1', { 'title': u'Information Science' }), ('2', { 'title': u'Information Science \xbb Book Collecting' }), ('3', { 'title': u'Information Science \xbb Chronology' }), ('5', { 'title': u'Information Science \xbb Sport' })])
def _widget_args(self, context, field, request): args = super(QueryStringWidget, self)._widget_args(context, field, request) registry = getUtility(IRegistry) config = IQuerystringRegistryReader(registry)() if 'pattern_options' not in args: args['pattern_options'] = {} args['pattern_options'].update(config) criterias = [dict(c) for c in field.getRaw(context)] args['value'] = request.get(field.getName(), json.dumps(criterias)) return args
def getConfig(self): """get the config""" registry = getUtility(IRegistry) registryreader = IQuerystringRegistryReader(registry) config = registryreader() # Group indices by "group", order alphabetically groupedIndexes = {} for indexName in config['indexes']: index = config['indexes'][indexName] if index['enabled']: group = index['group'] if group not in groupedIndexes: groupedIndexes[group] = [] groupedIndexes[group].append((index['title'], indexName)) # Sort each index list [a.sort() for a in groupedIndexes.values()] config['groupedIndexes'] = groupedIndexes return config
def test_querystring_widget(self): registry = queryUtility(IRegistry) config = IQuerystringRegistryReader(registry)() self.assertEqual( sorted(config["indexes"]["taxonomy_test"]["values"].items()), [ ("1", { "title": u"Information Science" }), ("2", { "title": u"Information Science \xbb Book Collecting" }), ("3", { "title": u"Information Science \xbb Chronology" }), ("5", { "title": u"Information Science \xbb Sport" }), ("55", { "title": u"Information Science \xbb Cars" }), ], )
def __call__(self): registry = getUtility(IRegistry) config = IQuerystringRegistryReader(registry)() self.request.response.setHeader('Content-Type', 'application/json; charset=utf-8') return json.dumps(config)
def __call__(self): registry = getUtility(IRegistry) config = IQuerystringRegistryReader(registry)() self.request.response.setHeader("Content-Type", "application/json") return json.dumps(config)
def test_parse_registry(self): """tests if the parsed registry data is correct""" registry = self.createRegistry(td.minimal_correct_xml) reader = IQuerystringRegistryReader(registry) result = reader.parseRegistry() self.assertEqual(result, td.parsed_correct)
def getConfig(self): """get the config""" registry = getUtility(IRegistry) prefix = self.registry_prefix if prefix is not None: # First grab the base config's operations registryreader = IQuerystringRegistryReader(registry) registryreader.prefix = "plone.app.querystring.operation" plone_config = registryreader.parseRegistry() # then merge custom fields registryreader = IQuerystringRegistryReader(registry) registryreader.prefix = prefix config = registryreader.parseRegistry() config = registryreader.getVocabularyValues(config) config.update(plone_config) config = registryreader.mapOperations(config) config = registryreader.mapSortableIndexes(config) config = { 'indexes': config.get(prefix + '.field'), 'sortable_indexes': config.get('sortable'), } else: # First grab the base config's operations registryreader = IQuerystringRegistryReader(registry) registryreader.prefix = "plone.app.querystring" config = registryreader() # Group indices by "group", order alphabetically groupedIndexes = {} for indexName in config['indexes']: index = config['indexes'][indexName] if index['enabled']: group = index['group'] if group not in groupedIndexes: groupedIndexes[group] = [] groupedIndexes[group].append((index['title'], indexName)) # Sort each index list [a.sort() for a in groupedIndexes.values()] config['groupedIndexes'] = groupedIndexes return config
def __call__(self): """Return the registry configuration in JSON format""" registry = getUtility(IRegistry) # First grab the base config, so we can use the operations registryreader = IQuerystringRegistryReader(registry) registryreader.prefix = "plone.app.querystring.operation" op_config = registryreader.parseRegistry() # Then combine our fields registryreader = IQuerystringRegistryReader(registry) registryreader.prefix = self.prefix config = registryreader.parseRegistry() config = registryreader.getVocabularyValues(config) config.update(op_config) registryreader.mapOperations(config) registryreader.mapSortableIndexes(config) config = { 'indexes': config.get(self.prefix + '.field'), 'sortable_indexes': config.get('sortable'), } return json.dumps(config)
def __call__(self): """Return the registry configuration in JSON format""" import pdb pdb.set_trace() registry = getUtility(IRegistry) # First grab the base config, so we can use the operations registryreader = IQuerystringRegistryReader(registry) registryreader.prefix = "plone.app.querystring.operation" op_config = registryreader.parseRegistry() # Then combine our fields registryreader = IQuerystringRegistryReader(registry) registryreader.prefix = self.prefix config = registryreader.parseRegistry() config = registryreader.getVocabularyValues(config) config.update(op_config) registryreader.mapOperations(config) registryreader.mapSortableIndexes(config) return {"indexes": config.get(self.prefix + ".field"), "sortable_indexes": config.get("sortable")}
def getConfig(self): """get the config""" registry = getUtility(IRegistry) prefix = self.registry_prefix if prefix: # First grab the base config's operations registryreader = IQuerystringRegistryReader(registry) registryreader.prefix = "plone.app.querystring.operation" plone_config = registryreader.parseRegistry() # then merge custom fields registryreader = IQuerystringRegistryReader(registry) registryreader.prefix = prefix config = registryreader.parseRegistry() config = registryreader.getVocabularyValues(config) config.update(plone_config) config = registryreader.mapOperations(config) config = registryreader.mapSortableIndexes(config) config = { 'indexes': config.get(prefix + '.field'), 'sortable_indexes': config.get('sortable'), } else: # First grab the base config's operations registryreader = IQuerystringRegistryReader(registry) registryreader.prefix = "plone.app.querystring" config = registryreader() # Group indices by "group", order alphabetically groupedIndexes = {} for indexName in config['indexes']: index = config['indexes'][indexName] if index['enabled']: group = index['group'] if group not in groupedIndexes: groupedIndexes[group] = [] groupedIndexes[group].append((index['title'], indexName)) # Sort each index list [a.sort() for a in groupedIndexes.values()] config['groupedIndexes'] = groupedIndexes return config
def getConfig(self): """get the config""" registry = getUtility(IRegistry) # First grab the base config, so we can use the operations registryreader = IQuerystringRegistryReader(registry) registryreader.prefix = "plone.app.querystring.operation" op_config = registryreader.parseRegistry() # Then combine our fields registryreader = IQuerystringRegistryReader(registry) registryreader.prefix = "bika.lims.analysisrequestquery" config = registryreader.parseRegistry() config = registryreader.getVocabularyValues(config) config.update(op_config) registryreader.mapOperations(config) registryreader.mapSortableIndexes(config) config = { "indexes": config.get("bika.lims.analysisrequestquery.field"), "sortable_indexes": config.get("sortable"), } # Group indices by "group", order alphabetically groupedIndexes = {} for indexName in config["indexes"]: index = config["indexes"][indexName] if index["enabled"]: group = index["group"] if group not in groupedIndexes: groupedIndexes[group] = [] groupedIndexes[group].append((index["title"], indexName)) # Sort each index list [a.sort() for a in groupedIndexes.values()] config["groupedIndexes"] = groupedIndexes return config
def getConfig(self): """get the config""" registry = getUtility(IRegistry) # First grab the base config, so we can use the operations registryreader = IQuerystringRegistryReader(registry) registryreader.prefix = "plone.app.querystring.operation" op_config = registryreader.parseRegistry() # Then combine our fields registryreader = IQuerystringRegistryReader(registry) registryreader.prefix = "bika.lims.analysisrequestquery" config = registryreader.parseRegistry() config = registryreader.getVocabularyValues(config) config.update(op_config) registryreader.mapOperations(config) registryreader.mapSortableIndexes(config) config = { 'indexes': config.get('bika.lims.analysisrequestquery.field'), 'sortable_indexes': config.get('sortable'), } # Group indices by "group", order alphabetically groupedIndexes = {} for indexName in config['indexes']: index = config['indexes'][indexName] if index['enabled']: group = index['group'] if group not in groupedIndexes: groupedIndexes[group] = [] groupedIndexes[group].append((index['title'], indexName)) # Sort each index list [a.sort() for a in groupedIndexes.values()] config['groupedIndexes'] = groupedIndexes return config