def setUp(self): self.env = EnvironmentStub(enable=['trac.*', 'tractags.*']) self.env.path = tempfile.mkdtemp() self.db = self.env.get_db_cnx() setup = TagSetup(self.env) setup.upgrade_environment(self.db) self.tag_twm = TagWikiMacros(self.env)
def setUp(self): _BaseTestCase.setUp(self) self.req = MockRequest(self.env, path_info='/wiki/TagCloudPage', authname='user') self.context = Mock(env=self.env, href=self.req.href, req=self.req) self.formatter = Mock(context=self.context, req=self.req) self.tag_twm = TagWikiMacros(self.env)
def setUp(self): _BaseTestCase.setUp(self) self.req = Mock(path_info='/wiki/ListTaggedPage', args={}, authname='user', perm=MockPerm(), href=Href('/'), abs_href=Href('http://example.org/trac/'), chrome={}, session={}, locale='', tz='') self.tag_twm = TagWikiMacros(self.env)
def setUp(self): self.env = EnvironmentStub( enable=['trac.*', 'tractags.*']) self.env.path = tempfile.mkdtemp() # TagTemplateProvider is abstract, test using a subclass self.tag_wm = TagWikiMacros(self.env)
class ListTaggedMacroTestCase(unittest.TestCase): def setUp(self): self.env = EnvironmentStub( enable=['trac.*', 'tractags.*']) self.env.path = tempfile.mkdtemp() self.req = Mock(path_info='/wiki/ListTaggedPage', args={}, authname='user', perm=MockPerm(), href=Href('/'), abs_href=Href('http://example.org/trac/'), chrome={}, session={}, locale='', tz='') self.db = self.env.get_db_cnx() cursor = self.db.cursor() cursor.execute("DROP TABLE IF EXISTS tags") cursor.execute("DROP TABLE IF EXISTS tags_change") cursor.execute("DELETE FROM system WHERE name='tags_version'") cursor.execute("DELETE FROM permission WHERE action %s" % self.db.like(), ('TAGS_%',)) setup = TagSetup(self.env) setup.upgrade_environment(self.db) self.tag_twm = TagWikiMacros(self.env) def tearDown(self): shutil.rmtree(self.env.path) def test_empty_content(self): context = Mock(env=self.env, href=Href('/'), req=self.req) formatter = Mock(context=context, req=self.req) self.assertTrue('No resources found' in str(self.tag_twm.expand_macro(formatter, 'ListTagged', '')))
class ListTaggedMacroTestCase(unittest.TestCase): def setUp(self): self.env = EnvironmentStub(enable=['trac.*', 'tractags.*']) self.env.path = tempfile.mkdtemp() self.db = self.env.get_db_cnx() setup = TagSetup(self.env) setup.upgrade_environment(self.db) self.tag_twm = TagWikiMacros(self.env) def tearDown(self): shutil.rmtree(self.env.path) def test_empty_content(self): req = Mock(path_info='/wiki/ListTaggedPage', args={}, authname='user', perm=PermissionCache(self.env, 'user'), href=Href('/'), abs_href='http://example.org/trac/', chrome={}, session={}, locale='', tz='') context = Mock(env=self.env, href=Href('/'), req=req) formatter = Mock(context=context, req=req) self.assertTrue('No resources found' in str( self.tag_twm.expand_macro(formatter, 'ListTagged', '')))
class ListTaggedMacroTestCase(unittest.TestCase): def setUp(self): self.env = EnvironmentStub( enable=['trac.*', 'tractags.*']) self.env.path = tempfile.mkdtemp() self.db = self.env.get_db_cnx() setup = TagSetup(self.env) setup.upgrade_environment(self.db) self.tag_twm = TagWikiMacros(self.env) def tearDown(self): shutil.rmtree(self.env.path) def test_empty_content(self): req = Mock(path_info='/wiki/ListTaggedPage', args={}, authname='user', perm=PermissionCache(self.env, 'user'), href=Href('/'), abs_href='http://example.org/trac/', chrome={}, session={}, locale='', tz='' ) context = Mock(env=self.env, href=Href('/'), req=req) formatter = Mock(context=context, req=req) self.assertTrue('No resources found' in str(self.tag_twm.expand_macro(formatter, 'ListTagged', '')))
def setUp(self): _BaseTestCase.setUp(self) self.req = Mock(path_info='/wiki/TagCloudPage', args={}, authname='user', perm=MockPerm(), href=Href('/'), abs_href='http://example.org/trac/', chrome={}, session={}, locale='', tz='') self.context = Mock(env=self.env, href=self.req.href, req=self.req) self.formatter = Mock(context=self.context, req=self.req) self.tag_twm = TagWikiMacros(self.env)
def setUp(self): self.env = EnvironmentStub( enable=['trac.*', 'tractags.*']) self.env.path = tempfile.mkdtemp() self.db = self.env.get_db_cnx() setup = TagSetup(self.env) setup.upgrade_environment(self.db) self.tag_twm = TagWikiMacros(self.env)
def setUp(self): self.env = EnvironmentStub( enable=['trac.*', 'tractags.*']) self.env.path = tempfile.mkdtemp() self.req = Mock(path_info='/wiki/ListTaggedPage', args={}, authname='user', perm=MockPerm(), href=Href('/'), abs_href=Href('http://example.org/trac/'), chrome={}, session={}, locale='', tz='') self.db = self.env.get_db_cnx() cursor = self.db.cursor() cursor.execute("DROP TABLE IF EXISTS tags") cursor.execute("DROP TABLE IF EXISTS tags_change") cursor.execute("DELETE FROM system WHERE name='tags_version'") cursor.execute("DELETE FROM permission WHERE action %s" % self.db.like(), ('TAGS_%',)) setup = TagSetup(self.env) setup.upgrade_environment(self.db) self.tag_twm = TagWikiMacros(self.env)
class ListTaggedMacroTestCase(_BaseTestCase): def setUp(self): _BaseTestCase.setUp(self) self.req = Mock(path_info='/wiki/ListTaggedPage', args={}, authname='user', perm=MockPerm(), href=Href('/'), abs_href=Href('http://example.org/trac/'), chrome={}, session={}, locale='', tz='') self.tag_twm = TagWikiMacros(self.env) def test_empty_content(self): context = Mock(env=self.env, href=Href('/'), req=self.req) formatter = Mock(context=context, req=self.req) self.assertTrue('No resources found' in str( self.tag_twm.expand_macro(formatter, 'ListTagged', ''))) def test_listtagged_exclude(self): self._insert_tags('wiki', 'InterTrac', ('blah', )) self._insert_tags('wiki', 'InterWiki', ('blah', )) self._insert_tags('wiki', 'WikiStart', ('blah', )) context = Mock(env=self.env, href=Href('/'), req=self.req) formatter = Mock(context=context, req=self.req) result = unicode( self.tag_twm.expand_macro(formatter, 'ListTagged', 'blah,exclude=Inter*')) self.assertFalse('InterTrac' in result) self.assertFalse('InterWiki' in result) self.assertTrue('WikiStart' in result) result = unicode( self.tag_twm.expand_macro(formatter, 'ListTagged', 'blah,exclude=Wi*:*ki')) self.assertTrue('InterTrac' in result) self.assertFalse('InterWiki' in result) self.assertFalse('WikiStart' in result) def _test_listtagged_paginate(self, page, per_page=2): self._insert_tags('wiki', 'InterTrac', ('blah', )) self._insert_tags('wiki', 'InterWiki', ('blah', )) self._insert_tags('wiki', 'WikiStart', ('blah', )) self.req.args['listtagged_per_page'] = per_page self.req.args['listtagged_page'] = page context = Mock(env=self.env, href=Href('/'), req=self.req) formatter = Mock(context=context, req=self.req) result = \ unicode(self.tag_twm.expand_macro(formatter, 'ListTagged', 'blah')) return result def test_listtagged_paginate_page1(self): """Paginate results for page 1 has two items.""" result = self._test_listtagged_paginate(1) self.assertTrue('InterTrac' in result) self.assertTrue('InterWiki' in result) self.assertFalse('WikiStart' in result) def test_listtagged_paginate_page2(self): """Paginate results for page 2 has one item.""" result = self._test_listtagged_paginate(2) self.assertFalse('InterTrac' in result) self.assertFalse('InterWiki' in result) self.assertTrue('WikiStart' in result) def test_listtagged_paginate_page_out_of_range(self): """Out of range page defaults to 1.""" result = self._test_listtagged_paginate(3) self.assertTrue('InterTrac' in result) self.assertTrue('InterWiki' in result) self.assertFalse('WikiStart' in result) def test_listtagged_paginate_page_invalid(self): """Invalid page default to 1.""" result = self._test_listtagged_paginate(-1) self.assertTrue('InterTrac' in result) self.assertTrue('InterWiki' in result) self.assertFalse('WikiStart' in result) def test_listtagged_paginate_per_page_invalid(self): """Invalid per_page defaults to items_per_page (100).""" result = self._test_listtagged_paginate(2, -1) self.assertTrue('InterTrac' in result) self.assertTrue('InterWiki' in result) self.assertTrue('WikiStart' in result)
def setUp(self): _BaseTestCase.setUp(self) # TagTemplateProvider is abstract, test using a subclass self.tag_wm = TagWikiMacros(self.env)
class TagCloudMacroTestCase(_BaseTestCase): def setUp(self): _BaseTestCase.setUp(self) self.req = Mock(path_info='/wiki/TagCloudPage', args={}, authname='user', perm=MockPerm(), href=Href('/'), abs_href='http://example.org/trac/', chrome={}, session={}, locale='', tz='') self.context = Mock(env=self.env, href=self.req.href, req=self.req) self.formatter = Mock(context=self.context, req=self.req) self.tag_twm = TagWikiMacros(self.env) # Helpers def _expand_macro(self, content): return self.tag_twm.expand_macro(self.formatter, 'TagCloud', content) # Tests def test_normal(self): self._insert_tags('wiki', 'CamelCase', ('blah', 'foo', 'bar')) self._insert_tags('wiki', 'InterMapTxt', ('blah', 'foo', 'bar')) self._insert_tags('wiki', 'InterTrac', ('blah', )) self._insert_tags('wiki', 'InterWiki', ('blah', )) self._insert_tags('wiki', 'PageTemplates', ('blah', )) self._insert_tags('wiki', 'RecentChanges', ('blah', 'foo')) self._insert_tags('wiki', 'SandBox', ('blah', 'foo')) self._insert_tags('ticket', '1', ('blah', )) self._insert_tags('ticket', '2', ('blah', 'bar')) self._insert_tags('ticket', '3', ('blah', 'bar')) self._insert_tags('ticket', '4', ('blah', 'bar')) result = unicode(self._expand_macro('')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertTrue('">foo</a>' in result, repr(result)) self.assertTrue('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('mincount=5')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertFalse('">foo</a>' in result, repr(result)) self.assertTrue('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('mincount=6')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertFalse('">foo</a>' in result, repr(result)) self.assertFalse('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('realm=ticket|wiki')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertTrue('">foo</a>' in result, repr(result)) self.assertTrue('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('realm=ticket')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertFalse('">foo</a>' in result, repr(result)) self.assertTrue('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('realm=ticket,mincount=4')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertFalse('">foo</a>' in result, repr(result)) self.assertFalse('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('realm=unknown')) self.assertEquals('No tags found', result) result = unicode(self._expand_macro('mincount=100')) self.assertEquals('No tags found', result)
def setUp(self): self.env = EnvironmentStub(enable=['trac.*', 'tractags.*']) self.env.path = tempfile.mkdtemp() self.tag_twm = TagWikiMacros(self.env)
def process_request(self, req): req.perm.require('TAGS_VIEW') match = re.match(r'/tags/?(.*)', req.path_info) tag_id = match.group(1) and match.group(1) or None query = req.args.get('q', '') realms = [p.get_taggable_realm() for p in self.tag_providers if (not hasattr(p, 'check_permission') or \ p.check_permission(req.perm, 'view'))] if not (tag_id or query) or [r for r in realms if r in req.args] == []: for realm in realms: if not realm in self.exclude_realms: req.args[realm] = 'on' checked_realms = [r for r in realms if r in req.args] realm_args = dict( zip([r for r in checked_realms], ['on' for r in checked_realms])) if tag_id and not re.match(r"""(['"]?)(\S+)\1$""", tag_id, re.UNICODE): # Convert complex, invalid tag ID's to query expression. req.redirect(req.href.tags(realm_args, q=tag_id)) elif query: single_page = re.match(r"""(['"]?)(\S+)\1$""", query, re.UNICODE) if single_page: # Convert simple query for single tag ID. req.redirect(req.href.tags(single_page.group(2), realm_args)) data = dict(page_title=_("Tags"), checked_realms=checked_realms) # Populate the TagsQuery form field. data['tag_query'] = tag_id and tag_id or query data['tag_realms'] = list( dict(name=realm, checked=realm in checked_realms) for realm in realms) if tag_id: page_name = tag_id page = WikiPage(self.env, page_name) data['tag_page'] = page macros = TagWikiMacros(self.env) if query or tag_id: # TRANSLATOR: The meta-nav link label. add_ctxtnav(req, _("Back to Cloud"), req.href.tags()) macro = 'ListTagged' args = '%s,format=%s,cols=%s,realm=%s' \ % (tag_id and tag_id or query, self.default_format, self.default_cols, '|'.join(checked_realms)) data['mincount'] = None else: macro = 'TagCloud' mincount = as_int(req.args.get('mincount', None), self.cloud_mincount) args = 'mincount=%s,realm=%s' % (mincount, '|'.join(checked_realms)) data['mincount'] = mincount formatter = Formatter(self.env, Context.from_request(req, Resource('tag'))) self.env.log.debug('Tag macro arguments: %s', args) try: # Query string without realm throws 'NotImplementedError'. data['tag_body'] = len(checked_realms) > 0 and \ macros.expand_macro(formatter, macro, args) \ or '' except InvalidQuery, e: data['tag_query_error'] = to_unicode(e) data['tag_body'] = macros.expand_macro(formatter, 'TagCloud', '')
def process_request(self, req): req.perm.require('TAGS_VIEW') match = re.match(r'/tags/?(.*)', req.path_info) tag_id = match.group(1) and match.group(1) or None query = req.args.get('q', '') # Consider only providers, that are permitted for display. tag_system = TagSystem(self.env) all_realms = tag_system.get_taggable_realms(req.perm) if not (tag_id or query) or [r for r in all_realms if r in req.args] == []: for realm in all_realms: if realm not in self.exclude_realms: req.args[realm] = 'on' checked_realms = [r for r in all_realms if r in req.args] if query: # Add permitted realms from query expression. checked_realms.extend(query_realms(query, all_realms)) realm_args = dict(zip([r for r in checked_realms], ['on' for r in checked_realms])) # Switch between single tag and tag query expression mode. if tag_id and not re.match(r"""(['"]?)(\S+)\1$""", tag_id, re.UNICODE): # Convert complex, invalid tag ID's --> query expression. req.redirect(req.href.tags(realm_args, q=tag_id)) elif query: single_page = re.match(r"""(['"]?)(\S+)\1$""", query, re.UNICODE) if single_page: # Convert simple query --> single tag. req.redirect(req.href.tags(single_page.group(2), realm_args)) data = dict(page_title=_("Tags"), checked_realms=checked_realms) # Populate the TagsQuery form field. data['tag_query'] = tag_id and tag_id or query data['tag_realms'] = list(dict(name=realm, checked=realm in checked_realms) for realm in all_realms) if tag_id: data['tag_page'] = WikiPage(self.env, tag_system.wiki_page_prefix + tag_id) if query or tag_id: macro = 'ListTagged' # TRANSLATOR: The meta-nav link label. add_ctxtnav(req, _("Back to Cloud"), req.href.tags()) args = "%s,format=%s,cols=%s" % \ (tag_id and tag_id or query, self.default_format, self.default_cols) data['mincount'] = 0 else: macro = 'TagCloud' mincount = as_int(req.args.get('mincount', 0), self.cloud_mincount) args = mincount and "mincount=%s" % mincount or None data['mincount'] = mincount # When using the given req the page isn't rendered properly. The call # to expand_macro() leads to Chrome().render_template(req, ...). # The function render_template() breaks something in the request handling. # That used to work with Genshi. # # With this mocked req everything is just fine. mock_req = MockRequest(self.env, path_info=req.path_info, authname=req.authname, script_name=req.href()) formatter = Formatter(self.env, web_context(mock_req, Resource('tag'))) self.env.log.debug("%s macro arguments: %s", macro, args and args or '(none)') macros = TagWikiMacros(self.env) try: # Query string without realm throws 'NotImplementedError'. data['tag_body'] = checked_realms and \ macros.expand_macro(formatter, macro, args, realms=checked_realms) \ or '' data['tag_body'] = Markup(to_unicode(data['tag_body'])) except InvalidQuery as e: data['tag_query_error'] = to_unicode(e) data['tag_body'] = macros.expand_macro(formatter, 'TagCloud', '') data['realm_args'] = realm_args add_stylesheet(req, 'tags/css/tractags.css') return 'tag_view.html', data, {'domain': 'tractags'}
class TagCloudMacroTestCase(unittest.TestCase): def setUp(self): self.env = EnvironmentStub( enable=['trac.*', 'tractags.*']) self.env.path = tempfile.mkdtemp() self.req = Mock(path_info='/wiki/TagCloudPage', args={}, authname='user', perm=MockPerm(), href=Href('/'), abs_href='http://example.org/trac/', chrome={}, session={}, locale='', tz='') self.context = Mock(env=self.env, href=self.req.href, req=self.req) self.formatter = Mock(context=self.context, req=self.req) self.db = self.env.get_db_cnx() cursor = self.db.cursor() cursor.execute("DROP TABLE IF EXISTS tags") cursor.execute("DROP TABLE IF EXISTS tags_change") cursor.execute("DELETE FROM system WHERE name='tags_version'") cursor.execute("DELETE FROM permission WHERE action %s" % self.db.like(), ('TAGS_%',)) setup = TagSetup(self.env) setup.upgrade_environment(self.db) self.tag_twm = TagWikiMacros(self.env) def tearDown(self): shutil.rmtree(self.env.path) def _insert_tags(self, tagspace, name, tags): cursor = self.db.cursor() args = [(tagspace, name, tag) for tag in tags] cursor.executemany("INSERT INTO tags (tagspace,name,tag) " "VALUES (%s,%s,%s)", args) def _expand_macro(self, content): return self.tag_twm.expand_macro(self.formatter, 'TagCloud', content) def test_normal(self): self._insert_tags('wiki', 'CamelCase', ('blah', 'foo', 'bar')) self._insert_tags('wiki', 'InterMapTxt', ('blah', 'foo', 'bar')) self._insert_tags('wiki', 'InterTrac', ('blah',)) self._insert_tags('wiki', 'InterWiki', ('blah',)) self._insert_tags('wiki', 'PageTemplates', ('blah',)) self._insert_tags('wiki', 'RecentChanges', ('blah', 'foo')) self._insert_tags('wiki', 'SandBox', ('blah', 'foo')) self._insert_tags('ticket', '1', ('blah',)) self._insert_tags('ticket', '2', ('blah', 'bar')) self._insert_tags('ticket', '3', ('blah', 'bar')) self._insert_tags('ticket', '4', ('blah', 'bar')) result = unicode(self._expand_macro('')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertTrue('">foo</a>' in result, repr(result)) self.assertTrue('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('mincount=5')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertFalse('">foo</a>' in result, repr(result)) self.assertTrue('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('mincount=6')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertFalse('">foo</a>' in result, repr(result)) self.assertFalse('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('realm=ticket|wiki')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertTrue('">foo</a>' in result, repr(result)) self.assertTrue('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('realm=ticket')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertFalse('">foo</a>' in result, repr(result)) self.assertTrue('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('realm=ticket,mincount=4')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertFalse('">foo</a>' in result, repr(result)) self.assertFalse('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('realm=unknown')) self.assertEquals('No tags found', result) result = unicode(self._expand_macro('mincount=100')) self.assertEquals('No tags found', result)
class TagCloudMacroTestCase(_BaseTestCase): def setUp(self): _BaseTestCase.setUp(self) self.req = Mock(path_info='/wiki/TagCloudPage', args={}, authname='user', perm=MockPerm(), href=Href('/'), abs_href='http://example.org/trac/', chrome={}, session={}, locale='', tz='') self.context = Mock(env=self.env, href=self.req.href, req=self.req) self.formatter = Mock(context=self.context, req=self.req) self.tag_twm = TagWikiMacros(self.env) # Helpers def _expand_macro(self, content): return self.tag_twm.expand_macro(self.formatter, 'TagCloud', content) # Tests def test_normal(self): self._insert_tags('wiki', 'CamelCase', ('blah', 'foo', 'bar')) self._insert_tags('wiki', 'InterMapTxt', ('blah', 'foo', 'bar')) self._insert_tags('wiki', 'InterTrac', ('blah',)) self._insert_tags('wiki', 'InterWiki', ('blah',)) self._insert_tags('wiki', 'PageTemplates', ('blah',)) self._insert_tags('wiki', 'RecentChanges', ('blah', 'foo')) self._insert_tags('wiki', 'SandBox', ('blah', 'foo')) self._insert_tags('ticket', '1', ('blah',)) self._insert_tags('ticket', '2', ('blah', 'bar')) self._insert_tags('ticket', '3', ('blah', 'bar')) self._insert_tags('ticket', '4', ('blah', 'bar')) result = unicode(self._expand_macro('')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertTrue('">foo</a>' in result, repr(result)) self.assertTrue('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('mincount=5')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertFalse('">foo</a>' in result, repr(result)) self.assertTrue('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('mincount=6')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertFalse('">foo</a>' in result, repr(result)) self.assertFalse('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('realm=ticket|wiki')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertTrue('">foo</a>' in result, repr(result)) self.assertTrue('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('realm=ticket')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertFalse('">foo</a>' in result, repr(result)) self.assertTrue('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('realm=ticket,mincount=4')) self.assertTrue('">blah</a>' in result, repr(result)) self.assertFalse('">foo</a>' in result, repr(result)) self.assertFalse('">bar</a>' in result, repr(result)) result = unicode(self._expand_macro('realm=unknown')) self.assertEquals('No tags found', result) result = unicode(self._expand_macro('mincount=100')) self.assertEquals('No tags found', result)
def setUp(self): _BaseTestCase.setUp(self) self.req = MockRequest(self.env, path_info='/wiki/ListTaggedPage', authname='user') self.tag_twm = TagWikiMacros(self.env)
class ListTaggedMacroTestCase(_BaseTestCase): def setUp(self): _BaseTestCase.setUp(self) self.req = Mock(path_info='/wiki/ListTaggedPage', args={}, authname='user', perm=MockPerm(), href=Href('/'), abs_href=Href('http://example.org/trac/'), chrome={}, session={}, locale='', tz='') self.tag_twm = TagWikiMacros(self.env) def test_empty_content(self): context = Mock(env=self.env, href=Href('/'), req=self.req) formatter = Mock(context=context, req=self.req) self.assertTrue('No resources found' in str(self.tag_twm.expand_macro(formatter, 'ListTagged', ''))) def test_listtagged_exclude(self): self._insert_tags('wiki', 'InterTrac', ('blah',)) self._insert_tags('wiki', 'InterWiki', ('blah',)) self._insert_tags('wiki', 'WikiStart', ('blah',)) context = Mock(env=self.env, href=Href('/'), req=self.req) formatter = Mock(context=context, req=self.req) result = unicode(self.tag_twm.expand_macro(formatter, 'ListTagged', 'blah,exclude=Inter*')) self.assertFalse('InterTrac' in result) self.assertFalse('InterWiki' in result) self.assertTrue('WikiStart' in result) result = unicode(self.tag_twm.expand_macro(formatter, 'ListTagged', 'blah,exclude=Wi*:*ki')) self.assertTrue('InterTrac' in result) self.assertFalse('InterWiki' in result) self.assertFalse('WikiStart' in result) def _test_listtagged_paginate(self, page, per_page=2): self._insert_tags('wiki', 'InterTrac', ('blah',)) self._insert_tags('wiki', 'InterWiki', ('blah',)) self._insert_tags('wiki', 'WikiStart', ('blah',)) self.req.args['listtagged_per_page'] = per_page self.req.args['listtagged_page'] = page context = Mock(env=self.env, href=Href('/'), req=self.req) formatter = Mock(context=context, req=self.req) result = \ unicode(self.tag_twm.expand_macro(formatter, 'ListTagged', 'blah')) return result def test_listtagged_paginate_page1(self): """Paginate results for page 1 has two items.""" result = self._test_listtagged_paginate(1) self.assertTrue('InterTrac' in result) self.assertTrue('InterWiki' in result) self.assertFalse('WikiStart' in result) def test_listtagged_paginate_page2(self): """Paginate results for page 2 has one item.""" result = self._test_listtagged_paginate(2) self.assertFalse('InterTrac' in result) self.assertFalse('InterWiki' in result) self.assertTrue('WikiStart' in result) def test_listtagged_paginate_page_out_of_range(self): """Out of range page defaults to 1.""" result = self._test_listtagged_paginate(3) self.assertTrue('InterTrac' in result) self.assertTrue('InterWiki' in result) self.assertFalse('WikiStart' in result) def test_listtagged_paginate_page_invalid(self): """Invalid page default to 1.""" result = self._test_listtagged_paginate(-1) self.assertTrue('InterTrac' in result) self.assertTrue('InterWiki' in result) self.assertFalse('WikiStart' in result) def test_listtagged_paginate_per_page_invalid(self): """Invalid per_page defaults to items_per_page (100).""" result = self._test_listtagged_paginate(2, -1) self.assertTrue('InterTrac' in result) self.assertTrue('InterWiki' in result) self.assertTrue('WikiStart' in result)
def process_request(self, req): req.perm.require("TAGS_VIEW") match = re.match(r"/tags/?(.*)", req.path_info) tag_id = match.group(1) and match.group(1) or None query = req.args.get("q", "") realms = [ p.get_taggable_realm() for p in self.tag_providers if (not hasattr(p, "check_permission") or p.check_permission(req.perm, "view")) ] if not (tag_id or query) or [r for r in realms if r in req.args] == []: for realm in realms: if not realm in self.exclude_realms: req.args[realm] = "on" checked_realms = [r for r in realms if r in req.args] realm_args = dict(zip([r for r in checked_realms], ["on" for r in checked_realms])) if tag_id and not re.match(r"""(['"]?)(\S+)\1$""", tag_id, re.UNICODE): # Convert complex, invalid tag ID's to query expression. req.redirect(req.href.tags(realm_args, q=tag_id)) elif query: single_page = re.match(r"""(['"]?)(\S+)\1$""", query, re.UNICODE) if single_page: # Convert simple query for single tag ID. req.redirect(req.href.tags(single_page.group(2), realm_args)) data = dict(page_title=_("Tags"), checked_realms=checked_realms) # Populate the TagsQuery form field. data["tag_query"] = tag_id and tag_id or query data["tag_realms"] = list(dict(name=realm, checked=realm in checked_realms) for realm in realms) if tag_id: page_name = tag_id page = WikiPage(self.env, page_name) data["tag_page"] = page macros = TagWikiMacros(self.env) if query or tag_id: # TRANSLATOR: The meta-nav link label. add_ctxtnav(req, _("Back to Cloud"), req.href.tags()) macro = "ListTagged" args = "%s,format=%s,cols=%s,realm=%s" % ( tag_id and tag_id or query, self.default_format, self.default_cols, "|".join(checked_realms), ) data["mincount"] = None else: macro = "TagCloud" mincount = as_int(req.args.get("mincount", None), self.cloud_mincount) args = "mincount=%s,realm=%s" % (mincount, "|".join(checked_realms)) data["mincount"] = mincount formatter = Formatter(self.env, Context.from_request(req, Resource("tag"))) self.env.log.debug("Tag macro arguments: %s", args) try: # Query string without realm throws 'NotImplementedError'. data["tag_body"] = len(checked_realms) > 0 and macros.expand_macro(formatter, macro, args) or "" except InvalidQuery, e: data["tag_query_error"] = to_unicode(e) data["tag_body"] = macros.expand_macro(formatter, "TagCloud", "")
def process_request(self, req): req.perm.require('TAGS_VIEW') match = re.match(r'/tags/?(.*)', req.path_info) tag_id = match.group(1) and match.group(1) or None query = req.args.get('q', '') # Consider only providers, that are permitted for display. tag_system = TagSystem(self.env) all_realms = tag_system.get_taggable_realms(req.perm) if not (tag_id or query) or [r for r in all_realms if r in req.args ] == []: for realm in all_realms: if not realm in self.exclude_realms: req.args[realm] = 'on' checked_realms = [r for r in all_realms if r in req.args] if query: # Add permitted realms from query expression. checked_realms.extend(query_realms(query, all_realms)) realm_args = dict( zip([r for r in checked_realms], ['on' for r in checked_realms])) # Switch between single tag and tag query expression mode. if tag_id and not re.match(r"""(['"]?)(\S+)\1$""", tag_id, re.UNICODE): # Convert complex, invalid tag ID's --> query expression. req.redirect(req.href.tags(realm_args, q=tag_id)) elif query: single_page = re.match(r"""(['"]?)(\S+)\1$""", query, re.UNICODE) if single_page: # Convert simple query --> single tag. req.redirect(req.href.tags(single_page.group(2), realm_args)) data = dict(page_title=_("Tags"), checked_realms=checked_realms) # Populate the TagsQuery form field. data['tag_query'] = tag_id and tag_id or query data['tag_realms'] = list( dict(name=realm, checked=realm in checked_realms) for realm in all_realms) if tag_id: data['tag_page'] = WikiPage(self.env, tag_system.wiki_page_prefix + tag_id) if query or tag_id: macro = 'ListTagged' # TRANSLATOR: The meta-nav link label. add_ctxtnav(req, _("Back to Cloud"), req.href.tags()) args = "%s,format=%s,cols=%s" % \ (tag_id and tag_id or query, self.default_format, self.default_cols) data['mincount'] = None else: macro = 'TagCloud' mincount = as_int(req.args.get('mincount', None), self.cloud_mincount) args = mincount and "mincount=%s" % mincount or None data['mincount'] = mincount formatter = Formatter(self.env, Context.from_request(req, Resource('tag'))) self.env.log.debug("%s macro arguments: %s" % (macro, args and args or '(none)')) macros = TagWikiMacros(self.env) try: # Query string without realm throws 'NotImplementedError'. data['tag_body'] = checked_realms and \ macros.expand_macro(formatter, macro, args, realms=checked_realms) \ or '' except InvalidQuery, e: data['tag_query_error'] = to_unicode(e) data['tag_body'] = macros.expand_macro(formatter, 'TagCloud', '')
def process_request(self, req): req.perm.require('TAGS_VIEW') match = re.match(r'/tags/?(.*)', req.path_info) tag_id = match.group(1) and match.group(1) or None query = req.args.get('q', '') # Consider only providers, that are permitted for display. tag_system = TagSystem(self.env) all_realms = tag_system.get_taggable_realms(req.perm) if not (tag_id or query) or [r for r in all_realms if r in req.args] == []: for realm in all_realms: if not realm in self.exclude_realms: req.args[realm] = 'on' checked_realms = [r for r in all_realms if r in req.args] if query: # Add permitted realms from query expression. checked_realms.extend(query_realms(query, all_realms)) realm_args = dict(zip([r for r in checked_realms], ['on' for r in checked_realms])) # Switch between single tag and tag query expression mode. if tag_id and not re.match(r"""(['"]?)(\S+)\1$""", tag_id, re.UNICODE): # Convert complex, invalid tag ID's --> query expression. req.redirect(req.href.tags(realm_args, q=tag_id)) elif query: single_page = re.match(r"""(['"]?)(\S+)\1$""", query, re.UNICODE) if single_page: # Convert simple query --> single tag. req.redirect(req.href.tags(single_page.group(2), realm_args)) data = dict(page_title=_("Tags"), checked_realms=checked_realms) # Populate the TagsQuery form field. data['tag_query'] = tag_id and tag_id or query data['tag_realms'] = list(dict(name=realm, checked=realm in checked_realms) for realm in all_realms) if tag_id: data['tag_page'] = WikiPage(self.env, tag_system.wiki_page_prefix + tag_id) if query or tag_id: macro = 'ListTagged' # TRANSLATOR: The meta-nav link label. add_ctxtnav(req, _("Back to Cloud"), req.href.tags()) args = "%s,format=%s,cols=%s" % \ (tag_id and tag_id or query, self.default_format, self.default_cols) data['mincount'] = None else: macro = 'TagCloud' mincount = as_int(req.args.get('mincount', None), self.cloud_mincount) args = mincount and "mincount=%s" % mincount or None data['mincount'] = mincount formatter = Formatter(self.env, Context.from_request(req, Resource('tag'))) self.env.log.debug("%s macro arguments: %s", macro, args and args or '(none)') macros = TagWikiMacros(self.env) try: # Query string without realm throws 'NotImplementedError'. data['tag_body'] = checked_realms and \ macros.expand_macro(formatter, macro, args, realms=checked_realms) \ or '' except InvalidQuery, e: data['tag_query_error'] = to_unicode(e) data['tag_body'] = macros.expand_macro(formatter, 'TagCloud', '')