Esempio n. 1
0
    def serve(self,
              headers: Headers,
              query: Optional[QueryString],
              context: Optional[Context] = None,
              session: Optional[AbstractSession] = None) -> str:

        if session is None:
            session = self.session_implementation.from_headers(
                headers=headers,
                configuration=self.configuration,
                credentials=RequestCredentials.on_behalf_of_agent(
                    agent=self.configuration.api_agent,
                    configuration=self.configuration),
                request_may_change_state=self.requests_may_change_state)

        if context is None:
            context = Context()

        if session is not None:
            context.add('agent', session.agent)
            context.add_javascript_constant('global_api_key', session.api_key)
            context.add_javascript_constant('global_session_id',
                                            session.session_id)
            context.add_javascript_constant('requesting_agent_id',
                                            session.agent_id)

        return super().serve(headers=headers,
                             query=query,
                             context=context,
                             session=session)
 def render(self, context: Optional[Context]) -> str:
     """
     Execute stage 2 render, returning a string HTML page for provision to
     a client.
     """
     if context is None:
         return self._template.render(Context().context)
     return self._template.render(**context.context)
Esempio n. 3
0
    def serve(self,
              headers: Headers,
              query: Optional[QueryString],
              context: Optional[Context] = None,
              session: Optional[AbstractSession] = None) -> str:
        """
        Return a string response to a request
        """
        if context is None:
            context = Context()

        computed_context = self.compute_response(query=query,
                                                 context=context,
                                                 requesting_agent=session)
        assert isinstance(computed_context, Context)
        return self._template.render(computed_context)
Esempio n. 4
0
    def serve(self,
              headers: Headers,
              query: Optional[QueryString],
              context: Optional[Context] = None,
              session: Optional[AbstractSession] = None) -> str:

        if session is None:
            session = self.session_implementation.from_headers(
                headers=headers,
                configuration=self.configuration,
                credentials=RequestCredentials.on_behalf_of_agent(
                    agent=self.configuration.api_agent,
                    configuration=self.configuration),
                request_may_change_state=self.requests_may_change_state)

        if session is None:
            redirect = self.login_redirect(query)
            if redirect is not None:
                raise redirect
            if self.login_redirect_path is not None:
                raise Redirect(
                    destination=self.login_redirect_path,
                    allow_next=True,
                    preserve_arguments=True,
                )
            raise NotAuthenticated

        if context is None:
            context = Context()

        assert isinstance(session, Session)
        self.enforce_perspective(session)

        return super().serve(headers=headers,
                             query=query,
                             context=context,
                             session=session)
Esempio n. 5
0
 def generate_context(self) -> Context:
     """Return a transient context for use in rendering the view"""
     self._transient_context = Context()
     return self._transient_context