From 95e509c4c11f10561d1e3550a77d839b5bdfd837 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Mon, 18 Jun 2018 18:26:33 +0200 Subject: [PATCH] Allow multiple levels for same supplies. Close #31 --- manuels/forms.py | 11 +++++++++-- manuels/views.py | 19 ++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/manuels/forms.py b/manuels/forms.py index 3e66514..16549ce 100644 --- a/manuels/forms.py +++ b/manuels/forms.py @@ -76,11 +76,18 @@ class EditSuppliesForm(forms.ModelForm): self.fields['fields'].widget.attrs.update(rows=3) self.fields['supplies'].widget.attrs.update(rows=3) self.fields['teacher'].widget.attrs.update({'class': 'custom-select'}) - self.fields['level'].widget.attrs.update({'class': 'custom-select'}) + if 'level' in self.fields: + self.fields['level'].widget.attrs.update({'class': 'custom-select'}) class AddSuppliesForm(EditSuppliesForm): class Meta(EditSuppliesForm.Meta): - pass + fields = ['teacher', 'levels', 'fields', 'supplies'] add_another = forms.BooleanField(label="Ajouter d'autres fournitures", required=False, initial=True) + levels = forms.ModelMultipleChoiceField( + queryset=Level.objects.all(), + label='Classes', + required=True, + help_text='Maintenez la touche Ctrl (ou Cmd) enfoncée pour en sélectionner plusieurs.' + ) diff --git a/manuels/views.py b/manuels/views.py index f33ca52..1bbfaec 100644 --- a/manuels/views.py +++ b/manuels/views.py @@ -110,9 +110,7 @@ class AddItemView(ItemView, CreateView): def form_valid(self, form): self.add_another = form.cleaned_data['add_another'] - response = super().form_valid(form) - messages.success(self.request, f'"{self.object}" a été ajouté.') - return response + return HttpResponseRedirect(self.get_success_url()) class BookView: @@ -127,7 +125,6 @@ class AddBookView(BookView, AddItemView): template_name = 'manuels/add_book.html' def form_valid(self, form: AddBookForm): - self.add_another = form.cleaned_data['add_another'] for level in form.cleaned_data['levels']: book = Book.objects.create( teacher=form.cleaned_data['teacher'], @@ -145,7 +142,7 @@ class AddBookView(BookView, AddItemView): ) messages.success(self.request, f'"{book}" a été ajouté.') - return HttpResponseRedirect(self.get_success_url()) + return super().form_valid(form) class SuppliesView: @@ -160,6 +157,18 @@ class AddSuppliesView(SuppliesView, AddItemView): message_template = 'manuels/supplies_message.html' template_name = 'manuels/add_supplies.html' + def form_valid(self, form: AddBookForm): + for level in form.cleaned_data['levels']: + supplies = SuppliesRequirement.objects.create( + teacher=form.cleaned_data['teacher'], + level=level, + fields=form.cleaned_data['fields'], + supplies=form.cleaned_data['supplies'], + ) + messages.success(self.request, f'"{supplies}" a été ajouté.') + + return super().form_valid(form) + class EditItemView(ItemView, UpdateView): teacher_field = 'teacher_pk'