Allow multiple levels for same supplies. Close #31

This commit is contained in:
Gabriel Augendre 2018-06-18 18:26:33 +02:00
parent 712153f425
commit 95e509c4c1
2 changed files with 23 additions and 7 deletions

View file

@ -76,11 +76,18 @@ class EditSuppliesForm(forms.ModelForm):
self.fields['fields'].widget.attrs.update(rows=3) self.fields['fields'].widget.attrs.update(rows=3)
self.fields['supplies'].widget.attrs.update(rows=3) self.fields['supplies'].widget.attrs.update(rows=3)
self.fields['teacher'].widget.attrs.update({'class': 'custom-select'}) self.fields['teacher'].widget.attrs.update({'class': 'custom-select'})
if 'level' in self.fields:
self.fields['level'].widget.attrs.update({'class': 'custom-select'}) self.fields['level'].widget.attrs.update({'class': 'custom-select'})
class AddSuppliesForm(EditSuppliesForm): class AddSuppliesForm(EditSuppliesForm):
class Meta(EditSuppliesForm.Meta): class Meta(EditSuppliesForm.Meta):
pass fields = ['teacher', 'levels', 'fields', 'supplies']
add_another = forms.BooleanField(label="Ajouter d'autres fournitures", required=False, initial=True) 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.'
)

View file

@ -110,9 +110,7 @@ class AddItemView(ItemView, CreateView):
def form_valid(self, form): def form_valid(self, form):
self.add_another = form.cleaned_data['add_another'] self.add_another = form.cleaned_data['add_another']
response = super().form_valid(form) return HttpResponseRedirect(self.get_success_url())
messages.success(self.request, f'"{self.object}" a été ajouté.')
return response
class BookView: class BookView:
@ -127,7 +125,6 @@ class AddBookView(BookView, AddItemView):
template_name = 'manuels/add_book.html' template_name = 'manuels/add_book.html'
def form_valid(self, form: AddBookForm): def form_valid(self, form: AddBookForm):
self.add_another = form.cleaned_data['add_another']
for level in form.cleaned_data['levels']: for level in form.cleaned_data['levels']:
book = Book.objects.create( book = Book.objects.create(
teacher=form.cleaned_data['teacher'], teacher=form.cleaned_data['teacher'],
@ -145,7 +142,7 @@ class AddBookView(BookView, AddItemView):
) )
messages.success(self.request, f'"{book}" a été ajouté.') messages.success(self.request, f'"{book}" a été ajouté.')
return HttpResponseRedirect(self.get_success_url()) return super().form_valid(form)
class SuppliesView: class SuppliesView:
@ -160,6 +157,18 @@ class AddSuppliesView(SuppliesView, AddItemView):
message_template = 'manuels/supplies_message.html' message_template = 'manuels/supplies_message.html'
template_name = 'manuels/add_supplies.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): class EditItemView(ItemView, UpdateView):
teacher_field = 'teacher_pk' teacher_field = 'teacher_pk'