示例#1
0
文件: server.py 项目: laurentb/assnet
 def _init_default_config(self):
     if not self.storage:
         return
     config = self.storage.get_config()
     self.cookie_secret = config.data["web"].get("cookie_secret")
     try:
         if self.cookie_secret is None:
             self.cookie_secret = new_secret()
             config.data["web"]["cookie_secret"] = self.cookie_secret
             config.save()
         # store the absolute root url (useful when in CLI)
         if not config.data["web"].get("root_url"):
             config.data["web"]["root_url"] = self.req.host_url + self.root_url.href
             config.save()
     except IOError:
         e = HTTPInternalServerError()
         self.template_vars.update({'scripts': [], 'stylesheets': []})
         e.body = self.render('error_writeconfig.html')
         raise e
示例#2
0
文件: server.py 项目: laurentb/assnet
    def dispatch(self):
        ctx = self.ctx
        router = ctx.router

        if not ctx.storage:
            e = HTTPInternalServerError()
            ctx.template_vars.update({'scripts': [], 'stylesheets': []})
            if 'ASSNET_ROOT' in ctx.req.environ:
                ctx.template_vars['root'] = ctx.req.environ['ASSNET_ROOT']
                e.body = ctx.render('error_notworkingdir.html')
            else:
                e.body = ctx.render('error_norootpath.html')
            raise e

        self._authenticate()
        ctx.template_vars["user"] = ctx.user if ctx.user.exists else None

        # actions: not related to a file or directory
        # if we are in the root app URL
        if ctx.url.setvars().href == ctx.root_url.href:
            action = router.find_action(ctx.req.GET.get('action'))
            if action is not None:
                return action(ctx).answer()

        # check perms
        f = ctx.file
        if ctx.object_type == 'directory':
            if not ctx.user.has_perms(f, f.PERM_LIST):
                if ctx.user.has_perms(f, f.PERM_IN):
                    raise HTTPForbidden()
                else:
                    raise HTTPNotFound('File not found')
        elif not ctx.user.has_perms(f, f.PERM_READ):
            if ctx.user.has_perms(f, f.PERM_IN):
                raise HTTPForbidden()
            else:
                raise HTTPNotFound('File not found')

        # normalize paths
        if ctx.object_type:
            goodpath = ctx.path
            if ctx.object_type == 'directory' and not goodpath.endswith('/'):
                # there should be a trailing slash in the client URL
                # for directories but not for files
                goodpath += '/'
            if ctx.req.path_info != goodpath:
                root_url = ctx.root_url.url
                if goodpath.startswith('/'):
                    goodpath = goodpath[1:]
                if not root_url.endswith('/'):
                    root_url += '/'
                goodlocation = URL(urlparse.urljoin(root_url, goodpath), vars=ctx.url.vars)
                ctx.res = HTTPFound(location=quote_url(goodlocation))
                return

        # no object type means no real file exists
        if ctx.object_type is None:
            raise HTTPNotFound('File not found')

        # find the action to forward the request to
        view, action = router.find_view(f, ctx.req.GET.get('view'))
        if view and action:
            # find out current action/view and available views
            ctx.template_vars['view'] = view.name
            ctx.template_vars['available_views'] = \
                sorted(router.get_available_views(f), key=str)
            return action(ctx).answer()

        # action/view not found
        raise HTTPNotFound('No route found')