From 241390d911639cb658bbbbbf1bf3d8c21e0c0270 Mon Sep 17 00:00:00 2001 From: Malthe Borch Date: Mon, 24 Aug 2009 17:21:10 +0000 Subject: Added ZCML directive to serve up static files from a directory. --- repoze/bfg/includes/meta.zcml | 6 ++ .../starter/+package+/configure.zcml | 5 +- .../starter/+package+/views.py_tmpl | 3 - repoze/bfg/tests/test_zcml.py | 70 ++++++++++++++++++++++ repoze/bfg/zcml.py | 34 +++++++++++ 5 files changed, 112 insertions(+), 6 deletions(-) (limited to 'repoze') diff --git a/repoze/bfg/includes/meta.zcml b/repoze/bfg/includes/meta.zcml index 89a540098..69e2eabc0 100644 --- a/repoze/bfg/includes/meta.zcml +++ b/repoze/bfg/includes/meta.zcml @@ -40,6 +40,12 @@ handler="repoze.bfg.zcml.resource" /> + + - diff --git a/repoze/bfg/paster_templates/starter/+package+/views.py_tmpl b/repoze/bfg/paster_templates/starter/+package+/views.py_tmpl index 9c5fe7a67..d9f0df378 100644 --- a/repoze/bfg/paster_templates/starter/+package+/views.py_tmpl +++ b/repoze/bfg/paster_templates/starter/+package+/views.py_tmpl @@ -1,7 +1,4 @@ from repoze.bfg.chameleon_zpt import render_template_to_response -from repoze.bfg.view import static - -static_view = static('templates/static') def my_view(context, request): return render_template_to_response('templates/mytemplate.pt', diff --git a/repoze/bfg/tests/test_zcml.py b/repoze/bfg/tests/test_zcml.py index 556f22983..d17babaf3 100644 --- a/repoze/bfg/tests/test_zcml.py +++ b/repoze/bfg/tests/test_zcml.py @@ -1037,6 +1037,76 @@ class TestRouteDirective(unittest.TestCase): self.assertEqual(route_discriminator[3], 'GET') self.assertEqual(route_args, ('path', 'name', None)) +class TestStaticDirective(unittest.TestCase): + def setUp(self): + cleanUp() + import os + here = os.path.dirname(__file__) + self.static_path = os.path.join(here, 'fixtures', 'static') + + def tearDown(self): + cleanUp() + + def _callFUT(self, *arg, **kw): + from repoze.bfg.zcml import static + return static(*arg, **kw) + + def test_absolute(self): + from repoze.bfg.zcml import handler + from repoze.bfg.zcml import connect_route + from repoze.bfg.interfaces import IView + context = DummyContext() + self._callFUT(context, 'name', self.static_path) + actions = context.actions + self.assertEqual(len(actions), 2) + + route_action = actions[0] + route_callable = route_action['callable'] + route_discriminator = route_action['discriminator'] + route_args = route_action['args'] + self.assertEqual(route_callable, handler) + self.assertEqual(route_discriminator[:3], ( + 'view', None, '')) + self.assertEqual(route_discriminator[4], IView) + + route_action = actions[1] + route_callable = route_action['callable'] + route_discriminator = route_action['discriminator'] + route_args = route_action['args'] + self.assertEqual(route_callable, connect_route) + self.assertEqual(route_discriminator, ( + 'route', 'name', None, None)) + self.assertEqual(route_args, ( + 'name*subpath', 'name', None)) + + def test_package_relative(self): + from repoze.bfg.zcml import handler + from repoze.bfg.zcml import connect_route + from repoze.bfg.interfaces import IView + context = DummyContext() + self._callFUT(context, 'name', 'repoze.bfg.tests:fixtures/static') + actions = context.actions + self.assertEqual(len(actions), 2) + + route_action = actions[0] + route_callable = route_action['callable'] + route_discriminator = route_action['discriminator'] + route_args = route_action['args'] + self.assertEqual(route_callable, handler) + self.assertEqual(route_discriminator[:3], ( + 'view', None, '')) + self.assertEqual(route_discriminator[4], IView) + + route_action = actions[1] + route_callable = route_action['callable'] + route_discriminator = route_action['discriminator'] + route_args = route_action['args'] + self.assertEqual(route_callable, connect_route) + self.assertEqual(route_discriminator, ( + 'route', 'name', None, None)) + self.assertEqual(route_args, ( + 'name*subpath', 'name', None)) + class TestResourceDirective(unittest.TestCase): def setUp(self): cleanUp() diff --git a/repoze/bfg/zcml.py b/repoze/bfg/zcml.py index 7c1a74ec2..e38fd3449 100644 --- a/repoze/bfg/zcml.py +++ b/repoze/bfg/zcml.py @@ -46,6 +46,7 @@ from repoze.bfg.security import ViewPermissionFactory from repoze.bfg.secpols import registerBBBAuthn +from repoze.bfg.view import static as static_view import martian @@ -338,6 +339,39 @@ def connect_route(path, name, factory): mapper = getUtility(IRoutesMapper) mapper.connect(path, name, factory) +class IStaticDirective(Interface): + name = TextLine( + title=u"The URL prefix of the static view", + description=u""" + The directory will be served up for the route that starts with + this prefix.""", + required=True) + + path = TextLine( + title=u'Path to the directory which contains resources', + description=u'May be package-relative by using a colon to ' + 'seperate package name and path relative to the package directory.', + required=True) + + cache_max_age = Int( + title=u"Cache maximum age", + required=False, + default=None) + +def static(_context, name, path, cache_max_age=3600): + """ Handle ``static`` ZCML directives + """ + + if ':' in path: + package_name, path = path.split(':') + else: + package_name = _context.resolve('.').__name__ + + view = static_view( + path, cache_max_age=cache_max_age, package_name=package_name) + + route(_context, name, "%s*subpath" % name, view=view) + class IViewDirective(Interface): for_ = GlobalObject( title=u"The interface or class this view is for.", -- cgit v1.2.3