def test_get_branches(self): tree = {'Rubrique': {'node1': {'subnode1': {}, 'subnode2': {}}}} result = get_branches(tree) self.assertEqual(len(result), 2) keywords = ['Rubrique/node1/subnode2', 'Rubrique/node1/subnode1'] for keyword in keywords: self.assertIn(keyword, result) tree = {'Rubrique': {'node1': {'subnode1': {'subsubnode2': {'level3': {}}}}}} result = get_branches(tree) self.assertEqual(len(result), 1) keywords = ['Rubrique/node1/subnode1/subsubnode2/level3'] for keyword in keywords: self.assertIn(keyword, result)
def object_keywords(self): tree = getattr(self.context, 'tree', None) branches = [] if tree: branches = get_branches(tree) keywords = [] if branches: keywords = list([k.lower() for k in branches]) return keywords
def test_get_branches(self): tree = {'Rubrique': {'node1': {'subnode1': {}, 'subnode2': {}}}} result = get_branches(tree) self.assertEqual(len(result), 2) keywords = ['Rubrique/node1/subnode2', 'Rubrique/node1/subnode1'] for keyword in keywords: self.assertIn(keyword, result) tree = { 'Rubrique': { 'node1': { 'subnode1': { 'subsubnode2': { 'level3': {} } } } } } result = get_branches(tree) self.assertEqual(len(result), 1) keywords = ['Rubrique/node1/subnode1/subsubnode2/level3'] for keyword in keywords: self.assertIn(keyword, result)
def get_tree_branches(self): return get_branches(getattr(self, 'tree', {}))
def contents_by_keywords(self): results = {} user = get_current() root = self.request.root formview = ContentsByKeywordsForm(self.context, self.request) formview.calculate_posted_filter() validated = getattr(formview, 'validated', {}) default_contents = self.request.analytics_default_content_types states = validated.get('states', []) tree = validated.get('tree', {}) if not tree: keywords = get_all_branches(root.tree) else: keywords = get_branches(tree) if ROOT_TREE in keywords: keywords.remove(ROOT_TREE) types = validated.get('content_types', default_contents) author = validated.get('author', None) date_date = validated.get('dates', {}) date_from = date_date.get('date_from', None) date_to = date_date.get('date_to', None) contribution_filter = {'authors': []} if author is not None: contribution_filter = {'authors': [author]} localizer = self.request.localizer has_value = False for type_ in types: title = getattr(core.SEARCHABLE_CONTENTS.get(type_), 'type_title', type_) filter_ = { 'metadata_filter': { 'content_types': [type_], 'states': states, 'tree': tree }, 'contribution_filter': contribution_filter } data = get_contents_by_keywords(filter_, user, root, date_from, date_to) has_value = has_value or data[1] > 0 results[localizer.translate(title)] = { 'data': data[0], 'len': data[1], 'color': self.color_mapping.get(type_) } labels = [list(v['data'].keys()) for v in results.values()] labels = sorted( list( set([(item, item) for sublist in labels for item in sublist if not keywords or item in keywords]))) study = self.content(args={'results': results}, template=self.analytics_study)['body'] values = { 'has_value': has_value, 'analytics': results, 'labels': OrderedDict(labels), 'view': self, 'charts': ['bar', 'doughnut'], 'study': study, 'id': 'keywords' } body = self.content(args=values, template=self.analytics_template)['body'] return {'body': body}