2023-06-10 20:32:45 +00:00
|
|
|
|
|
|
|
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
|
2023-06-11 18:01:36 +00:00
|
|
|
from django.views.generic.edit import CreateView, UpdateView, DeleteView
|
2023-06-10 20:32:45 +00:00
|
|
|
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
|
|
|
|
|
2023-06-11 18:01:36 +00:00
|
|
|
class AdventureUpdateView(LoginRequiredMixin, UpdateView):
|
|
|
|
model = Adventure
|
|
|
|
fields = ["name", "description", "master"]
|
|
|
|
|
|
|
|
class AdventureDeleteView(LoginRequiredMixin, DeleteView):
|
|
|
|
model = Adventure
|
|
|
|
success_url = "/"
|
|
|
|
|
|
|
|
class PlaySessionCreateView(LoginRequiredMixin, CreateView):
|
2023-06-10 20:32:45 +00:00
|
|
|
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)
|
|
|
|
|
2023-06-11 18:01:36 +00:00
|
|
|
class PlaySessionUpdateView(LoginRequiredMixin, UpdateView):
|
|
|
|
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)
|
|
|
|
|
|
|
|
class PlaySessionDeleteView(LoginRequiredMixin, DeleteView):
|
2023-06-10 20:32:45 +00:00
|
|
|
model = PlaySession
|
|
|
|
pk_url_kwarg = "playsession_pk"
|
|
|
|
|
2023-06-11 18:01:36 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
self.adventure = get_object_or_404(Adventure, pk=kwargs["pk"])
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_success_url(self) -> str:
|
|
|
|
return self.adventure.get_absolute_url()
|