2019-06-01 21:05:03 +02:00
|
|
|
import logging
|
2018-06-15 23:23:59 +02:00
|
|
|
import re
|
|
|
|
|
|
|
|
import bs4
|
|
|
|
import requests
|
2018-05-21 23:12:35 +02:00
|
|
|
from django.contrib import messages
|
2018-06-15 23:23:59 +02:00
|
|
|
from django.http import HttpResponseRedirect, JsonResponse
|
2019-06-01 21:05:03 +02:00
|
|
|
from django.shortcuts import redirect
|
2018-05-21 23:12:35 +02:00
|
|
|
from django.urls import reverse
|
2018-06-16 00:12:33 +02:00
|
|
|
from django.views.decorators.cache import cache_page
|
2021-07-10 12:11:58 +02:00
|
|
|
from django.views.generic import CreateView, DeleteView, TemplateView, UpdateView
|
2018-05-21 19:28:36 +02:00
|
|
|
|
2018-05-31 19:11:01 +02:00
|
|
|
from manuels.forms import AddBookForm, AddSuppliesForm, EditBookForm, EditSuppliesForm
|
2021-07-10 12:11:58 +02:00
|
|
|
from manuels.models import Book, CommonSupply, ISBNError, SuppliesRequirement, Teacher
|
2019-06-28 16:33:59 +02:00
|
|
|
from manuels.utils import validate_isbn
|
2018-05-21 21:03:08 +02:00
|
|
|
|
2018-05-23 15:36:18 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2018-05-21 21:03:08 +02:00
|
|
|
|
|
|
|
class HomePageView(CreateView):
|
|
|
|
model = Teacher
|
2021-07-10 12:11:58 +02:00
|
|
|
fields = ["first_name", "last_name", "phone_number", "email"]
|
|
|
|
template_name = "manuels/home_page.html"
|
2018-05-21 22:04:29 +02:00
|
|
|
|
2018-05-21 23:40:56 +02:00
|
|
|
def get(self, request, *args, **kwargs):
|
2021-07-10 12:11:58 +02:00
|
|
|
teacher_pk = request.session.get("teacher_pk")
|
2018-05-21 23:40:56 +02:00
|
|
|
if teacher_pk:
|
2021-07-10 12:11:58 +02:00
|
|
|
return redirect("list_books", pk=teacher_pk)
|
2018-05-21 23:40:56 +02:00
|
|
|
|
|
|
|
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 22:04:29 +02:00
|
|
|
|
2018-05-21 23:12:35 +02:00
|
|
|
class BaseTeacherView:
|
2018-05-21 22:23:53 +02:00
|
|
|
teacher = None
|
2021-07-10 12:11:58 +02:00
|
|
|
teacher_field = "pk"
|
2018-05-21 22:23:53 +02:00
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2021-07-10 12:11:58 +02:00
|
|
|
self.teacher = Teacher.objects.filter(
|
|
|
|
pk=self.kwargs[self.teacher_field]
|
|
|
|
).first()
|
2018-06-07 07:57:45 +02:00
|
|
|
if not self.teacher:
|
2021-07-10 12:11:58 +02:00
|
|
|
messages.warning(
|
|
|
|
request,
|
|
|
|
"Impossible de trouver le coordonnateur demandé. Si vous pensez que ceci est "
|
|
|
|
"une erreur, merci de vous adresser à votre documentaliste.",
|
|
|
|
)
|
|
|
|
return redirect("clear_teacher")
|
|
|
|
request.session["teacher_pk"] = str(self.teacher.pk)
|
2018-05-21 22:23:53 +02:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|
2018-05-21 22:04:29 +02:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data()
|
2021-07-10 12:11:58 +02:00
|
|
|
context["teacher"] = self.teacher
|
2018-05-21 22:04:29 +02:00
|
|
|
|
|
|
|
return context
|
2018-05-21 22:23:53 +02:00
|
|
|
|
2018-05-21 23:12:35 +02:00
|
|
|
|
2018-06-02 18:21:04 +02:00
|
|
|
class ListBooksView(BaseTeacherView, TemplateView):
|
2021-07-10 12:11:58 +02:00
|
|
|
template_name = "manuels/list_books_supplies.html"
|
2018-05-21 23:12:35 +02:00
|
|
|
|
|
|
|
|
2018-05-31 19:11:01 +02:00
|
|
|
class ItemView(BaseTeacherView):
|
2018-05-21 23:12:35 +02:00
|
|
|
add_another = False
|
2018-05-22 10:13:51 +02:00
|
|
|
item_text = None
|
2018-05-24 00:19:32 +02:00
|
|
|
item_text_plural = None
|
2018-05-22 10:13:51 +02:00
|
|
|
success_target = None
|
2018-05-30 18:14:53 +02:00
|
|
|
message_template = None
|
2018-05-31 19:11:01 +02:00
|
|
|
verb = None
|
2021-07-10 12:11:58 +02:00
|
|
|
button_class = "primary"
|
|
|
|
button_icon = "fas fa-check-circle"
|
2018-05-22 10:13:51 +02:00
|
|
|
|
2018-06-02 17:51:53 +02:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
response = super().dispatch(request, *args, **kwargs)
|
2019-05-28 12:53:10 +02:00
|
|
|
if self.teacher and self.teacher.has_confirmed_list:
|
2021-07-10 12:11:58 +02:00
|
|
|
messages.error(
|
|
|
|
request,
|
|
|
|
"Vous avez déjà confirmé vos listes. Il n'est plus possible de les modifier.",
|
|
|
|
)
|
|
|
|
return redirect("list_books", pk=self.teacher.pk)
|
2018-06-02 17:51:53 +02:00
|
|
|
return response
|
|
|
|
|
2018-05-22 10:13:51 +02:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2021-07-10 12:11:58 +02:00
|
|
|
context["item"] = self.item_text
|
|
|
|
context["item_plural"] = self.item_text_plural
|
|
|
|
context["message_template"] = self.message_template
|
|
|
|
context["verb"] = self.verb
|
|
|
|
context["button_class"] = self.button_class
|
|
|
|
context["button_icon"] = self.button_icon
|
2018-05-22 10:13:51 +02:00
|
|
|
return context
|
2018-05-21 23:12:35 +02:00
|
|
|
|
2018-05-21 22:23:53 +02:00
|
|
|
def get_initial(self):
|
2021-07-10 12:11:58 +02:00
|
|
|
return {"teacher": self.teacher}
|
2018-05-21 22:23:53 +02:00
|
|
|
|
|
|
|
def get_form(self, form_class=None):
|
|
|
|
form = super().get_form(form_class)
|
2021-07-10 12:11:58 +02:00
|
|
|
form.fields["teacher"].queryset = Teacher.objects.filter(pk=self.teacher.pk)
|
2018-05-21 22:23:53 +02:00
|
|
|
return form
|
2018-05-21 23:12:35 +02:00
|
|
|
|
2018-05-31 19:11:01 +02:00
|
|
|
|
|
|
|
class AddItemView(ItemView, CreateView):
|
2021-07-10 12:11:58 +02:00
|
|
|
verb = "Ajouter"
|
|
|
|
button_icon = "fas fa-plus-circle"
|
2018-05-21 23:12:35 +02:00
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
if self.add_another:
|
2018-05-22 10:13:51 +02:00
|
|
|
return reverse(self.success_target, args=[str(self.teacher.pk)])
|
2018-05-21 23:12:35 +02:00
|
|
|
else:
|
2021-07-10 12:11:58 +02:00
|
|
|
return reverse("list_books", args=[str(self.teacher.pk)])
|
2018-05-21 23:46:47 +02:00
|
|
|
|
2018-05-31 19:11:01 +02:00
|
|
|
def form_valid(self, form):
|
2021-07-10 12:11:58 +02:00
|
|
|
self.add_another = form.cleaned_data["add_another"]
|
2018-06-18 18:26:33 +02:00
|
|
|
return HttpResponseRedirect(self.get_success_url())
|
2018-05-31 19:11:01 +02:00
|
|
|
|
2018-05-21 23:46:47 +02:00
|
|
|
|
2018-05-31 19:11:01 +02:00
|
|
|
class BookView:
|
2018-05-22 10:13:51 +02:00
|
|
|
model = Book
|
2021-07-10 12:11:58 +02:00
|
|
|
success_target = "add_book"
|
|
|
|
item_text = "un livre"
|
|
|
|
item_text_plural = "livres"
|
2018-05-22 10:13:51 +02:00
|
|
|
|
|
|
|
|
2018-05-31 19:11:01 +02:00
|
|
|
class AddBookView(BookView, AddItemView):
|
|
|
|
form_class = AddBookForm
|
2021-07-10 12:11:58 +02:00
|
|
|
template_name = "manuels/add_book.html"
|
2018-05-31 19:11:01 +02:00
|
|
|
|
2018-06-10 20:03:00 +02:00
|
|
|
def form_valid(self, form: AddBookForm):
|
2021-07-10 12:11:58 +02:00
|
|
|
for level in form.cleaned_data["levels"]:
|
2018-06-10 20:03:00 +02:00
|
|
|
book = Book.objects.create(
|
2021-07-10 12:11:58 +02:00
|
|
|
teacher=form.cleaned_data["teacher"],
|
2018-06-10 20:03:00 +02:00
|
|
|
level=level,
|
2021-07-10 12:11:58 +02:00
|
|
|
field=form.cleaned_data["field"],
|
|
|
|
title=form.cleaned_data["title"],
|
|
|
|
authors=form.cleaned_data["authors"],
|
|
|
|
editor=form.cleaned_data["editor"],
|
|
|
|
other_editor=form.cleaned_data["other_editor"],
|
|
|
|
publication_year=form.cleaned_data["publication_year"],
|
|
|
|
isbn=form.cleaned_data["isbn"],
|
|
|
|
price=form.cleaned_data["price"],
|
|
|
|
previously_acquired=form.cleaned_data["previously_acquired"],
|
|
|
|
comments=form.cleaned_data["comments"],
|
|
|
|
consumable=form.cleaned_data["consumable"],
|
2018-06-10 20:03:00 +02:00
|
|
|
)
|
|
|
|
messages.success(self.request, f'"{book}" a été ajouté.')
|
|
|
|
|
2018-06-18 18:26:33 +02:00
|
|
|
return super().form_valid(form)
|
2018-06-10 20:03:00 +02:00
|
|
|
|
2018-05-31 19:11:01 +02:00
|
|
|
|
|
|
|
class SuppliesView:
|
2018-05-22 10:13:51 +02:00
|
|
|
model = SuppliesRequirement
|
2021-07-10 12:11:58 +02:00
|
|
|
success_target = "add_supplies"
|
|
|
|
item_text = "des fournitures"
|
|
|
|
item_text_plural = "fournitures"
|
2018-05-22 10:13:51 +02:00
|
|
|
|
|
|
|
|
2018-05-31 19:11:01 +02:00
|
|
|
class AddSuppliesView(SuppliesView, AddItemView):
|
|
|
|
form_class = AddSuppliesForm
|
2021-07-10 12:11:58 +02:00
|
|
|
message_template = "manuels/supplies_message.html"
|
|
|
|
template_name = "manuels/add_supplies.html"
|
2018-05-31 19:11:01 +02:00
|
|
|
|
2019-06-15 11:18:37 +02:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2021-07-10 12:11:58 +02:00
|
|
|
context["common_supplies"] = CommonSupply.objects.all()
|
2019-06-15 11:18:37 +02:00
|
|
|
return context
|
|
|
|
|
2018-06-18 18:26:33 +02:00
|
|
|
def form_valid(self, form: AddBookForm):
|
2021-07-10 12:11:58 +02:00
|
|
|
for level in form.cleaned_data["levels"]:
|
2018-06-18 18:26:33 +02:00
|
|
|
supplies = SuppliesRequirement.objects.create(
|
2021-07-10 12:11:58 +02:00
|
|
|
teacher=form.cleaned_data["teacher"],
|
2018-06-18 18:26:33 +02:00
|
|
|
level=level,
|
2021-07-10 12:11:58 +02:00
|
|
|
field=form.cleaned_data["field"],
|
|
|
|
supplies=form.cleaned_data["supplies"],
|
2018-06-18 18:26:33 +02:00
|
|
|
)
|
|
|
|
messages.success(self.request, f'"{supplies}" a été ajouté.')
|
|
|
|
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
2018-05-31 19:11:01 +02:00
|
|
|
|
|
|
|
class EditItemView(ItemView, UpdateView):
|
2021-07-10 12:11:58 +02:00
|
|
|
teacher_field = "teacher_pk"
|
2018-05-31 19:11:01 +02:00
|
|
|
item_text = None
|
|
|
|
item_text_plural = None
|
|
|
|
message_template = None
|
2021-07-10 12:11:58 +02:00
|
|
|
verb = "Modifier"
|
2018-05-31 19:11:01 +02:00
|
|
|
|
|
|
|
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é.')
|
2021-07-10 12:11:58 +02:00
|
|
|
return reverse("list_books", args=[str(self.teacher.pk)])
|
2018-05-31 19:11:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
class EditBookView(BookView, EditItemView):
|
|
|
|
form_class = EditBookForm
|
2021-07-10 12:11:58 +02:00
|
|
|
template_name = "manuels/add_book.html"
|
2018-05-31 19:11:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
class EditSuppliesView(SuppliesView, EditItemView):
|
|
|
|
form_class = EditSuppliesForm
|
2021-07-10 12:11:58 +02:00
|
|
|
template_name = "manuels/add_supplies.html"
|
2018-05-31 19:11:01 +02:00
|
|
|
|
|
|
|
|
2018-06-02 17:24:29 +02:00
|
|
|
class DeleteItemView(ItemView, DeleteView):
|
2021-07-10 12:11:58 +02:00
|
|
|
teacher_field = "teacher_pk"
|
2018-06-02 17:24:29 +02:00
|
|
|
item_text = None
|
|
|
|
item_text_plural = None
|
2021-07-10 12:11:58 +02:00
|
|
|
message_template = "manuels/confirm_delete.html"
|
|
|
|
verb = "Supprimer"
|
|
|
|
button_class = "danger"
|
|
|
|
button_icon = "fas fa-trash"
|
|
|
|
template_name = "manuels/add_supplies.html"
|
2018-06-02 17:24:29 +02:00
|
|
|
|
|
|
|
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é.')
|
2021-07-10 12:11:58 +02:00
|
|
|
return reverse("list_books", args=[str(self.teacher.pk)])
|
2018-06-02 17:24:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DeleteBookView(BookView, DeleteItemView):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class DeleteSuppliesView(SuppliesView, DeleteItemView):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-05-21 23:46:47 +02:00
|
|
|
def clear_teacher_view(request):
|
2021-07-10 12:11:58 +02:00
|
|
|
if "teacher_pk" in request.session:
|
|
|
|
del request.session["teacher_pk"]
|
|
|
|
return redirect("home_page")
|
2018-06-02 17:51:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ConfirmTeacherView(BaseTeacherView, UpdateView):
|
|
|
|
model = Teacher
|
|
|
|
fields = []
|
2021-07-10 12:11:58 +02:00
|
|
|
template_name = "manuels/confirm_teacher.html"
|
2018-06-02 17:51:53 +02:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
response = super().form_valid(form)
|
|
|
|
self.object.has_confirmed_list = True
|
|
|
|
self.object.save()
|
2018-06-02 18:03:18 +02:00
|
|
|
self.object.send_confirmation(request=self.request)
|
2021-07-10 12:11:58 +02:00
|
|
|
messages.success(
|
|
|
|
self.request,
|
|
|
|
"Vos listes ont été validées. Votre documentaliste a été notifiée par email.",
|
|
|
|
)
|
2018-06-02 17:51:53 +02:00
|
|
|
return response
|
2018-06-15 23:23:59 +02:00
|
|
|
|
|
|
|
|
2018-06-30 15:30:09 +02:00
|
|
|
# We are able to cache the response because it's very unlikely that the details of a book will change through time
|
2019-06-08 19:32:49 +02:00
|
|
|
@cache_page(2 * 60 * 60)
|
2018-06-15 23:23:59 +02:00
|
|
|
def isbn_api(request, isbn):
|
|
|
|
try:
|
2019-06-28 16:33:59 +02:00
|
|
|
return JsonResponse(Book.fetch_from_decitre(isbn))
|
|
|
|
except ISBNError as e:
|
|
|
|
return JsonResponse(e.data)
|