Ejemplo n.º 1
0
Archivo: app.py Proyecto: jelmer/wikkid
 def __init__(self, filestore, skin_name=None, execution_context=None):
     if execution_context is None:
         execution_context = ExecutionContext()
     self.execution_context = execution_context
     self.filestore = filestore
     self.resource_factory = ResourceFactory(self.filestore)
     # Need to load the initial templates for the skin.
     if skin_name is None:
         skin_name = 'default'
     self.skin = Skin(skin_name)
     self.logger = logging.getLogger('wikkid')
Ejemplo n.º 2
0
Archivo: app.py Proyecto: jelmer/wikkid
class WikkidApp(object):
    """The main wikkid application."""

    def __init__(self, filestore, skin_name=None, execution_context=None):
        if execution_context is None:
            execution_context = ExecutionContext()
        self.execution_context = execution_context
        self.filestore = filestore
        self.resource_factory = ResourceFactory(self.filestore)
        # Need to load the initial templates for the skin.
        if skin_name is None:
            skin_name = 'default'
        self.skin = Skin(skin_name)
        self.logger = logging.getLogger('wikkid')

    def preprocess_environ(self, environ):
        request = Request(environ)
        path = urllib.unquote(request.path)
        script_name = self.execution_context.script_name
        # Firstly check to see if the path is the same as the script_name
        if (path != script_name and
            not path.startswith(script_name + '/')):
            raise HTTPNotFound()

        shifted_prefix = ''
        while shifted_prefix != script_name:
            shifted = shift_path_info(environ)
            shifted_prefix = '{0}/{1}'.format(shifted_prefix, shifted)
        # Now we are just interested in the path_info having ignored the
        # script name.
        path = urllib.unquote(request.path_info)
        if path == '':
            path = '/' # Explicitly be the root (we need the /)
        return request, path

    def _get_view(self, request, path):
        """Get the view for the path specified."""
        resource_path, action = parse_url(path)
        model = self.resource_factory.get_resource_at_path(resource_path)
        return get_view(model, action, request, self.execution_context)

    def process_call(self, environ):
        """The actual implementation of dealing with the call."""
        # TODO: reject requests that aren't GET or POST
        try:
            request, path = self.preprocess_environ(environ)
        except HTTPException, e:
            return e

        if path == '/favicon.ico':
            if self.skin.favicon is not None:
                return serve_file(self.skin.favicon)
            else:
                return HTTPNotFound()

        if path.startswith('/static/'):
            if self.skin.static_dir is not None:
                static_dir = self.skin.static_dir.rstrip(os.sep) + os.sep
                static_file = os.path.abspath(
                    urlutils.joinpath(static_dir, path[8:]))
                if static_file.startswith(static_dir):
                    return serve_file(static_file)
                else:
                    return HTTPNotFound()
            else:
                return HTTPNotFound()

        try:
            view = self._get_view(request, path)
            return view.render(self.skin)
        except HTTPException, e:
            return e