2020-02-18 20:30:26 +00:00
|
|
|
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
2019-07-08 11:59:31 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-02-18 20:30:26 +00:00
|
|
|
|
2020-02-08 19:39:37 +00:00
|
|
|
from dal import autocomplete
|
2020-02-18 20:14:29 +00:00
|
|
|
from django.contrib.auth.forms import UserCreationForm
|
2019-08-11 14:22:52 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django import forms
|
2019-07-08 11:59:31 +00:00
|
|
|
|
2019-08-14 16:47:46 +00:00
|
|
|
from .models import Profile, Club, Membership
|
|
|
|
|
|
|
|
from crispy_forms.helper import FormHelper
|
2020-02-18 20:14:29 +00:00
|
|
|
from crispy_forms.bootstrap import Div
|
2019-08-14 16:47:46 +00:00
|
|
|
from crispy_forms.layout import Layout
|
|
|
|
|
2019-07-08 11:59:31 +00:00
|
|
|
|
2020-02-02 14:42:39 +00:00
|
|
|
class SignUpForm(UserCreationForm):
|
|
|
|
class Meta:
|
|
|
|
model = User
|
2020-02-18 11:31:15 +00:00
|
|
|
fields = ['first_name', 'last_name', 'username', 'email']
|
|
|
|
|
2020-02-02 14:42:39 +00:00
|
|
|
|
2019-08-11 14:22:52 +00:00
|
|
|
class ProfileForm(forms.ModelForm):
|
2019-08-11 15:52:41 +00:00
|
|
|
"""
|
2020-02-02 14:42:39 +00:00
|
|
|
A form for the extras field provided by the :model:`member.Profile` model.
|
2019-08-11 15:52:41 +00:00
|
|
|
"""
|
2019-08-11 14:22:52 +00:00
|
|
|
class Meta:
|
|
|
|
model = Profile
|
|
|
|
fields = '__all__'
|
2019-08-11 15:39:05 +00:00
|
|
|
exclude = ['user']
|
2019-08-11 21:25:27 +00:00
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
|
2019-08-11 21:25:27 +00:00
|
|
|
class ClubForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Club
|
2020-02-18 11:31:15 +00:00
|
|
|
fields = '__all__'
|
|
|
|
|
2019-08-14 16:47:46 +00:00
|
|
|
|
|
|
|
class AddMembersForm(forms.Form):
|
|
|
|
class Meta:
|
2020-02-18 11:31:15 +00:00
|
|
|
fields = ('', )
|
|
|
|
|
2019-08-14 16:47:46 +00:00
|
|
|
|
|
|
|
class MembershipForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Membership
|
2020-02-18 11:31:15 +00:00
|
|
|
fields = ('user', 'roles', 'date_start')
|
2020-02-08 20:40:32 +00:00
|
|
|
# Le champ d'utilisateur est remplacé par un champ d'auto-complétion.
|
|
|
|
# Quand des lettres sont tapées, une requête est envoyée sur l'API d'auto-complétion
|
|
|
|
# et récupère les noms d'utilisateur valides
|
2020-02-08 19:39:37 +00:00
|
|
|
widgets = {
|
2020-02-18 11:31:15 +00:00
|
|
|
'user':
|
|
|
|
autocomplete.ModelSelect2(
|
|
|
|
url='member:user_autocomplete',
|
|
|
|
attrs={
|
|
|
|
'data-placeholder': 'Nom ...',
|
|
|
|
'data-minimum-input-length': 1,
|
|
|
|
},
|
|
|
|
),
|
2020-02-08 19:39:37 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 16:47:46 +00:00
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
MemberFormSet = forms.modelformset_factory(
|
|
|
|
Membership,
|
|
|
|
form=MembershipForm,
|
|
|
|
extra=2,
|
|
|
|
can_delete=True,
|
|
|
|
)
|
|
|
|
|
2019-08-14 16:47:46 +00:00
|
|
|
|
|
|
|
class FormSetHelper(FormHelper):
|
2020-02-18 11:31:15 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2019-08-14 16:47:46 +00:00
|
|
|
self.form_tag = False
|
|
|
|
self.form_method = 'POST'
|
2020-02-18 11:31:15 +00:00
|
|
|
self.form_class = 'form-inline'
|
2019-08-14 16:47:46 +00:00
|
|
|
# self.template = 'bootstrap/table_inline_formset.html'
|
|
|
|
self.layout = Layout(
|
|
|
|
Div(
|
2020-02-18 11:31:15 +00:00
|
|
|
Div('user', css_class='col-sm-2'),
|
|
|
|
Div('roles', css_class='col-sm-2'),
|
|
|
|
Div('date_start', css_class='col-sm-2'),
|
2019-08-14 16:47:46 +00:00
|
|
|
css_class="row formset-row",
|
2020-02-18 11:31:15 +00:00
|
|
|
))
|