2021-03-31 01:15:49 +02:00
|
|
|
from django import forms
|
2021-05-04 23:13:35 +02:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2021-03-31 01:15:49 +02:00
|
|
|
from django.http import HttpResponseRedirect
|
2021-04-19 00:21:40 +02:00
|
|
|
from django.shortcuts import render, get_object_or_404
|
2021-03-30 21:25:52 +02:00
|
|
|
|
|
|
|
# Create your views here.
|
2021-04-19 00:21:40 +02:00
|
|
|
from django.urls import reverse_lazy, reverse
|
2021-03-31 01:15:49 +02:00
|
|
|
from django.views import generic
|
2021-04-19 00:21:40 +02:00
|
|
|
from django.views.generic import CreateView, UpdateView, RedirectView
|
2021-03-31 01:15:49 +02:00
|
|
|
|
2021-04-20 00:01:36 +02:00
|
|
|
from writingtogether.models import Story, StoryPart, StoryRound
|
2021-03-31 01:15:49 +02:00
|
|
|
|
|
|
|
|
2021-05-04 23:13:35 +02:00
|
|
|
class IndexView(LoginRequiredMixin, generic.ListView):
|
2021-03-31 01:15:49 +02:00
|
|
|
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]
|
|
|
|
|
|
|
|
|
2021-05-04 23:13:35 +02:00
|
|
|
class DetailView(LoginRequiredMixin, generic.DetailView):
|
2021-03-31 01:15:49 +02:00
|
|
|
model = StoryRound
|
|
|
|
template_name = 'writingtogether/detail.html'
|
|
|
|
|
|
|
|
|
2021-05-04 23:13:35 +02:00
|
|
|
class StoryRoundCreate(LoginRequiredMixin, CreateView):
|
2021-03-31 01:15:49 +02:00
|
|
|
model = StoryRound
|
|
|
|
fields = ['name', 'participants', 'number_of_rounds']
|
2021-04-19 00:22:06 +02:00
|
|
|
success_url = reverse_lazy('writing:index')
|
2021-03-31 01:15:49 +02:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
self.object = form.save()
|
2021-04-20 22:52:03 +02:00
|
|
|
sorted_participants = sorted([user.pk for user in form.cleaned_data['participants']])
|
2021-04-19 00:22:06 +02:00
|
|
|
number_of_participants = len(sorted_participants)
|
2021-03-31 01:15:49 +02:00
|
|
|
|
2021-04-21 00:23:20 +02:00
|
|
|
for user_index, user_id in enumerate(sorted_participants):
|
2021-04-19 00:22:06 +02:00
|
|
|
story = Story.objects.create(
|
2021-03-31 01:15:49 +02:00
|
|
|
part_of_round=self.object,
|
2021-04-20 22:52:03 +02:00
|
|
|
started_by_id=user_id
|
2021-03-31 01:15:49 +02:00
|
|
|
)
|
|
|
|
|
2021-04-19 00:22:06 +02:00
|
|
|
previous_part=None
|
|
|
|
for i in range(form.cleaned_data['number_of_rounds']):
|
|
|
|
current_part = StoryPart.objects.create(
|
2021-04-21 00:23:20 +02:00
|
|
|
user_id=sorted_participants[(user_index + i) % number_of_participants],
|
2021-04-19 00:22:06 +02:00
|
|
|
previous_part=previous_part,
|
2021-04-20 22:52:03 +02:00
|
|
|
part_of=story,
|
2021-04-21 00:23:20 +02:00
|
|
|
turn_number=i,
|
2021-04-19 00:22:06 +02:00
|
|
|
)
|
|
|
|
previous_part = current_part
|
2021-03-31 01:15:49 +02:00
|
|
|
|
2021-04-20 22:52:03 +02:00
|
|
|
return HttpResponseRedirect(self.get_success_url())
|
|
|
|
|
2021-03-31 01:15:49 +02:00
|
|
|
|
2021-05-04 23:13:35 +02:00
|
|
|
class StoryUpdate(LoginRequiredMixin, UpdateView):
|
2021-03-31 01:15:49 +02:00
|
|
|
model = Story
|
|
|
|
fields = ['name']
|
|
|
|
|
|
|
|
|
2021-05-04 23:13:35 +02:00
|
|
|
class StoryPartUpdate(LoginRequiredMixin, UpdateView):
|
2021-03-31 01:15:49 +02:00
|
|
|
model = StoryPart
|
|
|
|
fields = ['text']
|
2021-04-19 00:21:40 +02:00
|
|
|
template_name = 'writingtogether/story_part.html'
|
2021-05-04 23:41:06 +02:00
|
|
|
success_url = reverse_lazy('writing:index')
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(StoryPartUpdate, self).get_context_data(**kwargs)
|
|
|
|
context.update(self.kwargs)
|
|
|
|
return context
|
|
|
|
|
2021-03-31 01:15:49 +02:00
|
|
|
|
2021-04-19 00:21:40 +02:00
|
|
|
#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['story_pk']
|
|
|
|
# return super().form_valid(form)
|
|
|
|
|
|
|
|
|
2021-05-04 23:13:35 +02:00
|
|
|
class RedirectToNextOpenPart(LoginRequiredMixin, RedirectView):
|
2021-04-19 00:21:40 +02:00
|
|
|
permanent = False
|
|
|
|
query_string = True
|
|
|
|
pattern_name = 'writing:update_story_part'
|
|
|
|
|
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
|
|
|
story_round = get_object_or_404(StoryRound, pk=kwargs['story_round_pk'])
|
|
|
|
|
|
|
|
story_part = story_round.get_next_story_part(user=self.request.user)
|
|
|
|
|
|
|
|
kwargs['story_pk'] = story_part.part_of.pk
|
2021-05-04 23:41:06 +02:00
|
|
|
kwargs['pk'] = story_part.pk
|
2021-04-19 00:21:40 +02:00
|
|
|
return super().get_redirect_url(*args, **kwargs)
|