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 Meta:
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']
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):
super().__init__(*args, **kwargs)
@ -20,7 +24,7 @@ class EditBookForm(forms.ModelForm):
class AddBookForm(EditBookForm):
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']
add_another = forms.BooleanField(label='Ajouter un autre livre', required=False, initial=True)

View file

@ -1,46 +1,59 @@
document.addEventListener("DOMContentLoaded", function (event) {
var _selector = document.querySelector('#id_no_book');
if (_selector === null) return;
var data = {
title: document.querySelector('#id_title').value,
authors: document.querySelector('#id_authors').value,
publicationYear: document.querySelector('#id_publication_year').value,
isbn: document.querySelector('#id_isbn').value,
price: document.querySelector('#id_price').value,
editor: document.querySelector('#id_editor').value,
};
_selector.addEventListener('change', function (event) {
if (_selector.checked) {
data = {
title: document.querySelector('#id_title').value,
authors: document.querySelector('#id_authors').value,
publicationYear: document.querySelector('#id_publication_year').value,
isbn: document.querySelector('#id_isbn').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";
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;
var selectors = [
{
id: "#id_no_book",
value: "PAS DE LIVRE POUR CETTE CLASSE"
},
{
id: "#id_see_later",
value: "VOIR À LA RENTRÉE"
},
];
selectors.forEach(function (selector, index, array) {
var _selector = document.querySelector(selector.id);
if (_selector === null) return;
var data = {
title: document.querySelector('#id_title').value,
authors: document.querySelector('#id_authors').value,
publicationYear: document.querySelector('#id_publication_year').value,
isbn: document.querySelector('#id_isbn').value,
price: document.querySelector('#id_price').value,
editor: document.querySelector('#id_editor').value,
};
_selector.addEventListener('change', function (event) {
if (_selector.checked) {
data = {
title: document.querySelector('#id_title').value,
authors: document.querySelector('#id_authors').value,
publicationYear: document.querySelector('#id_publication_year').value,
isbn: document.querySelector('#id_isbn').value,
price: document.querySelector('#id_price').value,
editor: document.querySelector('#id_editor').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;
}
});
});
});