manuels-scolaires/manuels/views.py

183 lines
5.1 KiB
Python
Raw Normal View History

2018-05-21 23:12:35 +02:00
from django.contrib import messages
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.shortcuts import get_object_or_404, redirect
2018-05-21 23:12:35 +02:00
from django.urls import reverse
2018-06-02 17:24:29 +02:00
from django.views.generic import CreateView, ListView, UpdateView, DeleteView
2018-05-21 19:28:36 +02:00
from manuels.forms import AddBookForm, AddSuppliesForm, EditBookForm, EditSuppliesForm
from manuels.models import Teacher, Book, SuppliesRequirement
2018-05-21 21:03:08 +02:00
2018-05-23 15:36:18 +02:00
import logging
logger = logging.getLogger(__name__)
2018-05-21 21:03:08 +02:00
class HomePageView(CreateView):
model = Teacher
2018-05-22 00:45:54 +02:00
fields = '__all__'
2018-05-21 21:03:08 +02:00
template_name = 'manuels/home_page.html'
def get(self, request, *args, **kwargs):
teacher_pk = request.session.get('teacher_pk')
if teacher_pk:
return redirect('list_books', pk=teacher_pk)
return super().get(request, *args, **kwargs)
2018-05-22 00:45:54 +02:00
def form_valid(self, form):
response = super().form_valid(form)
2018-05-22 10:24:21 +02:00
self.object.send_link(self.request)
2018-05-22 00:45:54 +02:00
return response
2018-05-21 23:12:35 +02:00
class BaseTeacherView:
2018-05-21 22:23:53 +02:00
teacher = None
teacher_field = 'pk'
2018-05-21 22:23:53 +02:00
def dispatch(self, request, *args, **kwargs):
self.teacher = get_object_or_404(Teacher, pk=self.kwargs[self.teacher_field])
request.session['teacher_pk'] = str(self.teacher.pk)
2018-05-21 22:23:53 +02:00
return super().dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data()
2018-05-21 22:23:53 +02:00
context['teacher'] = self.teacher
return context
2018-05-21 22:23:53 +02:00
2018-05-21 23:12:35 +02:00
class ListBooksView(BaseTeacherView, ListView):
model = Book
template_name = 'manuels/list_books_supplies.html'
2018-05-21 23:12:35 +02:00
context_object_name = 'books'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['supplies'] = SuppliesRequirement.objects.filter(teacher=self.teacher)
return context
2018-05-21 23:12:35 +02:00
def get_queryset(self):
return Book.objects.filter(teacher=self.teacher)
class ItemView(BaseTeacherView):
2018-05-21 23:12:35 +02:00
add_another = False
item_text = None
2018-05-24 00:19:32 +02:00
item_text_plural = None
success_target = None
2018-05-30 18:14:53 +02:00
message_template = None
template_name = 'manuels/add_item.html'
verb = None
2018-06-02 17:24:29 +02:00
button_class = 'primary'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['item'] = self.item_text
2018-05-24 00:19:32 +02:00
context['item_plural'] = self.item_text_plural
2018-05-30 18:14:53 +02:00
context['message_template'] = self.message_template
context['verb'] = self.verb
2018-06-02 17:24:29 +02:00
context['button_class'] = self.button_class
return context
2018-05-21 23:12:35 +02:00
2018-05-21 22:23:53 +02:00
def get_initial(self):
return {
'teacher': self.teacher
}
def get_form(self, form_class=None):
form = super().get_form(form_class)
form.fields['teacher'].queryset = Teacher.objects.filter(pk=self.teacher.pk)
return form
2018-05-21 23:12:35 +02:00
class AddItemView(ItemView, CreateView):
verb = 'Ajouter'
2018-05-21 23:12:35 +02:00
def get_success_url(self):
messages.success(self.request, f'"{self.object}" a été ajouté.')
if self.add_another:
return reverse(self.success_target, args=[str(self.teacher.pk)])
2018-05-21 23:12:35 +02:00
else:
2018-05-21 23:23:43 +02:00
return reverse('list_books', args=[str(self.teacher.pk)])
2018-05-21 23:46:47 +02:00
def form_valid(self, form):
self.add_another = form.cleaned_data['add_another']
return super().form_valid(form)
2018-05-21 23:46:47 +02:00
class BookView:
model = Book
success_target = 'add_book'
item_text = 'un livre'
2018-05-24 00:19:32 +02:00
item_text_plural = 'livres'
class AddBookView(BookView, AddItemView):
form_class = AddBookForm
class SuppliesView:
model = SuppliesRequirement
success_target = 'add_supplies'
item_text = 'des fournitures'
2018-05-24 00:19:32 +02:00
item_text_plural = 'fournitures'
class AddSuppliesView(SuppliesView, AddItemView):
form_class = AddSuppliesForm
2018-06-02 17:24:29 +02:00
message_template = 'manuels/supplies_message.html'
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
2018-06-02 17:24:29 +02:00
class DeleteItemView(ItemView, DeleteView):
teacher_field = 'teacher_pk'
item_text = None
item_text_plural = None
message_template = 'manuels/confirm_delete.html'
verb = 'Supprimer'
button_class = 'danger'
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é supprimé.')
return reverse('list_books', args=[str(self.teacher.pk)])
class DeleteBookView(BookView, DeleteItemView):
pass
class DeleteSuppliesView(SuppliesView, DeleteItemView):
pass
2018-05-21 23:46:47 +02:00
def clear_teacher_view(request):
if request.session['teacher_pk']:
del request.session['teacher_pk']
return redirect('home_page')