1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2024-12-27 12:22:19 +00:00
plateforme-corres2math/apps/registration/models.py

197 lines
5.7 KiB
Python
Raw Normal View History

from corres2math.tokens import email_validation_token
2020-09-22 17:37:37 +00:00
from django.contrib.sites.models import Site
2020-09-21 13:41:55 +00:00
from django.db import models
2020-09-22 17:37:37 +00:00
from django.template import loader
2020-10-15 12:55:45 +00:00
from django.urls import reverse_lazy
2020-09-24 20:40:10 +00:00
from django.utils.crypto import get_random_string
2020-09-22 17:37:37 +00:00
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
2020-09-21 15:53:07 +00:00
from django.utils.translation import gettext_lazy as _
from polymorphic.models import PolymorphicModel
2020-09-22 17:37:37 +00:00
2020-09-21 15:53:07 +00:00
class Registration(PolymorphicModel):
2020-10-30 18:46:46 +00:00
"""
Registrations store extra content that are not asked in the User Model.
This is specific to the role of the user, see StudentRegistration,
ClassRegistration or AdminRegistration..
"""
2020-09-21 15:53:07 +00:00
user = models.OneToOneField(
"auth.User",
on_delete=models.CASCADE,
verbose_name=_("user"),
)
give_contact_to_animath = models.BooleanField(
default=False,
verbose_name=_("Grant Animath to contact me in the future about other actions"),
)
2020-09-22 17:37:37 +00:00
email_confirmed = models.BooleanField(
default=False,
verbose_name=_("email confirmed"),
)
def send_email_validation_link(self):
2020-10-30 18:46:46 +00:00
"""
The account got created or the email got changed.
Send an email that contains a link to validate the address.
"""
2020-09-22 17:37:37 +00:00
subject = "[Corres2math] " + str(_("Activate your Correspondances account"))
token = email_validation_token.make_token(self.user)
uid = urlsafe_base64_encode(force_bytes(self.user.pk))
site = Site.objects.first()
message = loader.render_to_string('registration/mails/email_validation_email.txt',
{
'user': self.user,
'domain': site.domain,
'token': token,
'uid': uid,
})
html = loader.render_to_string('registration/mails/email_validation_email.html',
{
'user': self.user,
'domain': site.domain,
'token': token,
'uid': uid,
})
self.user.email_user(subject, message, html_message=html)
2020-09-21 15:53:07 +00:00
@property
def type(self):
raise NotImplementedError
2020-09-24 16:39:55 +00:00
@property
def form_class(self):
raise NotImplementedError
2020-09-22 18:41:42 +00:00
@property
def participates(self):
return isinstance(self, StudentRegistration) or isinstance(self, CoachRegistration)
2020-09-27 14:35:31 +00:00
@property
def is_admin(self):
return isinstance(self, AdminRegistration) or self.user.is_superuser
@property
def matrix_username(self):
return f"corres2math_{self.user.pk}"
2020-10-15 12:55:45 +00:00
def get_absolute_url(self):
return reverse_lazy("registration:user_detail", args=(self.user_id,))
2020-09-21 15:53:07 +00:00
def __str__(self):
2020-09-24 08:21:50 +00:00
return f"{self.user.first_name} {self.user.last_name}"
2020-09-21 15:53:07 +00:00
class Meta:
verbose_name = _("registration")
verbose_name_plural = _("registrations")
2020-09-24 20:40:10 +00:00
def get_random_filename(instance, filename):
2020-09-27 10:36:37 +00:00
return "authorization/photo/" + get_random_string(64)
2020-09-24 20:40:10 +00:00
2020-09-21 15:53:07 +00:00
class StudentRegistration(Registration):
2020-10-30 18:46:46 +00:00
"""
Specific registration for students.
They have a team, a student class and a school.
"""
2020-09-22 10:27:03 +00:00
team = models.ForeignKey(
"participation.Team",
related_name="students",
on_delete=models.PROTECT,
null=True,
default=None,
verbose_name=_("team"),
)
2020-09-21 15:53:07 +00:00
student_class = models.IntegerField(
choices=[
(12, _("12th grade")),
(11, _("11th grade")),
(10, _("10th grade or lower")),
],
verbose_name=_("student class"),
)
school = models.CharField(
max_length=255,
verbose_name=_("school"),
)
2020-09-24 20:40:10 +00:00
photo_authorization = models.FileField(
verbose_name=_("photo authorization"),
upload_to=get_random_filename,
blank=True,
default="",
)
2020-09-21 15:53:07 +00:00
@property
def type(self):
return _("student")
2020-09-24 16:39:55 +00:00
@property
def form_class(self):
from registration.forms import StudentRegistrationForm
return StudentRegistrationForm
2020-09-21 15:53:07 +00:00
class Meta:
verbose_name = _("student registration")
verbose_name_plural = _("student registrations")
class CoachRegistration(Registration):
2020-10-30 18:46:46 +00:00
"""
Specific registration for coaches.
They have a team and a professional activity.
"""
2020-09-22 10:27:03 +00:00
team = models.ForeignKey(
"participation.Team",
related_name="coachs",
on_delete=models.PROTECT,
null=True,
default=None,
verbose_name=_("team"),
)
2020-09-21 15:53:07 +00:00
professional_activity = models.TextField(
verbose_name=_("professional activity"),
)
@property
def type(self):
return _("coach")
2020-09-24 16:39:55 +00:00
@property
def form_class(self):
from registration.forms import CoachRegistrationForm
return CoachRegistrationForm
2020-09-21 15:53:07 +00:00
class Meta:
verbose_name = _("coach registration")
verbose_name_plural = _("coach registrations")
class AdminRegistration(Registration):
2020-10-30 18:46:46 +00:00
"""
Specific registration for admins.
They have a field to justify they status.
"""
2020-09-21 15:53:07 +00:00
role = models.TextField(
verbose_name=_("role of the administrator"),
)
@property
def type(self):
return _("admin")
2020-09-24 16:39:55 +00:00
@property
def form_class(self):
from registration.forms import AdminRegistrationForm
return AdminRegistrationForm
2020-09-21 15:53:07 +00:00
class Meta:
verbose_name = _("admin registration")
verbose_name_plural = _("admin registrations")