Esempio n. 1
0
 def __call__(self, c=None, f='index', args=[], vars={},
              extension=None, target=None,ajax=False,ajax_trap=False,
              url=None):
     import globals
     target = target or 'c'+str(random.random())[2:]
     request = self.environment['request']
     if '.' in f:
         f, extension = f.split('.',1)
     if c and not url and not ajax:
         other_environment = copy.copy(self.environment)
         other_request = globals.Request()
         other_request.application = request.application
         other_request.controller = c
         other_request.function = f
         other_request.extension = extension or request.extension
         other_request.args = List(args)
         other_request.folder = request.folder
         other_request.env = request.env
         if not ajax_trap:
             other_request.vars = request.vars
             other_request.get_vars = request.get_vars
             other_request.post_vars = request.post_vars
         else:
             other_request.vars = vars
         other_environment['request'] = other_request
         other_response = globals.Response()
         other_environment['response'] = other_response
         other_response._view_environment = other_environment
         other_request.env.http_web2py_component_location = \
             request.env.path_info
         other_request.env.http_web2py_component_element = target
         other_response.view = '%s/%s.%s' % (c,f, other_request.extension)
         page = run_controller_in(c, f, other_environment)
         if isinstance(page, dict):
             other_response._vars = page
             for key in page:
                 other_response._view_environment[key] = page[key]
             run_view_in(other_response._view_environment)
             page = other_response.body.getvalue()
         script = ''
         if ajax_trap:
             script += "web2py_trap_form('%s','%s');" % \
                 (html.URL(request.application,c,f,
                           args=args,vars=vars,
                           extension=extension),target)
         #for (name,value) in other_response.headers:
         #    if name == 'web2py-component-command':
         #        script += value
         return html.TAG[''](html.DIV(html.XML(page),_id=target),
                             html.SCRIPT(script,_type="text/javascript"))
     else:
         url = url or html.URL(request.application,c,
                               f, args=args,vars=vars,
                               extension=extension)
         return html.TAG[''](html.SCRIPT('web2py_component("%s","%s")' % \
                                             (url,target),
                                         _type="text/javascript"),
                             html.DIV('loading...',_id=target))
Esempio n. 2
0
    def __call__(self, c=None, f='index', args=None, vars=None,
                 extension=None, target=None,ajax=False,ajax_trap=False,
                 url=None,user_signature=False, content='loading...',**attr):
        if args is None: args = []
        vars = Storage(vars or {})
        import globals
        target = target or 'c'+str(random.random())[2:]
        attr['_id']=target
        request = self.environment['request']
        if '.' in f:
            f, extension = f.rsplit('.',1)
        if url or ajax:
            url = url or html.URL(request.application, c, f, r=request,
                                  args=args, vars=vars, extension=extension,
                                  user_signature=user_signature)
            script = html.SCRIPT('web2py_component("%s","%s")' % (url, target),
                                 _type="text/javascript")
            return html.TAG[''](script, html.DIV(content,**attr))
        else:
            if not isinstance(args,(list,tuple)):
                args = [args]
            c = c or request.controller

            other_request = Storage(request)
            other_request['env'] = Storage(request.env)
            other_request.controller = c
            other_request.function = f
            other_request.extension = extension or request.extension
            other_request.args = List(args)
            other_request.vars = vars
            other_request.get_vars = vars
            other_request.post_vars = Storage()
            other_response = globals.Response()
            other_request.env.path_info = '/' + \
                '/'.join([request.application,c,f] + \
                             map(str, other_request.args))
            other_request.env.query_string = \
                vars and html.URL(vars=vars).split('?')[1] or ''
            other_request.env.http_web2py_component_location = \
                request.env.path_info
            other_request.cid = target
            other_request.env.http_web2py_component_element = target
            other_response.view = '%s/%s.%s' % (c,f, other_request.extension)
            other_environment = copy.copy(self.environment)
            other_response._view_environment = other_environment
            other_response.generic_patterns = \
                copy.copy(current.response.generic_patterns)
            other_environment['request'] = other_request
            other_environment['response'] = other_response

            ## some magic here because current are thread-locals

            original_request, current.request = current.request, other_request
            original_response, current.response = current.response, other_response
            page = run_controller_in(c, f, other_environment)
            if isinstance(page, dict):
                other_response._vars = page
                other_response._view_environment.update(page)
                run_view_in(other_response._view_environment)
                page = other_response.body.getvalue()
            current.request, current.response = original_request, original_response
            js = None
            if ajax_trap:
                link = html.URL(request.application, c, f, r=request,
                                args=args, vars=vars, extension=extension,
                                user_signature=user_signature)
                js = "web2py_trap_form('%s','%s');" % (link, target)
            script = js and html.SCRIPT(js,_type="text/javascript") or ''
            return html.TAG[''](html.DIV(html.XML(page),**attr),script)
Esempio n. 3
0
def render(content = "hello world",
           stream = None,
           filename = None,
           path = None,
           context = {},
           lexers  = {},
           delimiters = ('{{','}}')
           ):
    """
    >>> render()
    'hello world'
    >>> render(content='abc')
    'abc'
    >>> render(content='abc\\'')
    "abc'"
    >>> render(content='a"\\'bc')
    'a"\\'bc'
    >>> render(content='a\\nbc')
    'a\\nbc'
    >>> render(content='a"bcd"e')
    'a"bcd"e'
    >>> render(content="'''a\\nc'''")
    "'''a\\nc'''"
    >>> render(content="'''a\\'c'''")
    "'''a\'c'''"
    >>> render(content='{{for i in range(a):}}{{=i}}<br />{{pass}}', context=dict(a=5))
    '0<br />1<br />2<br />3<br />4<br />'
    >>> render(content='{%for i in range(a):%}{%=i%}<br />{%pass%}', context=dict(a=5),delimiters=('{%','%}'))
    '0<br />1<br />2<br />3<br />4<br />'
    >>> render(content="{{='''hello\\nworld'''}}")
    'hello\\nworld'
    >>> render(content='{{for i in range(3):\\n=i\\npass}}')
    '012'
    """
    # Here to avoid circular Imports
    import globals

    # If we don't have anything to render, why bother?
    if not content and not stream and not filename:
        raise SyntaxError, "Must specify a stream or filename or content"

    # Here for legacy purposes, probably can be reduced to something more simple.
    close_stream = False
    if not stream:
        if filename:
            stream = open(filename, 'rb')
            close_stream = True
        elif content:
            stream = cStringIO.StringIO(content)

    # Get a response class.
    context['response'] = globals.Response()

    # Execute the template.
    exec(str(TemplateParser(stream.read(), context=context, path=path, lexers=lexers, delimiters=delimiters))) in context

    if close_stream:
        stream.close()

    # Returned the rendered content.
    return context['response'].body.getvalue()
Esempio n. 4
0
    def __call__(self, c=None, f='index', args=[], vars={},
                 extension=None, target=None,ajax=False,ajax_trap=False,
                 url=None,user_signature=False, content='loading...',**attr):
        import globals
        target = target or 'c'+str(random.random())[2:]
        attr['_id']=target        
        request = self.environment['request']
        if '.' in f:
            f, extension = f.split('.',1)
        if url or ajax:
            url = url or html.URL(request.application, c, f, r=request,
                                  args=args, vars=vars, extension=extension,
                                  user_signature=user_signature)
            script = html.SCRIPT('web2py_component("%s","%s")' % (url, target),
                                 _type="text/javascript")
            return html.TAG[''](script, html.DIV(content,**attr))
        else:
            if not isinstance(args,(list,tuple)):
                args = [args]
            c = c or request.controller
            other_request = globals.Request()
            other_request.application = request.application
            other_request.controller = c
            other_request.function = f
            other_request.extension = extension or request.extension
            other_request.args = List(args)
            other_request.folder = request.folder
            other_request.env = request.env
            other_request.vars = request.vars
            other_request.get_vars = request.get_vars
            other_request.post_vars = request.post_vars
            other_response = globals.Response()
            other_request.env.http_web2py_component_location = \
                request.env.path_info
            other_request.env.http_web2py_component_element = target
            other_response.view = '%s/%s.%s' % (c,f, other_request.extension)

            other_environment = copy.copy(self.environment)
            other_response._view_environment = other_environment
            other_environment['request'] = other_request
            other_environment['response'] = other_response
            page = run_controller_in(c, f, other_environment)

            if isinstance(page, dict):
                other_response._vars = page
                for key in page:
                    other_response._view_environment[key] = page[key]
                run_view_in(other_response._view_environment)
                page = other_response.body.getvalue()
            js = None
            if ajax_trap:
                link = html.URL(request.application, c, f, r=request,
                                args=args, vars=vars, extension=extension,
                                user_signature=user_signature)
                js = "web2py_trap_form('%s','%s');" % (link, target)
            # not sure about this
            # for (name,value) in other_response.headers:
            #     if name == 'web2py-component-command':
            #         js += value
            script = js and html.SCRIPT(js,_type="text/javascript") or ''
            return html.TAG[''](html.DIV(html.XML(page),**attr),script)