From 553903227bd58bd7d66754db5e7e168626f660ec Mon Sep 17 00:00:00 2001 From: Eemeli Ranta Date: Thu, 9 Dec 2021 21:09:25 +0200 Subject: [PATCH 1/2] Allow creating a hearing using another hearing's files and polls as a template --- democracy/views/hearing.py | 1 + democracy/views/section.py | 30 ++++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/democracy/views/hearing.py b/democracy/views/hearing.py index 09cbb4aa..e5342f19 100644 --- a/democracy/views/hearing.py +++ b/democracy/views/hearing.py @@ -166,6 +166,7 @@ def create(self, validated_data): project_data = validated_data.pop('project', None) validated_data['organization'] = self.context['request'].user.get_default_organization() validated_data['created_by_id'] = self.context['request'].user.id + validated_data['published'] = False # Force new hearings to be unpublished initially hearing = super().create(validated_data) self._create_or_update_sections(hearing, sections_data, force_create=True) self._create_or_update_project(hearing, project_data) diff --git a/democracy/views/section.py b/democracy/views/section.py index 7ab0fc39..f63bb11c 100644 --- a/democracy/views/section.py +++ b/democracy/views/section.py @@ -151,7 +151,11 @@ def validate_options(self, data): serializer_params = {'data': option_data} if pk: try: - option = self.instance.options.get(pk=pk) + if self.instance.pk: + option = self.instance.options.get(pk=pk) + else: # Creating a copy of the hearing + option = SectionPollOption.objects.get(pk=pk) + option.pk = None except SectionPollOption.DoesNotExist: raise ValidationError('The Poll does not have an option with ID %s' % repr(pk)) serializer_params['instance'] = option @@ -263,10 +267,13 @@ def validate_images(self, data): if pk: try: - image = self.instance.images.get(pk=pk) + if self.instance is not None: + image = self.instance.images.get(pk=pk) + else: # Creating a copy of the hearing + image = SectionImage.objects.get(pk=pk) + image.pk = None except SectionImage.DoesNotExist: raise ValidationError('The Section does not have an image with ID %s' % pk) - serializer_params['instance'] = image serializer = SectionImageCreateUpdateSerializer(**serializer_params) @@ -290,10 +297,12 @@ def validate_files(self, data): if pk: try: - # only allow orphan files or files within this section already - file = SectionFile.objects.filter( - Q(section=None) | (Q(section=self.instance)) - ).get(pk=pk) + if self.instance is not None: + # only allow orphan files or files within this section already + file = SectionFile.objects.filter(Q(section=None) | (Q(section=self.instance))).get(pk=pk) + else: # Creating a copy of the hearing + file = SectionFile.objects.get(pk=pk) + file.pk = None except SectionImage.DoesNotExist: raise ValidationError('No file with ID %s available in this section' % pk) @@ -353,7 +362,12 @@ def validate_questions(self, data): serializer_params = {'data': poll_data} if pk: try: - poll = self.instance.polls.get(pk=pk) + if self.instance is not None: + poll = self.instance.polls.get(pk=pk) + else: # Creating a copy of the hearing + poll = SectionPoll.objects.get(pk=pk) + poll.pk = None + poll.n_answers = 0 except SectionPoll.DoesNotExist: raise ValidationError('The Section does not have a poll with ID %s' % repr(pk)) self._validate_question_update(poll_data, poll) From 43d79d179a85a4e6febcd25e135ae81e49cd4ef8 Mon Sep 17 00:00:00 2001 From: Eemeli Ranta Date: Mon, 13 Dec 2021 13:19:06 +0200 Subject: [PATCH 2/2] Fix tests for hearings for published field --- democracy/tests/test_hearing.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/democracy/tests/test_hearing.py b/democracy/tests/test_hearing.py index 5eda03d9..2f8fa9aa 100644 --- a/democracy/tests/test_hearing.py +++ b/democracy/tests/test_hearing.py @@ -36,7 +36,7 @@ def valid_hearing_json(contact_person, default_label): "sv": "Rooperi", }, "n_comments": 0, - "published": True, + "published": False, "labels": [{"id": default_label.id, "label": {default_lang_code: default_label.label}}], "open_at": "2016-09-29T11:39:12Z", "close_at": "2016-09-29T11:39:12Z", @@ -1260,6 +1260,7 @@ def test_PUT_hearing_other_organization_hearing(valid_hearing_json, john_smith_a data = get_data_from_response(response, status_code=201) hearing = Hearing.objects.first() hearing.organization = Organization.objects.create(name='The department for squirrel warfare') + hearing.published = True hearing.save() _update_hearing_data(data) response = john_smith_api_client.put('%s%s/' % (endpoint, data['id']), data=data, format='json') @@ -1344,6 +1345,7 @@ def test_PUT_hearing_no_organization(valid_hearing_json, john_smith_api_client): _update_hearing_data(data) hearing = Hearing.objects.filter(id=data['id']).first() hearing.organization = None + hearing.published = True hearing.save() response = john_smith_api_client.put('%s%s/' % (endpoint, data['id']), data=data, format='json') data = get_data_from_response(response, status_code=403)