2016-08-02 10:40:46 +02:00
|
|
|
from django import forms
|
2016-08-07 17:02:01 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2016-08-21 05:51:55 +02:00
|
|
|
from django.contrib.auth.models import User, Group, Permission
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.contrib.admin.widgets import FilteredSelectMultiple
|
2016-08-06 22:19:52 +02:00
|
|
|
from django.forms import modelformset_factory
|
2016-08-22 02:52:59 +02:00
|
|
|
from django.utils import timezone
|
2016-08-11 15:14:23 +02:00
|
|
|
from kfet.models import (Account, Checkout, Article, OperationGroup, Operation,
|
2016-08-22 03:57:13 +02:00
|
|
|
CheckoutStatement, ArticleCategory, Settings)
|
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):
|
2016-08-21 02:53:35 +02:00
|
|
|
class Meta(UserForm.Meta):
|
|
|
|
fields = ['first_name', 'last_name']
|
|
|
|
|
|
|
|
class UserRestrictTeamForm(UserForm):
|
2016-08-03 04:38:54 +02:00
|
|
|
class Meta(UserForm.Meta):
|
|
|
|
fields = ['first_name', 'last_name', 'email']
|
2016-08-04 05:21:04 +02:00
|
|
|
|
2016-08-21 02:53:35 +02:00
|
|
|
class UserGroupForm(forms.ModelForm):
|
|
|
|
groups = forms.ModelMultipleChoiceField(
|
|
|
|
Group.objects.filter(name__icontains='K-Fêt'))
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ['groups']
|
|
|
|
|
2016-08-21 05:51:55 +02:00
|
|
|
class GroupForm(forms.ModelForm):
|
|
|
|
permissions = forms.ModelMultipleChoiceField(
|
|
|
|
queryset= Permission.objects.filter(content_type__in=
|
|
|
|
ContentType.objects.filter(app_label='kfet')))
|
|
|
|
class Meta:
|
|
|
|
model = Group
|
|
|
|
fields = ['name', 'permissions']
|
|
|
|
|
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
|
|
|
|
2016-08-11 15:14:23 +02:00
|
|
|
class CheckoutStatementForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = CheckoutStatement
|
|
|
|
fields = ['balance_new', 'amount_taken']
|
|
|
|
|
2016-08-04 08:23:34 +02:00
|
|
|
# -----
|
|
|
|
# Article forms
|
|
|
|
# -----
|
|
|
|
|
|
|
|
class ArticleForm(forms.ModelForm):
|
2016-08-21 18:10:35 +02:00
|
|
|
category_new = forms.CharField(
|
|
|
|
max_length=45,
|
|
|
|
required = False)
|
|
|
|
category = forms.ModelChoiceField(
|
|
|
|
queryset = ArticleCategory.objects.all(),
|
|
|
|
required = False)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
category = self.cleaned_data.get('category')
|
|
|
|
category_new = self.cleaned_data.get('category_new')
|
|
|
|
|
|
|
|
if not category and not category_new:
|
|
|
|
raise ValidationError('Sélectionnez une catégorie ou créez en une')
|
|
|
|
elif not category:
|
|
|
|
category, _ = ArticleCategory.objects.get_or_create(name=category_new)
|
|
|
|
self.cleaned_data['category'] = category
|
|
|
|
super(ArticleForm, self).clean()
|
|
|
|
|
2016-08-04 08:23:34 +02:00
|
|
|
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):
|
2016-08-07 17:02:01 +02:00
|
|
|
checkout = forms.ModelChoiceField(
|
2016-08-22 02:52:59 +02:00
|
|
|
queryset = Checkout.objects.filter(
|
|
|
|
is_protected=False, valid_from__lte=timezone.now(),
|
|
|
|
valid_to__gte=timezone.now()),
|
2016-08-07 17:02:01 +02:00
|
|
|
widget = forms.HiddenInput())
|
2016-08-06 22:19:52 +02:00
|
|
|
class Meta:
|
|
|
|
model = OperationGroup
|
|
|
|
fields = ['on_acc', 'checkout']
|
|
|
|
widgets = {
|
|
|
|
'on_acc' : forms.HiddenInput(),
|
|
|
|
}
|
|
|
|
|
|
|
|
class KPsulAccountForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Account
|
|
|
|
fields = ['trigramme']
|
|
|
|
widgets = {
|
2016-08-16 03:36:14 +02:00
|
|
|
'trigramme': forms.TextInput(
|
|
|
|
attrs={
|
|
|
|
'autocomplete': 'off',
|
|
|
|
'spellcheck': 'false',
|
|
|
|
}),
|
2016-08-06 22:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class KPsulCheckoutForm(forms.Form):
|
|
|
|
checkout = forms.ModelChoiceField(
|
2016-08-22 02:52:59 +02:00
|
|
|
queryset=Checkout.objects.filter(
|
|
|
|
is_protected=False, valid_from__lte=timezone.now(),
|
|
|
|
valid_to__gte=timezone.now()),
|
2016-08-06 22:19:52 +02:00
|
|
|
widget=forms.Select(attrs={'id':'id_checkout_select'}))
|
|
|
|
|
|
|
|
class KPsulOperationForm(forms.ModelForm):
|
2016-08-12 10:03:39 +02:00
|
|
|
article = forms.ModelChoiceField(
|
2016-08-14 19:59:36 +02:00
|
|
|
queryset=Article.objects.select_related('category').all(),
|
2016-08-17 11:44:58 +02:00
|
|
|
required=False,
|
|
|
|
widget = forms.HiddenInput())
|
2016-08-06 22:19:52 +02:00
|
|
|
class Meta:
|
|
|
|
model = Operation
|
|
|
|
fields = ['type', 'amount', 'is_checkout', 'article', 'article_nb']
|
2016-08-17 11:44:58 +02:00
|
|
|
widgets = {
|
|
|
|
'type': forms.HiddenInput(),
|
|
|
|
'amount': forms.HiddenInput(),
|
|
|
|
'is_checkout': forms.HiddenInput(),
|
|
|
|
'article_nb': forms.HiddenInput(),
|
|
|
|
}
|
2016-08-06 23:44:58 +02:00
|
|
|
|
2016-08-07 17:02:01 +02:00
|
|
|
def clean(self):
|
|
|
|
super(KPsulOperationForm, self).clean()
|
|
|
|
type_ope = self.cleaned_data.get('type')
|
|
|
|
amount = self.cleaned_data.get('amount')
|
|
|
|
article = self.cleaned_data.get('article')
|
|
|
|
article_nb = self.cleaned_data.get('article_nb')
|
|
|
|
if type_ope and type_ope == Operation.PURCHASE:
|
|
|
|
if not article or not article_nb:
|
|
|
|
raise ValidationError(
|
|
|
|
"Un achat nécessite un article et une quantité")
|
|
|
|
if article_nb < 1:
|
|
|
|
raise ValidationError("Impossible d'acheter moins de 1 article")
|
|
|
|
self.cleaned_data['is_checkout'] = True
|
|
|
|
elif type_ope and type_ope in [Operation.DEPOSIT, Operation.WITHDRAW]:
|
|
|
|
if not amount or article or article_nb:
|
|
|
|
raise ValidationError("Bad request")
|
|
|
|
if type_ope == Operation.DEPOSIT and amount <= 0:
|
|
|
|
raise ValidationError("Charge non positive")
|
|
|
|
if type_ope == Operation.WITHDRAW and amount >= 0:
|
|
|
|
raise ValidationError("Retrait non négatif")
|
2016-08-07 23:41:46 +02:00
|
|
|
self.cleaned_data['article'] = None
|
|
|
|
self.cleaned_data['article_nb'] = None
|
2016-08-07 17:02:01 +02:00
|
|
|
|
2016-08-06 23:44:58 +02:00
|
|
|
KPsulOperationFormSet = modelformset_factory(
|
2016-08-07 23:41:46 +02:00
|
|
|
Operation,
|
|
|
|
form = KPsulOperationForm,
|
2016-08-17 11:44:58 +02:00
|
|
|
can_delete = True,
|
|
|
|
extra = 0,
|
2016-08-07 23:41:46 +02:00
|
|
|
min_num = 1, validate_min = True)
|
2016-08-22 03:57:13 +02:00
|
|
|
|
|
|
|
# -----
|
|
|
|
# Settings forms
|
|
|
|
# -----
|
|
|
|
|
|
|
|
class SettingsForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Settings
|
|
|
|
fields = ['value_decimal', 'value_account', 'value_duration']
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
name = self.instance.name
|
|
|
|
value_decimal = self.cleaned_data.get('value_decimal')
|
|
|
|
value_account = self.cleaned_data.get('value_account')
|
|
|
|
value_duration = self.cleaned_data.get('value_duration')
|
|
|
|
|
|
|
|
type_decimal = ['SUBVENTION_COF', 'ADDCOST_AMOUNT', 'OVERDRAFT_AMOUNT']
|
|
|
|
type_account = ['ADDCOST_FOR']
|
|
|
|
type_duration = ['OVERDRAFT_DURATION', 'CANCEL_DURATION']
|
|
|
|
|
|
|
|
self.cleaned_data['name'] = name
|
|
|
|
if name in type_decimal:
|
|
|
|
if not value_decimal:
|
|
|
|
raise ValidationError('Renseignez une valeur décimale')
|
|
|
|
self.cleaned_data['value_account'] = None
|
|
|
|
self.cleaned_data['value_duration'] = None
|
|
|
|
elif name in type_account:
|
|
|
|
self.cleaned_data['value_decimal'] = None
|
|
|
|
self.cleaned_data['value_duration'] = None
|
|
|
|
elif name in type_duration:
|
|
|
|
if not value_duration:
|
|
|
|
raise ValidationError('Renseignez une durée')
|
|
|
|
self.cleaned_data['value_decimal'] = None
|
|
|
|
self.cleaned_data['value_account'] = None
|
2016-08-22 04:25:44 +02:00
|
|
|
cache.delete(name)
|
2016-08-22 03:57:13 +02:00
|
|
|
super(SettingsForm, self).clean()
|