Add "other editor" field with custom validation
This commit is contained in:
parent
1c01e4f7dc
commit
f4881ecc77
5 changed files with 83 additions and 7 deletions
|
@ -1,4 +1,5 @@
|
||||||
from django import forms
|
from django import forms
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
|
|
||||||
from manuels.models import Book, SuppliesRequirement
|
from manuels.models import Book, SuppliesRequirement
|
||||||
|
|
||||||
|
@ -6,8 +7,8 @@ from manuels.models import Book, SuppliesRequirement
|
||||||
class EditBookForm(forms.ModelForm):
|
class EditBookForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Book
|
model = Book
|
||||||
fields = ['teacher', 'level', 'field', 'no_book', 'see_later', 'title', 'authors', 'editor', 'publication_year',
|
fields = ['teacher', 'level', 'field', 'no_book', 'see_later', 'title', 'authors', 'editor', 'other_editor',
|
||||||
'isbn', 'price', 'previously_acquired', 'comments']
|
'publication_year', 'isbn', 'price', 'previously_acquired', 'comments']
|
||||||
|
|
||||||
no_book = forms.BooleanField(label='Pas de livre pour cette classe/matière', required=False, initial=False)
|
no_book = forms.BooleanField(label='Pas de livre pour cette classe/matière', required=False, initial=False)
|
||||||
see_later = forms.BooleanField(
|
see_later = forms.BooleanField(
|
||||||
|
@ -21,11 +22,28 @@ class EditBookForm(forms.ModelForm):
|
||||||
self.fields['authors'].widget = forms.TextInput()
|
self.fields['authors'].widget = forms.TextInput()
|
||||||
self.fields['comments'].widget.attrs.update(rows=3)
|
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 AddBookForm(EditBookForm):
|
||||||
class Meta(EditBookForm.Meta):
|
class Meta(EditBookForm.Meta):
|
||||||
fields = ['teacher', 'level', 'field', 'no_book', 'see_later', 'title', 'authors', 'editor', 'publication_year',
|
fields = ['teacher', 'level', 'field', 'no_book', 'see_later', 'title', 'authors', 'editor', 'other_editor',
|
||||||
'isbn', 'price', 'previously_acquired', 'comments', 'add_another']
|
'publication_year', 'isbn', 'price', 'previously_acquired', 'comments', 'add_another']
|
||||||
|
|
||||||
add_another = forms.BooleanField(label='Ajouter un autre livre', required=False, initial=True)
|
add_another = forms.BooleanField(label='Ajouter un autre livre', required=False, initial=True)
|
||||||
|
|
||||||
|
|
18
manuels/migrations/0024_book_other_editor.py
Normal file
18
manuels/migrations/0024_book_other_editor.py
Normal 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'),
|
||||||
|
),
|
||||||
|
]
|
18
manuels/migrations/0025_auto_20180607_0746.py
Normal file
18
manuels/migrations/0025_auto_20180607_0746.py
Normal 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'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -141,7 +141,6 @@ def positive_float_validator(value):
|
||||||
raise ValidationError("%(value)s doit être un nombre positif")
|
raise ValidationError("%(value)s doit être un nombre positif")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Book(BaseModel):
|
class Book(BaseModel):
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = 'livre'
|
verbose_name = 'livre'
|
||||||
|
@ -153,6 +152,7 @@ class Book(BaseModel):
|
||||||
title = models.TextField('titre')
|
title = models.TextField('titre')
|
||||||
authors = models.TextField('auteurs')
|
authors = models.TextField('auteurs')
|
||||||
editor = models.ForeignKey(verbose_name='éditeur', to=Editor, on_delete=models.PROTECT, null=True)
|
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')
|
publication_year = models.PositiveIntegerField('année de publication')
|
||||||
isbn = models.CharField(
|
isbn = models.CharField(
|
||||||
'ISBN/EAN',
|
'ISBN/EAN',
|
||||||
|
@ -180,8 +180,7 @@ class Book(BaseModel):
|
||||||
comments = models.TextField(
|
comments = models.TextField(
|
||||||
'commentaires',
|
'commentaires',
|
||||||
blank=True,
|
blank=True,
|
||||||
help_text="Ce message sera visible par la documentaliste. Vous pouvez l'utiliser par exemple si vous souhaitez "
|
help_text="Ce message sera visible par la documentaliste."
|
||||||
"saisir un éditeur qui n'est pas proposé."
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -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';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue