Skip to content

Commit

Permalink
Allow creating a hearing using another hearing's files and polls as a…
Browse files Browse the repository at this point in the history
… template
  • Loading branch information
ranta committed Dec 13, 2021
1 parent 191f713 commit edbd54e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
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
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

0 comments on commit edbd54e

Please sign in to comment.