2021-03-31 01:15:49 +02:00
|
|
|
from django import forms
|
|
|
|
from django.http import HttpResponseRedirect
|
2021-03-30 21:25:52 +02:00
|
|
|
from django.shortcuts import render
|
|
|
|
|
|
|
|
# Create your views here.
|
2021-03-31 01:15:49 +02:00
|
|
|
from django.views import generic
|
|
|
|
from django.views.generic import CreateView, UpdateView
|
|
|
|
|
|
|
|
from writingtogether.models import Story, StoryPart, StoryRound, Participant
|
|
|
|
|
|
|
|
|
|
|
|
class IndexView(generic.ListView):
|
|
|
|
template_name = 'writingtogether/index.html'
|
2021-04-19 00:19:32 +02:00
|
|
|
context_object_name = 'open_story_round_list'
|
2021-03-31 01:15:49 +02:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2021-04-19 00:19:32 +02:00
|
|
|
# TODO: show open & finished rounds
|
2021-03-31 01:15:49 +02:00
|
|
|
return StoryRound.objects.order_by('-created')[:5]
|
|
|
|
|
|
|
|
|
|
|
|
class DetailView(generic.DetailView):
|
|
|
|
model = StoryRound
|
|
|
|
template_name = 'writingtogether/detail.html'
|
|
|
|
|
|
|
|
|
|
|
|
class StoryRoundCreate(CreateView):
|
|
|
|
model = StoryRound
|
|
|
|
fields = ['name', 'participants', 'number_of_rounds']
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
self.object = form.save()
|
|
|
|
|
|
|
|
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(
|
|
|
|
part_of_round=self.object,
|
|
|
|
started_by=participant
|
|
|
|
)
|
|
|
|
|
|
|
|
return HttpResponseRedirect(self.get_success_url())
|
|
|
|
|
|
|
|
|
|
|
|
class StoryUpdate(UpdateView):
|
|
|
|
model = Story
|
|
|
|
fields = ['name']
|
|
|
|
|
|
|
|
|
|
|
|
class StoryPartCreate(CreateView):
|
|
|
|
model = StoryPart
|
|
|
|
fields = ['text']
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.created_by = self.request.user
|
|
|
|
form.instance.previous_part_id = self.kwargs['previous']
|
|
|
|
form.instance.part_of_id = self.kwargs['pk']
|
|
|
|
return super().form_valid(form)
|