django-odb / odb / middleware.py

from django.http import HttpResponse
from django.forms.forms import Form
from django.views.generic.edit import FormView
from odb.traverse import get_object
from inspect import isclass
from django.template.base import Template
from django.template.context import RequestContext
from django.shortcuts import render

class TraversalMiddleware(object):
    def process_request(self, req):
        try:
            o = get_object(req.path)
            if hasattr(o, '__getitem__') and 'index' in o:
                o = o['index']
            if hasattr(o, 'render') and callable(o.render):
                return o.render(req)
            elif isinstance(o, HttpResponse):
                return o
            elif isinstance(o, str):
                return HttpResponse(o)
            elif isclass(o) and issubclass(o, Form):
                return FormView.as_view(form_class=o, template_name="odb/form.html")(req)
            elif isinstance(o, Template):
                return HttpResponse(o.render(RequestContext(req,{'GET':req.GET})))
            elif hasattr(o, 'keys') and callable(o.keys):
                if req.path[-1]!='/':
                    return None
                object_list = []
                for k, v in o.iteritems():
                    obj = {'name':k, 'class':v.__class__.__name__}
                    if isinstance(v, dict): obj.update({'is_directory':True})
                    object_list.append(obj)
                return render(req, "odb/object_list.html", {'object_list':object_list,'path':req.path})
            return None
        except KeyError:
            return None
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.