40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
|
|
||
|
from typing import Any, Dict, Optional, Type
|
||
|
from django.forms.models import BaseModelForm
|
||
|
from django.http import HttpResponse
|
||
|
from django.shortcuts import get_object_or_404
|
||
|
from django.views.generic import TemplateView, ListView, DetailView
|
||
|
from django.views.generic.edit import CreateView
|
||
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
||
|
from guild.forms import PlaySessionForm
|
||
|
|
||
|
from guild.models import Adventure, PlaySession
|
||
|
|
||
|
|
||
|
class CreateAdventureView(LoginRequiredMixin, CreateView):
|
||
|
model = Adventure
|
||
|
fields = ["name", "description", "master"]
|
||
|
|
||
|
class AdventureDetailView(LoginRequiredMixin, DetailView):
|
||
|
model = Adventure
|
||
|
|
||
|
class CreatePlaySessionView(LoginRequiredMixin, CreateView):
|
||
|
model = PlaySession
|
||
|
form_class = PlaySessionForm
|
||
|
|
||
|
def dispatch(self, request, *args, **kwargs):
|
||
|
self.adventure = get_object_or_404(Adventure, pk=kwargs["pk"])
|
||
|
return super().dispatch(request, *args, **kwargs)
|
||
|
|
||
|
def form_valid(self, form: BaseModelForm) -> HttpResponse:
|
||
|
form.instance.adventure = self.adventure
|
||
|
return super().form_valid(form)
|
||
|
|
||
|
|
||
|
class PlaySessionDetailView(LoginRequiredMixin, DetailView):
|
||
|
model = PlaySession
|
||
|
pk_url_kwarg = "playsession_pk"
|
||
|
|
||
|
def get_queryset(self):
|
||
|
return super().get_queryset().filter(adventure_id=self.kwargs["pk"])
|