From d221f98fe2f367ba6cfff070834efbc1013bc7d3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sun, 4 Dec 2022 12:17:20 +0100
Subject: [PATCH] misc: redirect categories to home by default (#71994)

---
 tests/form_pages/test_all.py |   5 +-
 wcs/admin/categories.py      |   2 +-
 wcs/forms/root.py            | 107 ++++++++++++++---------------------
 3 files changed, 46 insertions(+), 68 deletions(-)

diff --git a/tests/form_pages/test_all.py b/tests/form_pages/test_all.py
index 1cfbd03fb..2006f3f51 100644
--- a/tests/form_pages/test_all.py
+++ b/tests/form_pages/test_all.py
@@ -329,9 +329,8 @@ def test_category_page(pub):
     formdef = create_formdef()
     formdef.category_id = '1'
     formdef.store()
-    resp = get_app(pub).get('/foobar/')
-    assert '<h2>foobar</h2>' in resp.text
-    assert '<a class="" href="test/">test</a>' in resp.text
+    resp = get_app(pub).get('/foobar/', status=302)
+    assert resp.location == 'http://example.net/'
 
 
 def test_category_page_redirect(pub):
diff --git a/wcs/admin/categories.py b/wcs/admin/categories.py
index edff4bd72..876378f23 100644
--- a/wcs/admin/categories.py
+++ b/wcs/admin/categories.py
@@ -77,7 +77,7 @@ class CategoryUI:
                 'redirect_url',
                 size=32,
                 title=_('URL Redirection'),
-                hint=_('If set, redirect the site category page to the given URL.'),
+                hint=_('If set, redirect the site category page to the given URL instead of the site home.'),
                 value=self.category.redirect_url,
             )
 
diff --git a/wcs/forms/root.py b/wcs/forms/root.py
index 6f0f80900..afa964568 100644
--- a/wcs/forms/root.py
+++ b/wcs/forms/root.py
@@ -1871,7 +1871,7 @@ class RootDirectory(AccessControlled, Directory):
         if not self.category:
             redirect_url = get_cfg('misc', {}).get('homepage-redirect-url')
         else:
-            redirect_url = self.category.redirect_url
+            redirect_url = self.category.redirect_url or '/'
 
         if redirect_url:
             return redirect(
@@ -1887,27 +1887,20 @@ class RootDirectory(AccessControlled, Directory):
         request = get_request()
         user = request.user
 
-        if not self.category:
-            if user:
-                message = TextsDirectory.get_html_text('welcome-logged')
-            else:
-                message = TextsDirectory.get_html_text('welcome-unlogged')
-
-            if message:
-                r += htmltext('<div id="welcome-message">')
-                r += message
-                r += htmltext('</div>')
-
-        if self.category:
-            all_formdefs = FormDef.select(
-                lambda x: str(x.category_id) == str(self.category.id), order_by='name', ignore_errors=True
-            )
+        if user:
+            message = TextsDirectory.get_html_text('welcome-logged')
         else:
-            all_formdefs = FormDef.select(order_by='name', ignore_errors=True)
+            message = TextsDirectory.get_html_text('welcome-unlogged')
 
+        if message:
+            r += htmltext('<div id="welcome-message">')
+            r += message
+            r += htmltext('</div>')
+
+        all_formdefs = FormDef.select(order_by='name', ignore_errors=True)
         formdefs = [x for x in all_formdefs if (not x.is_disabled() or x.disabled_redirection)]
 
-        if not self.category and any(x for x in formdefs if x.enable_tracking_codes):
+        if any(x for x in formdefs if x.enable_tracking_codes):
             r += htmltext('<div id="side">')
             r += htmltext('<div id="tracking-code">')
             r += htmltext('<h3>%s</h3>') % _('Tracking code')
@@ -1932,59 +1925,48 @@ class RootDirectory(AccessControlled, Directory):
             epoch = time.localtime(1970)
             user_forms.sort(key=lambda x: x.receipt_time or epoch)
 
-        if self.category:
-            r += self.form_list(
-                list_forms,
-                category=self.category,
-                session=session,
-                user_forms=user_forms,
-                advertised_forms=advertised_forms,
-            )
-        else:
-            cats = Category.select()
-            Category.sort_by_position(cats)
-            one = False
-            for c in cats:
-                l2 = [x for x in list_forms if str(x.category_id) == str(c.id)]
-                l2_advertise = [x for x in advertised_forms if str(x.category_id) == str(c.id)]
-                if l2 or l2_advertise:
-                    r += self.form_list(
-                        l2, category=c, session=session, user_forms=user_forms, advertised_forms=l2_advertise
-                    )
-                    one = True
-
-            l2 = [x for x in list_forms if not x.category]
-            l2_advertise = [x for x in advertised_forms if not x.category]
+        cats = Category.select()
+        Category.sort_by_position(cats)
+        one = False
+        for c in cats:
+            l2 = [x for x in list_forms if str(x.category_id) == str(c.id)]
+            l2_advertise = [x for x in advertised_forms if str(x.category_id) == str(c.id)]
             if l2 or l2_advertise:
-                if one:
-                    title = _('Misc')
-                else:
-                    title = None
                 r += self.form_list(
-                    l2, title=title, session=session, user_forms=user_forms, advertised_forms=l2_advertise
+                    l2, category=c, session=session, user_forms=user_forms, advertised_forms=l2_advertise
                 )
+                one = True
+
+        l2 = [x for x in list_forms if not x.category]
+        l2_advertise = [x for x in advertised_forms if not x.category]
+        if l2 or l2_advertise:
+            if one:
+                title = _('Misc')
+            else:
+                title = None
+            r += self.form_list(
+                l2, title=title, session=session, user_forms=user_forms, advertised_forms=l2_advertise
+            )
 
         root_url = get_publisher().get_root_url()
         if user:
             r += self.user_forms(user_forms)
 
-            if not self.category:
-                r += htmltext('<p id="logout">')
-                if user.can_go_in_backoffice():
-                    r += htmltext('<a href="%sbackoffice/">%s</a> - ') % (root_url, _('Back Office'))
-                if user.anonymous:
-                    if get_cfg('saml_identities', {}).get('creation', 'admin') != 'admin':
-                        r += htmltext('<a href="%sregister">%s</a> - ') % (root_url, _('Register'))
+            r += htmltext('<p id="logout">')
+            if user.can_go_in_backoffice():
+                r += htmltext('<a href="%sbackoffice/">%s</a> - ') % (root_url, _('Back Office'))
+            if user.anonymous:
+                if get_cfg('saml_identities', {}).get('creation', 'admin') != 'admin':
+                    r += htmltext('<a href="%sregister">%s</a> - ') % (root_url, _('Register'))
 
-                r += htmltext('<a href="%slogout">%s</a></p>') % (root_url, _('Logout'))
+            r += htmltext('<a href="%slogout">%s</a></p>') % (root_url, _('Logout'))
 
         elif get_cfg('sp') or get_cfg('identification', {}).get('methods'):
-            if not self.category:
-                r += htmltext('<p id="login"><a href="%slogin">%s</a>') % (root_url, _('Login'))
-                identities_cfg = get_cfg('identities', {})
-                if identities_cfg.get('creation') in ('self', 'moderated'):
-                    r += htmltext(' - <a href="%sregister">%s</a>') % (root_url, _('Register'))
-                r += htmltext('</p>')
+            r += htmltext('<p id="login"><a href="%slogin">%s</a>') % (root_url, _('Login'))
+            identities_cfg = get_cfg('identities', {})
+            if identities_cfg.get('creation') in ('self', 'moderated'):
+                r += htmltext(' - <a href="%sregister">%s</a>') % (root_url, _('Register'))
+            r += htmltext('</p>')
         return r.getvalue()
 
     def user_forms(self, user_forms):
@@ -2056,10 +2038,7 @@ class RootDirectory(AccessControlled, Directory):
             r += htmltext('<h2>%s</h2>') % get_publisher().translate(category.name)
 
         formdefs_data = None
-        if self.category:
-            url_prefix = ''
-            r += self.category.get_description_html_text()
-        elif category:
+        if category:
             url_prefix = '%s/' % category.url_name
         else:
             url_prefix = ''
-- 
2.38.1

