From f4881ecc771d91cc952b9e18e5ad594cab21105c Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Thu, 7 Jun 2018 07:46:47 +0200 Subject: [PATCH] Add "other editor" field with custom validation --- manuels/forms.py | 26 ++++++++++++++++--- manuels/migrations/0024_book_other_editor.py | 18 +++++++++++++ manuels/migrations/0025_auto_20180607_0746.py | 18 +++++++++++++ manuels/models.py | 5 ++-- manuels/static/manuels/no_book.js | 23 ++++++++++++++++ 5 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 manuels/migrations/0024_book_other_editor.py create mode 100644 manuels/migrations/0025_auto_20180607_0746.py diff --git a/manuels/forms.py b/manuels/forms.py index a2236fe..ee6e914 100644 --- a/manuels/forms.py +++ b/manuels/forms.py @@ -1,4 +1,5 @@ from django import forms +from django.core.exceptions import ValidationError from manuels.models import Book, SuppliesRequirement @@ -6,8 +7,8 @@ from manuels.models import Book, SuppliesRequirement class EditBookForm(forms.ModelForm): class Meta: model = Book - fields = ['teacher', 'level', 'field', 'no_book', 'see_later', 'title', 'authors', 'editor', 'publication_year', - 'isbn', 'price', 'previously_acquired', 'comments'] + fields = ['teacher', 'level', 'field', 'no_book', 'see_later', 'title', 'authors', 'editor', 'other_editor', + 'publication_year', 'isbn', 'price', 'previously_acquired', 'comments'] no_book = forms.BooleanField(label='Pas de livre pour cette classe/matière', required=False, initial=False) see_later = forms.BooleanField( @@ -21,11 +22,28 @@ class EditBookForm(forms.ModelForm): self.fields['authors'].widget = forms.TextInput() self.fields['comments'].widget.attrs.update(rows=3) + def clean(self): + 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']): + self.add_error( + 'other_editor', + ValidationError( + "Vous devez préciser l'éditeur si vous n'en choisissez pas un parmi la liste.", + code='missing' + ) + ) + class AddBookForm(EditBookForm): class Meta(EditBookForm.Meta): - fields = ['teacher', 'level', 'field', 'no_book', 'see_later', 'title', 'authors', 'editor', 'publication_year', - 'isbn', 'price', 'previously_acquired', 'comments', 'add_another'] + fields = ['teacher', 'level', 'field', 'no_book', 'see_later', 'title', 'authors', 'editor', 'other_editor', + 'publication_year', 'isbn', 'price', 'previously_acquired', 'comments', 'add_another'] add_another = forms.BooleanField(label='Ajouter un autre livre', required=False, initial=True) diff --git a/manuels/migrations/0024_book_other_editor.py b/manuels/migrations/0024_book_other_editor.py new file mode 100644 index 0000000..bfb1372 --- /dev/null +++ b/manuels/migrations/0024_book_other_editor.py @@ -0,0 +1,18 @@ +# Generated by Django 2.0.5 on 2018-06-07 05:22 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('manuels', '0023_auto_20180604_1827'), + ] + + operations = [ + migrations.AddField( + model_name='book', + name='other_editor', + field=models.CharField(blank=True, max_length=100, verbose_name='préciser'), + ), + ] diff --git a/manuels/migrations/0025_auto_20180607_0746.py b/manuels/migrations/0025_auto_20180607_0746.py new file mode 100644 index 0000000..4fbe86d --- /dev/null +++ b/manuels/migrations/0025_auto_20180607_0746.py @@ -0,0 +1,18 @@ +# Generated by Django 2.0.5 on 2018-06-07 05:46 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('manuels', '0024_book_other_editor'), + ] + + operations = [ + migrations.AlterField( + model_name='book', + name='comments', + field=models.TextField(blank=True, help_text='Ce message sera visible par la documentaliste.', verbose_name='commentaires'), + ), + ] diff --git a/manuels/models.py b/manuels/models.py index c5380d8..cbae269 100644 --- a/manuels/models.py +++ b/manuels/models.py @@ -141,7 +141,6 @@ def positive_float_validator(value): raise ValidationError("%(value)s doit être un nombre positif") - class Book(BaseModel): class Meta: verbose_name = 'livre' @@ -153,6 +152,7 @@ class Book(BaseModel): title = models.TextField('titre') authors = models.TextField('auteurs') editor = models.ForeignKey(verbose_name='éditeur', to=Editor, on_delete=models.PROTECT, null=True) + other_editor = models.CharField(verbose_name='préciser', max_length=100, blank=True) publication_year = models.PositiveIntegerField('année de publication') isbn = models.CharField( 'ISBN/EAN', @@ -180,8 +180,7 @@ class Book(BaseModel): comments = models.TextField( 'commentaires', blank=True, - help_text="Ce message sera visible par la documentaliste. Vous pouvez l'utiliser par exemple si vous souhaitez " - "saisir un éditeur qui n'est pas proposé." + help_text="Ce message sera visible par la documentaliste." ) @property diff --git a/manuels/static/manuels/no_book.js b/manuels/static/manuels/no_book.js index 58b211f..0fc7de8 100644 --- a/manuels/static/manuels/no_book.js +++ b/manuels/static/manuels/no_book.js @@ -56,4 +56,27 @@ document.addEventListener("DOMContentLoaded", function (event) { }); }); + + var editor = document.querySelector('#id_editor'); + var otherEditor = document.querySelector('#id_other_editor').parentElement; + + if (editor.options[editor.selectedIndex].text.toLowerCase().indexOf('autre') !== -1) { + otherEditor.style.display = 'block'; + } + else { + otherEditor.style.display = 'none'; + } + + editor.addEventListener('change', function(event) { + console.log(editor.options); + console.log(editor.selectedIndex); + console.log(editor.options[editor.selectedIndex].text); + if (editor.options[editor.selectedIndex].text.toLowerCase().indexOf('autre') !== -1) { + otherEditor.style.display = 'block'; + } + else { + otherEditor.style.display = 'none'; + } + }); + });