Beispiel #1
0
def _register_error_handler(app: CKANApp):
    u'''Register error handler'''
    def error_handler(e: Exception) -> tuple[str, Optional[int]]:
        debug = config.get_value('debug')
        if isinstance(e, HTTPException):
            if debug:
                log.debug(e, exc_info=sys.exc_info)  # type: ignore
            else:
                log.info(e)
            extra_vars = {
                u'code': e.code,
                u'content': e.description,
                u'name': e.name
            }

            return base.render(u'error_document_template.html',
                               extra_vars), e.code

        log.error(e, exc_info=sys.exc_info)  # type: ignore
        extra_vars = {u'code': [500], u'content': u'Internal server error'}
        return base.render(u'error_document_template.html', extra_vars), 500

    for code in default_exceptions:
        app.register_error_handler(code, error_handler)
    if not app.debug and not app.testing:
        app.register_error_handler(Exception, error_handler)
        if config.get_value('email_to'):
            _setup_error_mail_handler(app)
Beispiel #2
0
def _setup_webassets(app: CKANApp):
    app.use_x_sendfile = config.get_value('ckan.webassets.use_x_sendfile')

    webassets_folder = get_webassets_path()

    def webassets(path: str):
        return send_from_directory(webassets_folder, path)

    app.add_url_rule('/webassets/<path:path>', 'webassets.index', webassets)
Beispiel #3
0
def _register_error_handler(app: CKANApp):
    u'''Register error handler'''
    def error_handler(
            e: Exception
    ) -> Union[tuple[str, Optional[int]], Optional[Response]]:
        debug = config.get_value('debug')
        if isinstance(e, HTTPException):
            # If the current_user.is_anonymous and the
            # Exception code is 401(Unauthorized)/403(Forbidden)
            # redirect the users to login page before trying to access it again
            # If you want to raise a 401 or 403 error instead,
            # set this setting to `False`
            if config.get_value('ckan.redirect_to_login_if_not_authorized'):
                # if the url is not local we dont want to redirect the user
                # instead, we want to show the actual 403(Forbidden)...
                # same for user.perform_reset if the current_user.is_deleted()
                # the view returns 403 hence we want to show the exception.
                endpoints = tuple(
                    ['util.internal_redirect', 'user.perform_reset'])
                if request.endpoint not in endpoints:
                    if current_user.is_anonymous and type(e) in (Unauthorized,
                                                                 Forbidden):
                        login_url = h.url_for('user.login', qualified=True)
                        next_url = request.url
                        redirect_url = h.make_login_url(login_url,
                                                        next_url=next_url)
                        h.flash_error(_('Please log in to access this page.'))
                        return h.redirect_to(redirect_url)

            if debug:
                log.debug(e, exc_info=sys.exc_info)  # type: ignore
            else:
                log.info(e)
            extra_vars = {
                u'code': e.code,
                u'content': e.description,
                u'name': e.name
            }

            return base.render(u'error_document_template.html',
                               extra_vars), e.code

        log.error(e, exc_info=sys.exc_info)  # type: ignore
        extra_vars = {u'code': [500], u'content': u'Internal server error'}
        return base.render(u'error_document_template.html', extra_vars), 500

    for code in default_exceptions:
        app.register_error_handler(code, error_handler)
    if not app.debug and not app.testing:
        app.register_error_handler(Exception, error_handler)
        if config.get_value('email_to'):
            _setup_error_mail_handler(app)
Beispiel #4
0
def _register_core_blueprints(app: CKANApp):
    u'''Register all blueprints defined in the `views` folder
    '''
    def is_blueprint(mm: Any):
        return isinstance(mm, Blueprint)

    path = os.path.join(os.path.dirname(__file__), '..', '..', 'views')

    for loader, name, _ in pkgutil.iter_modules([path], 'ckan.views.'):
        # type_ignore_reason: incorrect external type declarations
        module = loader.find_module(name).load_module(name)  # type: ignore
        for blueprint in inspect.getmembers(module, is_blueprint):
            app.register_blueprint(blueprint[1])
            log.debug(u'Registered core blueprint: {0!r}'.format(blueprint[0]))