-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathforms.py
More file actions
413 lines (330 loc) · 11.9 KB
/
forms.py
File metadata and controls
413 lines (330 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
from django import forms
from django.conf import settings
import users
from app import config
from app.models import (
TV,
Anime,
BoardGame,
Book,
Comic,
Episode,
Game,
Item,
Manga,
MediaTypes,
Movie,
Season,
Sources,
)
def get_form_class(media_type):
"""Return the form class for the media type."""
class_name = media_type.capitalize() + "Form"
return globals().get(class_name, None)
class CustomDurationField(forms.CharField):
"""Custom form field for duration input that accepts multiple time formats."""
def _parse_hours_minutes(self, value):
"""Parse hours and minutes from various time formats.
Supported formats:
- Plain number (hours only): "5"
- HH:MM: "5:30"
- Nh Nmin: "5h 30min"
- NhNmin: "5h30min"
- Nmin: "30min"
- Nh: "5h"
"""
if value.isdigit(): # hours only
return int(value), 0
if ":" in value: # hh:mm format
hours, minutes = value.split(":")
return int(hours), int(minutes)
if " " in value: # [n]h [n]min format
hours, minutes = value.split(" ")
return int(hours.strip("h")), int(minutes.strip("min"))
if "h" in value and "min" in value: # [n]h[n]min format
hours, minutes = value.split("h")
return int(hours), int(minutes.strip("min"))
if "min" in value: # [n]min format
return 0, int(value.strip("min"))
if "h" in value: # [n]h format
return int(value.strip("h")), 0
msg = "Invalid time format"
raise ValueError(msg)
def _validate_minutes(self, minutes):
"""Validate that minutes are within acceptable range."""
max_min = 59
if not (0 <= minutes <= max_min):
msg = f"Minutes must be between 0 and {max_min}."
raise forms.ValidationError(msg)
def clean(self, value):
"""Validate and convert the time string to total minutes."""
cleaned_value = super().clean(value)
if not cleaned_value:
return 0
try:
hours, minutes = self._parse_hours_minutes(cleaned_value)
self._validate_minutes(minutes)
return hours * 60 + minutes
except ValueError as e:
msg = "Invalid time played format. Please use hh:mm, [n]h [n]min or [n]h[n]min format." # noqa: E501
raise forms.ValidationError(msg) from e
class ManualItemForm(forms.ModelForm):
"""Form for adding items to the database."""
parent_tv = forms.ModelChoiceField(
required=False,
queryset=TV.objects.none(),
empty_label="Select",
label="Parent TV Show",
)
parent_season = forms.ModelChoiceField(
required=False,
queryset=Season.objects.none(),
empty_label="Select",
label="Parent Season",
)
class Meta:
"""Bind form to model."""
model = Item
fields = [
"media_type",
"title",
"image",
"season_number",
"episode_number",
]
def __init__(self, *args, **kwargs):
"""Initialize the form."""
self.user = kwargs.pop("user", None)
super().__init__(*args, **kwargs)
if self.user:
self.fields["parent_tv"].queryset = TV.objects.filter(
user=self.user,
item__source=Sources.MANUAL.value,
item__media_type=MediaTypes.TV.value,
)
self.fields["parent_season"].queryset = Season.objects.filter(
user=self.user,
item__source=Sources.MANUAL.value,
item__media_type=MediaTypes.SEASON.value,
)
self.fields["image"].required = False
self.fields["title"].required = False
def clean(self):
"""Validate the form."""
cleaned_data = super().clean()
image = cleaned_data.get("image")
media_type = cleaned_data.get("media_type")
if not image:
cleaned_data["image"] = settings.IMG_NONE
# Title not required for season/episode
if media_type in [MediaTypes.SEASON.value, MediaTypes.EPISODE.value]:
if media_type == MediaTypes.SEASON.value:
parent = cleaned_data.get("parent_tv")
if not parent:
self.add_error(
"parent_tv",
"Parent TV show is required for seasons",
)
return cleaned_data
cleaned_data["title"] = parent.item.title
cleaned_data["episode_number"] = None
else: # episode
parent = cleaned_data.get("parent_season")
if not parent:
self.add_error(
"parent_season",
"Parent season is required for episodes",
)
return cleaned_data
cleaned_data["title"] = parent.item.title
cleaned_data["season_number"] = parent.item.season_number
else:
# For standalone media, title is required
if not cleaned_data.get("title"):
self.add_error("title", "Title is required for this media type")
cleaned_data["season_number"] = None
cleaned_data["episode_number"] = None
return cleaned_data
def save(self, commit=True): # noqa: FBT002
"""Save the form and handle manual media ID generation."""
instance = super().save(commit=False)
instance.source = Sources.MANUAL.value
if instance.media_type == MediaTypes.SEASON.value:
parent_tv = self.cleaned_data["parent_tv"]
instance.media_id = parent_tv.item.media_id
elif instance.media_type == MediaTypes.EPISODE.value:
parent_season = self.cleaned_data["parent_season"]
instance.media_id = parent_season.item.media_id
instance.season_number = parent_season.item.season_number
else:
instance.media_id = Item.generate_manual_id(instance.media_type)
if commit:
instance.save()
return instance
class MediaForm(forms.ModelForm):
"""Base form for all media types."""
can_toggle_unit = False
instance_id = forms.CharField(widget=forms.HiddenInput(), required=False)
media_type = forms.CharField(widget=forms.HiddenInput(), required=True)
source = forms.CharField(widget=forms.HiddenInput(), required=True)
media_id = forms.CharField(widget=forms.HiddenInput(), required=True)
def __init__(self, *args, **kwargs):
"""Initialize the form."""
self.user = kwargs.pop("user", None)
super().__init__(*args, **kwargs)
class Meta:
"""Define fields and input types."""
fields = [
"score",
"progress",
"status",
"start_date",
"end_date",
"notes",
]
widgets = {
"score": forms.NumberInput(
attrs={"min": 0, "max": 10, "step": 0.1, "placeholder": "0-10"},
),
"progress": forms.NumberInput(attrs={"min": 0}),
"start_date": forms.DateTimeInput(attrs={"type": "datetime-local"})
if settings.TRACK_TIME
else forms.DateInput(attrs={"type": "date"}),
"end_date": forms.DateTimeInput(attrs={"type": "datetime-local"})
if settings.TRACK_TIME
else forms.DateInput(attrs={"type": "date"}),
"notes": forms.Textarea(
attrs={"placeholder": "Add any notes or comments...", "rows": "5"},
),
}
class MangaForm(MediaForm):
"""Form for manga."""
class Meta(MediaForm.Meta):
"""Bind form to model."""
model = Manga
labels = {
"progress": (
f"Progress ({config.get_unit(MediaTypes.MANGA.value, short=False)}s)"
),
}
class AnimeForm(MediaForm):
"""Form for anime."""
class Meta(MediaForm.Meta):
"""Bind form to model."""
model = Anime
class MovieForm(MediaForm):
"""Form for movies."""
class Meta(MediaForm.Meta):
"""Bind form to model."""
model = Movie
fields = [
"score",
"status",
"start_date",
"end_date",
"notes",
]
class GameForm(MediaForm):
"""Form for games."""
progress = CustomDurationField(
required=False,
widget=forms.TextInput(attrs={"placeholder": "hh:mm"}),
label="Progress (Time Played)",
)
class Meta(MediaForm.Meta):
"""Bind form to model."""
model = Game
class BookForm(MediaForm):
"""Form for books."""
can_toggle_unit = True
progress_unit = forms.ChoiceField(
choices=users.models.ProgressUnit.choices,
widget=forms.HiddenInput(),
required=False,
)
class Meta(MediaForm.Meta):
"""Bind form to model."""
model = Book
fields = MediaForm.Meta.fields + ["progress_unit"]
labels = {
"progress": (
f"Progress ({config.get_unit(MediaTypes.BOOK.value, short=False)}s)"
),
}
def __init__(self, *args, **kwargs):
"""Initialize the form and set progress unit."""
super().__init__(*args, **kwargs)
# Set initial progress unit
if self.instance and self.instance.pk:
unit = self.instance.get_progress_unit()
self.initial["progress_unit"] = unit
else:
# For new items, use user preference if available
user = getattr(self, "user", None)
if user:
self.initial["progress_unit"] = user.book_progress_unit
else:
self.initial["progress_unit"] = users.models.ProgressUnit.PAGES
# Update label based on unit
current_unit = self.initial.get("progress_unit")
if current_unit == users.models.ProgressUnit.PERCENTAGE:
self.fields["progress"].label = "Progress (%)"
self.fields["progress"].widget.attrs["max"] = 100
class ComicForm(MediaForm):
"""Form for comics."""
class Meta(MediaForm.Meta):
"""Bind form to model."""
model = Comic
labels = {
"progress": (
f"Progress ({config.get_unit(MediaTypes.COMIC.value, short=False)}s)"
),
}
class BoardgameForm(MediaForm):
"""Form for board games."""
class Meta(MediaForm.Meta):
"""Bind form to model."""
model = BoardGame
labels = {
"progress": (
"Progress "
f"({config.get_unit(MediaTypes.BOARDGAME.value, short=False)}s)"
),
}
class TvForm(MediaForm):
"""Form for TV shows."""
class Meta(MediaForm.Meta):
"""Bind form to model."""
model = TV
fields = ["score", "status", "notes"]
class SeasonForm(MediaForm):
"""Form for seasons."""
season_number = forms.IntegerField(widget=forms.HiddenInput(), required=False)
class Meta(MediaForm.Meta):
"""Bind form to model."""
model = Season
fields = [
"score",
"status",
"notes",
]
class EpisodeForm(forms.ModelForm):
"""Form for episodes."""
class Meta:
"""Bind form to model."""
model = Episode
fields = ("end_date",)
widgets = {
"end_date": forms.DateInput(attrs={"type": "date"}),
}
def __init__(self, *args, **kwargs):
"""Initialize the form."""
super().__init__(*args, **kwargs)
if settings.TRACK_TIME:
self.fields["end_date"].widget = forms.DateTimeInput(
attrs={"type": "datetime-local"},
)
else:
self.fields["end_date"].widget = forms.DateInput(
attrs={"type": "date"},
)