from datetime import date from django.views.generic import TemplateView, ListView, DetailView from django.views.generic.edit import CreateView from django.contrib.auth.mixins import LoginRequiredMixin 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