Merge pull request #2703 from normanjaeckel/LeadingZero

Added settings value MOTION_IDENTIFIER_MIN_DIGITS. Solved #2696.
This commit is contained in:
Norman Jäckel 2016-12-01 14:46:15 +01:00 committed by GitHub
commit cdc37b4dd5
5 changed files with 32 additions and 3 deletions

View File

@ -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.

View File

@ -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()

View File

@ -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

View File

@ -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'

View File

@ -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'