Example #1
0
 def _build_url(self, request, path, url, base):
     if url is None:
         return
     if not is_absolute_uri(url):
         base = base or request.absolute_uri('/')
         url = urljoin(base, url)
         if not is_absolute_uri(url):
             base = request.absolute_uri('/')
             url = urljoin(base, url)
     return url_path(url, path)
Example #2
0
    def middleware(self, app):
        middleware = [self]
        for backend in self.backends:
            middleware.extend(backend.middleware(app) or ())

        dotted_path = app.config['PAGINATION']
        pagination = module_attribute(dotted_path)
        if not pagination:
            raise ImproperlyConfigured('Could not load paginator "%s"',
                                       dotted_path)
        app.pagination = pagination()

        url = app.config['API_URL']
        # If the api url is not absolute, add the api middleware
        if url is not None:
            if not is_absolute_uri(url):
                # Add the preflight and token events
                events = ('on_preflight', 'on_token')
                app.add_events(events)
                for backend in self.backends:
                    app.bind_events(backend, events)

                api = RestRoot(url)
                middleware.append(api)
                for extension in app.extensions.values():
                    api_sections = getattr(extension, 'api_sections', None)
                    if api_sections:
                        for router in api_sections(app):
                            api.add_child(router)

            app.api = ApiClient(app)

        return middleware
Example #3
0
def auth_router(api, url, Router, path=None):
    params = {'form_enctype': 'application/json'}
    if is_absolute_uri(api) and hasattr(Router, 'post'):
        action = luxrest('', path=url)
    else:
        params['post'] = None
        action = luxrest(api, name='authorizations_url')

    params['form_action'] = action
    if path is None:
        path = url
    if path:
        action['path'] = path
    return Router(url, **params)
Example #4
0
def auth_router(api, url, Router, path=None):
    params = {'form_enctype': 'application/json'}
    if is_absolute_uri(api) and hasattr(Router, 'post'):
        action = luxrest('', path=url)
    else:
        params['post'] = None
        action = luxrest(api, name='authorizations_url')

    params['form_action'] = action
    if path is None:
        path = url
    if path:
        action['path'] = path
    return Router(url, **params)
Example #5
0
    def middleware(self, app):
        middleware = []
        cfg = app.config
        api_url = cfg['API_URL']
        if not is_absolute_uri(api_url):
            api_url = None

        if cfg['LOGIN_URL']:
            middleware.append(Login(cfg['LOGIN_URL'], api_url=api_url))
            middleware.append(Logout(cfg['LOGOUT_URL'], api_url=api_url))
        if cfg['REGISTER_URL']:
            middleware.append(SignUp(cfg['REGISTER_URL'], api_url=api_url))
        if cfg['RESET_PASSWORD_URL']:
            middleware.append(
                ForgotPassword(cfg['RESET_PASSWORD_URL'], api_url=api_url))
        return middleware
Example #6
0
    def middleware(self, app):
        middleware = [self]
        for backend in self.backends:
            middleware.extend(backend.middleware(app) or ())

        url = app.config['API_URL']
        if not is_absolute_uri(url):

            app.api = api = RestRoot(url)
            app.config['API_URL'] = str(api.route)
            for extension in app.extensions.values():
                api_sections = getattr(extension, 'api_sections', None)
                if api_sections:
                    for router in api_sections(app):
                        api.add_child(router)

        return middleware
Example #7
0
    def middleware(self, app):
        middleware = [self]
        for backend in self.backends:
            middleware.extend(backend.middleware(app) or ())

        url = app.config['API_URL']
        if not is_absolute_uri(url):

            app.api = api = RestRoot(url)
            app.config['API_URL'] = str(api.route)
            for extension in app.extensions.values():
                api_sections = getattr(extension, 'api_sections', None)
                if api_sections:
                    for router in api_sections(app):
                        api.add_child(router)

        return middleware
Example #8
0
    def absolute_uri(self, location=None, scheme=None):
        """Builds an absolute URI from ``location`` and variables
        available in this request.

        If no ``location`` is specified, the relative URI is built from
        :meth:`full_path`.
        """
        if not is_absolute_uri(location):
            location = self.full_path(location)
            if not scheme:
                scheme = self.is_secure and 'https' or 'http'
            base = '%s://%s' % (scheme, self.get_host())
            return '%s%s' % (base, location)
        elif not scheme:
            return iri_to_uri(location)
        else:
            raise ValueError('Absolute location with scheme not valid')
Example #9
0
    def absolute_uri(self, location=None, scheme=None):
        """Builds an absolute URI from ``location`` and variables
        available in this request.

        If no ``location`` is specified, the relative URI is built from
        :meth:`full_path`.
        """
        if not is_absolute_uri(location):
            location = self.full_path(location)
            if not scheme:
                scheme = self.is_secure and 'https' or 'http'
            base = '%s://%s' % (scheme, self.get_host())
            return '%s%s' % (base, location)
        elif not scheme:
            return iri_to_uri(location)
        else:
            raise ValueError('Absolute location with scheme not valid')
Example #10
0
    def http(self):
        '''Get the HTTP client
        '''
        if self._http is None:
            api_url = self.app.config['API_URL']
            headers = [('content-type', 'application/json')]
            # Remote API
            if is_absolute_uri(api_url):
                if self.app.green_pool:
                    self._http = HttpClient(headers=headers)
                else:
                    self._http = HttpClient(loop=new_event_loop())
            # Local API
            else:
                self._http = LocalClient(self.app, headers)
            token = self.app.config['API_AUTHENTICATION_TOKEN']
            if token:
                self._http.headers['Athentication'] = 'Bearer %s' % token

        return self._http
Example #11
0
    def http(self):
        '''Get the HTTP client
        '''
        if self._http is None:
            api_url = self.app.config['API_URL']
            headers = [('content-type', 'application/json')]
            # Remote API
            if is_absolute_uri(api_url):
                if self.app.green_pool:
                    self._http = HttpClient(headers=headers)
                else:
                    self._http = HttpClient(loop=new_event_loop())
            # Local API
            else:
                self._http = LocalClient(self.app, headers)
            token = self.app.config['API_AUTHENTICATION_TOKEN']
            if token:
                self._http.headers['Athentication'] = 'Bearer %s' % token

        return self._http
Example #12
0
    def http(self):
        if self._http is None:
            if self.app.green_pool:
                from pulsar.apps.greenio import wait
                self._wait = wait

            api_url = self.app.config['API_URL']
            # Remote API
            if is_absolute_uri(api_url):
                if self._wait:
                    self._http = HttpClient()
                else:
                    self._http = HttpClient(loop=new_event_loop())
            # Local API
            else:
                self._http = LocalClient(self.app)
            token = self.app.config['API_AUTHENTICATION_TOKEN']
            if token:
                self._http.headers['Athentication'] = 'Bearer %s' % token

        return self._http
Example #13
0
    def middleware(self, app):
        '''Add two middleware handlers if configured to do so.'''
        middleware = []
        if app.config['CLEAN_URL']:
            middleware.append(wsgi.clean_path_middleware)
        path = app.config['MEDIA_URL']

        if is_absolute_uri(path):
            app.config['SERVE_STATIC_FILES'] = False

        if os.path.isdir(app.config['SERVE_STATIC_FILES'] or ''):
            if path.endswith('/'):
                path = path[:-1]
            location = app.config['SERVE_STATIC_FILES']
            middleware.append(wsgi.MediaRouter(path, location,
                                               show_indexes=app.debug))

        if app.config['REDIRECTS']:
            for url, to in app.config['REDIRECTS'].items():
                middleware.append(RedirectRouter(url, to))

        return middleware
Example #14
0
    def routes(self, app):
        '''Add two middleware handlers if configured to do so.'''
        middleware = []
        if app.config['CLEAN_URL']:
            app.event('on_request', self.clean_path_middleware)
        path = app.config['MEDIA_URL']

        if is_absolute_uri(path):
            app.config['SERVE_STATIC_FILES'] = False

        if os.path.isdir(app.config['SERVE_STATIC_FILES'] or ''):
            if path.endswith('/'):
                path = path[:-1]
            location = app.config['SERVE_STATIC_FILES']
            middleware.append(wsgi.MediaRouter(path, location,
                                               show_indexes=app.debug))

        if app.config['REDIRECTS']:
            for url, to in app.config['REDIRECTS'].items():
                middleware.append(RedirectRouter(url, to))

        return middleware
Example #15
0
    def on_config(self, app):
        self.backends = []

        url = app.config['API_URL']
        if url is not None and not is_absolute_uri(url):
            app.config['API_URL'] = str(RestRoot(url))

        module = import_module(app.meta.module_name)

        for dotted_path in app.config['AUTHENTICATION_BACKENDS']:
            backend = module_attribute(dotted_path)
            if not backend:
                self.logger.error('Could not load backend "%s"', dotted_path)
                continue
            backend = backend()
            backend.setup(app.config, module, app.params)
            self.backends.append(backend)
            app.bind_events(backend, exclude=('on_config',))

        for backend in self.backends:
            if hasattr(backend, 'on_config'):
                backend.on_config(app)

        app.auth_backend = self
Example #16
0
def absolute_uri(request, url):
    if url and not is_absolute_uri(url):
        return request.absolute_uri(url)
    return url
Example #17
0
 def on_html_document(self, app, request, doc):
     if is_absolute_uri(app.config['API_URL']):
         add_ng_modules(doc, ('lux.restapi', 'lux.users'))
     else:
         add_ng_modules(doc, ('lux.webapi', 'lux.users'))
Example #18
0
 def on_html_document(self, app, request, doc):
     if is_absolute_uri(app.config['API_URL']):
         add_ng_modules(doc, ('lux.restapi', 'lux.users'))
     else:
         add_ng_modules(doc, ('lux.webapi', 'lux.users'))
Example #19
0
def absolute_uri(request, url):
    if url and not is_absolute_uri(url):
        return request.absolute_uri(url)
    return url