Exemplo n.º 1
0
    def __init__(self, app, environ, start_response, **args):
        '''__init__ the Context.

		'''

        super().__init__()

        # application Object
        #self.APP        = app

        # use this for now request_time
        self.now = datetime.datetime.now()

        self.environ = environ

        self.wsgi_callback = start_response

        # available when doing request process
        self.active_action = None

        # assets of css and js. it is still here even response type is changed from Page to Form.
        # response may make use of it if needed
        self.asset = Asset()
        self.page_menu_tab = PageMenuTab()
        self.page_menu_sub_tab = PageMenuTab()
        self.page_menu_sub_tab_2 = PageMenuTab()

        # themer will use it
        self.page_title = ''
        self.display_title = True

        self.headers = wsgiref.headers.Headers([])
        self.cookie = Cookie()

        # additional args for this context
        self.args = args

        #moved to IN
        # theme for the current context. each context output may be rendered by different theme engine.
        #self.themer      = None

        themer = IN.themer
        APP = IN.APP

        default_theme_name = APP.config.default_theme_name
        context_theme_name = APP.decide_theme(self)

        # current theme for this request

        if default_theme_name != themer.default_theme_name:
            self.current_theme = themer.load_theme(context_theme_name)
        else:
            self.current_theme = themer.default_theme

        # simple dict cache for the current context
        self.static_cache = {}

        # TODO: session for the current nabar
        self.session = {}

        # database connection class
        self.db_connections = []

        # __disabled_hooks__ - to disable the hook invoke on some hooks temporarly
        # IN.Registry will use this - for context based state.
        self.__disabled_hooks__ = []

        # hooks will be added here if those are not allowed for recursive calls
        self.__not_recursive_hooks__ = []

        # TODO
        #self.__In_static__ = {} # context static value container

        # init the request

        self.request = In.core.request.Request(self)
        #IN.hook_invoke('__context_request_process__', self, self.request)

        # current logged in nabar

        path_parts = self.request.path_parts

        nabar = None

        # ignore nabar for static files path
        if len(path_parts) == 0 or path_parts[0] != IN.APP.config.pfpp:

            try:
                nabar_id = IN.nabar.auth_cookie(self)

                if nabar_id:
                    nabar = IN.entitier.load('Nabar', nabar_id)
            except Exception as e:
                IN.logger.debug()

                # TODO: delete the cookie if nabar is None

        if nabar is None:  # use the default
            nabar = In.nabar.anonymous()

        self.nabar = nabar

        try:  # context may not available
            self.language = nabar.language
        except AttributeError:
            self.language = APP.config.language

        # init the response

        #IN.hook_invoke('__context_response_init__', self, self.request)

        # use default page from default theme

        # SPEED # TODO: do we want this even for Form submit /  File request?
        res_args = {}
        page_class = APP.decide_page_class(self)
        if type(page_class) is str:
            page = Object.new(page_class)
        else:
            page = page_class()

        #res_args['output'] = page_class()

        # default to PageResponse
        self.response = In.core.response.PageResponse(output=page)

        IN.hook_invoke('__context_init__', self)