2016-08-02 10:40:46 +02:00
|
|
|
from django import forms
|
|
|
|
from django.contrib.auth.models import User
|
2016-08-06 22:19:52 +02:00
|
|
|
from django.forms import modelformset_factory
|
|
|
|
from kfet.models import Account, Checkout, Article, OperationGroup, Operation
|
2016-08-02 10:40:46 +02:00
|
|
|
from gestioncof.models import CofProfile
|
|
|
|
|
2016-08-04 05:21:04 +02:00
|
|
|
# -----
|
|
|
|
# Widgets
|
|
|
|
# -----
|
|
|
|
|
|
|
|
class DateTimeWidget(forms.DateTimeInput):
|
|
|
|
def __init__(self, attrs = None):
|
|
|
|
super(DateTimeWidget, self).__init__(attrs)
|
|
|
|
self.attrs['format'] = '%Y-%m-%d %H:%M'
|
|
|
|
class Media:
|
|
|
|
css = {
|
|
|
|
'all': ('bootstrap-datetimepicker.min.css',)
|
|
|
|
}
|
|
|
|
js = (
|
|
|
|
'moment.js',
|
|
|
|
'moment-fr.js',
|
|
|
|
'bootstrap-datetimepicker.min.js',
|
|
|
|
)
|
|
|
|
# -----
|
|
|
|
# Account forms
|
|
|
|
# -----
|
|
|
|
|
2016-08-03 04:38:54 +02:00
|
|
|
class AccountForm(forms.ModelForm):
|
|
|
|
|
|
|
|
# Surcharge pour passer data à Account.save()
|
|
|
|
def save(self, data = {}, *args, **kwargs):
|
|
|
|
obj = super(AccountForm, self).save(commit = False, *args, **kwargs)
|
|
|
|
obj.save(data = data)
|
|
|
|
return obj
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class Meta:
|
|
|
|
model = Account
|
2016-08-03 04:38:54 +02:00
|
|
|
fields = ['trigramme', 'promo', 'nickname']
|
2016-08-02 10:40:46 +02:00
|
|
|
widgets = {
|
|
|
|
'trigramme': forms.TextInput(attrs={'autocomplete': 'off'}),
|
|
|
|
}
|
|
|
|
|
2016-08-03 04:38:54 +02:00
|
|
|
class AccountTriForm(AccountForm):
|
|
|
|
class Meta(AccountForm.Meta):
|
|
|
|
fields = ['trigramme']
|
|
|
|
|
|
|
|
class AccountNoTriForm(AccountForm):
|
|
|
|
class Meta(AccountForm.Meta):
|
|
|
|
exclude = ['trigramme']
|
|
|
|
|
|
|
|
class AccountRestrictForm(AccountForm):
|
|
|
|
class Meta(AccountForm.Meta):
|
|
|
|
fields = ['promo']
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
class CofForm(forms.ModelForm):
|
|
|
|
def clean_is_cof(self):
|
|
|
|
instance = getattr(self, 'instance', None)
|
|
|
|
if instance and instance.pk:
|
|
|
|
return instance.is_cof
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
class Meta:
|
|
|
|
model = CofProfile
|
|
|
|
fields = ['login_clipper', 'is_cof', 'departement']
|
|
|
|
|
2016-08-03 04:38:54 +02:00
|
|
|
class CofRestrictForm(CofForm):
|
|
|
|
class Meta(CofForm.Meta):
|
|
|
|
fields = ['departement']
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class UserForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ['username', 'first_name', 'last_name', 'email']
|
|
|
|
help_texts = {
|
|
|
|
'username': ''
|
|
|
|
}
|
2016-08-03 04:38:54 +02:00
|
|
|
|
|
|
|
class UserRestrictForm(UserForm):
|
|
|
|
class Meta(UserForm.Meta):
|
|
|
|
fields = ['first_name', 'last_name', 'email']
|
2016-08-04 05:21:04 +02:00
|
|
|
|
|
|
|
# -----
|
|
|
|
# Checkout forms
|
|
|
|
# -----
|
|
|
|
|
|
|
|
class CheckoutForm(forms.ModelForm):
|
|
|
|
class Meta:
|
2016-08-04 08:23:34 +02:00
|
|
|
model = Checkout
|
|
|
|
fields = ['name', 'valid_from', 'valid_to', 'balance', 'is_protected']
|
2016-08-04 05:21:04 +02:00
|
|
|
widgets = {
|
|
|
|
'valid_from': DateTimeWidget(),
|
|
|
|
'valid_to' : DateTimeWidget(),
|
|
|
|
}
|
|
|
|
|
|
|
|
class CheckoutRestrictForm(CheckoutForm):
|
|
|
|
class Meta(CheckoutForm.Meta):
|
|
|
|
fields = ['name', 'valid_from', 'valid_to']
|
2016-08-04 08:23:34 +02:00
|
|
|
|
|
|
|
# -----
|
|
|
|
# Article forms
|
|
|
|
# -----
|
|
|
|
|
|
|
|
class ArticleForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Article
|
|
|
|
fields = ['name', 'is_sold', 'price', 'stock', 'category']
|
|
|
|
|
|
|
|
class ArticleRestrictForm(ArticleForm):
|
|
|
|
class Meta(ArticleForm.Meta):
|
|
|
|
fields = ['name', 'is_sold', 'price', 'category']
|
2016-08-06 22:19:52 +02:00
|
|
|
|
|
|
|
# -----
|
|
|
|
# K-Psul forms
|
|
|
|
# -----
|
|
|
|
|
|
|
|
class KPsulOperationGroupForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = OperationGroup
|
|
|
|
fields = ['on_acc', 'checkout']
|
|
|
|
widgets = {
|
|
|
|
'on_acc' : forms.HiddenInput(),
|
|
|
|
'checkout': forms.HiddenInput(),
|
|
|
|
}
|
|
|
|
|
|
|
|
class KPsulAccountForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Account
|
|
|
|
fields = ['trigramme']
|
|
|
|
widgets = {
|
|
|
|
'trigramme': forms.TextInput(attrs={'autocomplete': 'off'}),
|
|
|
|
}
|
|
|
|
|
|
|
|
class KPsulCheckoutForm(forms.Form):
|
|
|
|
checkout = forms.ModelChoiceField(
|
|
|
|
queryset=Checkout.objects.filter(is_protected=False),
|
|
|
|
widget=forms.Select(attrs={'id':'id_checkout_select'}))
|
|
|
|
|
|
|
|
class KPsulOperationForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Operation
|
|
|
|
fields = ['type', 'amount', 'is_checkout', 'article', 'article_nb']
|
2016-08-06 23:44:58 +02:00
|
|
|
|
|
|
|
KPsulOperationFormSet = modelformset_factory(
|
2016-08-06 22:19:52 +02:00
|
|
|
Operation,
|
|
|
|
form = KPsulOperationForm,
|
|
|
|
extra = 0,
|
|
|
|
min_num = 1, validate_min = True)
|