Add "other editor" field with custom validation

This commit is contained in:
Gabriel Augendre 2018-06-07 07:46:47 +02:00
parent 1c01e4f7dc
commit f4881ecc77
5 changed files with 83 additions and 7 deletions

View file

@ -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)

View file

@ -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'),
),
]

View file

@ -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'),
),
]

View file

@ -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

View file

@ -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';
}
});
});