create all stories + story parts on round create

This commit is contained in:
Gesche 2021-04-19 00:22:06 +02:00
parent cd427ad974
commit be0be5a38f
1 changed files with 14 additions and 10 deletions

View File

@ -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):