Expose the workflow id during motion serialization

This commit is contained in:
FinnStutzenstein 2018-09-06 13:39:16 +02:00
parent 4feb2a8e21
commit 9226874010
4 changed files with 8 additions and 8 deletions

View File

@ -50,6 +50,7 @@ class MotionManager(models.Manager):
return (self.get_queryset() return (self.get_queryset()
.select_related('active_version', 'state') .select_related('active_version', 'state')
.prefetch_related( .prefetch_related(
'state__workflow',
'versions', 'versions',
'agenda_items', 'agenda_items',
'log_messages', 'log_messages',
@ -629,11 +630,10 @@ class Motion(RESTModelMixin, models.Model):
raise WorkflowError('You can not create a poll in state %s.' % self.state.name) raise WorkflowError('You can not create a poll in state %s.' % self.state.name)
@property @property
def workflow(self): def workflow_id(self):
""" """
Returns the id of the workflow of the motion. Returns the id of the workflow of the motion.
""" """
# TODO: Rename to workflow_id
return self.state.workflow.pk return self.state.workflow.pk
def set_state(self, state): def set_state(self, state):

View File

@ -369,8 +369,7 @@ class MotionSerializer(ModelSerializer):
workflow_id = IntegerField( workflow_id = IntegerField(
min_value=1, min_value=1,
required=False, required=False,
validators=[validate_workflow_field], validators=[validate_workflow_field])
write_only=True)
agenda_type = IntegerField(write_only=True, required=False, min_value=1, max_value=3) agenda_type = IntegerField(write_only=True, required=False, min_value=1, max_value=3)
agenda_parent_id = IntegerField(write_only=True, required=False, min_value=1) agenda_parent_id = IntegerField(write_only=True, required=False, min_value=1)
submitters = SubmitterSerializer(many=True, read_only=True) submitters = SubmitterSerializer(many=True, read_only=True)
@ -475,7 +474,7 @@ class MotionSerializer(ModelSerializer):
# Workflow. # Workflow.
workflow_id = validated_data.get('workflow_id') workflow_id = validated_data.get('workflow_id')
if workflow_id is not None and workflow_id != motion.workflow: if workflow_id is not None and workflow_id != motion.workflow_id:
motion.reset_state(workflow_id) motion.reset_state(workflow_id)
# Decide if a new version is saved to the database. # Decide if a new version is saved to the database.

View File

@ -557,7 +557,7 @@ class MotionViewSet(ModelViewSet):
recommendation_state_id = int(recommendation_state) recommendation_state_id = int(recommendation_state)
except ValueError: except ValueError:
raise ValidationError({'detail': _('Invalid data. Recommendation must be an integer.')}) raise ValidationError({'detail': _('Invalid data. Recommendation must be an integer.')})
recommendable_states = State.objects.filter(workflow=motion.workflow, recommendation_label__isnull=False) recommendable_states = State.objects.filter(workflow=motion.workflow_id, recommendation_label__isnull=False)
if recommendation_state_id not in [item.id for item in recommendable_states]: if recommendation_state_id not in [item.id for item in recommendable_states]:
raise ValidationError( raise ValidationError(
{'detail': _('You can not set the recommendation to {recommendation_state_id}.').format( {'detail': _('You can not set the recommendation to {recommendation_state_id}.').format(

View File

@ -30,6 +30,7 @@ def test_motion_db_queries():
""" """
Tests that only the following db queries are done: Tests that only the following db queries are done:
* 1 requests to get the list of all motions, * 1 requests to get the list of all motions,
* 1 request to get the associated workflow
* 1 request to get the motion versions, * 1 request to get the motion versions,
* 1 request to get the agenda item, * 1 request to get the agenda item,
* 1 request to get the motion log, * 1 request to get the motion log,
@ -45,7 +46,7 @@ def test_motion_db_queries():
password='password') password='password')
# TODO: Create some polls etc. # TODO: Create some polls etc.
assert count_queries(Motion.get_elements) == 9 assert count_queries(Motion.get_elements) == 10
@pytest.mark.django_db(transaction=False) @pytest.mark.django_db(transaction=False)
@ -436,7 +437,7 @@ class UpdateMotion(TestCase):
motion = Motion.objects.get() motion = Motion.objects.get()
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(motion.title, 'test_title_aeng7ahChie3waiR8xoh') self.assertEqual(motion.title, 'test_title_aeng7ahChie3waiR8xoh')
self.assertEqual(motion.workflow, 2) self.assertEqual(motion.workflow_id, 2)
def test_patch_supporters(self): def test_patch_supporters(self):
supporter = get_user_model().objects.create_user( supporter = get_user_model().objects.create_user(