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
|
|
|
|
|
|
|
from django.conf import settings
|
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
|
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
|
|
|
|
|
|
|
|
2018-05-22 01:45:55 +02:00
|
|
|
class BaseModel(models.Model):
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2018-06-02 16:39:10 +02:00
|
|
|
|
2018-05-22 01:45:55 +02:00
|
|
|
created_at = models.DateTimeField('créé le', auto_now_add=True)
|
|
|
|
updated_at = models.DateTimeField('mis à jour le', auto_now=True)
|
|
|
|
|
|
|
|
|
2018-05-24 00:12:16 +02:00
|
|
|
def phone_validator(value):
|
|
|
|
regex = re.compile(r'^\d{10}$')
|
|
|
|
if not regex.match(value):
|
|
|
|
raise ValidationError(
|
|
|
|
"%(value)s n'est pas un numéro de téléphone valide. Format attendu : 10 chiffres.",
|
|
|
|
params={'value': value}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-05-22 01:45:55 +02:00
|
|
|
class Teacher(BaseModel):
|
2018-05-21 22:04:29 +02:00
|
|
|
class Meta:
|
2018-06-02 16:31:29 +02:00
|
|
|
verbose_name = 'coordonnateur'
|
|
|
|
verbose_name_plural = 'coordonnateurs'
|
2018-06-02 16:39:10 +02:00
|
|
|
ordering = ['first_name']
|
|
|
|
|
2018-05-21 23:23:43 +02:00
|
|
|
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
2018-05-21 22:04:29 +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(
|
|
|
|
'numéro de téléphone',
|
|
|
|
help_text="En cas d'urgence, 10 chiffres.",
|
|
|
|
max_length=10,
|
|
|
|
validators=[phone_validator]
|
|
|
|
)
|
2018-05-22 00:55:14 +02:00
|
|
|
email = models.EmailField(
|
|
|
|
'adresse email',
|
|
|
|
help_text='Utilisée pour vous transmettre votre lien personnel',
|
|
|
|
unique=True
|
|
|
|
)
|
2018-06-02 17:51:53 +02:00
|
|
|
has_confirmed_list = models.BooleanField(
|
|
|
|
'a confirmé les listes',
|
|
|
|
default=False,
|
|
|
|
blank=True
|
|
|
|
)
|
2018-05-21 22:04:29 +02:00
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
from django.urls import reverse
|
2018-05-21 23:40:56 +02:00
|
|
|
return reverse('list_books', kwargs={'pk': str(self.pk)})
|
2018-05-21 22:04:29 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def full_name(self):
|
|
|
|
return f'{self.first_name} {self.last_name}'
|
|
|
|
|
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
|
|
|
|
link = request.build_absolute_uri(reverse('list_books', args=[str(self.pk)]))
|
|
|
|
msg = EmailMultiAlternatives(
|
|
|
|
subject='Gestion des manuels scolaires',
|
|
|
|
body=f'Bonjour {self.first_name},\n'
|
|
|
|
f'Voici votre lien pour la gestion des manuels scolaires : {link}',
|
|
|
|
from_email=settings.SERVER_EMAIL,
|
|
|
|
to=[dest],
|
|
|
|
)
|
|
|
|
reply_to = [os.getenv('REPLY_TO')]
|
|
|
|
if reply_to:
|
|
|
|
msg.reply_to = reply_to
|
|
|
|
msg.attach_alternative(
|
2018-06-02 18:03:18 +02:00
|
|
|
render_to_string('manuels/emails_link.html', {'link': link, 'teacher': self}), "text/html"
|
|
|
|
)
|
|
|
|
msg.send()
|
|
|
|
|
|
|
|
def send_confirmation(self, request):
|
|
|
|
dest = settings.LIBRARIAN_EMAILS
|
|
|
|
link = request.build_absolute_uri(reverse('home_page'))
|
|
|
|
msg = EmailMultiAlternatives(
|
|
|
|
subject="Gestion des manuels scolaires - Confirmation d'un coordonnateur",
|
|
|
|
body=f'Bonjour,\n'
|
|
|
|
f'{self.first_name} a confirmé ses listes sur {link}',
|
|
|
|
from_email=settings.SERVER_EMAIL,
|
|
|
|
to=dest,
|
|
|
|
)
|
|
|
|
reply_to = [os.getenv('REPLY_TO')]
|
|
|
|
if reply_to:
|
|
|
|
msg.reply_to = reply_to
|
|
|
|
msg.attach_alternative(
|
|
|
|
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:
|
|
|
|
verbose_name = 'classe'
|
2018-05-21 23:12:35 +02:00
|
|
|
verbose_name_plural = 'classes'
|
2018-06-02 16:39:10 +02:00
|
|
|
|
2018-05-24 00:16:59 +02:00
|
|
|
name = models.CharField('nom', max_length=50)
|
2018-05-21 22:04:29 +02:00
|
|
|
|
2018-05-21 23:12:35 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
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:
|
|
|
|
verbose_name = 'éditeur'
|
|
|
|
verbose_name_plural = 'éditeurs'
|
2018-06-02 16:39:10 +02:00
|
|
|
ordering = ['name']
|
|
|
|
|
2018-05-24 00:16:59 +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):
|
|
|
|
regex = re.compile(r'(\d-?){10,13}X?')
|
|
|
|
if not regex.match(value):
|
|
|
|
raise ValidationError("%(value)s n'est pas un ISBN valide.", params={'value': value})
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
verbose_name = 'livre'
|
|
|
|
verbose_name_plural = 'livres'
|
2018-05-21 23:12:35 +02:00
|
|
|
|
2018-06-02 16:31:29 +02:00
|
|
|
teacher = models.ForeignKey(verbose_name='coordonnateur', to=Teacher, on_delete=models.PROTECT, null=True)
|
2018-05-22 10:52:03 +02:00
|
|
|
level = models.ForeignKey(verbose_name='classe', to=Level, on_delete=models.PROTECT, null=True)
|
2018-05-30 18:01:26 +02:00
|
|
|
field = models.CharField('discipline', max_length=200)
|
2018-05-21 22:04:29 +02:00
|
|
|
title = models.TextField('titre')
|
|
|
|
authors = models.TextField('auteurs')
|
2018-05-22 10:52:03 +02:00
|
|
|
editor = models.ForeignKey(verbose_name='éditeur', to=Editor, on_delete=models.PROTECT, null=True)
|
2018-05-21 22:04:29 +02:00
|
|
|
publication_year = models.PositiveIntegerField('année de publication')
|
2018-05-24 00:15:35 +02:00
|
|
|
isbn = models.CharField(
|
|
|
|
'ISBN/EAN',
|
|
|
|
max_length=20,
|
|
|
|
help_text="Format attendu : 10 ou 13 chiffres, éventuellement séparés par des tirets et éventuellement "
|
|
|
|
"suivis de la lettre <code>X</code>",
|
|
|
|
validators=[isbn_validator]
|
|
|
|
)
|
2018-06-04 18:28:40 +02:00
|
|
|
price = models.FloatField('prix', validators=[positive_float_validator])
|
2018-05-21 23:12:35 +02:00
|
|
|
YES_NO_CHOICE = (
|
|
|
|
(False, 'Non'),
|
2018-05-22 10:13:51 +02:00
|
|
|
(True, 'Oui'),
|
|
|
|
)
|
|
|
|
previously_acquired = models.BooleanField(
|
|
|
|
"manuel acquis précédemment par l'élève",
|
|
|
|
choices=YES_NO_CHOICE,
|
|
|
|
blank=False,
|
|
|
|
default=False,
|
2018-05-21 23:12:35 +02:00
|
|
|
)
|
2018-05-30 18:06:27 +02:00
|
|
|
done = models.BooleanField(
|
|
|
|
'Traité',
|
|
|
|
blank=True,
|
|
|
|
default=False
|
|
|
|
)
|
2018-05-30 18:23:19 +02:00
|
|
|
comments = models.TextField(
|
|
|
|
'commentaires',
|
2018-05-31 08:15:20 +02:00
|
|
|
blank=True,
|
2018-05-30 18:23:19 +02:00
|
|
|
help_text="Ce message sera visible par la documentaliste. Vous pouvez l'utiliser par exemple si vous souhaitez "
|
|
|
|
"saisir un éditeur qui n'est pas proposé."
|
|
|
|
)
|
2018-05-21 23:12:35 +02:00
|
|
|
|
|
|
|
@property
|
2018-06-02 16:29:06 +02:00
|
|
|
def previously_acquired_text(self):
|
2018-05-21 23:12:35 +02:00
|
|
|
if self.previously_acquired:
|
2018-06-02 16:29:06 +02:00
|
|
|
return 'Oui'
|
2018-05-21 23:12:35 +02:00
|
|
|
else:
|
2018-06-02 16:29:06 +02:00
|
|
|
return 'Non'
|
2018-05-21 23:12:35 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
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
|
|
|
|
return reverse('edit_book', kwargs={'teacher_pk': str(self.teacher.pk), 'pk': str(self.pk)})
|
|
|
|
|
2018-05-22 10:13:51 +02:00
|
|
|
|
|
|
|
class SuppliesRequirement(BaseModel):
|
|
|
|
class Meta:
|
|
|
|
verbose_name = 'demande de fournitures'
|
|
|
|
verbose_name_plural = 'demandes de fournitures'
|
|
|
|
|
2018-06-02 16:31:29 +02:00
|
|
|
teacher = models.ForeignKey(verbose_name='coordonnateur', to=Teacher, on_delete=models.PROTECT, null=True)
|
2018-05-22 10:52:03 +02:00
|
|
|
level = models.ForeignKey(verbose_name='classe', to=Level, on_delete=models.PROTECT, null=True)
|
2018-05-30 18:01:26 +02:00
|
|
|
fields = models.TextField('disciplines')
|
2018-05-22 10:13:51 +02:00
|
|
|
supplies = models.TextField('fournitures')
|
2018-05-30 18:06:27 +02:00
|
|
|
done = models.BooleanField(
|
|
|
|
'Traité',
|
|
|
|
blank=True,
|
|
|
|
default=False
|
|
|
|
)
|
2018-05-22 10:13:51 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f'{self.supplies} pour {self.level} ({self.teacher})'
|