Allow books and supplies editing + refactor. Close #1

This commit is contained in:
Gabriel Augendre 2018-05-31 19:11:01 +02:00
parent 8c54fce37e
commit 796d1874da
7 changed files with 83 additions and 23 deletions

View file

@ -3,19 +3,31 @@ from django import forms
from manuels.models import Book, SuppliesRequirement from manuels.models import Book, SuppliesRequirement
class AddBookForm(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',
'isbn', 'price', 'previously_acquired', 'comments']
no_book = forms.BooleanField(label='Pas de livre pour cette classe/matière', required=False, initial=False)
class AddBookForm(EditBookForm):
class Meta(EditBookForm.Meta):
fields = ['teacher', 'level', 'field', 'no_book', 'title', 'authors', 'editor', 'publication_year', fields = ['teacher', 'level', 'field', 'no_book', '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)
no_book = forms.BooleanField(label='Pas de livre pour cette classe/matière', required=False, initial=False)
class AddSuppliesForm(forms.ModelForm): class EditSuppliesForm(forms.ModelForm):
class Meta: class Meta:
model = SuppliesRequirement model = SuppliesRequirement
exclude = ['done'] exclude = ['done']
class AddSuppliesForm(forms.ModelForm):
class Meta(EditSuppliesForm.Meta):
pass
add_another = forms.BooleanField(label="Ajouter d'autres fournitures", required=False, initial=True) add_another = forms.BooleanField(label="Ajouter d'autres fournitures", required=False, initial=True)

View file

@ -154,6 +154,10 @@ class Book(BaseModel):
def __str__(self): def __str__(self):
return f'{self.title} ({self.authors}) - {self.isbn}' return f'{self.title} ({self.authors}) - {self.isbn}'
def get_absolute_url(self):
from django.urls import reverse
return reverse('edit_book', kwargs={'teacher_pk': str(self.teacher.pk), 'pk': str(self.pk)})
class SuppliesRequirement(BaseModel): class SuppliesRequirement(BaseModel):
class Meta: class Meta:

View file

@ -8,7 +8,7 @@
<div class="col-12"> <div class="col-12">
<h1>Bienvenue {{ teacher.full_name }}</h1> <h1>Bienvenue {{ teacher.full_name }}</h1>
<h2> <h2>
{% block title %}Ajouter {{ item }}{% endblock %} {% block title %}{{ verb }} {{ item }}{% endblock %}
<a href="{% url 'list_books' teacher.pk %}" class="btn btn-secondary">Retour à la liste des {{ item_plural }}</a> <a href="{% url 'list_books' teacher.pk %}" class="btn btn-secondary">Retour à la liste des {{ item_plural }}</a>
</h2> </h2>
{% if message_template %} {% if message_template %}

View file

@ -6,6 +6,10 @@
<title>Manuels - {% block title %}{% endblock %}</title> <title>Manuels - {% block title %}{% endblock %}</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script defer src="https://use.fontawesome.com/releases/v5.0.13/js/all.js"
integrity="sha384-xymdQtn1n3lH2wcu0qhcdaOpQwyoarkgLVxC/wZ5q7h9gHtxICrpcaSUfygqZGOe"
crossorigin="anonymous"></script>
</head> </head>
<body> <body>
<div class="container-fluid"> <div class="container-fluid">

View file

@ -31,7 +31,7 @@
<tr> <tr>
<th scope="col">#</th> <th scope="col">#</th>
<th scope="col">Classe</th> <th scope="col">Classe</th>
<th scope="col">Matière</th> <th scope="col">Discipline</th>
<th scope="col">Titre</th> <th scope="col">Titre</th>
<th scope="col">Auteurs</th> <th scope="col">Auteurs</th>
<th scope="col">Éditeur</th> <th scope="col">Éditeur</th>
@ -45,7 +45,7 @@
<tbody> <tbody>
{% for book in books %} {% for book in books %}
<tr> <tr>
<th scope="row">{{ forloop.counter }}</th> <th scope="row"><a href="{% url 'edit_book' teacher_pk=book.teacher.pk pk=book.pk %}"><i class="fas fa-edit"></i></a></th>
<td>{{ book.level }}</td> <td>{{ book.level }}</td>
<td>{{ book.field }}</td> <td>{{ book.field }}</td>
<td>{{ book.title }}</td> <td>{{ book.title }}</td>
@ -73,16 +73,16 @@
<tr> <tr>
<th scope="col">#</th> <th scope="col">#</th>
<th scope="col">Classe</th> <th scope="col">Classe</th>
<th scope="col">Matière</th> <th scope="col">Disciplines</th>
<th scope="col">Liste de fournitures</th> <th scope="col">Liste de fournitures</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for supply in supplies %} {% for supply in supplies %}
<tr> <tr>
<th scope="row">{{ forloop.counter }}</th> <th scope="row"><a href="{% url 'edit_supplies' teacher_pk=supply.teacher.pk pk=supply.pk %}"><i class="fas fa-edit"></i></a></th>
<td>{{ supply.level }}</td> <td>{{ supply.level }}</td>
<td>{{ supply.field }}</td> <td>{{ supply.fields }}</td>
<td>{{ supply.supplies|linebreaksbr }}</td> <td>{{ supply.supplies|linebreaksbr }}</td>
</tr> </tr>
{% endfor %} {% endfor %}

View file

@ -1,10 +1,13 @@
from django.urls import path from django.urls import path
from manuels.views import AddBookView, ListBooksView, clear_teacher_view, AddSuppliesView from manuels.views import AddBookView, ListBooksView, clear_teacher_view, AddSuppliesView, EditBookView,\
EditSuppliesView
urlpatterns = [ urlpatterns = [
path('teacher/<uuid:pk>/add_book', AddBookView.as_view(), name='add_book'), path('teacher/<uuid:pk>/add_book', AddBookView.as_view(), name='add_book'),
path('teacher/<uuid:pk>/add_supplies', AddSuppliesView.as_view(), name='add_supplies'), path('teacher/<uuid:pk>/add_supplies', AddSuppliesView.as_view(), name='add_supplies'),
path('teacher/<uuid:pk>', ListBooksView.as_view(), name='list_books'), path('teacher/<uuid:pk>', ListBooksView.as_view(), name='list_books'),
path('teacher/<uuid:teacher_pk>/book/<int:pk>', EditBookView.as_view(), name='edit_book'),
path('teacher/<uuid:teacher_pk>/supplies/<int:pk>', EditSuppliesView.as_view(), name='edit_supplies'),
path('clear', clear_teacher_view, name='clear_teacher'), path('clear', clear_teacher_view, name='clear_teacher'),
] ]

View file

@ -1,9 +1,10 @@
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse from django.urls import reverse
from django.views.generic import CreateView, ListView from django.views.generic import CreateView, ListView, UpdateView
from manuels.forms import AddBookForm, AddSuppliesForm from manuels.forms import AddBookForm, AddSuppliesForm, EditBookForm, EditSuppliesForm
from manuels.models import Teacher, Book, SuppliesRequirement from manuels.models import Teacher, Book, SuppliesRequirement
import logging import logging
@ -31,9 +32,10 @@ class HomePageView(CreateView):
class BaseTeacherView: class BaseTeacherView:
teacher = None teacher = None
teacher_field = 'pk'
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
self.teacher = get_object_or_404(Teacher, pk=self.kwargs['pk']) self.teacher = get_object_or_404(Teacher, pk=self.kwargs[self.teacher_field])
request.session['teacher_pk'] = str(self.teacher.pk) request.session['teacher_pk'] = str(self.teacher.pk)
return super().dispatch(request, *args, **kwargs) return super().dispatch(request, *args, **kwargs)
@ -58,18 +60,21 @@ class ListBooksView(BaseTeacherView, ListView):
return Book.objects.filter(teacher=self.teacher) return Book.objects.filter(teacher=self.teacher)
class AddItemView(BaseTeacherView, CreateView): class ItemView(BaseTeacherView):
add_another = False add_another = False
item_text = None item_text = None
item_text_plural = None item_text_plural = None
success_target = None success_target = None
message_template = None message_template = None
template_name = 'manuels/add_item.html'
verb = None
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['item'] = self.item_text context['item'] = self.item_text
context['item_plural'] = self.item_text_plural context['item_plural'] = self.item_text_plural
context['message_template'] = self.message_template context['message_template'] = self.message_template
context['verb'] = self.verb
return context return context
def get_initial(self): def get_initial(self):
@ -82,9 +87,9 @@ class AddItemView(BaseTeacherView, CreateView):
form.fields['teacher'].queryset = Teacher.objects.filter(pk=self.teacher.pk) form.fields['teacher'].queryset = Teacher.objects.filter(pk=self.teacher.pk)
return form return form
def form_valid(self, form):
self.add_another = form.cleaned_data['add_another'] class AddItemView(ItemView, CreateView):
return super().form_valid(form) verb = 'Ajouter'
def get_success_url(self): def get_success_url(self):
messages.success(self.request, f'"{self.object}" a été ajouté.') messages.success(self.request, f'"{self.object}" a été ajouté.')
@ -93,26 +98,58 @@ class AddItemView(BaseTeacherView, CreateView):
else: else:
return reverse('list_books', args=[str(self.teacher.pk)]) return reverse('list_books', args=[str(self.teacher.pk)])
def form_valid(self, form):
self.add_another = form.cleaned_data['add_another']
return super().form_valid(form)
class AddBookView(AddItemView):
class BookView:
model = Book model = Book
template_name = 'manuels/add_item.html'
form_class = AddBookForm
success_target = 'add_book' success_target = 'add_book'
item_text = 'un livre' item_text = 'un livre'
item_text_plural = 'livres' item_text_plural = 'livres'
class AddSuppliesView(AddItemView): class AddBookView(BookView, AddItemView):
form_class = AddBookForm
class SuppliesView:
model = SuppliesRequirement model = SuppliesRequirement
template_name = 'manuels/add_item.html'
form_class = AddSuppliesForm
success_target = 'add_supplies' success_target = 'add_supplies'
item_text = 'des fournitures' item_text = 'des fournitures'
item_text_plural = 'fournitures' item_text_plural = 'fournitures'
message_template = 'manuels/supplies_message.html' message_template = 'manuels/supplies_message.html'
class AddSuppliesView(SuppliesView, AddItemView):
form_class = AddSuppliesForm
class EditItemView(ItemView, UpdateView):
template_name = 'manuels/add_item.html'
teacher_field = 'teacher_pk'
item_text = None
item_text_plural = None
message_template = None
verb = 'Modifier'
def get_queryset(self):
return self.model.objects.filter(teacher=self.teacher)
def get_success_url(self):
messages.success(self.request, f'"{self.object}" a été modifié.')
return reverse('list_books', args=[str(self.teacher.pk)])
class EditBookView(BookView, EditItemView):
form_class = EditBookForm
class EditSuppliesView(SuppliesView, EditItemView):
form_class = EditSuppliesForm
def clear_teacher_view(request): def clear_teacher_view(request):
if request.session['teacher_pk']: if request.session['teacher_pk']:
del request.session['teacher_pk'] del request.session['teacher_pk']