From d98ac17eae5c72f255dc9784aaae47e283d2e7bd Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Mon, 18 Jun 2018 18:05:44 +0200 Subject: [PATCH] Default to no choice for previously acquired. Close #32 --- manuels/forms.py | 7 +++++++ manuels/models.py | 3 ++- manuels/static/manuels/no_book.js | 4 ++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/manuels/forms.py b/manuels/forms.py index e13bf89..3e66514 100644 --- a/manuels/forms.py +++ b/manuels/forms.py @@ -44,6 +44,13 @@ class EditBookForm(forms.ModelForm): ) ) + def clean_previously_acquired(self): + data = self.cleaned_data['previously_acquired'] + if data is None or data == '': + raise ValidationError('Vous devez choisir une valeur') + + return data + class AddBookForm(EditBookForm): class Meta(EditBookForm.Meta): diff --git a/manuels/models.py b/manuels/models.py index 9d892b0..6210ccd 100644 --- a/manuels/models.py +++ b/manuels/models.py @@ -164,6 +164,7 @@ class Book(BaseModel): ) price = models.FloatField('prix', validators=[positive_float_validator]) YES_NO_CHOICE = ( + (None, '------------'), (False, 'Non'), (True, 'Oui'), ) @@ -171,7 +172,7 @@ class Book(BaseModel): "manuel acquis précédemment par l'élève", choices=YES_NO_CHOICE, blank=False, - default=False, + default=None, ) done = models.BooleanField( 'Traité', diff --git a/manuels/static/manuels/no_book.js b/manuels/static/manuels/no_book.js index 675c1e6..9bf1925 100644 --- a/manuels/static/manuels/no_book.js +++ b/manuels/static/manuels/no_book.js @@ -19,6 +19,7 @@ document.addEventListener("DOMContentLoaded", function (event) { isbn: document.querySelector('#id_isbn').value, price: document.querySelector('#id_price').value, editor: document.querySelector('#id_editor').value, + previouslyAcquired: document.querySelector('#id_previously_acquired').value, }; _selector.addEventListener('change', function (event) { if (_selector.checked) { @@ -29,12 +30,14 @@ document.addEventListener("DOMContentLoaded", function (event) { isbn: document.querySelector('#id_isbn').value, price: document.querySelector('#id_price').value, editor: document.querySelector('#id_editor').value, + previouslyAcquired: document.querySelector('#id_previously_acquired').value, }; document.querySelector('#id_title').value = selector.value; document.querySelector('#id_authors').value = "N/A"; document.querySelector('#id_publication_year').value = 1900; document.querySelector('#id_isbn').value = "0000000000"; document.querySelector('#id_price').value = 0; + document.querySelector('#id_previously_acquired').value = "False"; var editorValue = null; for (var option of document.querySelector('#id_editor').children) { if (editorValue === null && option.value !== "") { @@ -52,6 +55,7 @@ document.addEventListener("DOMContentLoaded", function (event) { document.querySelector('#id_publication_year').value = data.publicationYear; document.querySelector('#id_isbn').value = data.isbn; document.querySelector('#id_price').value = data.price; + document.querySelector('#id_previously_acquired').value = data.previouslyAcquired; } }); });