示例#1
0
文件: app.py 项目: degville/docs
def find_file_or_redirect(path=None):
    """
    If a file doesn't exist at the requested path, see if it exists at one
    of the other paths, and redirect there if necessary
    """

    template_finder = routing.TemplateFinder(app.template_folder)
    file_path = routing.get_file(''.join(['/', path]))
    preferred_languages = routing.requested_languages(flask.request)
    if 'en' not in preferred_languages:
        preferred_languages.append('en')
    languages = template_finder.get_languages(preferred_languages)
    versions = routing.get_versions(
        app.config.get('VERSION_FILEPATH', 'versions')
    )

    if os.path.isfile(app.template_folder + file_path):
        return flask.render_template(file_path)
    else:
        new_path = template_finder.find_alternate_path(
            flask.request.path,
            languages,
            versions
        )

        if new_path:
            return flask.redirect(new_path)
示例#2
0
def find_file_or_redirect():
    """
    If a file doesn't exist at the requested path, see if it exists at one
    of the other paths, and redirect there if necessary
    """

    # Check the request is within this application
    if not flask.request.path.startswith(application_root):
        return

    # Get a contextual request_path by stripping application_root
    local_request_path = flask.request.path[len(application_root):]

    template_finder = routing.TemplateFinder(app.template_folder)
    file_path = routing.get_file(local_request_path)
    preferred_languages = routing.requested_languages(flask.request)
    if 'en' not in preferred_languages:
        preferred_languages.append('en')
    languages = template_finder.get_languages(preferred_languages)
    versions = routing.get_versions(
        app.config.get('VERSION_FILEPATH', 'versions'))

    if os.path.isfile(app.template_folder + file_path):
        return flask.render_template(file_path)
    else:
        new_path = template_finder.find_alternate_path(local_request_path,
                                                       languages, versions)

        if new_path:
            return flask.redirect(application_root + new_path)
示例#3
0
def find_file_or_redirect():
    """
    If a file doesn't exist at the requested path, see if it exists at one
    of the other paths, and redirect there if necessary
    """

    request_path = flask.request.path
    template_finder = routing.TemplateFinder(app.template_folder)
    file_path = routing.get_file(request_path)
    preferred_languages = routing.requested_languages(flask.request)
    if 'en' not in preferred_languages:
        preferred_languages.append('en')
    languages = template_finder.get_languages(preferred_languages)
    versions = routing.get_versions()

    if os.path.isfile(app.template_folder + file_path):
        return flask.render_template(file_path)
    else:
        new_path = template_finder.find_alternate_path(request_path, languages,
                                                       versions)

        if new_path:
            return flask.redirect(new_path)
示例#4
0
文件: app.py 项目: maaudet-ca/docs
def find_file_or_redirect(path=None):
    """
    If a file doesn't exist at the requested path, see if it exists at one
    of the other paths, and redirect there if necessary
    """

    template_finder = routing.TemplateFinder(app.template_folder)
    file_path = routing.get_file(''.join(['/', path]))
    preferred_languages = routing.requested_languages(flask.request)
    if 'en' not in preferred_languages:
        preferred_languages.append('en')
    languages = template_finder.get_languages(preferred_languages)
    versions = routing.get_versions(
        app.config.get('VERSION_FILEPATH', 'versions'))

    if os.path.isfile(app.template_folder + file_path):
        return flask.render_template(file_path)
    else:
        new_path = template_finder.find_alternate_path(flask.request.path,
                                                       languages, versions)

        if new_path:
            return flask.redirect(new_path)