-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
forms.py
42 lines (35 loc) · 1.61 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
from django import forms
from django.utils.translation import gettext_lazy as _
from .models import Reservation
class ReservationForm(forms.ModelForm):
in_book = forms.BooleanField(
initial=False, required=False,
label=_("I'm a host and I will appear in the book"),
help_text=_("I can have my book at a discounted price"))
class Meta:
model = Reservation
fields = ['amount', 'discount', 'support']
def __init__(self, *args, **kwargs):
is_in_book = kwargs.pop('is_in_book')
self.user = kwargs.pop('user')
self.product = kwargs.pop('product')
super().__init__(*args, **kwargs)
self.fields['in_book'].initial = is_in_book
self.fields['in_book'].widget.attrs.update({
'data-bind': "checked: inBook"})
self.fields['amount'].widget.attrs.update({
'class': "form-control input-lg text-center",
'data-bind': "textInput: productAmount"})
self.fields['discount'].widget.attrs['data-bind'] = "checked: hasTejoDiscount, cli"
self.fields['support'].localize = True
self.fields['support'].widget = forms.TextInput(attrs={
'class': "form-control pull-right same-as-body",
'style': "width: 5em; text-align: right; padding-right: calc(1em - 1px)",
'pattern': "[0-9]{1,4},[0-9]{2}",
'data-bind': "textInput: supportInput"})
def save(self, commit=True):
instance = super().save(commit=False)
instance.user, instance.product = self.user, self.product
if commit:
instance.save()
return instance