guild-journal/guild/views/__init__.py

44 lines
1.5 KiB
Python

from datetime import date
from django.urls import reverse
from django.views.generic import TemplateView, ListView, DetailView
from django.views.generic.edit import CreateView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from guild.models import Adventure, Character, Player, Reward
# Create your views here.
class HomeView(LoginRequiredMixin, TemplateView):
template_name = "guild/home.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
advs = Adventure.objects.prefetch_related("playsession_set", "master").all()
advs = sorted(
advs,
key=lambda x: x.last_session.date if x.last_session else date.min,
reverse=True,
)
context["adventures"] = advs
context["players"] = Player.objects.all()
context["characters"] = Character.objects.all()
context["rewards"] = Reward.objects.filter(character=None).order_by(
"-playsession__date"
)
context["resources"] = {}
for reward in context["rewards"]:
if reward.resource.name not in context["resources"]:
context["resources"][reward.resource.name] = 0
context["resources"][reward.resource.name] += reward.amount
return context
class RegisterView(CreateView):
model = User
form_class = UserCreationForm
def get_success_url(self):
return reverse("guild:login")