From be0be5a38fc8da496733389123335fc12809d061 Mon Sep 17 00:00:00 2001 From: Gesche Gierse Date: Mon, 19 Apr 2021 00:22:06 +0200 Subject: [PATCH] create all stories + story parts on round create --- stories/writingtogether/views.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/stories/writingtogether/views.py b/stories/writingtogether/views.py index 4207023..9f9760e 100644 --- a/stories/writingtogether/views.py +++ b/stories/writingtogether/views.py @@ -27,23 +27,27 @@ class DetailView(generic.DetailView): class StoryRoundCreate(CreateView): model = StoryRound fields = ['name', 'participants', 'number_of_rounds'] + success_url = reverse_lazy('writing:index') def form_valid(self, form): self.object = form.save() + sorted_participants = sorted(form.cleaned_data['participants']) + number_of_participants = len(sorted_participants) - for user in form.cleaned_data['participants']: - participant = Participant.objects.create( - user=user, - order_by=1, # TODO: get order by from form - story_round=self.object - ) - - Story.objects.create( + for user in sorted_participants: + story = Story.objects.create( part_of_round=self.object, - started_by=participant + started_by=user ) - return HttpResponseRedirect(self.get_success_url()) + previous_part=None + for i in range(form.cleaned_data['number_of_rounds']): + current_part = StoryPart.objects.create( + user=sorted_participants[i % number_of_participants], + previous_part=previous_part, + part_of=story + ) + previous_part = current_part class StoryUpdate(UpdateView):