Anonymous committed bbcc561

Change the app template to use ``add_static_route`` as a ``config`` method.

  • Participants
  • Parent commits cb28c1a

Comments (0)

Files changed (7)

+- ``add_static_route`` is now a Pyramid config method if you call the new
+  ``includeme`` function. This is used in the application template.
+- 100% test coverage contributed by Chris McDonough.
 - Delete unneeded development code in
 1.0rc1  (2010-01-26)
 * The model, application settings, middleware, and logging are preconfigured
   for a Pylons 1-like SQLAlchemy application
 * The static directory is served under "/" instead of "/static", overlaying 
-  your dynamic URLs
+  your dynamic URLs.
 * Routing using URL dispatch and view handlers, similar to Routes and
   controllers in Pylons 1
 * Listen on localhost:5000 by default (localhost for security, 5000 per Pylons
 * Configures logging in .ini and handlers
 * A script to initialize your database
 * A production.ini (new in 1.0rc1)
+* The function to add a static directory is now a Configurator method (new in
+  1.0rc2)
 pyramid_sqla has five dependencies: Pyramid_, SQLAlchemy_, repoze.tm2_, 
 zope.sqlalchemy_, and transaction_. It's tested on Python 2.6/Linux but should


 "/", overlaying your dynamic URLs. This lets you serve all your static files
 the same way. It's enabled by the following lines in *myapp/*::
-    from pyramid_sqla.static import add_static_route
+    config.include('pyramid_sqla')
     # Set up routes and views
     config.add_handler('home', '/', 'pyramidapp.handlers:MainHandler',
     config.add_handler('main', '/{action}', 'pyramidapp.handlers:MainHandler',
-    add_static_route(config, 'pyramidapp', 'static', cache_max_age=3600)
+    config.add_static_route('pyramidapp', 'static', cache_max_age=3600)
+The 'include' line creates the ``config.add_static_route`` method which we call
+in the last line.
 The first ``config.add_handler()`` call is an ordinary home page route, nothing
 special about it. 
 accidentally match your static URLs, which are generally routes with a variable
 first component.
-The ``add_static_route()`` call is a convenience function that mounts the
-static directory onto "/". Then it just serves the files. If you look at the
-wrapper's source code, it uses a custom route predicate so that if the implied
-file doesn't exist, the route won't match the URL. This gives later routes or
-traversal a chance to work, otherwise they would be blocked. 
+The ``config.add_static_route()`` call mounts the static directory onto "/" as
+an overlay.
+This method is defined as a function,
+``pyramid_sqla.static.add_static_route()``. If you look at the source code, it
+adds a route with a custom route predicate so that if the file doesn't exist, 
+the route won't match the URL. This gives later routes or
+traversal a chance to work, otherwise they would be blocked. If the file does
+exist, the static view registered by this method will serve it.
+(You can call the function directly if you wish, passing ``config`` as the
+first argument. But it's more convenient to call it as a method.)
+.. autofunction:: pyramid_sqla.static.add_static_route
 .. _w3c: 
 .. autofunction:: reset
+.. autofunction:: includeme
+(``.add_static_route`` is explained on the Non-Database Features page.)
 Managed transactions


     return _base
 def includeme(config):
+    """Add certain useful methods to a Pyramid ``Configurator`` instance.
+    Currently this adds the ``.add_static_route()`` method. (See
+    ``pyramid_sqla.static.add_static_route()``.)
+    """
     from pyramid_sqla.static import add_static_route
     config.add_directive('add_static_route', add_static_route)


 from pyramid.config import Configurator
 import pyramid_beaker
-import pyramid_handlers
 import pyramid_sqla
-from pyramid_sqla.static import add_static_route
 def main(global_config, **settings):
     """ This function returns a Pyramid WSGI application.
     config = Configurator(settings=settings)
-    config.include(pyramid_handlers.includeme)
+    config.include('pyramid_handlers')
+    config.include('pyramid_sqla')
     # Initialize database
     pyramid_sqla.add_engine(settings, prefix='sqlalchemy.')
     config.add_handler('main', '/{action}', '{{package}}.handlers:MainHandler',
-    add_static_route(config, '{{package}}', 'static', cache_max_age=3600)
+    config.add_static_route('{{package}}', 'static', cache_max_age=3600)
     return config.make_wsgi_app()


+import os
 import pkg_resources
 from pyramid.view import static
     the next route or traversal. The route name is 'static', which must not
     conflict with your application's other routes.
-    Usage in
+    Usage in the application's
         add_static_route(config, "myapp", "static")
+    Or, more conveniently::
+        config.include("pyramid_sqla")
+        config.add_static_route("myapp", "static")
     This serves URLs from the "static" directory in package "myapp".
     for bad_arg in ["pattern", "view"]:
         if bad_arg in add_route_args:
-            raise TypeError("keyword arg '%s' not allowed")
+            raise TypeError("keyword arg '%s' is not allowed")
     name = add_route_args.pop("name", "static")
     pattern = "/*subpath"
     asset = "%s:%s" % (package, subdir)
     custom_predicates = add_route_args.pop("custom_predicates", [])
     custom_predicates = list(custom_predicates)
     custom_predicates.insert(0, pred)
-    config.add_route(name, pattern, view=view, 
-                     custom_predicates=custom_predicates, **add_route_args)
+    config.add_route(name, pattern, view, 
+        custom_predicates=custom_predicates, **add_route_args)
 #### Private stuff