From 57446c1822e0d2a6d7e4be63870e9e914ffbc009 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 18 Nov 2022 18:06:27 +0100
Subject: [PATCH] admin: fix category ordering with missing id (#71390)

---
 tests/admin_pages/test_block.py           |  2 +-
 tests/admin_pages/test_carddefcategory.py |  2 +-
 tests/admin_pages/test_category.py        |  2 +-
 tests/admin_pages/test_datasource.py      |  3 ++-
 tests/test_mail_templates.py              |  2 +-
 wcs/admin/categories.py                   | 11 ++++++-----
 6 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/tests/admin_pages/test_block.py b/tests/admin_pages/test_block.py
index 73cf95ddc..b5e178d48 100644
--- a/tests/admin_pages/test_block.py
+++ b/tests/admin_pages/test_block.py
@@ -379,7 +379,7 @@ def test_blocks_category(pub):
     BlockCategory.sort_by_position(categories)
     assert [x.id for x in categories] == [str(category2.id), str(category.id)]
 
-    app.get('/backoffice/forms/blocks/categories/update_order?order=%s;%s;' % (category.id, category2.id))
+    app.get('/backoffice/forms/blocks/categories/update_order?order=%s;%s;0' % (category.id, category2.id))
     categories = BlockCategory.select()
     BlockCategory.sort_by_position(categories)
     assert [x.id for x in categories] == [str(category.id), str(category2.id)]
diff --git a/tests/admin_pages/test_carddefcategory.py b/tests/admin_pages/test_carddefcategory.py
index 3364a8a70..7d5d5e454 100644
--- a/tests/admin_pages/test_carddefcategory.py
+++ b/tests/admin_pages/test_carddefcategory.py
@@ -223,7 +223,7 @@ def test_categories_reorder(pub):
     CardDefCategory.sort_by_position(categories)
     assert [x.id for x in categories] == ['1', '2', '3']
 
-    app.get('/backoffice/cards/categories/update_order?order=3;1;2;')
+    app.get('/backoffice/cards/categories/update_order?order=3;1;2;0')
     categories = CardDefCategory.select()
     CardDefCategory.sort_by_position(categories)
     assert [x.id for x in categories] == ['3', '1', '2']
diff --git a/tests/admin_pages/test_category.py b/tests/admin_pages/test_category.py
index 836187518..c5b14934a 100644
--- a/tests/admin_pages/test_category.py
+++ b/tests/admin_pages/test_category.py
@@ -208,7 +208,7 @@ def test_categories_reorder(pub):
     Category.sort_by_position(categories)
     assert [x.id for x in categories] == ['1', '2', '3']
 
-    app.get('/backoffice/forms/categories/update_order?order=3;1;2;')
+    app.get('/backoffice/forms/categories/update_order?order=3;1;2;0')
     categories = Category.select()
     Category.sort_by_position(categories)
     assert [x.id for x in categories] == ['3', '1', '2']
diff --git a/tests/admin_pages/test_datasource.py b/tests/admin_pages/test_datasource.py
index f3af090af..9a3dee7d4 100644
--- a/tests/admin_pages/test_datasource.py
+++ b/tests/admin_pages/test_datasource.py
@@ -403,7 +403,8 @@ def test_data_sources_category(pub):
     assert [x.id for x in categories] == [str(category2.id), str(category.id)]
 
     app.get(
-        '/backoffice/settings/data-sources/categories/update_order?order=%s;%s;' % (category.id, category2.id)
+        '/backoffice/settings/data-sources/categories/update_order?order=%s;%s;0'
+        % (category.id, category2.id)
     )
     categories = DataSourceCategory.select()
     DataSourceCategory.sort_by_position(categories)
diff --git a/tests/test_mail_templates.py b/tests/test_mail_templates.py
index 4bce65f57..6ffe2766b 100644
--- a/tests/test_mail_templates.py
+++ b/tests/test_mail_templates.py
@@ -276,7 +276,7 @@ def test_mail_templates_category(pub, superuser):
     assert [x.id for x in categories] == [str(category2.id), str(category.id)]
 
     app.get(
-        '/backoffice/workflows/mail-templates/categories/update_order?order=%s;%s;'
+        '/backoffice/workflows/mail-templates/categories/update_order?order=%s;%s;0'
         % (category.id, category2.id)
     )
     categories = MailTemplateCategory.select()
diff --git a/wcs/admin/categories.py b/wcs/admin/categories.py
index ceafdbf91..148df6b58 100644
--- a/wcs/admin/categories.py
+++ b/wcs/admin/categories.py
@@ -380,12 +380,13 @@ class CategoriesDirectory(Directory):
         request = get_request()
         new_order = request.form['order'].strip(';').split(';')
         categories = self.category_class.select()
-        dict = {}
-        for l in categories:
-            dict[str(l.id)] = l
+        categories_by_id = {}
+        for cat in categories:
+            categories_by_id[str(cat.id)] = cat
+        new_order = [o for o in new_order if o in categories_by_id.keys()]
         for i, o in enumerate(new_order):
-            dict[o].position = i + 1
-            dict[o].store()
+            categories_by_id[o].position = i + 1
+            categories_by_id[o].store()
         return 'ok'
 
     def new(self):
-- 
2.35.1

