From b0070d13b99cb7b8b69268a81f7b8f36b08a9a41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20J=C3=A4ckel?= Date: Mon, 15 Aug 2016 23:53:21 +0200 Subject: [PATCH] Added possibility to sort motions in category numbering view. --- CHANGELOG | 2 +- openslides/motions/views.py | 16 ++++++++++++++-- tests/integration/motions/test_viewset.py | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 549333a55..118e79ecf 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -21,7 +21,7 @@ Core: Motions: - Added origin field. -- Added button to number all motions in a category. +- Added button to sort and number all motions in a category. Users: - Added field is_committee and new default group Committees. diff --git a/openslides/motions/views.py b/openslides/motions/views.py index a895cb8a6..703558528 100644 --- a/openslides/motions/views.py +++ b/openslides/motions/views.py @@ -307,6 +307,11 @@ class CategoryViewSet(ModelViewSet): Special view endpoint to number all motions in this category. Only managers can use this view. + + Send POST {'motions': []} to sort the given + motions in a special order. Ids of motions which do not belong to + the category are just ignored. Send just POST {} to sort all + motions in the category by id. """ category = self.get_object() number = 0 @@ -314,13 +319,20 @@ class CategoryViewSet(ModelViewSet): prefix = '' else: prefix = '%s ' % category.prefix + motions = category.motion_set.all() + motion_list = request.data.get('motions') + if motion_list: + motion_dict = {} + for motion in motions.filter(id__in=motion_list): + motion_dict[motion.pk] = motion + motions = [motion_dict[pk] for pk in motion_list] with transaction.atomic(): - for motion in category.motion_set.all(): + for motion in motions: motion.identifier = None motion.save() - for motion in category.motion_set.all(): + for motion in motions: if motion.is_amendment(): parent_identifier = motion.parent.identifier or '' prefix = '%s %s ' % (parent_identifier, config['motions_amendments_prefix']) diff --git a/tests/integration/motions/test_viewset.py b/tests/integration/motions/test_viewset.py index 55f10a734..f18c5aaf2 100644 --- a/tests/integration/motions/test_viewset.py +++ b/tests/integration/motions/test_viewset.py @@ -487,3 +487,21 @@ class NumberMotionsInCategory(TestCase): self.assertEqual(response.data, {'detail': 'All motions in category test_cateogory_name_zah6Ahd4Ifofaeree6ai numbered successfully.'}) self.assertEqual(Motion.objects.get(pk=self.motion.pk).identifier, 'test_prefix_ahz6tho2mooH8 1') self.assertEqual(Motion.objects.get(pk=self.motion_2.pk).identifier, 'test_prefix_ahz6tho2mooH8 2') + + def test_numbering_with_given_order(self): + self.motion_3 = Motion( + title='test_title_eeb0kua5ciike4su2auJ', + text='test_text_ahshuGhaew3eim8yoht7', + category=self.category) + self.motion_3.save() + self.motion_3.identifier = '' + self.motion_3.save() + response = self.client.post( + reverse('category-numbering', args=[self.category.pk]), + {'motions': [3, 2]}, + format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data, {'detail': 'All motions in category test_cateogory_name_zah6Ahd4Ifofaeree6ai numbered successfully.'}) + self.assertEqual(Motion.objects.get(pk=self.motion.pk).identifier, None) + self.assertEqual(Motion.objects.get(pk=self.motion_2.pk).identifier, 'test_prefix_ahz6tho2mooH8 2') + self.assertEqual(Motion.objects.get(pk=self.motion_3.pk).identifier, 'test_prefix_ahz6tho2mooH8 1')