def __init__(self, context, request, view, manager, data): self.context = context self.data = data self.request = request tags = getTagRootTags(context) tag_root = getInterfaceRoot(context, ITagRoot) root_path ='/'.join(tag_root.getPhysicalPath()) catalog_tool = getToolByName(context, "portal_catalog") query = {} query['object_provides'] = ITaggable.__identifier__ query['path'] = root_path tag_occurrence = {} for tag in tags: query['tags'] = safe_utf8(tag) tag_occurrence[tag] = len(catalog_tool(query)) weight_list = tag_occurrence.values() weight_list.sort() if weight_list: minimal = weight_list[:1][0] maximal = weight_list[-1:][0] maxsize = float(self.data.maxsize) minsize = float(self.data.minsize) tag_cloud = [] for tag in tags: try: size = float((maxsize * \ (tag_occurrence[tag] - minimal))) / \ float((maximal - minimal)) except ZeroDivisionError: size = 1 if tag_occurrence[tag] <= minimal or size < minsize: size = float(self.data.minsize) info = dict(title=tag, font_size=round(size, 1)) tag_cloud.append(info) tag_cloud.sort(lambda x, y: cmp(x['title'].lower(), y['title'].lower())) self.tag_cloud = tag_cloud else: self.tag_cloud = [] self.tag_root_url = tag_root.absolute_url()
def __init__(self, context, request, view, manager, data): self.context = context self.data = data self.request = request tags = getTagRootTags(context) tag_root = getInterfaceRoot(context, ITagRoot) root_path = '/'.join(tag_root.getPhysicalPath()) catalog_tool = getToolByName(context, "portal_catalog") query = {} query['object_provides'] = ITaggable.__identifier__ query['path'] = root_path tag_occurrence = {} for tag in tags: query['tags'] = tag.decode('utf-8') tag_occurrence[tag] = len(catalog_tool(query)) weight_list = tag_occurrence.values() weight_list.sort() if weight_list: minimal = weight_list[:1][0] maximal = weight_list[-1:][0] maxsize = float(self.data.maxsize) minsize = float(self.data.minsize) tag_cloud = [] for tag in tags: try: size = float((maxsize * \ (tag_occurrence[tag] - minimal))) / \ float((maximal - minimal)) except ZeroDivisionError: size = 1 if tag_occurrence[tag] <= minimal or size < minsize: size = float(self.data.minsize) info = dict(title=tag, font_size=round(size, 1)) tag_cloud.append(info) tag_cloud.sort(lambda x, y: cmp(x['title'], y['title'])) self.tag_cloud = tag_cloud else: self.tag_cloud = [] self.tag_root_url = tag_root.absolute_url()
def test_getTagRootTags(self): brain1 = self.stub() self.expect(brain1.tags).result(('foo', 'bar', u'\xe4', '\xc3\xa4')) brain2 = self.stub() self.expect(brain2.tags).result(('bar', u'baz')) brain3 = self.stub() self.expect(brain3.tags).result(None) root = self.providing_stub([ITagRoot]) obj = self.set_parent(self.stub(), root) self.expect(root.getPhysicalPath()).result(['', 'path', 'to', 'root']) catalog = self.stub() self.mock_tool(catalog, 'portal_catalog') query = {'path': '/path/to/root', 'object_provides': ITaggable.__identifier__} self.expect(catalog(query)).result([brain1, brain2]) self.replay() self.assertEqual(set(utils.getTagRootTags(obj)), set(['foo', 'bar', 'baz', '\xc3\xa4']))
def tagVocabulary(context): """Vocabulary factory for tags within a given section of the site, delimited by an ITagRoot interface Inspired by "plone.app.vocabularies.catalog.KeywordsVocabulary" for the encoding stuff. """ root_tags = getTagRootTags(context) def safe_encode(term): if isinstance(term, unicode): # no need to use portal encoding for transitional encoding from # unicode to ascii. utf-8 should be fine. term = term.encode('utf-8') return term # Vocabulary term tokens *must* be 7 bit values, term titles *must* be unicode. items = [ SimpleTerm(tag, b2a_qp(safe_encode(tag)), safe_unicode(tag)) for tag in root_tags ] return SimpleVocabulary(items)
def tagVocabulary(context): """Vocabulary factory for tags within a given section of the site, delimited by an ITagRoot interface """ return SimpleVocabulary.fromValues(getTagRootTags(context))