Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy hearing #363

Merged
merged 2 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion democracy/tests/test_hearing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions democracy/views/hearing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 22 additions & 8 deletions democracy/views/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down