guild-journal/guild/forms.py

39 lines
1.1 KiB
Python

from django import forms
from guild.models import 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 PlaySessionForm(forms.ModelForm):
characters = PlaySessionCharacterForm(
queryset=Character.objects.all(), widget=forms.CheckboxSelectMultiple
)
class Meta:
model = PlaySession
fields = ["date", "characters", "summary"]
# 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"]