diff --git a/stories/stories/urls.py b/stories/stories/urls.py index c6d8375..7da4322 100644 --- a/stories/stories/urls.py +++ b/stories/stories/urls.py @@ -14,8 +14,9 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import path, include urlpatterns = [ + path('writing/', include('writingtogether.urls')), path('admin/', admin.site.urls), ] diff --git a/stories/writingtogether/templates/writingtogether/detail.html b/stories/writingtogether/templates/writingtogether/detail.html new file mode 100644 index 0000000..566549b --- /dev/null +++ b/stories/writingtogether/templates/writingtogether/detail.html @@ -0,0 +1,10 @@ + + + + + Title + + + + + \ No newline at end of file diff --git a/stories/writingtogether/templates/writingtogether/index.html b/stories/writingtogether/templates/writingtogether/index.html new file mode 100644 index 0000000..30e5dce --- /dev/null +++ b/stories/writingtogether/templates/writingtogether/index.html @@ -0,0 +1,31 @@ + + + + + Geschichten! + + + +

Geschichten!

+ +{% if latest_story_round_list %} + + + +{% else %} +

Noch keine Geschichten vorhanden.

+{% endif %} + +
+ {% csrf_token %} + {{ form.as_p }} + +
+ + + + \ No newline at end of file diff --git a/stories/writingtogether/templates/writingtogether/storyround_form.html b/stories/writingtogether/templates/writingtogether/storyround_form.html new file mode 100644 index 0000000..3cc9967 --- /dev/null +++ b/stories/writingtogether/templates/writingtogether/storyround_form.html @@ -0,0 +1,16 @@ + + + + + Title + + + +
+ {% csrf_token %} + {{ form.as_p }} + +
+ + + \ No newline at end of file diff --git a/stories/writingtogether/urls.py b/stories/writingtogether/urls.py new file mode 100644 index 0000000..337c0d1 --- /dev/null +++ b/stories/writingtogether/urls.py @@ -0,0 +1,12 @@ +from django.urls import path + +from . import views + +app_name = 'writing' + +urlpatterns = [ + path('', views.IndexView.as_view(), name='index'), + path('/', views.DetailView.as_view(), name='detail'), + path('create', views.StoryRoundCreate.as_view(), name='create_story_round'), + path('//', views.StoryPartCreate, name='create_story_part'), +] diff --git a/stories/writingtogether/views.py b/stories/writingtogether/views.py index 91ea44a..8298f75 100644 --- a/stories/writingtogether/views.py +++ b/stories/writingtogether/views.py @@ -1,3 +1,60 @@ +from django import forms +from django.http import HttpResponseRedirect from django.shortcuts import render # Create your views here. +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' + context_object_name = 'latest_story_round_list' + + def get_queryset(self): + 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)