From ad651300d9779716ffd85c7db934da16b5de4a2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20J=C3=A4ckel?= Date: Wed, 30 Nov 2016 11:51:18 +0100 Subject: [PATCH] Added settings value MOTION_IDENTIFIER_MIN_DIGITS. Solved #2696. --- openslides/motions/models.py | 18 ++++++++++++++++-- openslides/motions/views.py | 2 +- openslides/utils/settings.py.tpl | 5 +++++ tests/old/settings.py | 5 +++++ tests/settings.py | 5 +++++ 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/openslides/motions/models.py b/openslides/motions/models.py index ea90dfa55..46bf39b51 100644 --- a/openslides/motions/models.py +++ b/openslides/motions/models.py @@ -1,5 +1,6 @@ from django.conf import settings from django.contrib.contenttypes.fields import GenericRelation +from django.core.exceptions import ImproperlyConfigured from django.db import models from django.db.models import Max from django.utils import formats @@ -330,14 +331,27 @@ class Motion(RESTModelMixin, models.Model): prefix = '%s ' % self.category.prefix number += 1 - identifier = '%s%d' % (prefix, number) + identifier = '%s%s' % (prefix, self.extend_identifier_number(number)) while Motion.objects.filter(identifier=identifier).exists(): number += 1 - identifier = '%s%d' % (prefix, number) + identifier = '%s%s' % (prefix, self.extend_identifier_number(number)) self.identifier = identifier self.identifier_number = number + def extend_identifier_number(self, number): + """ + Returns the number used in the set_identifier method with leading + zero charaters according to the settings value + MOTION_IDENTIFIER_MIN_DIGITS. + """ + result = str(number) + if hasattr(settings, 'MOTION_IDENTIFIER_MIN_DIGITS') and settings.MOTION_IDENTIFIER_MIN_DIGITS: + if not isinstance(settings.MOTION_IDENTIFIER_MIN_DIGITS, int): + raise ImproperlyConfigured('Settings value MOTION_IDENTIFIER_MIN_DIGITS must be an integer.') + result = '0' * (settings.MOTION_IDENTIFIER_MIN_DIGITS - len(str(number))) + result + return result + def get_title(self): """ Get the title of the motion. diff --git a/openslides/motions/views.py b/openslides/motions/views.py index 04e30c93f..1e486953b 100644 --- a/openslides/motions/views.py +++ b/openslides/motions/views.py @@ -431,7 +431,7 @@ class CategoryViewSet(ModelViewSet): parent_identifier = motion.parent.identifier or '' prefix = '%s %s ' % (parent_identifier, config['motions_amendments_prefix']) number += 1 - identifier = '%s%d' % (prefix, number) + identifier = '%s%s' % (prefix, motion.extend_identifier_number(number)) motion.identifier = identifier motion.identifier_number = number motion.save() diff --git a/openslides/utils/settings.py.tpl b/openslides/utils/settings.py.tpl index 7ca33e101..a4a97749e 100644 --- a/openslides/utils/settings.py.tpl +++ b/openslides/utils/settings.py.tpl @@ -115,3 +115,8 @@ MEDIA_ROOT = os.path.join(OPENSLIDES_USER_DATA_PATH, 'media', '') # https://whoosh.readthedocs.io/en/latest/ SEARCH_INDEX = os.path.join(OPENSLIDES_USER_DATA_PATH, 'search_index') + + +# Customization of OpenSlides apps + +MOTION_IDENTIFIER_MIN_DIGITS = 1 diff --git a/tests/old/settings.py b/tests/old/settings.py index a75422d77..93b3426f5 100644 --- a/tests/old/settings.py +++ b/tests/old/settings.py @@ -66,6 +66,11 @@ MEDIA_ROOT = os.path.join(OPENSLIDES_USER_DATA_PATH, '') SEARCH_INDEX = 'ram' +# Customization of OpenSlides apps + +MOTION_IDENTIFIER_MIN_DIGITS = 1 + + # Special settings only for testing TEST_RUNNER = 'openslides.utils.test.OpenSlidesDiscoverRunner' diff --git a/tests/settings.py b/tests/settings.py index e3483c5cc..9aabb13af 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -66,6 +66,11 @@ MEDIA_ROOT = os.path.join(OPENSLIDES_USER_DATA_PATH, '') SEARCH_INDEX = 'ram' +# Customization of OpenSlides apps + +MOTION_IDENTIFIER_MIN_DIGITS = 1 + + # Special settings only for testing TEST_RUNNER = 'openslides.utils.test.OpenSlidesDiscoverRunner'