Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add initial project setup and GenericTemplateFinderMiddleware

GenericTemplateFinderMiddleware will find a template based on the
request url, by adding a .html suffix and munging the path components,
and render it.
  • Loading branch information...
commit a66b3cd43221a5ae9925f6ea8c3e080e9a5212e9 0 parents
@gavinwahl gavinwahl authored
1  .gitignore
@@ -0,0 +1 @@
+*.pyc
29 LICENSE
@@ -0,0 +1,29 @@
+Copyright (c) 2011, Gavin Wahl <gwahl@fusionbox.com>
+Copyright (c) 2011, Aaron Merriam <amerriam@fusionbox.com>
+Copyright (c) 2011, Fusionbox
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are
+permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice, this list of
+ conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ of conditions and the following disclaimer in the documentation and/or other materials
+ provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
+CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+The views and conclusions contained in the software and documentation are those of the
+authors and should not be interpreted as representing official policies, either expressed
+or implied, of Fusionbox.
5 MANIFEST
@@ -0,0 +1,5 @@
+setup.py
+fusionbox/__init__.py
+fusionbox/middleware.py
+README.md
+LICENSE
17 README.md
@@ -0,0 +1,17 @@
+# django-fusionbox
+
+Useful tools for Django development.
+
+
+## `GenericTemplateFinderMiddleware`
+This middleware will automagically find a template based on the request url and
+render it. Useful during early design stages to allow quick static pages to use
+the template system.
+
+Add `fusionbox.middleware.GenericTemplateFinderMiddleware` to your
+`MIDDLEWARE_CLASSES` to activate.
+
+### Usage examples
+- `/` renders `index.html`
+- `/foo/` renders `/foo.html` OR `/foo/index.html` OR `/foo`
+- This works for an directory depth
1  fusionbox/__init__.py
@@ -0,0 +1 @@
+__all__ = ['middleware']
40 fusionbox/middleware.py
@@ -0,0 +1,40 @@
+from django.template import TemplateDoesNotExist, RequestContext
+from django.http import Http404
+from django.shortcuts import render_to_response
+
+
+def generic_template_finder_view(request):
+ """
+ Find a template based on the request url and render it.
+
+ / -> index.html
+ /foo/ -> foo.html OR foo/index.html
+
+
+ """
+ path = request.path
+ if not path.endswith('/'):
+ path += '/'
+ possibilities = (
+ path.strip('/') + '.html',
+ path.lstrip('/') + 'index.html',
+ path.strip('/'),
+ )
+ for t in possibilities:
+ try:
+ return render_to_response(t, context_instance=RequestContext(request))
+ except TemplateDoesNotExist:
+ pass
+ raise Http404('Template not found in any of %r' % (possibilities,))
+
+
+class GenericTemplateFinderMiddleware(object):
+ """
+ Response middleware that uses generic_template_finder_view to attempt to
+ autolocate a template for otherwise 404 responses.
+ """
+ def process_response(self, request, response):
+ if response.status_code == 404:
+ return generic_template_finder_view(request)
+ else:
+ return response
28 setup.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+import os
+import re
+from distutils.core import setup
+
+__doc__="""
+Useful stuff for django
+
+"""
+
+version = '0.0.1'
+
+setup(name='django-fusionbox',
+ version=version,
+ description='Useful stuff for django',
+ author='Fusionbox programmers',
+ author_email='programmers@fusionbox.com',
+ keywords='django boilerplate',
+ long_description=__doc__,
+ url='https://github.com/fusionbox/django-fusionbox',
+ packages=['fusionbox'],
+ platforms = "any",
+ license='BSD',
+ classifiers=[
+ 'Environment :: Web Environment',
+ 'Framework :: Django',
+ ],
+)
Please sign in to comment.
Something went wrong with that request. Please try again.