From 41817421332f435deac926f35e95cd08746575a0 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 2 Nov 2020 18:25:32 +0100 Subject: [PATCH] Linting --- apps/participation/tests.py | 3 +- apps/participation/views.py | 102 ++++++++++++++++++++---------------- apps/registration/tests.py | 5 +- 3 files changed, 59 insertions(+), 51 deletions(-) diff --git a/apps/participation/tests.py b/apps/participation/tests.py index 9cdecfb..f695b67 100644 --- a/apps/participation/tests.py +++ b/apps/participation/tests.py @@ -113,8 +113,7 @@ class TestStudentParticipation(TestCase): response = self.client.get(reverse("admin:index") + "participation/phase/") self.assertEqual(response.status_code, 200) - response = self.client.get(reverse("admin:index") - + f"participation/phase/1/change/") + response = self.client.get(reverse("admin:index") + "participation/phase/1/change/") self.assertEqual(response.status_code, 200) def test_create_team(self): diff --git a/apps/participation/views.py b/apps/participation/views.py index 3098a06..ee43067 100644 --- a/apps/participation/views.py +++ b/apps/participation/views.py @@ -176,55 +176,65 @@ class TeamDetailView(LoginRequiredMixin, FormMixin, ProcessFormView, DetailView) def form_valid(self, form): self.object = self.get_object() if isinstance(form, RequestValidationForm): - if not self.request.user.registration.participates: - form.add_error(None, _("You don't participate, so you can't request the validation of the team.")) - return self.form_invalid(form) - if self.object.participation.valid is not None: - form.add_error(None, _("The validation of the team is already done or pending.")) - return self.form_invalid(form) - if not (self.object.students.count() >= 3 and - all(r.email_confirmed for r in self.object.students.all()) and - all(r.photo_authorization for r in self.object.students.all()) and - self.object.participation.problem): - form.add_error(None, _("The team can't be validated: missing email address confirmations, " - "photo authorizations, people or the chosen problem is not set.")) - return self.form_invalid(form) - - self.object.participation.valid = False - self.object.participation.save() - - for admin in AdminRegistration.objects.all(): - mail_context = dict(user=admin.user, team=self.object) - mail_plain = render_to_string("participation/mails/request_validation.txt", mail_context) - mail_html = render_to_string("participation/mails/request_validation.html", mail_context) - admin.user.email_user("[Corres2math] Validation d'équipe", mail_plain, html_message=mail_html) + return self.handle_request_validation(form) elif isinstance(form, ValidateParticipationForm): - if not self.request.user.registration.is_admin: - form.add_error(None, _("You are not an administrator.")) - return self.form_invalid(form) - elif self.object.participation.valid is not False: - form.add_error(None, _("This team has no pending validation.")) - return self.form_invalid(form) + return self.handle_validate_participation(form) + return self.form_invalid(form) - if "validate" in self.request.POST: - self.object.participation.valid = True - self.object.participation.save() - mail_context = dict(team=self.object, message=form.cleaned_data["message"]) - mail_plain = render_to_string("participation/mails/team_validated.txt", mail_context) - mail_html = render_to_string("participation/mails/team_validated.html", mail_context) - send_mail("[Corres2math] Équipe validée", mail_plain, None, [self.object.email], html_message=mail_html) - elif "invalidate" in self.request.POST: - self.object.participation.valid = None - self.object.participation.save() - mail_context = dict(team=self.object, message=form.cleaned_data["message"]) - mail_plain = render_to_string("participation/mails/team_not_validated.txt", mail_context) - mail_html = render_to_string("participation/mails/team_not_validated.html", mail_context) - send_mail("[Corres2math] Équipe non validée", mail_plain, None, [self.object.email], - html_message=mail_html) - else: - form.add_error(None, _("You must specify if you validate the registration or not.")) - return self.form_invalid(form) + def handle_request_validation(self, form): + """ + A team requests to be validated + """ + if not self.request.user.registration.participates: + form.add_error(None, _("You don't participate, so you can't request the validation of the team.")) + return self.form_invalid(form) + if self.object.participation.valid is not None: + form.add_error(None, _("The validation of the team is already done or pending.")) + 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, " + "photo authorizations, people or the chosen problem is not set.")) + return self.form_invalid(form) + self.object.participation.valid = False + self.object.participation.save() + + for admin in AdminRegistration.objects.all(): + mail_context = dict(user=admin.user, team=self.object) + mail_plain = render_to_string("participation/mails/request_validation.txt", mail_context) + mail_html = render_to_string("participation/mails/request_validation.html", mail_context) + admin.user.email_user("[Corres2math] Validation d'équipe", mail_plain, html_message=mail_html) + return self.form_valid(form) + + def handle_validate_participation(self, form): + """ + An admin validates the team (or not) + """ + if not self.request.user.registration.is_admin: + form.add_error(None, _("You are not an administrator.")) + return self.form_invalid(form) + elif self.object.participation.valid is not False: + form.add_error(None, _("This team has no pending validation.")) + return self.form_invalid(form) + + if "validate" in self.request.POST: + self.object.participation.valid = True + self.object.participation.save() + mail_context = dict(team=self.object, message=form.cleaned_data["message"]) + mail_plain = render_to_string("participation/mails/team_validated.txt", mail_context) + mail_html = render_to_string("participation/mails/team_validated.html", mail_context) + send_mail("[Corres2math] Équipe validée", mail_plain, None, [self.object.email], html_message=mail_html) + elif "invalidate" in self.request.POST: + self.object.participation.valid = None + self.object.participation.save() + mail_context = dict(team=self.object, message=form.cleaned_data["message"]) + mail_plain = render_to_string("participation/mails/team_not_validated.txt", mail_context) + mail_html = render_to_string("participation/mails/team_not_validated.html", mail_context) + send_mail("[Corres2math] Équipe non validée", mail_plain, None, [self.object.email], + html_message=mail_html) + else: + form.add_error(None, _("You must specify if you validate the registration or not.")) + return self.form_invalid(form) return super().form_valid(form) def get_success_url(self): diff --git a/apps/registration/tests.py b/apps/registration/tests.py index d239349..5991cdb 100644 --- a/apps/registration/tests.py +++ b/apps/registration/tests.py @@ -1,10 +1,9 @@ import os -from django.contrib.sites.models import Site - from corres2math.tokens import email_validation_token -from django.contrib.contenttypes.models import ContentType from django.contrib.auth.models import User +from django.contrib.contenttypes.models import ContentType +from django.contrib.sites.models import Site from django.core.management import call_command from django.test import TestCase from django.urls import reverse