Fixed bug in motion set_identifier method. Fixed #2911.

This commit is contained in:
Norman Jäckel 2017-01-26 21:03:10 +01:00
parent 8897e22df0
commit 2872cd437c
2 changed files with 20 additions and 10 deletions

View File

@ -304,16 +304,16 @@ class Motion(RESTModelMixin, models.Model):
Sets the motion identifier automaticly according to the config value if Sets the motion identifier automaticly according to the config value if
it is not set yet. it is not set yet.
""" """
# The identifier is already set or should be set manually # The identifier is already set or should be set manually.
if config['motions_identifier'] == 'manually' or self.identifier: if config['motions_identifier'] == 'manually' or self.identifier:
# Do not set an identifier. # Do not set an identifier.
return return
# The motion is an amendment # The motion is an amendment.
elif self.is_amendment(): elif self.is_amendment():
motions = self.parent.amendments.all() motions = self.parent.amendments.all()
# The motions should be counted per category # The motions should be counted per category.
elif config['motions_identifier'] == 'per_category': elif config['motions_identifier'] == 'per_category':
motions = Motion.objects.filter(category=self.category) motions = Motion.objects.filter(category=self.category)
@ -321,25 +321,35 @@ class Motion(RESTModelMixin, models.Model):
else: else:
motions = Motion.objects.all() motions = Motion.objects.all()
# Get biggest number.
number = motions.aggregate(Max('identifier_number'))['identifier_number__max'] or 0 number = motions.aggregate(Max('identifier_number'))['identifier_number__max'] or 0
# If MOTION_IDENTIFIER_WITHOUT_BLANKS is set, don't use blanks when building identifier.
without_blank = hasattr(settings, 'MOTION_IDENTIFIER_WITHOUT_BLANKS') and settings.MOTION_IDENTIFIER_WITHOUT_BLANKS
# Build prefix.
if self.is_amendment(): if self.is_amendment():
parent_identifier = self.parent.identifier or '' parent_identifier = self.parent.identifier or ''
prefix = '%s %s ' % (parent_identifier, config['motions_amendments_prefix']) if without_blank:
prefix = '%s%s' % (parent_identifier, config['motions_amendments_prefix'])
else:
prefix = '%s %s ' % (parent_identifier, config['motions_amendments_prefix'])
elif self.category is None or not self.category.prefix: elif self.category is None or not self.category.prefix:
prefix = '' prefix = ''
else: else:
prefix = '%s ' % self.category.prefix if without_blank:
prefix = '%s' % self.category.prefix
else:
prefix = '%s ' % self.category.prefix
# Calculate new identifier.
number += 1 number += 1
identifier = '%s%s' % (prefix, self.extend_identifier_number(number)) identifier = '%s%s' % (prefix, self.extend_identifier_number(number))
while Motion.objects.filter(identifier=identifier).exists(): while Motion.objects.filter(identifier=identifier).exists():
number += 1 number += 1
identifier = '%s%s' % (prefix, self.extend_identifier_number(number)) identifier = '%s%s' % (prefix, self.extend_identifier_number(number))
# remove all whitespaces from identifier if MOTION_IDENTIFIER_WITHOUT_BLANKS is set # Set identifier and identifier_number.
if hasattr(settings, 'MOTION_IDENTIFIER_WITHOUT_BLANKS') and settings.MOTION_IDENTIFIER_WITHOUT_BLANKS:
identifier = identifier.replace(' ', '')
self.identifier = identifier self.identifier = identifier
self.identifier_number = number self.identifier_number = number

View File

@ -274,7 +274,7 @@ class MotionViewSet(ModelViewSet):
motion.reset_state() motion.reset_state()
# Save motion. # Save motion.
motion.save(update_fields=['state', 'identifier']) motion.save(update_fields=['state', 'identifier', 'identifier_number'])
message = _('The state of the motion was set to %s.') % motion.state.name message = _('The state of the motion was set to %s.') % motion.state.name
# Write the log message and initiate response. # Write the log message and initiate response.