diff --git a/apps/participation/forms.py b/apps/participation/forms.py index 02a8279..782cdc1 100644 --- a/apps/participation/forms.py +++ b/apps/participation/forms.py @@ -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. diff --git a/apps/participation/management/commands/fix_matrix_channels.py b/apps/participation/management/commands/fix_matrix_channels.py index ee033d0..5b67eb1 100644 --- a/apps/participation/management/commands/fix_matrix_channels.py +++ b/apps/participation/management/commands/fix_matrix_channels.py @@ -22,14 +22,16 @@ class Command(BaseCommand): avatar_uri = "plop" else: # pragma: no cover if not os.path.isfile(".matrix_avatar"): - 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", - filesize=stat_file.st_size)[0][0] - avatar_uri = resp.content_uri - with open(".matrix_avatar", "w") as f: - f.write(avatar_uri) - Matrix.set_avatar(avatar_uri) + 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", + filesize=stat_file.st_size)[0][0] + avatar_uri = resp.content_uri + with open(".matrix_avatar", "w") as f: + f.write(avatar_uri) + Matrix.set_avatar(avatar_uri) with open(".matrix_avatar", "r") as f: avatar_uri = f.read().rstrip(" \t\r\n") diff --git a/apps/participation/migrations/0003_team_motivation_letter.py b/apps/participation/migrations/0003_team_motivation_letter.py new file mode 100644 index 0000000..fb298af --- /dev/null +++ b/apps/participation/migrations/0003_team_motivation_letter.py @@ -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'), + ), + ] diff --git a/apps/participation/models.py b/apps/participation/models.py index d19a0d6..912125b 100644 --- a/apps/participation/models.py +++ b/apps/participation/models.py @@ -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) diff --git a/apps/participation/templates/participation/team_detail.html b/apps/participation/templates/participation/team_detail.html index 03dc45f..e3a7649 100644 --- a/apps/participation/templates/participation/team_detail.html +++ b/apps/participation/templates/participation/team_detail.html @@ -85,7 +85,25 @@ {% endif %} {% endfor %} + +
{% trans "Motivation letter:" %}
+
+ {% if team.motivation_letter %} + {% trans "Download" %} + {% else %} + {% trans "Not uploaded yet" %} + {% endif %} + {% if user.registration.team == team and not user.registration.team.participation.valid or user.registration.is_admin %} + + {% endif %} +
+ +
+ + {% trans "Download all authorizations" %} + +