guild-journal/guild/views/character.py

42 lines
1.4 KiB
Python
Raw Normal View History

2023-06-10 19:33:06 +00:00
from django.views.generic import TemplateView, ListView, DetailView
2023-06-11 18:37:03 +00:00
from django.views.generic.edit import CreateView, UpdateView, DeleteView
2023-06-10 19:33:06 +00:00
from django.contrib.auth.mixins import LoginRequiredMixin
2023-06-20 19:22:56 +00:00
from django.db.models import Q
from guild.models import Character, Reward
2023-06-10 19:33:06 +00:00
2023-06-11 19:20:45 +00:00
class CharacterListView(LoginRequiredMixin, ListView):
model = Character
context_object_name = "characters"
2023-06-11 19:25:30 +00:00
2023-06-10 19:33:06 +00:00
class CreateCharacterView(LoginRequiredMixin, CreateView):
model = Character
2023-08-24 18:58:22 +00:00
fields = ["name", "picture", "description", "player", "status"]
2023-06-10 19:33:06 +00:00
2023-06-11 19:25:30 +00:00
2023-06-10 19:33:06 +00:00
class CharacterDetailView(LoginRequiredMixin, DetailView):
2023-06-11 18:37:03 +00:00
model = Character
2023-06-20 19:22:56 +00:00
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
2023-06-11 19:25:30 +00:00
2023-06-11 18:37:03 +00:00
class CharacterUpdateView(LoginRequiredMixin, UpdateView):
model = Character
2023-08-24 18:58:22 +00:00
fields = ["name", "picture", "description", "player", "status"]
2023-06-11 18:37:03 +00:00
2023-06-11 19:25:30 +00:00
2023-06-11 18:37:03 +00:00
class CharacterDeleteView(LoginRequiredMixin, DeleteView):
model = Character
2023-06-11 19:25:30 +00:00
success_url = "/"