2019-06-28 16:33:59 +02:00
|
|
|
import logging
|
2018-05-22 10:24:21 +02:00
|
|
|
import os
|
2018-05-21 22:04:29 +02:00
|
|
|
import re
|
2018-05-21 23:23:43 +02:00
|
|
|
import uuid as uuid
|
2018-05-22 10:24:21 +02:00
|
|
|
|
2019-06-28 16:33:59 +02:00
|
|
|
import bs4
|
|
|
|
import requests
|
2018-05-22 10:24:21 +02:00
|
|
|
from django.conf import settings
|
2019-06-27 19:12:04 +02:00
|
|
|
from django.contrib.postgres.fields import CIEmailField
|
2018-05-21 22:04:29 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2018-05-22 10:24:21 +02:00
|
|
|
from django.core.mail import EmailMultiAlternatives
|
2018-05-21 19:28:36 +02:00
|
|
|
from django.db import models
|
2019-06-28 14:47:35 +02:00
|
|
|
from django.db.models import Sum
|
2018-05-22 10:24:21 +02:00
|
|
|
from django.template.loader import render_to_string
|
|
|
|
from django.urls import reverse
|
2018-05-21 19:28:36 +02:00
|
|
|
|
2019-06-28 16:33:59 +02:00
|
|
|
from manuels.utils import validate_isbn
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class ISBNError(Exception):
|
|
|
|
def __init__(self, data):
|
|
|
|
self.data = data
|
|
|
|
|
2018-05-21 19:28:36 +02:00
|
|
|
|
2018-05-22 01:45:55 +02:00
|
|
|
class BaseModel(models.Model):
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2018-06-02 16:39:10 +02:00
|
|
|
|
2021-07-10 12:11:58 +02:00
|
|
|
created_at = models.DateTimeField("créé le", auto_now_add=True)
|
|
|
|
updated_at = models.DateTimeField("mis à jour le", auto_now=True)
|
2018-05-22 01:45:55 +02:00
|
|
|
|
|
|
|
|
2018-05-24 00:12:16 +02:00
|
|
|
def phone_validator(value):
|
2021-07-10 12:11:58 +02:00
|
|
|
regex = re.compile(r"^\d{10}$")
|
2018-05-24 00:12:16 +02:00
|
|
|
if not regex.match(value):
|
|
|
|
raise ValidationError(
|
|
|
|
"%(value)s n'est pas un numéro de téléphone valide. Format attendu : 10 chiffres.",
|
2021-07-10 12:11:58 +02:00
|
|
|
params={"value": value},
|
2018-05-24 00:12:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-05-22 01:45:55 +02:00
|
|
|
class Teacher(BaseModel):
|
2018-05-21 22:04:29 +02:00
|
|
|
class Meta:
|
2021-07-10 12:11:58 +02:00
|
|
|
verbose_name = "coordonnateur"
|
|
|
|
verbose_name_plural = "coordonnateurs"
|
|
|
|
ordering = ["first_name"]
|
2018-06-02 16:39:10 +02:00
|
|
|
|
2018-05-21 23:23:43 +02:00
|
|
|
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
2021-07-10 12:11:58 +02:00
|
|
|
first_name = models.CharField("prénom", max_length=100)
|
|
|
|
last_name = models.CharField("nom", max_length=100)
|
2018-05-24 00:12:16 +02:00
|
|
|
phone_number = models.CharField(
|
2021-07-10 12:11:58 +02:00
|
|
|
"numéro de téléphone",
|
2018-05-24 00:12:16 +02:00
|
|
|
help_text="En cas d'urgence, 10 chiffres.",
|
|
|
|
max_length=10,
|
2021-07-10 12:11:58 +02:00
|
|
|
validators=[phone_validator],
|
2018-05-24 00:12:16 +02:00
|
|
|
)
|
2019-06-27 19:12:04 +02:00
|
|
|
email = CIEmailField(
|
2021-07-10 12:11:58 +02:00
|
|
|
"adresse email",
|
|
|
|
help_text="Utilisée pour vous transmettre votre lien personnel",
|
|
|
|
unique=True,
|
2018-05-22 00:55:14 +02:00
|
|
|
)
|
2018-06-02 17:51:53 +02:00
|
|
|
has_confirmed_list = models.BooleanField(
|
2021-07-10 12:11:58 +02:00
|
|
|
"a confirmé les listes", default=False, blank=True
|
2018-06-02 17:51:53 +02:00
|
|
|
)
|
2018-05-21 22:04:29 +02:00
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
from django.urls import reverse
|
2021-07-10 12:11:58 +02:00
|
|
|
|
|
|
|
return reverse("list_books", kwargs={"pk": str(self.pk)})
|
2018-05-21 22:04:29 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def full_name(self):
|
2021-07-10 12:11:58 +02:00
|
|
|
return f"{self.first_name} {self.last_name}"
|
2018-05-21 22:04:29 +02:00
|
|
|
|
2018-05-21 23:12:35 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.full_name
|
|
|
|
|
2018-05-22 10:24:21 +02:00
|
|
|
def send_link(self, request):
|
|
|
|
dest = self.email
|
2021-07-10 12:11:58 +02:00
|
|
|
link = request.build_absolute_uri(reverse("list_books", args=[str(self.pk)]))
|
2018-05-22 10:24:21 +02:00
|
|
|
msg = EmailMultiAlternatives(
|
2021-07-10 12:11:58 +02:00
|
|
|
subject="Gestion des manuels scolaires",
|
|
|
|
body=f"Bonjour {self.first_name},\n"
|
|
|
|
f"Voici votre lien pour la gestion des manuels scolaires : {link}",
|
2018-05-22 10:24:21 +02:00
|
|
|
from_email=settings.SERVER_EMAIL,
|
|
|
|
to=[dest],
|
|
|
|
)
|
2021-07-10 12:11:58 +02:00
|
|
|
reply_to = [os.getenv("REPLY_TO")]
|
2018-05-22 10:24:21 +02:00
|
|
|
if reply_to:
|
|
|
|
msg.reply_to = reply_to
|
|
|
|
msg.attach_alternative(
|
2021-07-10 12:11:58 +02:00
|
|
|
render_to_string(
|
|
|
|
"manuels/emails_link.html", {"link": link, "teacher": self}
|
|
|
|
),
|
|
|
|
"text/html",
|
2018-06-02 18:03:18 +02:00
|
|
|
)
|
|
|
|
msg.send()
|
|
|
|
|
|
|
|
def send_confirmation(self, request):
|
|
|
|
dest = settings.LIBRARIAN_EMAILS
|
2021-07-10 12:11:58 +02:00
|
|
|
link = request.build_absolute_uri(reverse("home_page"))
|
2018-06-02 18:03:18 +02:00
|
|
|
msg = EmailMultiAlternatives(
|
|
|
|
subject="Gestion des manuels scolaires - Confirmation d'un coordonnateur",
|
2021-07-10 12:11:58 +02:00
|
|
|
body=f"Bonjour,\n" f"{self.first_name} a confirmé ses listes sur {link}",
|
2018-06-02 18:03:18 +02:00
|
|
|
from_email=settings.SERVER_EMAIL,
|
|
|
|
to=dest,
|
|
|
|
)
|
2021-07-10 12:11:58 +02:00
|
|
|
reply_to = [os.getenv("REPLY_TO")]
|
2018-06-02 18:03:18 +02:00
|
|
|
if reply_to:
|
|
|
|
msg.reply_to = reply_to
|
|
|
|
msg.attach_alternative(
|
2021-07-10 12:11:58 +02:00
|
|
|
render_to_string(
|
|
|
|
"manuels/emails_confirmation.html", {"link": link, "teacher": self}
|
|
|
|
),
|
|
|
|
"text/html",
|
2018-05-22 10:24:21 +02:00
|
|
|
)
|
|
|
|
msg.send()
|
|
|
|
|
2018-05-21 22:04:29 +02:00
|
|
|
|
2018-05-22 01:45:55 +02:00
|
|
|
class Level(BaseModel):
|
2018-05-21 22:04:29 +02:00
|
|
|
class Meta:
|
2021-07-10 12:11:58 +02:00
|
|
|
verbose_name = "classe"
|
|
|
|
verbose_name_plural = "classes"
|
|
|
|
ordering = ["order", "name"]
|
2018-06-02 16:39:10 +02:00
|
|
|
|
2021-07-10 12:11:58 +02:00
|
|
|
name = models.CharField("nom", max_length=50)
|
|
|
|
order = models.IntegerField("ordre", default=0)
|
2018-05-21 22:04:29 +02:00
|
|
|
|
2018-05-21 23:12:35 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2020-06-01 13:44:54 +02:00
|
|
|
@property
|
|
|
|
def non_acquired_consumables(self):
|
|
|
|
if hasattr(self, "prefetched_books"):
|
2021-07-10 12:11:58 +02:00
|
|
|
return filter(
|
|
|
|
lambda book: book.consumable and not book.previously_acquired,
|
|
|
|
self.prefetched_books,
|
|
|
|
)
|
2020-06-01 13:44:54 +02:00
|
|
|
return self.book_set.filter(consumable=True, previously_acquired=False)
|
|
|
|
|
2019-06-28 15:40:21 +02:00
|
|
|
@property
|
|
|
|
def non_acquired_consumable_count(self):
|
2020-06-01 13:44:54 +02:00
|
|
|
if hasattr(self, "prefetched_books"):
|
|
|
|
return len(list(self.non_acquired_consumables))
|
|
|
|
return self.non_acquired_consumables.count()
|
2019-06-28 15:40:21 +02:00
|
|
|
|
2019-06-28 14:47:35 +02:00
|
|
|
@property
|
2019-06-28 15:24:03 +02:00
|
|
|
def non_acquired_consumable_price(self):
|
2020-06-01 13:44:54 +02:00
|
|
|
if hasattr(self, "prefetched_books"):
|
|
|
|
return sum(map(lambda book: book.price, self.non_acquired_consumables))
|
2021-07-10 12:11:58 +02:00
|
|
|
return self.non_acquired_consumables.aggregate(Sum("price")).get(
|
|
|
|
"price__sum", 0
|
|
|
|
)
|
2020-06-01 13:44:54 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def non_acquired_books(self):
|
|
|
|
if hasattr(self, "prefetched_books"):
|
2021-07-10 12:11:58 +02:00
|
|
|
return filter(
|
|
|
|
lambda book: not book.consumable and not book.previously_acquired,
|
|
|
|
self.prefetched_books,
|
|
|
|
)
|
2020-06-01 13:44:54 +02:00
|
|
|
return self.book_set.filter(consumable=False, previously_acquired=False)
|
2019-06-28 15:15:12 +02:00
|
|
|
|
2019-06-28 15:40:21 +02:00
|
|
|
@property
|
|
|
|
def non_acquired_book_count(self):
|
2020-06-01 13:44:54 +02:00
|
|
|
if hasattr(self, "prefetched_books"):
|
|
|
|
return len(list(self.non_acquired_books))
|
|
|
|
return self.non_acquired_books.count()
|
2019-06-28 15:40:21 +02:00
|
|
|
|
2019-06-28 14:47:35 +02:00
|
|
|
@property
|
2019-06-28 15:24:03 +02:00
|
|
|
def non_acquired_book_price(self):
|
2020-06-01 13:44:54 +02:00
|
|
|
if hasattr(self, "prefetched_books"):
|
|
|
|
return sum(map(lambda book: book.price, self.non_acquired_books))
|
2021-07-10 12:11:58 +02:00
|
|
|
return self.non_acquired_books.aggregate(Sum("price")).get("price__sum", 0)
|
2020-06-01 13:44:54 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def non_acquired_items(self):
|
|
|
|
if hasattr(self, "prefetched_books"):
|
2021-07-10 12:11:58 +02:00
|
|
|
return filter(
|
|
|
|
lambda book: not book.previously_acquired, self.prefetched_books
|
|
|
|
)
|
2020-06-01 13:44:54 +02:00
|
|
|
return self.book_set.filter(previously_acquired=False)
|
2019-06-28 14:47:35 +02:00
|
|
|
|
2019-06-28 15:15:12 +02:00
|
|
|
@property
|
2019-06-28 15:24:03 +02:00
|
|
|
def non_acquired_total_price(self):
|
2020-06-01 13:44:54 +02:00
|
|
|
if hasattr(self, "prefetched_books"):
|
|
|
|
return sum(map(lambda book: book.price, self.non_acquired_items))
|
2021-07-10 12:11:58 +02:00
|
|
|
return self.non_acquired_items.aggregate(Sum("price")).get("price__sum", 0)
|
2019-06-28 15:15:12 +02:00
|
|
|
|
2018-05-21 22:04:29 +02:00
|
|
|
|
2018-05-22 01:45:55 +02:00
|
|
|
class Editor(BaseModel):
|
2018-05-22 01:35:05 +02:00
|
|
|
class Meta:
|
2021-07-10 12:11:58 +02:00
|
|
|
verbose_name = "éditeur"
|
|
|
|
verbose_name_plural = "éditeurs"
|
|
|
|
ordering = ["name"]
|
2018-06-02 16:39:10 +02:00
|
|
|
|
2021-07-10 12:11:58 +02:00
|
|
|
name = models.CharField("nom", max_length=100)
|
2018-05-22 01:35:05 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
2018-05-21 22:04:29 +02:00
|
|
|
def isbn_validator(value):
|
2021-07-10 12:11:58 +02:00
|
|
|
regex = re.compile(r"(\d-?){10,13}X?")
|
2018-05-21 22:04:29 +02:00
|
|
|
if not regex.match(value):
|
2021-07-10 12:11:58 +02:00
|
|
|
raise ValidationError(
|
|
|
|
"%(value)s n'est pas un ISBN valide.", params={"value": value}
|
|
|
|
)
|
2018-05-21 22:04:29 +02:00
|
|
|
|
|
|
|
|
2018-06-04 18:28:40 +02:00
|
|
|
def positive_float_validator(value):
|
|
|
|
try:
|
|
|
|
value = float(value)
|
|
|
|
except ValueError:
|
|
|
|
raise ValidationError("%(value)s doit être un nombre décimal")
|
|
|
|
|
|
|
|
if value < 0:
|
|
|
|
raise ValidationError("%(value)s doit être un nombre positif")
|
|
|
|
|
|
|
|
|
2018-05-22 01:45:55 +02:00
|
|
|
class Book(BaseModel):
|
2018-05-21 22:04:29 +02:00
|
|
|
class Meta:
|
2021-07-10 12:11:58 +02:00
|
|
|
verbose_name = "livre"
|
|
|
|
verbose_name_plural = "livres"
|
|
|
|
|
|
|
|
teacher = models.ForeignKey(
|
|
|
|
verbose_name="coordonnateur", to=Teacher, on_delete=models.PROTECT, null=True
|
|
|
|
)
|
|
|
|
level = models.ForeignKey(
|
|
|
|
verbose_name="classe", to=Level, on_delete=models.PROTECT, null=True
|
|
|
|
)
|
|
|
|
field = models.CharField("discipline", max_length=200)
|
|
|
|
title = models.TextField("titre")
|
|
|
|
authors = models.TextField("auteurs")
|
|
|
|
editor = models.ForeignKey(
|
|
|
|
verbose_name="éditeur", to=Editor, on_delete=models.PROTECT, null=True
|
|
|
|
)
|
|
|
|
other_editor = models.CharField(verbose_name="préciser", max_length=100, blank=True)
|
|
|
|
publication_year = models.PositiveIntegerField("année de publication")
|
2018-05-24 00:15:35 +02:00
|
|
|
isbn = models.CharField(
|
2021-07-10 12:11:58 +02:00
|
|
|
"ISBN/EAN du manuel élève (hors specimen)",
|
2018-05-24 00:15:35 +02:00
|
|
|
max_length=20,
|
|
|
|
help_text="Format attendu : 10 ou 13 chiffres, éventuellement séparés par des tirets et éventuellement "
|
2021-07-10 12:11:58 +02:00
|
|
|
"suivis de la lettre <code>X</code>. La recherche sur Decitre ne fonctionnera qu'avec un code ISBN à "
|
|
|
|
"13 chiffres (ou EAN)",
|
|
|
|
validators=[isbn_validator],
|
2018-05-24 00:15:35 +02:00
|
|
|
)
|
2021-07-10 12:11:58 +02:00
|
|
|
price = models.FloatField("prix", validators=[positive_float_validator])
|
2018-05-21 23:12:35 +02:00
|
|
|
YES_NO_CHOICE = (
|
2021-07-10 12:11:58 +02:00
|
|
|
(None, "------------"),
|
|
|
|
(False, "Non"),
|
|
|
|
(True, "Oui"),
|
2018-05-22 10:13:51 +02:00
|
|
|
)
|
|
|
|
previously_acquired = models.BooleanField(
|
|
|
|
"manuel acquis précédemment par l'élève",
|
|
|
|
choices=YES_NO_CHOICE,
|
|
|
|
blank=False,
|
2018-06-18 18:05:44 +02:00
|
|
|
default=None,
|
2018-05-21 23:12:35 +02:00
|
|
|
)
|
2021-07-10 12:11:58 +02:00
|
|
|
done = models.BooleanField("Traité", blank=True, default=False)
|
2018-05-30 18:23:19 +02:00
|
|
|
comments = models.TextField(
|
2021-07-10 12:11:58 +02:00
|
|
|
"commentaires",
|
2018-05-31 08:15:20 +02:00
|
|
|
blank=True,
|
2021-07-10 12:11:58 +02:00
|
|
|
help_text="Ce message sera visible par la documentaliste.",
|
2018-05-30 18:23:19 +02:00
|
|
|
)
|
2019-04-06 19:25:04 +02:00
|
|
|
consumable = models.BooleanField(
|
2021-07-10 12:11:58 +02:00
|
|
|
"consommable",
|
2019-04-06 19:25:04 +02:00
|
|
|
help_text="Exemple : un cahier d'exercices est un consommable",
|
|
|
|
choices=YES_NO_CHOICE,
|
|
|
|
blank=False,
|
|
|
|
default=None,
|
|
|
|
)
|
2018-05-21 23:12:35 +02:00
|
|
|
|
|
|
|
@property
|
2018-06-02 16:29:06 +02:00
|
|
|
def previously_acquired_text(self):
|
2019-04-06 19:25:04 +02:00
|
|
|
if self.previously_acquired:
|
2021-07-10 12:11:58 +02:00
|
|
|
return "Oui"
|
2019-04-06 19:25:04 +02:00
|
|
|
else:
|
2021-07-10 12:11:58 +02:00
|
|
|
return "Non"
|
2019-04-06 19:25:04 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def consumable_text(self):
|
2019-06-25 10:43:51 +02:00
|
|
|
if self.consumable:
|
2021-07-10 12:11:58 +02:00
|
|
|
return "Oui"
|
2018-05-21 23:12:35 +02:00
|
|
|
else:
|
2021-07-10 12:11:58 +02:00
|
|
|
return "Non"
|
2018-05-21 23:12:35 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
2021-07-10 12:11:58 +02:00
|
|
|
return f"{self.title} ({self.authors}) - {self.isbn}"
|
2018-05-22 10:13:51 +02:00
|
|
|
|
2018-05-31 19:11:01 +02:00
|
|
|
def get_absolute_url(self):
|
|
|
|
from django.urls import reverse
|
2021-07-10 12:11:58 +02:00
|
|
|
|
|
|
|
return reverse(
|
|
|
|
"edit_book", kwargs={"teacher_pk": str(self.teacher.pk), "pk": str(self.pk)}
|
|
|
|
)
|
2018-05-31 19:11:01 +02:00
|
|
|
|
2019-06-28 16:33:59 +02:00
|
|
|
def update_from_decitre(self):
|
|
|
|
decitre_data = self.fetch_from_decitre(self.isbn)
|
2021-07-10 12:11:58 +02:00
|
|
|
self.title = decitre_data.get("title")
|
|
|
|
self.authors = decitre_data.get("authors")
|
|
|
|
self.price = decitre_data.get("price")
|
|
|
|
self.publication_year = decitre_data.get("year")
|
2020-06-30 13:14:47 +02:00
|
|
|
|
2021-07-10 12:11:58 +02:00
|
|
|
editor = decitre_data.get("editor")
|
2020-06-30 13:14:47 +02:00
|
|
|
potential_editor = (
|
|
|
|
Editor.objects.filter(name__iexact=editor).first()
|
|
|
|
or Editor.objects.filter(name__icontains=editor).first()
|
|
|
|
)
|
2020-06-30 13:18:54 +02:00
|
|
|
if potential_editor:
|
|
|
|
self.other_editor = ""
|
|
|
|
else:
|
|
|
|
potential_editor = Editor.objects.get(name__istartswith="autre")
|
|
|
|
self.other_editor = editor
|
2019-06-28 16:33:59 +02:00
|
|
|
|
2020-06-30 13:14:47 +02:00
|
|
|
self.editor = potential_editor
|
2019-06-28 16:33:59 +02:00
|
|
|
self.save()
|
|
|
|
|
2020-06-01 13:53:46 +02:00
|
|
|
@property
|
|
|
|
def decitre_url(self):
|
2021-07-10 12:11:58 +02:00
|
|
|
isbn = self.isbn.strip().replace("-", "")
|
2020-06-01 13:53:46 +02:00
|
|
|
if not validate_isbn(isbn) or len(isbn) == 10:
|
|
|
|
return ""
|
2021-07-10 12:11:58 +02:00
|
|
|
return f"https://www.decitre.fr/livres/{isbn}.html"
|
2020-06-01 13:53:46 +02:00
|
|
|
|
2019-06-28 16:33:59 +02:00
|
|
|
@staticmethod
|
|
|
|
def fetch_from_decitre(isbn: str):
|
2021-07-10 12:11:58 +02:00
|
|
|
isbn = isbn.strip().replace("-", "")
|
2019-06-28 16:33:59 +02:00
|
|
|
|
|
|
|
if not validate_isbn(isbn):
|
2021-07-10 12:11:58 +02:00
|
|
|
raise ISBNError({"error": "L'ISBN saisi n'est pas valide."})
|
2019-06-28 16:33:59 +02:00
|
|
|
|
|
|
|
if len(isbn) == 10:
|
2021-07-10 12:11:58 +02:00
|
|
|
raise ISBNError(
|
|
|
|
{
|
|
|
|
"error": "La recherche sur Decitre ne fonctionne qu'avec un ISBN 13 (ou EAN)."
|
|
|
|
}
|
|
|
|
)
|
2019-06-28 16:33:59 +02:00
|
|
|
|
|
|
|
try:
|
2021-07-10 12:11:58 +02:00
|
|
|
res = requests.get(f"https://www.decitre.fr/livres/{isbn}.html", timeout=10)
|
2019-06-28 16:33:59 +02:00
|
|
|
except requests.exceptions.Timeout as exc:
|
2021-07-10 12:11:58 +02:00
|
|
|
raise ISBNError(
|
|
|
|
{
|
|
|
|
"error": "Decitre n'a pas répondu dans les temps. Message : {}".format(
|
|
|
|
str(exc)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2019-06-28 16:33:59 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
res.raise_for_status()
|
|
|
|
except Exception as exc:
|
|
|
|
message = (
|
|
|
|
"Erreur lors de la recherche. Il se peut que le livre n'existe pas dans la base de connaissances "
|
|
|
|
"de Decitre ou que vous ayez mal saisi l'ISBN. Vous pouvez toujours saisir "
|
2021-07-10 12:11:58 +02:00
|
|
|
"les informations du livre à la main. Message : {}"
|
|
|
|
).format(str(exc))
|
|
|
|
raise ISBNError({"error": message})
|
2019-06-28 16:33:59 +02:00
|
|
|
|
|
|
|
decitre_soup = bs4.BeautifulSoup(res.text, "html.parser")
|
2021-07-10 12:11:58 +02:00
|
|
|
title = decitre_soup.select("h1.product-title")
|
2019-06-28 16:33:59 +02:00
|
|
|
if title:
|
|
|
|
title = title[0]
|
|
|
|
if title.span:
|
|
|
|
title.span.extract()
|
|
|
|
title = title.get_text(strip=True)
|
|
|
|
|
2021-07-10 12:11:58 +02:00
|
|
|
authors = decitre_soup.select(".authors")
|
2019-06-28 16:33:59 +02:00
|
|
|
if authors:
|
|
|
|
authors = authors[0]
|
|
|
|
authors = authors.get_text(strip=True)
|
|
|
|
|
2021-07-10 12:11:58 +02:00
|
|
|
price = decitre_soup.select(".fp-top--add-to-cart div.price span.final-price")
|
2019-06-28 16:33:59 +02:00
|
|
|
if price:
|
|
|
|
price = price[0]
|
2021-07-10 12:11:58 +02:00
|
|
|
price = price.get_text().replace("€", "").replace(",", ".").strip()
|
2019-06-28 16:33:59 +02:00
|
|
|
|
|
|
|
year = None
|
|
|
|
editor = None
|
2021-07-10 12:11:58 +02:00
|
|
|
extra_info = decitre_soup.select(".informations-container")
|
|
|
|
logger.debug("raw extra_info")
|
2019-06-28 16:33:59 +02:00
|
|
|
logger.debug(extra_info)
|
|
|
|
if not extra_info:
|
2021-07-10 12:11:58 +02:00
|
|
|
logger.debug("#fiche-technique")
|
|
|
|
extra_info = decitre_soup.select("#fiche-technique")
|
|
|
|
logger.debug("raw extra_info fiche technique")
|
2019-06-28 16:33:59 +02:00
|
|
|
logger.debug(extra_info)
|
|
|
|
|
|
|
|
if extra_info:
|
|
|
|
extra_info = extra_info[0].get_text(strip=True)
|
2021-07-10 12:11:58 +02:00
|
|
|
logger.debug("extra_info")
|
2019-06-28 16:33:59 +02:00
|
|
|
logger.debug(extra_info)
|
|
|
|
matches = re.search(
|
2021-07-10 12:11:58 +02:00
|
|
|
r"Date de parution(?: :)?\d{2}/\d{2}/(?P<year>\d{4})Editeur(?: :)?(?P<editor>.+?)(?:ISBN|Collection)",
|
|
|
|
extra_info,
|
|
|
|
)
|
2019-06-28 16:33:59 +02:00
|
|
|
if matches:
|
|
|
|
groups = matches.groupdict()
|
2021-07-10 12:11:58 +02:00
|
|
|
year = groups.get("year")
|
|
|
|
editor = groups.get("editor").strip()
|
2019-06-28 16:33:59 +02:00
|
|
|
|
|
|
|
return {
|
2021-07-10 12:11:58 +02:00
|
|
|
"title": title,
|
|
|
|
"authors": authors,
|
|
|
|
"isbn": isbn,
|
|
|
|
"price": float(price) if price else None,
|
|
|
|
"year": year,
|
|
|
|
"editor": editor,
|
2019-06-28 16:33:59 +02:00
|
|
|
}
|
|
|
|
|
2018-05-22 10:13:51 +02:00
|
|
|
|
|
|
|
class SuppliesRequirement(BaseModel):
|
|
|
|
class Meta:
|
2021-07-10 12:11:58 +02:00
|
|
|
verbose_name = "demande de fournitures"
|
|
|
|
verbose_name_plural = "demandes de fournitures"
|
|
|
|
|
|
|
|
teacher = models.ForeignKey(
|
|
|
|
verbose_name="coordonnateur", to=Teacher, on_delete=models.PROTECT, null=True
|
2018-05-30 18:06:27 +02:00
|
|
|
)
|
2021-07-10 12:11:58 +02:00
|
|
|
level = models.ForeignKey(
|
|
|
|
verbose_name="classe", to=Level, on_delete=models.PROTECT, null=True
|
|
|
|
)
|
|
|
|
field = models.CharField("discipline", max_length=50)
|
|
|
|
supplies = models.TextField("fournitures")
|
|
|
|
done = models.BooleanField("Traité", blank=True, default=False)
|
2018-05-22 10:13:51 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
2021-07-10 12:11:58 +02:00
|
|
|
return f"{self.supplies} pour {self.level} ({self.teacher})"
|
2019-06-15 11:18:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CommonSupply(BaseModel):
|
|
|
|
class Meta:
|
2021-07-10 12:11:58 +02:00
|
|
|
verbose_name = "fourniture commune"
|
|
|
|
verbose_name_plural = "fournitures communes"
|
|
|
|
ordering = (
|
|
|
|
"order",
|
|
|
|
"name",
|
|
|
|
)
|
2019-06-15 11:18:37 +02:00
|
|
|
|
2021-07-10 12:11:58 +02:00
|
|
|
name = models.CharField("nom", max_length=200)
|
|
|
|
order = models.IntegerField("ordre")
|
2019-06-15 11:18:37 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|