Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically remove any trailing slashes #726

Merged
merged 9 commits into from
Apr 26, 2020
10 changes: 4 additions & 6 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand All @@ -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:])
Expand Down Expand Up @@ -162,7 +161,6 @@ def accessibility_statement(lang):


@app.route('/sitemap.xml')
@validate
def sitemap():
xml = render_template('sitemap.xml')
resp = app.make_response(xml)
Expand Down
13 changes: 10 additions & 3 deletions src/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def decorated_function(*args, **kwargs):
year = kwargs.get('year')
chapter = kwargs.get('chapter')

# Handle the pages that don't follow /lang/year/page structure
# which can end up here if they have trailing slashes in URL
if year == "accessibility-statement":
return redirect('/%s/accessibility-statement' % lang, code=301)

accepted_args = inspect.getargspec(func).args

lang, year = validate_lang_and_year(lang, year)
Expand All @@ -49,10 +54,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:
Expand All @@ -63,7 +71,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
Expand Down