Пример #1
0
 def render(self, *a, **b):
     from compileapp import run_view_in
     if len(a) > 2:
         raise SyntaxError(
             'Response.render can be called with two arguments, at most')
     elif len(a) == 2:
         (view, self._vars) = (a[0], a[1])
     elif len(a) == 1 and isinstance(a[0], str):
         (view, self._vars) = (a[0], {})
     elif len(a) == 1 and hasattr(a[0], 'read') and callable(a[0].read):
         (view, self._vars) = (a[0], {})
     elif len(a) == 1 and isinstance(a[0], dict):
         (view, self._vars) = (None, a[0])
     else:
         (view, self._vars) = (None, {})
     self._vars.update(b)
     self._view_environment.update(self._vars)
     if view:
         import cStringIO
         (obody, oview) = (self.body, self.view)
         (self.body, self.view) = (cStringIO.StringIO(), view)
         run_view_in(self._view_environment)
         page = self.body.getvalue()
         self.body.close()
         (self.body, self.view) = (obody, oview)
     else:
         run_view_in(self._view_environment)
         page = self.body.getvalue()
     return page
Пример #2
0
 def render(self, *a, **b):
     from compileapp import run_view_in
     if len(a) > 2:
         raise SyntaxError(
             'Response.render can be called with two arguments, at most')
     elif len(a) == 2:
         (view, self._vars) = (a[0], a[1])
     elif len(a) == 1 and isinstance(a[0], str):
         (view, self._vars) = (a[0], {})
     elif len(a) == 1 and hasattr(a[0], 'read') and callable(a[0].read):
         (view, self._vars) = (a[0], {})
     elif len(a) == 1 and isinstance(a[0], dict):
         (view, self._vars) = (None, a[0])
     else:
         (view, self._vars) = (None, {})
     self._vars.update(b)
     self._view_environment.update(self._vars)
     if view:
         import cStringIO
         (obody, oview) = (self.body, self.view)
         (self.body, self.view) = (cStringIO.StringIO(), view)
         run_view_in(self._view_environment)
         page = self.body.getvalue()
         self.body.close()
         (self.body, self.view) = (obody, oview)
     else:
         run_view_in(self._view_environment)
         page = self.body.getvalue()
     return page
Пример #3
0
def serve_controller(request, response, session):
    """
    this function is used to generate a dynamic page.
    It first runs all models, then runs the function in the controller,
    and then tries to render the output using a view/template.
    this function must run from the [application] folder.
    A typical example would be the call to the url
    /[application]/[controller]/[function] that would result in a call
    to [function]() in applications/[application]/[controller].py
    rendered by applications/[application]/views/[controller]/[function].html
    """

    # ##################################################
    # build environment for controller and view
    # ##################################################

    environment = build_environment(request, response, session)

    # set default view, controller can override it

    response.view = '%s/%s.%s' % (request.controller,
                                  request.function,
                                  request.extension)

    # also, make sure the flash is passed through
    # ##################################################
    # process models, controller and view (if required)
    # ##################################################

    run_models_in(environment)
    response._view_environment = copy.copy(environment)
    page = run_controller_in(request.controller, request.function, environment)
    if isinstance(page, dict):
        response._vars = page
        response._view_environment.update(page)
        run_view_in(response._view_environment)
        page = response.body.getvalue()
    # logic to garbage collect after exec, not always, once every 100 requests
    global requests
    requests = ('requests' in globals()) and (requests + 1) % 100 or 0
    if not requests:
        gc.collect()
    # end garbage collection logic

    # ##################################################
    # set default headers it not set
    # ##################################################

    default_headers = [
        ('Content-Type', contenttype('.' + request.extension)),
        ('Cache-Control',
         'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'),
        ('Expires', time.strftime('%a, %d %b %Y %H:%M:%S GMT',
                                  time.gmtime())),
        ('Pragma', 'no-cache')]
    for key, value in default_headers:
        response.headers.setdefault(key, value)

    raise HTTP(response.status, page, **response.headers)
Пример #4
0
def serve_controller(request, response, session):
    """
    this function is used to generate a dynamic page.
    It first runs all models, then runs the function in the controller,
    and then tries to render the output using a view/template.
    this function must run from the [application] folder.
    A typical example would be the call to the url
    /[application]/[controller]/[function] that would result in a call
    to [function]() in applications/[application]/[controller].py
    rendered by applications/[application]/views/[controller]/[function].html
    """

    # ##################################################
    # build environment for controller and view
    # ##################################################

    environment = build_environment(request, response, session)

    # set default view, controller can override it

    response.view = '%s/%s.%s' % (request.controller,
                                  request.function,
                                  request.extension)

    # also, make sure the flash is passed through
    # ##################################################
    # process models, controller and view (if required)
    # ##################################################

    run_models_in(environment)
    response._view_environment = copy.copy(environment)
    page = run_controller_in(request.controller, request.function, environment)
    if isinstance(page, dict):
        response._vars = page
        response._view_environment.update(page)
        run_view_in(response._view_environment)
        page = response.body.getvalue()
    # logic to garbage collect after exec, not always, once every 100 requests
    global requests
    requests = ('requests' in globals()) and (requests + 1) % 100 or 0
    if not requests:
        gc.collect()
    # end garbage collection logic

    # ##################################################
    # set default headers it not set
    # ##################################################

    default_headers = [
        ('Content-Type', contenttype('.' + request.extension)),
        ('Cache-Control',
         'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'),
        ('Expires', time.strftime('%a, %d %b %Y %H:%M:%S GMT',
                                  time.gmtime())),
        ('Pragma', 'no-cache')]
    for key, value in default_headers:
        response.headers.setdefault(key, value)

    raise HTTP(response.status, page, **response.headers)
Пример #5
0
def serve_controller(request, response, session):
    """
    this function is used to generate a dynamic page.
    It first runs all models, then runs the function in the controller,
    and then tries to render the output using a view/template.
    this function must run from the [application] folder.
    A typical example would be the call to the url
    /[application]/[controller]/[function] that would result in a call
    to [function]() in applications/[application]/[controller].py
    rendered by applications/[application]/views/[controller]/[function].html
    """

    # ##################################################
    # build environment for controller and view
    # ##################################################

    environment = build_environment(request, response, session)

    # set default view, controller can override it

    response.view = '%s/%s.%s' % (request.controller,
                                  request.function,
                                  request.extension)

    # also, make sure the flash is passed through
    # ##################################################
    # process models, controller and view (if required)
    # ##################################################

    run_models_in(environment)
    response._view_environment = copy.copy(environment)
    page = run_controller_in(request.controller, request.function, environment)
    if isinstance(page, dict):
        response._vars = page
        for key in page:
            response._view_environment[key] = page[key]
        run_view_in(response._view_environment)
        page = response.body.getvalue()
    # logic to garbage collect after exec, not always, once every 100 requests
    global requests
    requests = ('requests' in globals()) and (requests+1) % 100 or 0
    if not requests: gc.collect()
    # end garbage collection logic
    raise HTTP(response.status, page, **response.headers)
Пример #6
0
def serve_controller(request, response, session):
    """
    this function is used to generate a dynamic page.
    It first runs all models, then runs the function in the controller,
    and then tries to render the output using a view/template.
    this function must run from the [application] folder.
    A typical example would be the call to the url
    /[application]/[controller]/[function] that would result in a call
    to [function]() in applications/[application]/[controller].py
    rendered by applications/[application]/views/[controller]/[function].html
    """

    # ##################################################
    # build environment for controller and view
    # ##################################################

    environment = build_environment(request, response, session)

    # set default view, controller can override it

    response.view = '%s/%s.%s' % (request.controller,
                                  request.function,
                                  request.extension)

    # also, make sure the flash is passed through
    # ##################################################
    # process models, controller and view (if required)
    # ##################################################

    run_models_in(environment)
    response._view_environment = copy.copy(environment)
    page = run_controller_in(request.controller, request.function, environment)
    if isinstance(page, dict):
        response._vars = page
        for key in page:
            response._view_environment[key] = page[key]
        run_view_in(response._view_environment)
        page = response.body.getvalue()
    # logic to garbage collect after exec, not always, once every 100 requests
    global requests
    requests = ('requests' in globals()) and (requests+1) % 100 or 0
    if not requests: gc.collect()
    # end garbage collection logic
    raise HTTP(response.status, page, **response.headers)
Пример #7
0
def serve_controller(request, response, session):
    """
    this function is used to generate a dynmaic page.
    It first runs all models, then runs the function in the controller,
    and then tries to render the output using a view/template.
    this function must run from the [applciation] folder.
    A typical examples would be the call to the url
    /[applicaiton]/[controller]/[function] that would result in a call
    to [function]() in applications/[application]/[controller].py
    renedred by applications/[application]/[controller]/[view].html
    """

    # ##################################################
    # build evnironment for controller and view
    # ##################################################

    environment = build_environment(request, response, session)

    # set default view, controller can override it

    response.view = '%s/%s.html' % (request.controller,
                                    request.function)

    # also, make sure the flash is passed through
    # ##################################################
    # process models, controller and view (if required)
    # ##################################################

    run_models_in(environment)
    response._view_environment = copy.copy(environment)
    run_controller_in(request.controller, request.function, environment)
    if not type(response.body) in [types.StringType,
                                   types.GeneratorType]:
        for (key, value) in response._vars.items():
            response._view_environment[key] = value
        run_view_in(response._view_environment)
        response.body = response.body.getvalue()
    raise HTTP(200, response.body, **response.headers)
Пример #8
0
def serve_controller(request, response, session):
    """
    this function is used to generate a dynamic page.
    It first runs all models, then runs the function in the controller,
    and then tries to render the output using a view/template.
    this function must run from the [application] folder.
    A typical examples would be the call to the url
    /[application]/[controller]/[function] that would result in a call
    to [function]() in applications/[application]/[controller].py
    rendered by applications/[application]/[controller]/[view].html
    """

    # ##################################################
    # build environment for controller and view
    # ##################################################

    environment = build_environment(request, response, session)

    # set default view, controller can override it

    response.view = "%s/%s.%s" % (request.controller, request.function, request.extension)

    # also, make sure the flash is passed through
    # ##################################################
    # process models, controller and view (if required)
    # ##################################################

    run_models_in(environment)
    response._view_environment = copy.copy(environment)
    page = run_controller_in(request.controller, request.function, environment)
    if isinstance(page, dict):
        response._vars = page
        for key in page:
            response._view_environment[key] = page[key]
        run_view_in(response._view_environment)
        page = response.body.getvalue()
    raise HTTP(200, page, **response.headers)
Пример #9
0
 def render(self, *a, **b):
     if len(a) > 2:
         raise SyntaxError
     elif len(a) == 2:
         (view, self._vars) = (a[0], a[1])
     elif len(a) == 1 and isinstance(a[0], str):
         (view, self._vars) = (a[0], {})
     elif len(a) == 1 and isinstance(a[0], dict):
         (view, self._vars) = (None, a[0])
     else:
         (view, self._vars) = (None, {})
     self._vars.update(b)
     self._view_environment.update(self._vars)
     if view:
         import cStringIO
         (obody, oview) = (self.body, self.view)
         (self.body, self.view) = (cStringIO.StringIO(), view)
         run_view_in(self._view_environment)
         page = self.body.getvalue()
         (self.body, self.view) = (obody, oview)
     else:
         run_view_in(self._view_environment)
         page = self.body.getvalue()
     return page
Пример #10
0
def serve_controller(request, response, session):
    """
    this function is used to generate a dynmaic page.
    It first runs all models, then runs the function in the controller,
    and then tries to render the output using a view/template.
    this function must run from the [applciation] folder.
    A typical examples would be the call to the url
    /[applicaiton]/[controller]/[function] that would result in a call
    to [function]() in applications/[application]/[controller].py
    renedred by applications/[application]/[controller]/[view].html
    """

    # ##################################################
    # build evnironment for controller and view
    # ##################################################

    environment = build_environment(request, response, session)

    # set default view, controller can override it

    response.view = '%s/%s.html' % (request.controller, request.function)

    # also, make sure the flash is passed through
    # ##################################################
    # process models, controller and view (if required)
    # ##################################################

    run_models_in(environment)
    response._view_environment = copy.copy(environment)
    run_controller_in(request.controller, request.function, environment)
    if not type(response.body) in [types.StringType, types.GeneratorType]:
        for (key, value) in response._vars.items():
            response._view_environment[key] = value
        run_view_in(response._view_environment)
        response.body = response.body.getvalue()
    raise HTTP(200, response.body, **response.headers)
Пример #11
0
 def render(self, *a, **b):
     if len(a) > 2:
         raise SyntaxError
     elif len(a) == 2:
         (view, self._vars) = (a[0], a[1])
     elif len(a) == 1 and isinstance(a[0], str):
         (view, self._vars) = (a[0], {})
     elif len(a) == 1 and isinstance(a[0], dict):
         (view, self._vars) = (None, a[0])
     else:
         (view, self._vars) = (None, {})
     self._vars.update(b)
     self._view_environment.update(self._vars)
     if view:
         import cStringIO
         (obody, oview) = (self.body, self.view)
         (self.body, self.view) = (cStringIO.StringIO(), view)
         run_view_in(self._view_environment)
         page = self.body.getvalue()
         (self.body, self.view) = (obody, oview)
     else:
         run_view_in(self._view_environment)
         page = self.body.getvalue()
     return page
 def run_view(self):
     return run_view_in(self.env)