modify url patterns to show steps in rounds; add views for that + html template

This commit is contained in:
Gesche 2021-04-19 00:21:40 +02:00
parent f6c958eed3
commit cd427ad974
3 changed files with 44 additions and 10 deletions

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="{% url 'writing:create_story_part' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
</body>
</html>

View File

@ -7,6 +7,7 @@ app_name = 'writing'
urlpatterns = [ urlpatterns = [
path('', views.IndexView.as_view(), name='index'), path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'), path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('create', views.StoryRoundCreate.as_view(), name='create_story_round'), path('create/', views.StoryRoundCreate.as_view(), name='create_story_round'),
path('<int:pk>/<int:previous>/', views.StoryPartCreate, name='create_story_part'), path('open/<int:story_round_pk>/<int:story_pk>/<int:story_part_pk>/', views.StoryPartUpdate.as_view(), name='update_story_part'),
path('open/<int:story_round_pk>/', views.RedirectToNextOpenPart.as_view(), name='redirect_story_part'),
] ]

View File

@ -1,10 +1,11 @@
from django import forms from django import forms
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import render from django.shortcuts import render, get_object_or_404
# Create your views here. # Create your views here.
from django.urls import reverse_lazy, reverse
from django.views import generic from django.views import generic
from django.views.generic import CreateView, UpdateView from django.views.generic import CreateView, UpdateView, RedirectView
from writingtogether.models import Story, StoryPart, StoryRound, Participant from writingtogether.models import Story, StoryPart, StoryRound, Participant
@ -50,12 +51,28 @@ class StoryUpdate(UpdateView):
fields = ['name'] fields = ['name']
class StoryPartCreate(CreateView): class StoryPartUpdate(UpdateView):
model = StoryPart model = StoryPart
fields = ['text'] fields = ['text']
template_name = 'writingtogether/story_part.html'
def form_valid(self, form): #def form_valid(self, form):
form.instance.created_by = self.request.user # form.instance.created_by = self.request.user
form.instance.previous_part_id = self.kwargs['previous'] # form.instance.previous_part_id = self.kwargs['previous']
form.instance.part_of_id = self.kwargs['pk'] # form.instance.part_of_id = self.kwargs['story_pk']
return super().form_valid(form) # return super().form_valid(form)
class RedirectToNextOpenPart(RedirectView):
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
kwargs['story_part_pk'] = story_part.pk
return super().get_redirect_url(*args, **kwargs)