Example #1
0
 def make_form(cls, controller, parent = None):
     tp = controller.params.get('type', 'Folder')
     if '.' in tp:
         plugin, cname = tp.split('.')
     else:
         plugin = ''
         cname = tp
     ctrl_clz = get_controller_class(cname, plugin)
     try:
         form = ctrl_clz.make_add_form(controller.content)
     except:
         form = ctrl_clz.get_form('add', controller)
     form.set_action(controller.content.get_path()+'/add'+\
                                 '?type='+tp)
     return form
Example #2
0
    def process_data(self, controller):
        from google.appengine.api import memcache

        v = controller.form.validate_result
        tp = controller.params.get('type', 'Folder')
        if '.' in tp:
            plugin, cname = tp.split('.')
        else:
            plugin = ''
            cname = tp
        ctrl_clz = get_controller_class(cname, plugin)
        o = ctrl_clz.add_new_object(v, controller)
        memcache.flush_all()
        controller.content.add(o)

        controller.set_state(FormControl.INITIAL)
        if not controller.has_redirected:
            p = config.site_root+controller.content.get_path()+'/list'
            controller.redirect(p)
Example #3
0
def dispatch(hnd):
    """
    A function to dispatch request to appropriate handler class
    """
    # resolve the URL
    url = hnd.request.path
    r = get_router()
    route = r.match(url)

    if not route:
        fr = get_fallback_router()
        fr.match(url)
        route = fr.match(url)
        if not route:
            # raise exception because we couldn't find route for given url
            hnd.response.set_status(404)
            raise Exception('No route for url:%s' % url)

    # create the appropriate controller
    ctrlname = route['controller']
    plugin = ''
    if '.' in ctrlname:
        plugin, ctrlname = ctrlname.split('.')
    ctrl_clz = get_controller_class(ctrlname, plugin)

    # create a controller instance
    ctrl = ctrl_clz(hnd, route)
    #setting attributes

    # mixin application base controller
    try:
        exec('from controller import application') in globals()
        if application.Application not in ctrl_clz.__bases__:
            ctrl_clz.__bases__ += (application.Application,)
        if hasattr(ctrl, 'application_init'):
            ctrl.application_init()
    except:
        pass

    # dispatch
    logging.debug('URL "%s" is dispatched to: %sController#%s',
                 url,
                 route['controller'].capitalize(),
                 route.get('action', 'index'))

    ctrl.config = Config()

    # get the action from the controller
    actionmethod = getattr(ctrl, route.get('action', 'index'), None)

    # if the action is none ,
    #   or it is not decorated by using expose, raise exception
    #   to avoid unintended method traversal.
    if not actionmethod or not getattr(actionmethod, '_exposed_', False):
        if not ctrl.config.debug:
            try:
                PAGE_CACHE_EXPIRE = config.page_cache_expire
            except AttributeError:
                PAGE_CACHE_EXPIRE = 60*60
            p = urlsplit(hnd.request.url)[2]
            memcache.set(p, 'error', PAGE_CACHE_EXPIRE)
            logging.debug('%s is cahed as a error page' % p)
        ctrl.response.set_status(404)
        m = '%s %s (Method not found)'
        raise Exception(m % ctrl.response._Response__status)

    # if before_action returns False, terminate the remain action
    if ctrl.before_action() != False:
        if ismethod(actionmethod):
            actionmethod()
        else:
            actionmethod()
        ctrl.after_action()

    #check status
    st = ctrl.response._Response__status[0]
    if st == 401 and ctrl.response.headers.get('WWW-Authenticate'):
        ctrl.put_cookies()
        return
    if st >= 400:
        # error occured
        raise Exception('%s %s' % ctrl.response._Response__status)

    if not ctrl.has_rendered and not ctrl.has_redirected:
        ctrl.render(template = route['action'], values = ctrl.__dict__)

    ctrl.put_cookies()
Example #4
0
def dispatch(hnd, path, params):
    """
    A function to call appropriate controller and metod, 
    according to given path object and action.
    """
    # getting controller from the object type in given path object.
    action = ''
    if params:
        action = params[0]
        params = params[1:]
    # If content type includes '.', it means 'PLUGINNAME.CONTENTTYPE'
    if '.' in path.ctype:
        plugin, controller = path.ctype.split('.')
        ctrl_clz = get_controller_class(controller, plugin)
    else:
        controller = path.ctype
        ctrl_clz = get_controller_class(controller)

    # create a controller instance
    ctrl = ctrl_clz(hnd, {'controller':controller.lower(),
                        'action':action})

    # assigning common objects to controller instance
    #    so that these can be used in template
    ctrl.controller = ctrl                    # contrlller itself
    ctrl.path_obj = hnd.path_obj              # Path object
    ctrl.content = hnd.path_obj.get_content() # content object
    ctrl.site_data = SiteData.get_data()      # site_data object

    # mixin application base controller
    try:
        exec('from controller import application') in globals()
        if application.Application not in ctrl_clz.__bases__:
            ctrl_clz.__bases__ += (application.Application,)
        if hasattr(ctrl, 'application_init'):
            ctrl.application_init()
    except:
        pass

    # dispatch
    logging.debug('URL "%s" is dispatched to: %sController#%s',
                         hnd.request.url, controller, action)

    # setting action to index, in case the action is not given.
    if not action: action = 'index';

    # getting action method from the controller instance.
    actionmethod = getattr(ctrl, action, None)

    # if the action is none ,
    #   or it is not decorated by using expose, raise exception
    #   to avoid unintended method traversal.
    if not actionmethod or not getattr(actionmethod, '_exposed_', False):
        if not ctrl.config.debug:
            try:
                PAGE_CACHE_EXPIRE = config.page_cache_expire
            except:
                PAGE_CACHE_EXPIRE = 60*60
            p = urlsplit(hnd.request.url)[2]
            memcache.set(p, 'error', PAGE_CACHE_EXPIRE)
            logging.debug('%s is cahed as a error page' % p)
        ctrl.response.set_status(404)
        m = '%s %s (Method not found)'
        raise Exception(m % ctrl.response._Response__status)

    # if before_action returns False, terminate the remain action
    if ctrl.before_action() != False:
        if ismethod(actionmethod):
            actionmethod(*params)
        else:
            actionmethod(*([ctrl]+params))
        ctrl.after_action()

    #check status
    st = ctrl.response._Response__status[0]
    if st >= 400:
        # error occured
        raise Exception('%s %s' % ctrl.response._Response__status)

    if not ctrl.has_rendered:
        ctrl.render(template = action, values = ctrl.__dict__)

    # manage cookies
    if ctrl.post_cookie.keys():
        c = ctrl.post_cookie
        cs = c.output().replace('Set-Cookie: ', '')
        ctrl.response.headers.add_header('Set-Cookie', cs)