From c6d5ae604a8fd0ea3b0b9a755844a7aac03e5f57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Tue, 24 Jul 2018 10:25:04 +0200 Subject: [PATCH] misc: don't redirect to /cat/form/ if a formdef exists with the slug (#25450) --- auquotidien/modules/root.py | 13 +++++++------ tests/test_user_pages.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/auquotidien/modules/root.py b/auquotidien/modules/root.py index 0e0282d..eb37336 100644 --- a/auquotidien/modules/root.py +++ b/auquotidien/modules/root.py @@ -852,19 +852,20 @@ class AlternateRootDirectory(OldRootDirectory): try: category = Category.get_by_urlname(component) except KeyError: - pass - else: - return FormsRootDirectory(category) + category = None # is this a formdef ? try: formdef = FormDef.get_by_urlname(component) except KeyError: - pass + if category: + return FormsRootDirectory(category) else: - # if there's no category, or the request is a POST, directly call + # if the form has no category, or the request is a POST, or the + # slug matches both a category and a formdef, directly call # into FormsRootDirectory. - if formdef.category_id is None or get_request().get_method() == 'POST': + if formdef.category_id is None or get_request().get_method() == 'POST' or ( + formdef and category): get_response().filter['bigdiv'] = 'rub_service' return FormsRootDirectory()._q_lookup(component) diff --git a/tests/test_user_pages.py b/tests/test_user_pages.py index fb47ead..ebad7e9 100644 --- a/tests/test_user_pages.py +++ b/tests/test_user_pages.py @@ -180,3 +180,35 @@ def test_agenda(): resp = app.get('/agenda/filter') assert 'tags$element0' in resp.form.fields assert 'calendars$element0' in resp.form.fields + +def test_form_category_redirection(): + Category.wipe() + cat = Category(name='baz') + cat.store() + + FormDef.wipe() + formdef = FormDef() + formdef.name = 'foobar' + formdef.category_id = cat.id + formdef.fields = [] + formdef.store() + + # check we get a redirection to /category/formdef/ + resp = get_app(pub).get('/foobar/') + assert resp.location.endswith('/baz/foobar/') + +def test_form_and_category_same_slug(): + Category.wipe() + cat = Category(name='foobar') + cat.store() + + FormDef.wipe() + formdef = FormDef() + formdef.name = 'foobar' + formdef.category_id = cat.id + formdef.fields = [] + formdef.store() + + # check we get to the form, not the category + resp = get_app(pub).get('/foobar/') + assert resp.form -- 2.18.0