2018-05-21 22:23:53 +02:00
|
|
|
from django import forms
|
2018-06-07 07:46:47 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2018-05-21 22:23:53 +02:00
|
|
|
|
2021-07-10 12:11:58 +02:00
|
|
|
from manuels.models import Book, Level, SuppliesRequirement
|
2018-05-21 22:23:53 +02:00
|
|
|
|
|
|
|
|
2018-05-31 19:11:01 +02:00
|
|
|
class EditBookForm(forms.ModelForm):
|
2018-05-21 22:23:53 +02:00
|
|
|
class Meta:
|
|
|
|
model = Book
|
2021-07-10 12:11:58 +02:00
|
|
|
fields = [
|
|
|
|
"teacher",
|
|
|
|
"level",
|
|
|
|
"field",
|
|
|
|
"no_book",
|
|
|
|
"see_later",
|
|
|
|
"title",
|
|
|
|
"authors",
|
|
|
|
"editor",
|
|
|
|
"other_editor",
|
|
|
|
"publication_year",
|
|
|
|
"isbn",
|
|
|
|
"price",
|
|
|
|
"previously_acquired",
|
|
|
|
"comments",
|
|
|
|
"consumable",
|
|
|
|
]
|
|
|
|
|
|
|
|
no_book = forms.BooleanField(
|
|
|
|
label="Pas de livre pour cette classe/matière", required=False, initial=False
|
|
|
|
)
|
2018-06-04 18:24:09 +02:00
|
|
|
see_later = forms.BooleanField(
|
2021-07-10 12:11:58 +02:00
|
|
|
label="Voir à la rentrée",
|
|
|
|
help_text="Notamment en cas de désaccord sur l'adoption ou non d'un manuel",
|
|
|
|
required=False,
|
|
|
|
initial=False,
|
2018-06-04 18:24:09 +02:00
|
|
|
)
|
2018-05-31 19:11:01 +02:00
|
|
|
|
2018-06-02 09:58:57 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2021-07-10 12:11:58 +02:00
|
|
|
self.fields["title"].widget = forms.TextInput()
|
|
|
|
self.fields["authors"].widget = forms.TextInput()
|
|
|
|
self.fields["comments"].widget.attrs.update(rows=3)
|
|
|
|
self.fields["teacher"].widget.attrs.update({"class": "custom-select"})
|
|
|
|
self.fields["editor"].widget.attrs.update({"class": "custom-select"})
|
|
|
|
self.fields["previously_acquired"].widget.attrs.update(
|
|
|
|
{"class": "custom-select"}
|
|
|
|
)
|
|
|
|
self.fields["consumable"].widget.attrs.update({"class": "custom-select"})
|
|
|
|
if "level" in self.fields:
|
|
|
|
self.fields["level"].widget.attrs.update({"class": "custom-select"})
|
2018-06-02 09:58:57 +02:00
|
|
|
|
2018-06-07 07:46:47 +02:00
|
|
|
def clean(self):
|
2021-07-10 12:11:58 +02:00
|
|
|
editor = self.cleaned_data["editor"]
|
|
|
|
other_editor = self.cleaned_data["other_editor"]
|
|
|
|
title = self.cleaned_data["title"]
|
|
|
|
|
|
|
|
if (
|
|
|
|
editor
|
|
|
|
and "autre" in editor.name.lower()
|
|
|
|
and not other_editor
|
|
|
|
and title not in ["PAS DE LIVRE POUR CETTE CLASSE", "VOIR À LA RENTRÉE"]
|
|
|
|
):
|
2018-06-07 07:46:47 +02:00
|
|
|
self.add_error(
|
2021-07-10 12:11:58 +02:00
|
|
|
"other_editor",
|
2018-06-07 07:46:47 +02:00
|
|
|
ValidationError(
|
|
|
|
"Vous devez préciser l'éditeur si vous n'en choisissez pas un parmi la liste.",
|
2021-07-10 12:11:58 +02:00
|
|
|
code="missing",
|
|
|
|
),
|
2018-06-07 07:46:47 +02:00
|
|
|
)
|
|
|
|
|
2018-06-18 18:05:44 +02:00
|
|
|
def clean_previously_acquired(self):
|
2021-07-10 12:11:58 +02:00
|
|
|
data = self.cleaned_data["previously_acquired"]
|
|
|
|
if data is None or data == "":
|
|
|
|
raise ValidationError("Vous devez choisir une valeur")
|
2018-06-18 18:05:44 +02:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
2018-05-31 19:11:01 +02:00
|
|
|
|
|
|
|
class AddBookForm(EditBookForm):
|
|
|
|
class Meta(EditBookForm.Meta):
|
2021-07-10 12:11:58 +02:00
|
|
|
fields = [
|
|
|
|
"teacher",
|
|
|
|
"levels",
|
|
|
|
"field",
|
|
|
|
"no_book",
|
|
|
|
"see_later",
|
|
|
|
"title",
|
|
|
|
"authors",
|
|
|
|
"editor",
|
|
|
|
"other_editor",
|
|
|
|
"publication_year",
|
|
|
|
"isbn",
|
|
|
|
"price",
|
|
|
|
"previously_acquired",
|
|
|
|
"comments",
|
|
|
|
"add_another",
|
|
|
|
"consumable",
|
|
|
|
]
|
|
|
|
|
|
|
|
add_another = forms.BooleanField(
|
|
|
|
label="Ajouter un autre livre", required=False, initial=True
|
|
|
|
)
|
2018-06-10 20:03:00 +02:00
|
|
|
levels = forms.ModelMultipleChoiceField(
|
|
|
|
queryset=Level.objects.all(),
|
2021-07-10 12:11:58 +02:00
|
|
|
label="Classes",
|
2018-06-10 20:14:17 +02:00
|
|
|
required=True,
|
2021-07-10 12:11:58 +02:00
|
|
|
help_text="Maintenez la touche Ctrl (ou Cmd) enfoncée pour en sélectionner plusieurs.",
|
2018-06-10 20:03:00 +02:00
|
|
|
)
|
2018-05-22 10:13:51 +02:00
|
|
|
|
|
|
|
|
2018-05-31 19:11:01 +02:00
|
|
|
class EditSuppliesForm(forms.ModelForm):
|
2018-05-22 10:13:51 +02:00
|
|
|
class Meta:
|
|
|
|
model = SuppliesRequirement
|
2021-07-10 12:11:58 +02:00
|
|
|
fields = ["teacher", "level", "field", "supplies"]
|
2018-06-02 16:52:17 +02:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2021-07-10 12:11:58 +02:00
|
|
|
self.fields["supplies"].widget.attrs.update(rows=3)
|
|
|
|
self.fields["teacher"].widget.attrs.update({"class": "custom-select"})
|
|
|
|
if "level" in self.fields:
|
|
|
|
self.fields["level"].widget.attrs.update({"class": "custom-select"})
|
2018-05-22 10:13:51 +02:00
|
|
|
|
2018-05-31 19:11:01 +02:00
|
|
|
|
2018-06-02 16:34:15 +02:00
|
|
|
class AddSuppliesForm(EditSuppliesForm):
|
2018-05-31 19:11:01 +02:00
|
|
|
class Meta(EditSuppliesForm.Meta):
|
2021-07-10 12:11:58 +02:00
|
|
|
fields = ["teacher", "levels", "field", "supplies"]
|
2018-05-31 19:11:01 +02:00
|
|
|
|
2021-07-10 12:11:58 +02:00
|
|
|
add_another = forms.BooleanField(
|
|
|
|
label="Ajouter d'autres fournitures", required=False, initial=True
|
|
|
|
)
|
2018-06-18 18:26:33 +02:00
|
|
|
levels = forms.ModelMultipleChoiceField(
|
|
|
|
queryset=Level.objects.all(),
|
2021-07-10 12:11:58 +02:00
|
|
|
label="Classes",
|
2018-06-18 18:26:33 +02:00
|
|
|
required=True,
|
2021-07-10 12:11:58 +02:00
|
|
|
help_text="Maintenez la touche Ctrl (ou Cmd) enfoncée pour en sélectionner plusieurs.",
|
2018-06-18 18:26:33 +02:00
|
|
|
)
|