Add "see_later" field in form

This commit is contained in:
Gabriel Augendre 2018-06-04 18:24:09 +02:00
parent 51be90baad
commit 651907682c
2 changed files with 60 additions and 43 deletions

View file

@ -6,10 +6,14 @@ 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', 'title', 'authors', 'editor', 'publication_year', fields = ['teacher', 'level', 'field', 'no_book', 'see_later', 'title', 'authors', 'editor', 'publication_year',
'isbn', 'price', 'previously_acquired', 'comments'] '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(
label='Voir à la rentrée', help_text="Notamment en cas de désaccord sur l'adoption ou non d'un manuel",
required=False, initial=False
)
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@ -20,7 +24,7 @@ class EditBookForm(forms.ModelForm):
class AddBookForm(EditBookForm): class AddBookForm(EditBookForm):
class Meta(EditBookForm.Meta): class Meta(EditBookForm.Meta):
fields = ['teacher', 'level', 'field', 'no_book', 'title', 'authors', 'editor', 'publication_year', fields = ['teacher', 'level', 'field', 'no_book', 'see_later', 'title', 'authors', 'editor', 'publication_year',
'isbn', 'price', 'previously_acquired', 'comments', 'add_another'] '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)

View file

@ -1,46 +1,59 @@
document.addEventListener("DOMContentLoaded", function (event) { document.addEventListener("DOMContentLoaded", function (event) {
var _selector = document.querySelector('#id_no_book'); var selectors = [
if (_selector === null) return; {
var data = { id: "#id_no_book",
title: document.querySelector('#id_title').value, value: "PAS DE LIVRE POUR CETTE CLASSE"
authors: document.querySelector('#id_authors').value, },
publicationYear: document.querySelector('#id_publication_year').value, {
isbn: document.querySelector('#id_isbn').value, id: "#id_see_later",
price: document.querySelector('#id_price').value, value: "VOIR À LA RENTRÉE"
editor: document.querySelector('#id_editor').value, },
}; ];
_selector.addEventListener('change', function (event) { selectors.forEach(function (selector, index, array) {
if (_selector.checked) { var _selector = document.querySelector(selector.id);
data = { if (_selector === null) return;
title: document.querySelector('#id_title').value, var data = {
authors: document.querySelector('#id_authors').value, title: document.querySelector('#id_title').value,
publicationYear: document.querySelector('#id_publication_year').value, authors: document.querySelector('#id_authors').value,
isbn: document.querySelector('#id_isbn').value, publicationYear: document.querySelector('#id_publication_year').value,
price: document.querySelector('#id_price').value, isbn: document.querySelector('#id_isbn').value,
editor: document.querySelector('#id_editor').value, price: document.querySelector('#id_price').value,
}; editor: document.querySelector('#id_editor').value,
document.querySelector('#id_title').value = "PAS DE LIVRE POUR CETTE CLASSE"; };
document.querySelector('#id_authors').value = "N/A"; _selector.addEventListener('change', function (event) {
document.querySelector('#id_publication_year').value = 1900; if (_selector.checked) {
document.querySelector('#id_isbn').value = "0000000000"; data = {
document.querySelector('#id_price').value = 0; title: document.querySelector('#id_title').value,
var editorValue = null; authors: document.querySelector('#id_authors').value,
for (var option of document.querySelector('#id_editor').children) { publicationYear: document.querySelector('#id_publication_year').value,
if (editorValue === null && option.value !== "") { isbn: document.querySelector('#id_isbn').value,
editorValue = option.value; price: document.querySelector('#id_price').value,
} editor: document.querySelector('#id_editor').value,
if (option.firstChild.data.toLowerCase().indexOf('autre') !== -1) { };
editorValue = option.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;
var editorValue = null;
for (var option of document.querySelector('#id_editor').children) {
if (editorValue === null && option.value !== "") {
editorValue = option.value;
}
if (option.firstChild.data.toLowerCase().indexOf('autre') !== -1) {
editorValue = option.value;
}
} }
document.querySelector('#id_editor').value = editorValue;
} else {
document.querySelector('#id_title').value = data.title;
document.querySelector('#id_authors').value = data.authors;
document.querySelector('#id_editor').value = data.editor;
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_editor').value = editorValue; });
} else {
document.querySelector('#id_title').value = data.title;
document.querySelector('#id_authors').value = data.authors;
document.querySelector('#id_editor').value = data.editor;
document.querySelector('#id_publication_year').value = data.publicationYear;
document.querySelector('#id_isbn').value = data.isbn;
document.querySelector('#id_price').value = data.price;
}
}); });
}); });