guild-journal/guild/forms.py

48 lines
1.4 KiB
Python

from typing import Any
from django import forms
from guild.models import NPC, PlaySession, Character, Reward
from django.utils.translation import gettext as _
class PlaySessionCharacterForm(forms.ModelMultipleChoiceField):
def label_from_instance(self, character):
if character.player:
return "{} ({})".format(character.name, character.player.name)
else:
return character.name
class PlaySessionNPCForm(forms.ModelMultipleChoiceField):
def label_from_instance(self, npc):
return npc.name
class PlaySessionForm(forms.ModelForm):
characters = PlaySessionCharacterForm(
queryset=Character.objects.all(), widget=forms.CheckboxSelectMultiple
)
npcs = PlaySessionNPCForm(
queryset=NPC.objects.all(), widget=forms.CheckboxSelectMultiple, required=False
)
class Meta:
model = PlaySession
fields = ["date", "characters", "summary", "npcs"]
# set date wideget to type="date"
widgets = {
"date": forms.DateInput(attrs={"type": "date"}),
"summary": forms.Textarea(attrs={"rows": 32}),
}
class RewardForm(forms.ModelForm):
character = forms.ModelChoiceField(
queryset=Character.objects.all(),
empty_label="All Guild Members",
required=False,
)
class Meta:
model = Reward
fields = ["amount", "resource", "character"]