Added viewpoint to number all motions in a category.

This commit is contained in:
Norman Jäckel 2016-07-13 01:39:28 +02:00
parent b96d7d0514
commit 7e6dc50b39
3 changed files with 79 additions and 1 deletions

View File

@ -17,6 +17,7 @@ Core:
Motions:
- Added origin field.
- Added button to number all motions in a category.
Users:
- Added field is_committee and new default group Committees.

View File

@ -294,13 +294,46 @@ class CategoryViewSet(ModelViewSet):
result = self.get_access_permissions().can_retrieve(self.request.user)
elif self.action in ('metadata', 'list'):
result = self.request.user.has_perm('motions.can_see')
elif self.action in ('create', 'partial_update', 'update', 'destroy'):
elif self.action in ('create', 'partial_update', 'update', 'destroy', 'numbering'):
result = (self.request.user.has_perm('motions.can_see') and
self.request.user.has_perm('motions.can_manage'))
else:
result = False
return result
@detail_route(methods=['post'])
def numbering(self, request, pk=None):
"""
Special view endpoint to number all motions in this category.
Only managers can use this view.
"""
category = self.get_object()
number = 0
if not category.prefix:
prefix = ''
else:
prefix = '%s ' % category.prefix
with transaction.atomic():
for motion in category.motion_set.all():
motion.identifier = None
motion.save()
for motion in category.motion_set.all():
if motion.is_amendment():
parent_identifier = motion.parent.identifier or ''
prefix = '%s %s ' % (parent_identifier, config['motions_amendments_prefix'])
number += 1
identifier = '%s%d' % (prefix, number)
motion.identifier = identifier
motion.identifier_number = number
motion.save()
message = _('All motions in category {category} numbered '
'successfully.').format(category=category)
return Response({'detail': message})
class WorkflowViewSet(ModelViewSet):
"""

View File

@ -443,3 +443,47 @@ class UpdateMotionPoll(TestCase):
{'motion_id': self.motion.pk,
'votesvalid': ''})
self.assertEqual(response.status_code, status.HTTP_200_OK)
class NumberMotionsInCategory(TestCase):
"""
Tests numbering motions in a category.
"""
def setUp(self):
self.client = APIClient()
self.client.login(username='admin', password='admin')
self.category = Category.objects.create(
name='test_cateogory_name_zah6Ahd4Ifofaeree6ai',
prefix='test_prefix_ahz6tho2mooH8')
self.motion = Motion(
title='test_title_Eeha8Haf6peulu8ooc0z',
text='test_text_faghaZoov9ooV4Acaquk',
category=self.category)
self.motion.save()
self.motion.identifier = ''
self.motion.save()
self.motion_2 = Motion(
title='test_title_kuheih2eja2Saeshusha',
text='test_text_Ha5ShaeraeSuthooP2Bu',
category=self.category)
self.motion_2.save()
self.motion_2.identifier = ''
self.motion_2.save()
def test_numbering(self):
response = self.client.post(
reverse('category-numbering', args=[self.category.pk]))
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, 'test_prefix_ahz6tho2mooH8 1')
self.assertEqual(Motion.objects.get(pk=self.motion_2.pk).identifier, 'test_prefix_ahz6tho2mooH8 2')
def test_numbering_existing_identifier(self):
self.motion_2.identifier = 'test_prefix_ahz6tho2mooH8 1'
self.motion_2.save()
response = self.client.post(
reverse('category-numbering', args=[self.category.pk]))
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, 'test_prefix_ahz6tho2mooH8 1')
self.assertEqual(Motion.objects.get(pk=self.motion_2.pk).identifier, 'test_prefix_ahz6tho2mooH8 2')