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 django.db.models import Q from guild.models import Character, Reward class CharacterListView(LoginRequiredMixin, ListView): model = Character context_object_name = "characters" class CreateCharacterView(LoginRequiredMixin, CreateView): model = Character fields = ["name", "picture", "description", "player", "status"] class CharacterDetailView(LoginRequiredMixin, DetailView): model = Character def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["rewards"] = Reward.objects.filter( Q(character=self.object) | Q(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 CharacterUpdateView(LoginRequiredMixin, UpdateView): model = Character fields = ["name", "picture", "description", "player", "status"] class CharacterDeleteView(LoginRequiredMixin, DeleteView): model = Character success_url = "/"