guild-journal/guild/views/playsession.py

79 lines
2.7 KiB
Python
Raw Normal View History

2023-06-20 19:22:56 +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.urls import reverse
from django.views.generic import TemplateView, ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.contrib.auth.mixins import LoginRequiredMixin
from guild.forms import PlaySessionForm, RewardForm
from guild.models import Adventure, PlaySession, Reward
class PlaySessionCreateView(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
class PlaySessionUpdateView(LoginRequiredMixin, UpdateView):
model = PlaySession
form_class = PlaySessionForm
pk_url_kwarg = "playsession_pk"
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):
model = PlaySession
pk_url_kwarg = "playsession_pk"
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()
class CreateRewardView(LoginRequiredMixin, CreateView):
model = Reward
form_class = RewardForm
def dispatch(self, request, *args, **kwargs):
self.playsession = get_object_or_404(PlaySession, pk=kwargs["pk"])
return super().dispatch(request, *args, **kwargs)
def form_valid(self, form: BaseModelForm) -> HttpResponse:
form.instance.playsession = self.playsession
return super().form_valid(form)
def get_success_url(self) -> str:
return reverse("guild:playsession_detail", kwargs={"pk": self.playsession.pk})
class RewardDeleteView(LoginRequiredMixin, DeleteView):
model = Reward
pk_url_kwarg = "reward_pk"
def dispatch(self, request, *args, **kwargs):
self.playsession = get_object_or_404(PlaySession, pk=kwargs["pk"])
return super().dispatch(request, *args, **kwargs)
def get_success_url(self) -> str:
return reverse("guild:playsession_detail", kwargs={"pk": self.playsession.pk})