timetracker/games/forms.py

96 lines
2.7 KiB
Python
Raw Normal View History

2022-12-31 13:18:27 +00:00
from django import forms
2023-01-15 22:39:52 +00:00
2023-02-18 20:12:18 +00:00
from games.models import Game, Platform, Purchase, Session, Edition, Device
2022-12-31 13:18:27 +00:00
custom_date_widget = forms.DateInput(attrs={"type": "date"})
custom_datetime_widget = forms.DateTimeInput(
attrs={"type": "datetime-local"}, format="%Y-%m-%d %H:%M"
)
2023-09-30 10:40:02 +00:00
autofocus_select_widget = forms.Select(attrs={"autofocus": "autofocus"})
autofocus_input_widget = forms.TextInput(attrs={"autofocus": "autofocus"})
2022-12-31 13:18:27 +00:00
class SessionForm(forms.ModelForm):
# purchase = forms.ModelChoiceField(
# queryset=Purchase.objects.filter(date_refunded=None).order_by("edition__name")
# )
2023-02-18 19:49:46 +00:00
purchase = forms.ModelChoiceField(
2023-09-30 10:40:02 +00:00
queryset=Purchase.objects.order_by("edition__name"),
widget=autofocus_select_widget,
2023-02-18 19:49:46 +00:00
)
2022-12-31 13:18:27 +00:00
class Meta:
widgets = {
"timestamp_start": custom_datetime_widget,
"timestamp_end": custom_datetime_widget,
}
2022-12-31 13:18:27 +00:00
model = Session
fields = [
"purchase",
"timestamp_start",
"timestamp_end",
"duration_manual",
2023-02-18 20:12:18 +00:00
"device",
2022-12-31 13:18:27 +00:00
"note",
]
class EditionChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj) -> str:
return f"{obj.name} ({obj.platform}, {obj.year_released})"
2022-12-31 13:18:27 +00:00
class PurchaseForm(forms.ModelForm):
2023-09-30 10:40:02 +00:00
edition = EditionChoiceField(
queryset=Edition.objects.order_by("name"), widget=autofocus_select_widget
)
platform = forms.ModelChoiceField(queryset=Platform.objects.order_by("name"))
2022-12-31 13:18:27 +00:00
class Meta:
widgets = {
"date_purchased": custom_date_widget,
"date_refunded": custom_date_widget,
}
2022-12-31 13:18:27 +00:00
model = Purchase
2023-02-18 19:53:47 +00:00
fields = [
"edition",
"platform",
"date_purchased",
"date_refunded",
"price",
"price_currency",
"ownership_type",
2023-02-18 19:53:47 +00:00
]
2023-02-18 19:49:46 +00:00
class EditionForm(forms.ModelForm):
2023-09-30 10:40:02 +00:00
game = forms.ModelChoiceField(
queryset=Game.objects.order_by("name"), widget=autofocus_select_widget
)
platform = forms.ModelChoiceField(queryset=Platform.objects.order_by("name"))
2023-02-18 19:49:46 +00:00
class Meta:
model = Edition
2023-02-20 16:13:21 +00:00
fields = ["game", "name", "platform", "year_released", "wikidata"]
2022-12-31 13:18:27 +00:00
class GameForm(forms.ModelForm):
class Meta:
model = Game
fields = ["name", "wikidata"]
2023-09-30 10:40:02 +00:00
widgets = {"name": autofocus_input_widget}
2023-01-04 16:23:34 +00:00
class PlatformForm(forms.ModelForm):
class Meta:
model = Platform
fields = ["name", "group"]
2023-09-30 10:40:02 +00:00
widgets = {"name": autofocus_input_widget}
2023-02-18 20:12:18 +00:00
class DeviceForm(forms.ModelForm):
class Meta:
model = Device
fields = ["name", "type"]
2023-09-30 10:40:02 +00:00
widgets = {"name": autofocus_input_widget}