1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2025-10-18 21:06:41 +02:00

Comment code, fix minor issues

This commit is contained in:
Yohann D'ANELLO
2020-05-11 14:08:19 +02:00
parent c9b9d01523
commit a561364bd0
22 changed files with 650 additions and 179 deletions

View File

@@ -12,26 +12,33 @@ from django.utils.translation import gettext_lazy as _
from django.views import View
from django.views.generic import CreateView, UpdateView, DetailView, FormView
from django_tables2 import SingleTableView
from tournament.forms import TeamForm, JoinTeam
from tournament.models import Team
from tournament.views import AdminMixin, TeamMixin
from .forms import SignUpForm, TFJMUserForm, AdminUserForm, CoachUserForm
from .models import TFJMUser, Document, Solution, MotivationLetter, Synthesis
from .tables import UserTable
class CreateUserView(CreateView):
"""
Signup form view.
"""
model = TFJMUser
form_class = SignUpForm
template_name = "registration/signup.html"
class MyAccountView(LoginRequiredMixin, UpdateView):
"""
Update our personal data.
"""
model = TFJMUser
template_name = "member/my_account.html"
def get_form_class(self):
# The used form can change according to the role of the user.
return AdminUserForm if self.request.user.organizes else TFJMUserForm \
if self.request.user.role == "3participant" else CoachUserForm
@@ -40,6 +47,10 @@ class MyAccountView(LoginRequiredMixin, UpdateView):
class UserDetailView(LoginRequiredMixin, DetailView):
"""
View the personal information of a given user.
Only organizers can see this page, since there are personal data.
"""
model = TFJMUser
form_class = TFJMUserForm
context_object_name = "tfjmuser"
@@ -57,7 +68,10 @@ class UserDetailView(LoginRequiredMixin, DetailView):
return super().dispatch(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
if "view_as" in request.POST:
"""
An administrator can log in through this page as someone else, and act as this other person.
"""
if "view_as" in request.POST and self.request.admin:
session = request.session
session["admin"] = request.user.pk
obj = self.get_object()
@@ -74,6 +88,10 @@ class UserDetailView(LoginRequiredMixin, DetailView):
class AddTeamView(LoginRequiredMixin, CreateView):
"""
Register a new team.
Users can choose the name, the trigram and a preferred tournament.
"""
model = Team
form_class = TeamForm
@@ -86,6 +104,7 @@ class AddTeamView(LoginRequiredMixin, CreateView):
form.add_error('name', _("You are already in a team."))
return self.form_invalid(form)
# Generate a random access code
team = form.instance
alphabet = "0123456789abcdefghijklmnopqrstuvwxyz0123456789"
code = ""
@@ -107,6 +126,9 @@ class AddTeamView(LoginRequiredMixin, CreateView):
class JoinTeamView(LoginRequiredMixin, FormView):
"""
Join a team with a given access code.
"""
model = Team
form_class = JoinTeam
template_name = "tournament/team_form.html"
@@ -122,7 +144,7 @@ class JoinTeamView(LoginRequiredMixin, FormView):
form.add_error('access_code', _("You are already in a team."))
return self.form_invalid(form)
if self.request.user.role == '2coach' and len(team.encadrants) == 3:
if self.request.user.role == '2coach' and len(team.coaches) == 3:
form.add_error('access_code', _("This team is full of coachs."))
return self.form_invalid(form)
@@ -130,6 +152,9 @@ class JoinTeamView(LoginRequiredMixin, FormView):
form.add_error('access_code', _("This team is full of participants."))
return self.form_invalid(form)
if not team.invalid:
form.add_error('access_code', _("This team is already validated or waiting for validation."))
self.request.user.team = team
self.request.user.save()
return super().form_valid(form)
@@ -139,11 +164,24 @@ class JoinTeamView(LoginRequiredMixin, FormView):
class MyTeamView(TeamMixin, View):
"""
Redirect to the page of the information of our personal team.
"""
def get(self, request, *args, **kwargs):
return redirect("tournament:team_detail", pk=request.user.team.pk)
class DocumentView(LoginRequiredMixin, View):
"""
View a PDF document, if we have the right.
- Everyone can see the documents that concern itself.
- An administrator can see anything.
- An organizer can see documents that are related to its tournament.
- A jury can see solutions and syntheses that are evaluated in their pools.
"""
def get(self, request, *args, **kwargs):
doc = Document.objects.get(file=self.kwargs["file"])
@@ -172,6 +210,9 @@ class DocumentView(LoginRequiredMixin, View):
class ProfileListView(AdminMixin, SingleTableView):
"""
List all registered profiles.
"""
model = TFJMUser
queryset = TFJMUser.objects.order_by("role", "last_name", "first_name")
table_class = UserTable
@@ -180,6 +221,9 @@ class ProfileListView(AdminMixin, SingleTableView):
class OrphanedProfileListView(AdminMixin, SingleTableView):
"""
List all orphaned profiles, ie. participants that have no team.
"""
model = TFJMUser
queryset = TFJMUser.objects.filter((Q(role="2coach") | Q(role="3participant")) & Q(team__isnull=True))\
.order_by("role", "last_name", "first_name")
@@ -189,6 +233,9 @@ class OrphanedProfileListView(AdminMixin, SingleTableView):
class OrganizersListView(AdminMixin, SingleTableView):
"""
List all organizers.
"""
model = TFJMUser
queryset = TFJMUser.objects.filter(Q(role="0admin") | Q(role="1volunteer"))\
.order_by("role", "last_name", "first_name")
@@ -198,6 +245,10 @@ class OrganizersListView(AdminMixin, SingleTableView):
class ResetAdminView(AdminMixin, View):
"""
Return to admin view, clear the session field that let an administrator to log in as someone else.
"""
def dispatch(self, request, *args, **kwargs):
if "_fake_user_id" in request.session:
del request.session["_fake_user_id"]