Merge branch 'dev' into 'master'

Dev

See merge request animath/si/plateforme-tfjm!7
This commit is contained in:
Yohann D'ANELLO 2021-01-22 08:51:59 +00:00
commit 641e53e617
22 changed files with 539 additions and 277 deletions

View File

@ -59,6 +59,21 @@ class ParticipationForm(forms.ModelForm):
fields = ('tournament',)
class MotivationLetterForm(forms.ModelForm):
def clean_file(self):
if "file" in self.files:
file = self.files["motivation_letter"]
if file.size > 2e6:
raise ValidationError(_("The uploaded file size must be under 2 Mo."))
if file.content_type not in ["application/pdf", "image/png", "image/jpeg"]:
raise ValidationError(_("The uploaded file must be a PDF, PNG of JPEG file."))
return self.cleaned_data["motivation_letter"]
class Meta:
model = Team
fields = ('motivation_letter',)
class RequestValidationForm(forms.Form):
"""
Form to ask about validation.

View File

@ -22,6 +22,8 @@ class Command(BaseCommand):
avatar_uri = "plop"
else: # pragma: no cover
if not os.path.isfile(".matrix_avatar"):
avatar_uri = Matrix.get_avatar()
if not isinstance(avatar_uri, str):
stat_file = os.stat("tfjm/static/logo.png")
with open("tfjm/static/logo.png", "rb") as f:
resp = Matrix.upload(f, filename="logo.png", content_type="image/png",

View File

@ -0,0 +1,19 @@
# Generated by Django 3.0.11 on 2021-01-22 08:15
from django.db import migrations, models
import participation.models
class Migration(migrations.Migration):
dependencies = [
('participation', '0002_auto_20210121_2206'),
]
operations = [
migrations.AddField(
model_name='team',
name='motivation_letter',
field=models.FileField(blank=True, default='', upload_to=participation.models.get_motivation_letter_filename, verbose_name='motivation letter'),
),
]

View File

@ -20,6 +20,10 @@ from tfjm.lists import get_sympa_client
from tfjm.matrix import Matrix, RoomPreset, RoomVisibility
def get_motivation_letter_filename(instance, filename):
return f"authorization/motivation_letters/motivation_letter_{instance.trigram}"
class Team(models.Model):
"""
The Team model represents a real team that participates to the TFJM².
@ -45,6 +49,13 @@ class Team(models.Model):
help_text=_("The access code let other people to join the team."),
)
motivation_letter = models.FileField(
verbose_name=_("motivation letter"),
upload_to=get_motivation_letter_filename,
blank=True,
default="",
)
@property
def students(self):
return self.participants.filter(studentregistration__isnull=False)

View File

@ -85,7 +85,25 @@
{% endif %}
{% endfor %}
</dd>
<dt class="col-sm-6 text-right">{% trans "Motivation letter:" %}</dt>
<dd class="col-sm-6">
{% if team.motivation_letter %}
<a href="{{ team.motivation_letter.url }}" data-turbolinks="false">{% trans "Download" %}</a>
{% else %}
<em>{% trans "Not uploaded yet" %}</em>
{% endif %}
{% if user.registration.team == team and not user.registration.team.participation.valid or user.registration.is_admin %}
<button class="btn btn-primary" data-toggle="modal" data-target="#uploadMotivationLetterModal">{% trans "Replace" %}</button>
{% endif %}
</dd>
</dl>
<div class="text-center">
<a class="btn btn-info" href="{% url "participation:team_authorizations" pk=team.pk %}" data-turbolinks="false">
<i class="fas fa-file-archive"></i> {% trans "Download all authorizations" %}
</a>
</div>
</div>
<div class="card-footer text-center">
<button class="btn btn-primary" data-toggle="modal" data-target="#updateTeamModal">{% trans "Update" %}</button>
@ -146,6 +164,11 @@
{% endif %}
{% endif %}
{% trans "Upload motivation letter" as modal_title %}
{% trans "Upload" as modal_button %}
{% url "participation:upload_team_motivation_letter" pk=team.pk as modal_action %}
{% include "base_modal.html" with modal_id="uploadMotivationLetter" modal_enctype="multipart/form-data" %}
{% trans "Update team" as modal_title %}
{% trans "Update" as modal_button %}
{% url "participation:update_team" pk=team.pk as modal_action %}
@ -160,6 +183,11 @@
{% block extrajavascript %}
<script>
$(document).ready(function() {
$('button[data-target="#uploadMotivationLetterModal"]').click(function() {
let modalBody = $("#uploadMotivationLetterModal div.modal-body");
if (!modalBody.html().trim())
modalBody.load("{% url "participation:upload_team_motivation_letter" pk=team.pk %} #form-content");
});
$('button[data-target="#updateTeamModal"]').click(function() {
let modalBody = $("#updateTeamModal div.modal-body");
if (!modalBody.html().trim())

View File

@ -0,0 +1,15 @@
{% extends "base.html" %}
{% load i18n static crispy_forms_filters %}
{% block content %}
<a class="btn btn-info" href="{% url "participation:team_detail" pk=object.pk %}"><i class="fas fa-arrow-left"></i> {% trans "Back to the team detail" %}</a>
<hr>
<form method="post" enctype="multipart/form-data">
<div id="form-content">
{% csrf_token %}
{{ form|crispy }}
</div>
<button class="btn btn-success" type="submit">{% trans "Upload" %}</button>
</form>
{% endblock %}

View File

@ -288,6 +288,12 @@ class TestStudentParticipation(TestCase):
self.user.registration.parental_authorization = "authorization/parental/ananas"
self.user.registration.save()
resp = self.client.get(reverse("participation:team_detail", args=(self.team.pk,)))
self.assertEqual(resp.status_code, 200)
self.assertFalse(resp.context["can_validate"])
self.team.motivation_letter = "i_am_motivated.pdf"
self.team.save()
resp = self.client.get(reverse("participation:team_detail", args=(self.team.pk,)))
self.assertEqual(resp.status_code, 200)
self.assertTrue(resp.context["can_validate"])

View File

@ -7,8 +7,8 @@ from django.views.generic import TemplateView
from .views import CreateTeamView, JoinTeamView, MyParticipationDetailView, MyTeamDetailView, NoteUpdateView, \
ParticipationDetailView, PassageCreateView, PassageDetailView, PassageUpdateView, PoolCreateView, PoolDetailView, \
PoolUpdateTeamsView, PoolUpdateView, SolutionUploadView, SynthesisUploadView, TeamAuthorizationsView, \
TeamDetailView, TeamLeaveView, TeamListView, TeamUpdateView, TournamentCreateView, TournamentDetailView, \
TournamentListView, TournamentUpdateView
TeamDetailView, TeamLeaveView, TeamListView, TeamUpdateView, TeamUploadMotivationLetterView, TournamentCreateView, \
TournamentDetailView, TournamentListView, TournamentUpdateView
app_name = "participation"
@ -20,6 +20,8 @@ urlpatterns = [
path("team/", MyTeamDetailView.as_view(), name="my_team_detail"),
path("team/<int:pk>/", TeamDetailView.as_view(), name="team_detail"),
path("team/<int:pk>/update/", TeamUpdateView.as_view(), name="update_team"),
path("team/<int:pk>/upload-motivation-letter/", TeamUploadMotivationLetterView.as_view(),
name="upload_team_motivation_letter"),
path("team/<int:pk>/authorizations/", TeamAuthorizationsView.as_view(), name="team_authorizations"),
path("team/leave/", TeamLeaveView.as_view(), name="team_leave"),
path("detail/", MyParticipationDetailView.as_view(), name="my_participation_detail"),

View File

@ -2,6 +2,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from io import BytesIO
import os
from zipfile import ZipFile
from django.contrib.auth.mixins import LoginRequiredMixin
@ -9,13 +10,13 @@ from django.contrib.sites.models import Site
from django.core.exceptions import PermissionDenied
from django.core.mail import send_mail
from django.db import transaction
from django.http import Http404, HttpResponse
from django.http import FileResponse, Http404, HttpResponse
from django.shortcuts import redirect
from django.template.loader import render_to_string
from django.urls import reverse_lazy
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django.views.generic import CreateView, DetailView, FormView, RedirectView, TemplateView, UpdateView
from django.views.generic import CreateView, DetailView, FormView, RedirectView, TemplateView, UpdateView, View
from django.views.generic.edit import FormMixin, ProcessFormView
from django_tables2 import SingleTableView
from magic import Magic
@ -24,8 +25,9 @@ from tfjm.lists import get_sympa_client
from tfjm.matrix import Matrix
from tfjm.views import AdminMixin, VolunteerMixin
from .forms import JoinTeamForm, NoteForm, ParticipationForm, PassageForm, PoolForm, PoolTeamsForm, \
RequestValidationForm, SolutionForm, SynthesisForm, TeamForm, TournamentForm, ValidateParticipationForm
from .forms import JoinTeamForm, MotivationLetterForm, NoteForm, ParticipationForm, PassageForm, PoolForm, \
PoolTeamsForm, RequestValidationForm, SolutionForm, SynthesisForm, TeamForm, TournamentForm, \
ValidateParticipationForm
from .models import Note, Participation, Passage, Pool, Solution, Synthesis, Team, Tournament
from .tables import NoteTable, ParticipationTable, PassageTable, PoolTable, TeamTable, TournamentTable
@ -178,7 +180,8 @@ class TeamDetailView(LoginRequiredMixin, FormMixin, ProcessFormView, DetailView)
all(r.email_confirmed for r in team.students.all()) and \
all(r.photo_authorization for r in team.participants.all()) and \
all(r.health_sheet for r in team.students.all() if r.under_18) and \
all(r.parental_authorization for r in team.students.all() if r.under_18)
all(r.parental_authorization for r in team.students.all() if r.under_18) and \
team.motivation_letter
return context
@ -209,7 +212,7 @@ class TeamDetailView(LoginRequiredMixin, FormMixin, ProcessFormView, DetailView)
return self.form_invalid(form)
if not self.get_context_data()["can_validate"]:
form.add_error(None, _("The team can't be validated: missing email address confirmations, "
"authorizations, people or the chosen problem is not set."))
"authorizations, people, motivation letter or the tournament is not set."))
return self.form_invalid(form)
self.object.participation.valid = False
@ -304,6 +307,55 @@ class TeamUpdateView(LoginRequiredMixin, UpdateView):
return super().form_valid(form)
class TeamUploadMotivationLetterView(LoginRequiredMixin, UpdateView):
"""
A team can send its motivation letter.
"""
model = Team
form_class = MotivationLetterForm
template_name = "participation/upload_motivation_letter.html"
extra_context = dict(title=_("Upload motivation letter"))
def dispatch(self, request, *args, **kwargs):
if not self.request.user.is_authenticated or \
not self.request.user.registration.is_admin \
and self.request.user.registration.team != self.get_object():
return self.handle_no_permission()
return super().dispatch(request, *args, **kwargs)
@transaction.atomic
def form_valid(self, form):
old_instance = Team.objects.get(pk=self.object.pk)
if old_instance.motivation_letter:
old_instance.motivation_letter.delete()
old_instance.save()
return super().form_valid(form)
class MotivationLetterView(LoginRequiredMixin, View):
"""
Display the sent motivation letter.
"""
def get(self, request, *args, **kwargs):
filename = kwargs["filename"]
path = f"media/authorization/motivation_letters/{filename}"
if not os.path.exists(path):
raise Http404
team = Team.objects.get(motivation_letter__endswith=filename)
user = request.user
if not (user.registration in team.participants.all() or user.registration.is_admin
or user.registration.is_volunteer
and team.participation.tournament in user.registration.organized_tournaments.all()):
raise PermissionDenied
# Guess mime type of the file
mime = Magic(mime=True)
mime_type = mime.from_file(path)
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
# Replace file name
true_file_name = _("Motivation letter of {team}.{ext}").format(team=str(team), ext=ext)
return FileResponse(open(path, "rb"), content_type=mime_type, filename=true_file_name)
class TeamAuthorizationsView(LoginRequiredMixin, DetailView):
"""
Get as a ZIP archive all the authorizations that are sent
@ -322,10 +374,10 @@ class TeamAuthorizationsView(LoginRequiredMixin, DetailView):
def get(self, request, *args, **kwargs):
team = self.get_object()
magic = Magic(mime=True)
output = BytesIO()
zf = ZipFile(output, "w")
for participant in team.participants.all():
magic = Magic(mime=True)
if participant.photo_authorization:
mime_type = magic.from_file("media/" + participant.photo_authorization.name)
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
@ -344,6 +396,12 @@ class TeamAuthorizationsView(LoginRequiredMixin, DetailView):
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
zf.write("media/" + participant.health_sheet.name,
_("Health sheet of {participant}.{ext}").format(participant=str(participant), ext=ext))
if team.motivation_letter:
mime_type = magic.from_file("media/" + team.motivation_letter.name)
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
zf.write("media/" + team.motivation_letter.name,
_("Motivation letter of {team}.{ext}").format(team=str(team), ext=ext))
zf.close()
response = HttpResponse(content_type="application/zip")
response["Content-Disposition"] = "attachment; filename=\"{filename}\"" \
@ -518,6 +576,7 @@ class SolutionUploadView(LoginRequiredMixin, FormView):
# Drop previous solution if existing
for sol in sol_qs.all():
sol.file.delete()
sol.save()
sol.delete()
form_sol.participation = self.participation
form_sol.final = self.participation.final
@ -698,6 +757,7 @@ class SynthesisUploadView(LoginRequiredMixin, FormView):
# Drop previous solution if existing
for syn in syn_qs.all():
syn.file.delete()
syn.save()
syn.delete()
form_syn.participation = self.participation
form_syn.passage = self.passage

View File

@ -100,7 +100,7 @@ class StudentRegistrationForm(forms.ModelForm):
"""
class Meta:
model = StudentRegistration
fields = ('team', 'student_class', 'birth_date', 'address', 'phone_number',
fields = ('team', 'student_class', 'birth_date', 'gender', 'address', 'phone_number',
'school', 'responsible_name', 'responsible_phone', 'responsible_email',
'give_contact_to_animath', 'email_confirmed',)
@ -177,7 +177,7 @@ class CoachRegistrationForm(forms.ModelForm):
"""
class Meta:
model = CoachRegistration
fields = ('team', 'birth_date', 'address', 'phone_number', 'professional_activity',
fields = ('team', 'birth_date', 'gender', 'address', 'phone_number', 'professional_activity',
'give_contact_to_animath', 'email_confirmed',)

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.11 on 2021-01-22 08:00
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('registration', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='participantregistration',
name='gender',
field=models.CharField(choices=[('female', 'Female'), ('male', 'Male'), ('other', 'Other')], default='other', max_length=6, verbose_name='gender'),
),
]

View File

@ -128,6 +128,17 @@ class ParticipantRegistration(Registration):
default=date.today,
)
gender = models.CharField(
max_length=6,
verbose_name=_("gender"),
choices=[
("female", _("Female")),
("male", _("Male")),
("other", _("Other")),
],
default="other",
)
address = AddressField(
verbose_name=_("address"),
null=True,

View File

@ -13,7 +13,7 @@
</p>
<p>
Vous avez été invités par {{ inviter }} à rejoindre la plateforme du TFJM², accessible à l'adresse
Vous avez été invités par {{ inviter.registration }} à rejoindre la plateforme du TFJM², accessible à l'adresse
<a href="https://{{ domain }}/">https://{{ domain }}/</a>. Vous disposez d'un compte d'organisateur.
</p>

View File

@ -2,7 +2,7 @@
Bonjour {{ user.registration }},
Vous avez été invités par {{ inviter }} à rejoindre la plateforme du TFJM², accessible à l'adresse
Vous avez été invités par {{ inviter.registration }} à rejoindre la plateforme du TFJM², accessible à l'adresse
https://{{ domain }}/. Vous disposez d'un compte d'organisateur.
Un mot de passe aléatoire a été défini : {{ password }}.

View File

@ -16,7 +16,7 @@
<dt class="col-sm-6 text-right">{% trans "First name:" %}</dt>
<dd class="col-sm-6">{{ user_object.first_name }}</dd>
StudentRegistrationForm(
<dt class="col-sm-6 text-right">{% trans "Email:" %}</dt>
<dd class="col-sm-6"><a href="mailto:{{ user_object.email }}">{{ user_object.email }}</a>
{% if not user_object.registration.email_confirmed %} (<em>{% trans "Not confirmed" %}, <a href="{% url "registration:email_validation_resend" pk=user_object.pk %}">{% trans "resend the validation link" %}</a></em>){% endif %}</dd>
@ -42,6 +42,9 @@
<dt class="col-sm-6 text-right">{% trans "Birth date:" %}</dt>
<dd class="col-sm-6">{{ user_object.registration.birth_date }}</dd>
<dt class="col-sm-6 text-right">{% trans "Gender:" %}</dt>
<dd class="col-sm-6">{{ user_object.registration.get_gender_display }}</dd>
<dt class="col-sm-6 text-right">{% trans "Address:" %}</dt>
<dd class="col-sm-6">{{ user_object.registration.address }}</dd>

View File

@ -130,6 +130,7 @@ class TestRegistration(TestCase):
student_class=12,
school="God",
birth_date="2000-01-01",
gender="other",
address="1 Rue de Rivoli, 75001 Paris, France",
phone_number="0123456789",
responsible_name="Toto",
@ -153,6 +154,7 @@ class TestRegistration(TestCase):
student_class=12,
school="God",
birth_date="2000-01-01",
gender="other",
address="1 Rue de Rivoli, 75001 Paris, France",
phone_number="0123456789",
responsible_name="Toto",
@ -173,6 +175,7 @@ class TestRegistration(TestCase):
password2="azertyuiopazertyuiop",
role="coach",
birth_date="1980-01-01",
gender="other",
address="1 Rue de Rivoli, 75001 Paris, France",
phone_number="0123456789",
professional_activity="God",
@ -252,10 +255,10 @@ class TestRegistration(TestCase):
for user, data in [(self.user, dict(role="Bot")),
(self.student, dict(student_class=11, school="Sky", birth_date="2001-01-01",
address="1 Rue de Rivoli, 75001 Paris, France", responsible_name="Toto",
responsible_email="toto@example.com")),
gender="female", address="1 Rue de Rivoli, 75001 Paris, France",
responsible_name="Toto", responsible_email="toto@example.com")),
(self.coach, dict(professional_activity="God", birth_date="2001-01-01",
address="1 Rue de Rivoli, 75001 Paris, France"))]:
gender="male", address="1 Rue de Rivoli, 75001 Paris, France"))]:
response = self.client.get(reverse("registration:update_user", args=(user.pk,)))
self.assertEqual(response.status_code, 200)
@ -340,6 +343,7 @@ class TestRegistration(TestCase):
self.student.registration.refresh_from_db()
self.student.registration.photo_authorization.delete()
self.student.registration.save()
def test_user_detail_forbidden(self):
"""

View File

@ -329,6 +329,7 @@ class UserUploadPhotoAuthorizationView(UserMixin, UpdateView):
old_instance = StudentRegistration.objects.get(pk=self.object.pk)
if old_instance.photo_authorization:
old_instance.photo_authorization.delete()
old_instance.save()
return super().form_valid(form)
def get_success_url(self):
@ -355,6 +356,7 @@ class UserUploadHealthSheetView(UserMixin, UpdateView):
old_instance = StudentRegistration.objects.get(pk=self.object.pk)
if old_instance.health_sheet:
old_instance.health_sheet.delete()
old_instance.save()
return super().form_valid(form)
def get_success_url(self):
@ -381,6 +383,7 @@ class UserUploadParentalAuthorizationView(UserMixin, UpdateView):
old_instance = StudentRegistration.objects.get(pk=self.object.pk)
if old_instance.parental_authorization:
old_instance.parental_authorization.delete()
old_instance.save()
return super().form_valid(form)
def get_success_url(self):

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,7 @@ _client = None
def get_sympa_client():
global _client
if _client is None:
if os.getenv("SYMPA_PASSWORD", None) is not None: # pragma: no cover
if os.getenv("SYMPA_PASSWORD", None): # pragma: no cover
from sympasoap import Client
_client = Client("https://" + os.getenv("SYMPA_URL"))
_client.login(os.getenv("SYMPA_EMAIL"), os.getenv("SYMPA_PASSWORD"))

View File

@ -68,6 +68,16 @@ class Matrix:
client = await cls._get_client()
return await client.set_avatar(avatar_url)
@classmethod
@async_to_sync
async def get_avatar(cls): # pragma: no cover
"""
Set the display avatar of the bot account.
"""
client = await cls._get_client()
resp = await client.get_avatar()
return resp.avatar_url if resp.status_code == 200 else resp
@classmethod
@async_to_sync
async def upload(

View File

@ -20,8 +20,8 @@ EMAIL_PORT = os.getenv("SMTP_PORT")
EMAIL_HOST_USER = os.getenv("SMTP_HOST_USER")
EMAIL_HOST_PASSWORD = os.getenv("SMTP_HOST_PASSWORD")
DEFAULT_FROM_EMAIL = os.getenv('FROM_EMAIL', 'Contact TFJM² <contact@tfjm.org>')
SERVER_EMAIL = os.getenv('SERVER_EMAIL', 'contact@tfjm.org')
DEFAULT_FROM_EMAIL = os.getenv('FROM_EMAIL', 'Contact TFJM²') + f" <{SERVER_EMAIL}>"
# Security settings
SECURE_CONTENT_TYPE_NOSNIFF = False

View File

@ -21,6 +21,7 @@ from django.contrib import admin
from django.urls import include, path
from django.views.defaults import bad_request, page_not_found, permission_denied, server_error
from django.views.generic import TemplateView
from participation.views import MotivationLetterView
from registration.views import HealthSheetView, ParentalAuthorizationView, PhotoAuthorizationView, \
ScholarshipView, SolutionView, SynthesisView
@ -47,6 +48,8 @@ urlpatterns = [
name='parental_authorization'),
path('media/authorization/scholarship/<str:filename>/', ScholarshipView.as_view(),
name='scholarship'),
path('media/authorization/motivation_letters/<str:filename>/', MotivationLetterView.as_view(),
name='scholarship'),
path('media/solutions/<str:filename>/', SolutionView.as_view(),
name='solution'),