Exemple #1
0
def init_for(self, url, comp, http_method, request):
    """Initialisation from an url

    In:
      - ``url`` -- rest of the url to process
      - ``comp`` -- the component
      - ``http_method`` -- the HTTP method
      - ``request`` -- the complete Request object

    Return:
      - ``presentation.NOT_FOUND`` if the url is invalid, else ``None``
    """
    presentation.init(self(), url, self, http_method, request)
Exemple #2
0
def init_for(self, url, comp, http_method, request):
    """Initialisation from an url

    In:
      - ``url`` -- rest of the url to process
      - ``comp`` -- the component
      - ``http_method`` -- the HTTP method
      - ``request`` -- the complete Request object

    Return:
      - ``presentation.NOT_FOUND`` if the url is invalid, else ``None``
    """
    presentation.init(self(), url, self, http_method, request)
Exemple #3
0
    def __call__(self, environ, start_response):
        """WSGI interface

        In:
          - ``environ`` -- dictionary of the received elements
          - ``start_response`` -- callback to send the headers to the browser

        Return:
          - the content to send back to the browser
        """
        local.request.clear()

        # Create the ``WebOb`` request and response objects
        # -------------------------------------------------

        request = webob.Request(environ, charset='utf-8')
        response = Response('text/html' if self.always_html else str(request.accept))

        channel_id = request.params.get('_channel')
        nb = request.params.get('_nb')
        if channel_id and nb:
            if environ['wsgi.multiprocess']:
                response.status = 501 # "Not Implemented"
            else:
                comet.channels.connect(channel_id, int(nb), environ['wsgi.input'].file.fileno(), response)

            return response(environ, start_response)

        xhr_request = request.is_xhr or ('_a' in request.params)

        session = None
        self.last_exception = None

        log.set_logger('nagare.application.'+self.name) # Set the dedicated application logger
        
        # Create a database transaction for each request
        with database.session.begin():
            try:
                # Phase 1
                # -------

                # Test the request validity
                if not request.path_info:
                    self.on_incomplete_url(request, response)

                try:
                    session = self.sessions.get(request, response, xhr_request)
                except ExpirationError:
                    self.on_session_expired(request, response)

                if session.is_new:
                    # A new session is created
                    root = self.create_root()   # Create a new application root component
                    callbacks = Callbacks()     # Create a new callbacks registry
                else:
                    # An existing session is used, retrieve the application root component
                    # and the callbacks registry
                    (root, callbacks) = session.data

                request.method = request.params.get('_method', request.method)
                if not session.is_new and request.method not in ('GET', 'POST'):
                    self.on_bad_http_method(request, response)

                self.start_request(root, request, response)

                url = request.path_info.strip('/')
                if session.is_new and url:
                    # If a URL is given, initialize the objects graph with it
                    presentation.init(root, tuple([u.decode('utf-8') for u in url.split('/')]), None, request.method, request)

                try:
                    render = self._phase1(request, response, callbacks)
                except CallbackLookupError:
                    render = self.on_callback_lookuperror(request, response, xhr_request)
            except exc.HTTPException, response:
                # When a ``webob.exc`` object is raised during phase 1, skip the
                # phase 2 and use it as the response object
                pass
            except Exception:
                self.last_exception = (request,  sys.exc_info())
                response = self.on_exception(request, response)
Exemple #4
0
    def init(self, url, http_method, request):
        """Initialisation from an url

        Forward the call to the generic method of the ``presentation`` service
        """
        return presentation.init(self, url, self, http_method, request)
Exemple #5
0
    def init(self, url, http_method, request):
        """Initialisation from an url

        Forward the call to the generic method of the ``presentation`` service
        """
        return presentation.init(self, url, self, http_method, request)
Exemple #6
0
    def __call__(self, environ, start_response):
        """WSGI interface

        In:
          - ``environ`` -- dictionary of the received elements
          - ``start_response`` -- callback to send the headers to the browser

        Return:
          - the content to send back to the browser
        """
        local.request.clear()

        # Create the ``WebOb`` request and response objects
        # -------------------------------------------------

        request = self.create_request(environ)
        try:
            request.params, request.url
        except UnicodeDecodeError:
            return exc.HTTPClientError()(environ, start_response)

        response = self.create_response(
            request, 'text/html' if self.always_html else str(request.accept))

        channel_id = request.params.get('_channel')
        nb = request.params.get('_nb')
        if channel_id and nb:
            if environ['wsgi.multiprocess']:
                response.status = 501  # "Not Implemented"
            else:
                comet.channels.connect(channel_id, int(nb),
                                       environ['wsgi.input'].file.fileno(),
                                       response)

            return response(environ, start_response)

        xhr_request = request.is_xhr

        state = None
        self.last_exception = None

        log.set_logger('nagare.application.' +
                       self.name)  # Set the dedicated application logger

        # Create a database transaction for each request
        with database.session.begin():
            try:
                # Phase 1
                # -------

                # Test the request validity
                if not request.path_info:
                    self.on_incomplete_url(request, response)

                try:
                    state = self.sessions.get_state(request, response,
                                                    xhr_request)
                except ExpirationError:
                    self.on_expired_session(request, response)
                except SessionSecurityError:
                    self.on_invalid_session(request, response)

                state.acquire()

                try:
                    root, callbacks = state.get_root() or (self.create_root(),
                                                           None)
                except ExpirationError:
                    self.on_expired_session(request, response)
                except SessionSecurityError:
                    self.on_invalid_session(request, response)

                self.start_request(root, request, response)

                if callbacks is None:
                    # New state
                    request.method = request.params.get(
                        '_method', request.method)
                    if request.method not in ('GET', 'POST'):
                        self.on_bad_http_method(request, response)

                    url = request.path_info.strip('/')
                    if url:
                        # If a URL is given, initialize the objects graph with it
                        presentation.init(root, tuple(url.split('/')), None,
                                          request.method, request)

                try:
                    render = self._phase1(root, request, response, callbacks)
                except CallbackLookupError:
                    render = self.on_callback_lookuperror(
                        request, response, xhr_request)
            except exc.HTTPException, response:
                # When a ``webob.exc`` object is raised during phase 1, skip the
                # phase 2 and use it as the response object
                pass
            except Exception:
                self.last_exception = (request, sys.exc_info())
                response = self.on_exception(request, response)
Exemple #7
0
    def __call__(self, environ, start_response):
        """WSGI interface

        In:
          - ``environ`` -- dictionary of the received elements
          - ``start_response`` -- callback to send the headers to the browser

        Return:
          - the content to send back to the browser
        """
        local.request.clear()

        # Create the ``WebOb`` request and response objects
        # -------------------------------------------------

        request = self.create_request(environ)
        try:
            request.params, request.url
        except UnicodeDecodeError:
            return exc.HTTPClientError()(environ, start_response)

        response = self.create_response(request, "text/html" if self.always_html else str(request.accept))

        channel_id = request.params.get("_channel")
        nb = request.params.get("_nb")
        if channel_id and nb:
            if environ["wsgi.multiprocess"]:
                response.status = 501  # "Not Implemented"
            else:
                comet.channels.connect(channel_id, int(nb), environ["wsgi.input"].file.fileno(), response)

            return response(environ, start_response)

        xhr_request = request.is_xhr

        state = None
        self.last_exception = None

        log.set_logger("nagare.application." + self.name)  # Set the dedicated application logger

        # Create a database transaction for each request
        with database.session.begin():
            try:
                # Phase 1
                # -------

                # Test the request validity
                if not request.path_info:
                    self.on_incomplete_url(request, response)

                try:
                    state = self.sessions.get_state(request, response, xhr_request)
                except ExpirationError:
                    self.on_expired_session(request, response)
                except SessionSecurityError:
                    self.on_invalid_session(request, response)

                state.acquire()

                try:
                    root, callbacks = state.get_root() or (self.create_root(), None)
                except ExpirationError:
                    self.on_expired_session(request, response)
                except SessionSecurityError:
                    self.on_invalid_session(request, response)

                self.start_request(root, request, response)

                if callbacks is None:
                    # New state
                    request.method = request.params.get("_method", request.method)
                    if request.method not in ("GET", "POST"):
                        self.on_bad_http_method(request, response)

                    url = request.path_info.strip("/")
                    if url:
                        # If a URL is given, initialize the objects graph with it
                        presentation.init(root, tuple(url.split("/")), None, request.method, request)

                try:
                    render = self._phase1(root, request, response, callbacks)
                except CallbackLookupError:
                    render = self.on_callback_lookuperror(request, response, xhr_request)
            except exc.HTTPException, response:
                # When a ``webob.exc`` object is raised during phase 1, skip the
                # phase 2 and use it as the response object
                pass
            except Exception:
                self.last_exception = (request, sys.exc_info())
                response = self.on_exception(request, response)