plateforme-tfjm2/apps/member/views.py

202 lines
6.8 KiB
Python
Raw Normal View History

2020-05-04 22:56:34 +00:00
import random
2020-05-05 02:45:38 +00:00
from datetime import datetime
2020-05-04 22:56:34 +00:00
2020-04-29 23:20:50 +00:00
from django.contrib.auth.mixins import LoginRequiredMixin
2020-05-04 19:02:57 +00:00
from django.contrib.auth.models import AnonymousUser
2020-04-29 23:20:50 +00:00
from django.core.exceptions import PermissionDenied
2020-04-30 19:07:12 +00:00
from django.db.models import Q
2020-04-29 23:20:50 +00:00
from django.http import FileResponse
2020-05-04 19:02:57 +00:00
from django.shortcuts import redirect
2020-05-04 22:56:34 +00:00
from django.urls import reverse_lazy
2020-04-30 19:07:12 +00:00
from django.utils.translation import gettext_lazy as _
2020-04-29 23:20:50 +00:00
from django.views import View
2020-05-04 22:56:34 +00:00
from django.views.generic import CreateView, UpdateView, DetailView, FormView
2020-04-30 19:07:12 +00:00
from django_tables2 import SingleTableView
2020-04-29 02:06:02 +00:00
2020-05-04 22:56:34 +00:00
from tournament.forms import TeamForm, JoinTeam
2020-05-04 20:27:45 +00:00
from tournament.models import Team
from tournament.views import AdminMixin, TeamMixin
2020-05-04 23:04:07 +00:00
from .forms import SignUpForm, TFJMUserForm, AdminUserForm, CoachUserForm
2020-05-04 21:37:21 +00:00
from .models import TFJMUser, Document, Solution, MotivationLetter, Synthesis
2020-04-30 19:07:12 +00:00
from .tables import UserTable
2020-04-29 13:29:01 +00:00
class CreateUserView(CreateView):
model = TFJMUser
form_class = SignUpForm
template_name = "registration/signup.html"
2020-04-29 23:20:50 +00:00
2020-05-04 18:21:53 +00:00
class MyAccountView(LoginRequiredMixin, UpdateView):
model = TFJMUser
template_name = "member/my_account.html"
2020-05-04 23:04:07 +00:00
def get_form_class(self):
return AdminUserForm if self.request.user.organizes else TFJMUserForm \
if self.request.user.role == "3participant" else CoachUserForm
2020-05-04 18:21:53 +00:00
def get_object(self, queryset=None):
return self.request.user
class UserDetailView(LoginRequiredMixin, DetailView):
model = TFJMUser
form_class = TFJMUserForm
2020-05-04 19:02:57 +00:00
context_object_name = "tfjmuser"
2020-05-04 18:21:53 +00:00
def dispatch(self, request, *args, **kwargs):
2020-05-04 19:02:57 +00:00
if isinstance(request.user, AnonymousUser):
raise PermissionDenied
self.object = self.get_object()
2020-05-04 18:21:53 +00:00
if not request.user.admin \
2020-05-04 19:02:57 +00:00
and (self.object.team is not None and request.user not in self.object.team.tournament.organizers.all())\
2020-05-04 18:21:53 +00:00
and self.request.user != self.object:
raise PermissionDenied
return super().dispatch(request, *args, **kwargs)
2020-05-04 19:02:57 +00:00
def post(self, request, *args, **kwargs):
if "view_as" in request.POST:
session = request.session
session["admin"] = request.user.pk
obj = self.get_object()
session["_fake_user_id"] = obj.pk
return redirect(request.path)
return self.get(request, *args, **kwargs)
2020-05-04 18:21:53 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["title"] = str(self.object)
return context
2020-05-04 22:56:34 +00:00
class AddTeamView(LoginRequiredMixin, CreateView):
2020-05-04 20:27:45 +00:00
model = Team
2020-05-04 22:56:34 +00:00
form_class = TeamForm
2020-05-04 20:27:45 +00:00
2020-05-04 22:56:34 +00:00
def form_valid(self, form):
2020-05-05 00:20:45 +00:00
if self.request.user.organizes:
form.add_error('name', _("You can't organize and participate at the same time."))
return self.form_invalid(form)
if self.request.user.team:
form.add_error('name', _("You are already in a team."))
return self.form_invalid(form)
2020-05-04 22:56:34 +00:00
team = form.instance
alphabet = "0123456789abcdefghijklmnopqrstuvwxyz0123456789"
code = ""
2020-05-05 00:20:45 +00:00
for i in range(6):
2020-05-04 22:56:34 +00:00
code += random.choice(alphabet)
team.access_code = code
team.validation_status = "0invalid"
2020-05-04 20:27:45 +00:00
2020-05-04 22:56:34 +00:00
team.save()
team.refresh_from_db()
2020-05-04 20:27:45 +00:00
2020-05-04 22:56:34 +00:00
self.request.user.team = team
self.request.user.save()
2020-05-04 20:27:45 +00:00
2020-05-04 22:56:34 +00:00
return super().form_valid(form)
2020-05-04 20:27:45 +00:00
2020-05-04 22:56:34 +00:00
def get_success_url(self):
return reverse_lazy("member:my_team")
2020-05-04 20:27:45 +00:00
2020-05-04 22:56:34 +00:00
class JoinTeamView(LoginRequiredMixin, FormView):
model = Team
form_class = JoinTeam
template_name = "tournament/team_form.html"
def form_valid(self, form):
team = form.cleaned_data["team"]
2020-05-05 00:20:45 +00:00
if self.request.user.organizes:
form.add_error('access_code', _("You can't organize and participate at the same time."))
return self.form_invalid(form)
if self.request.user.team:
form.add_error('access_code', _("You are already in a team."))
return self.form_invalid(form)
if self.request.user.role == '2coach' and team.encadrants.size() == 3:
form.add_error('access_code', _("This team is full of coachs."))
return self.form_invalid(form)
if self.request.user.role == '3participant' and team.participants.size() == 5:
form.add_error('access_code', _("This team is full of participants."))
return self.form_invalid(form)
2020-05-04 22:56:34 +00:00
self.request.user.team = team
self.request.user.save()
return super().form_valid(form)
def get_success_url(self):
return reverse_lazy("member:my_team")
class MyTeamView(TeamMixin, View):
def get(self, request, *args, **kwargs):
return redirect("tournament:team_detail", pk=request.user.team.pk)
2020-05-04 20:27:45 +00:00
2020-04-29 23:20:50 +00:00
class DocumentView(LoginRequiredMixin, View):
def get(self, request, *args, **kwargs):
doc = Document.objects.get(file=self.kwargs["file"])
2020-05-04 21:37:21 +00:00
grant = request.user.admin
if isinstance(doc, Solution) or isinstance(doc, Synthesis) or isinstance(doc, MotivationLetter):
grant = grant or doc.team == request.user.team or request.user in doc.team.tournament.organizers.all()
2020-05-05 02:45:38 +00:00
if isinstance(doc, Synthesis) and request.user.organizes:
grant = True
if isinstance(doc, Solution):
for pool in doc.pools.all():
if pool.round == 2 and datetime.now() < doc.tournament.date_solutions_2:
continue
if self.request.user.team in pool.teams.all():
grant = True
2020-05-04 21:37:21 +00:00
if not grant:
2020-04-29 23:20:50 +00:00
raise PermissionDenied
return FileResponse(doc.file, content_type="application/pdf")
2020-04-30 19:07:12 +00:00
2020-05-04 18:21:53 +00:00
class ProfileListView(AdminMixin, SingleTableView):
2020-04-30 19:07:12 +00:00
model = TFJMUser
queryset = TFJMUser.objects.order_by("role", "last_name", "first_name")
table_class = UserTable
template_name = "member/profile_list.html"
extra_context = dict(title=_("All profiles"))
2020-05-04 18:21:53 +00:00
class OrphanedProfileListView(AdminMixin, SingleTableView):
2020-04-30 19:07:12 +00:00
model = TFJMUser
queryset = TFJMUser.objects.filter((Q(role="2coach") | Q(role="3participant")) & Q(team__isnull=True))\
.order_by("role", "last_name", "first_name")
table_class = UserTable
template_name = "member/profile_list.html"
extra_context = dict(title=_("Orphaned profiles"))
2020-05-04 18:21:53 +00:00
class OrganizersListView(AdminMixin, SingleTableView):
2020-04-30 19:07:12 +00:00
model = TFJMUser
queryset = TFJMUser.objects.filter(Q(role="0admin") | Q(role="1volunteer"))\
.order_by("role", "last_name", "first_name")
table_class = UserTable
template_name = "member/profile_list.html"
extra_context = dict(title=_("Organizers"))
2020-05-04 19:02:57 +00:00
class ResetAdminView(AdminMixin, View):
def dispatch(self, request, *args, **kwargs):
if "_fake_user_id" in request.session:
del request.session["_fake_user_id"]
return redirect(request.GET["path"])