-
Notifications
You must be signed in to change notification settings - Fork 1
/
forms.py
45 lines (36 loc) · 1.71 KB
/
forms.py
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
from django import forms
from .models import Player, Punishment, Constants
from django.forms import BaseInlineFormSet, ValidationError, inlineformset_factory
from django.core.validators import MaxValueValidator, MinValueValidator
class PunishmentForm(forms.ModelForm):
amount = forms.IntegerField(min_value=0, required=True, widget=forms.NumberInput(attrs={'required': True}))
class Meta:
model = Punishment
fields = ['amount']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
maxval = self.instance.sender.punishment_endowment
self.fields['amount'].validators += [MaxValueValidator(maxval)]
self.fields['amount'].widget.attrs['max'] = maxval
class PunishmentFormset(BaseInlineFormSet):
non_field_errors = None
def clean(self):
super().clean()
if any(self.errors):
self.non_field_errors = 'Please check your answers'
return
amounts = []
punishment_endowment = self.instance.punishment_endowment
for form in self.forms:
amounts.append(form.cleaned_data['amount'])
if sum(amounts) > punishment_endowment:
self.non_field_errors = "In total you can't send more than {endowment} points!".format(
endowment=punishment_endowment)
raise ValidationError(self.non_field_errors)
PFormset = inlineformset_factory(Player, Punishment,
formset=PunishmentFormset,
form=PunishmentForm,
extra=0,
can_delete=False,
fk_name='sender',
fields=['amount'])