diff --git a/src/main.py b/src/main.py index a2b46d9ef26..1ac92dccaef 100644 --- a/src/main.py +++ b/src/main.py @@ -43,13 +43,11 @@ def add_header(response): def render_template(template, *args, **kwargs): # If the year has already been set (e.g. for error pages) then use that # Otherwise the requested year, otherwise the default year - year = kwargs.get('year',request.view_args.get('year', DEFAULT_YEAR)) - - supported_languages = SUPPORTED_LANGUAGES.get(year, (DEFAULT_LANGUAGE,)) + year = kwargs.get('year', request.view_args.get('year', DEFAULT_YEAR)) + # If the lang has already been set (e.g. for error pages) then use that # Otherwise the requested lang, otherwise the default lang - lang = kwargs.get('lang',request.view_args.get('lang', DEFAULT_LANGUAGE.lang_code)) - + lang = kwargs.get('lang', request.view_args.get('lang', DEFAULT_LANGUAGE.lang_code)) language = get_language(lang) langcode_length = len(lang) + 1 # Probably always 2-character language codes but who knows! @@ -59,6 +57,7 @@ def render_template(template, *args, **kwargs): # Although a langauge may be enabled, all templates may not have been translated yet # So check if each language exists and only return languages for templates that do exist + supported_languages = SUPPORTED_LANGUAGES.get(year, (DEFAULT_LANGUAGE,)) template_supported_languages = [] for l in supported_languages: langTemplate = 'templates/%s/%s' % (l.lang_code, template[langcode_length:]) @@ -155,14 +154,19 @@ def methodology(lang, year): return render_template('%s/%s/methodology.html' % (lang, year)) -@app.route('//accessibility-statement') +# Accessibility Statement needs special case handling for trailing slashes +# as, unlike Flask, we generally prefer no trailing slashes +# For chapters we handle this in the validate function +@app.route('//accessibility-statement', strict_slashes=False) @validate def accessibility_statement(lang): - return render_template('%s/2019/accessibility_statement.html' % (lang)) + if request.base_url[-1] == "/": + return redirect("/%s/accessibility-statement" % (lang)), 301 + else: + return render_template('%s/2019/accessibility_statement.html' % (lang)) @app.route('/sitemap.xml') -@validate def sitemap(): xml = render_template('sitemap.xml') resp = app.make_response(xml) diff --git a/src/validate.py b/src/validate.py index b5967c2a2ec..9aa102a5353 100644 --- a/src/validate.py +++ b/src/validate.py @@ -49,10 +49,13 @@ def decorated_function(*args, **kwargs): def validate_chapter(chapter,year): - chapters_for_year = SUPPORTED_CHAPTERS.get(year) if chapter not in chapters_for_year: - if chapter in TYPO_CHAPTERS: + if (chapter[-1] == "/"): + # Automatically remove any trailing slashes + return chapter[:-1] + elif chapter in TYPO_CHAPTERS: + # Automatically redirect for configured typos logging.debug('Typo chapter requested: %s, redirecting to %s' % (chapter, TYPO_CHAPTERS.get(chapter))) return TYPO_CHAPTERS.get(chapter) else: @@ -63,7 +66,6 @@ def validate_chapter(chapter,year): def validate_lang_and_year(lang, year): - if year is None: logging.debug('Defaulting the year to: %s' % year) year = DEFAULT_YEAR