2020-09-27 16:40:45 +00:00
|
|
|
from io import BytesIO
|
|
|
|
from zipfile import ZipFile
|
|
|
|
|
2020-10-09 11:58:42 +00:00
|
|
|
from corres2math.lists import get_sympa_client
|
2020-09-22 18:41:42 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2020-09-24 08:21:50 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2020-09-23 21:20:44 +00:00
|
|
|
from django.db import transaction
|
2020-10-09 11:58:42 +00:00
|
|
|
from django.http import HttpResponse
|
2020-09-23 21:20:44 +00:00
|
|
|
from django.urls import reverse_lazy
|
2020-09-22 18:41:42 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-09-24 10:06:33 +00:00
|
|
|
from django.views.generic import CreateView, DetailView, FormView, RedirectView, UpdateView
|
2020-09-27 16:40:45 +00:00
|
|
|
from magic import Magic
|
2020-09-21 13:41:55 +00:00
|
|
|
|
2020-09-27 12:32:05 +00:00
|
|
|
from .forms import JoinTeamForm, ParticipationForm, TeamForm, UploadVideoForm
|
|
|
|
from .models import Participation, Team, Video
|
2020-09-22 18:41:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CreateTeamView(LoginRequiredMixin, CreateView):
|
|
|
|
model = Team
|
|
|
|
form_class = TeamForm
|
|
|
|
extra_context = dict(title=_("Create team"))
|
|
|
|
template_name = "participation/create_team.html"
|
|
|
|
|
2020-09-27 14:35:31 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
user = request.user
|
2020-09-23 21:20:44 +00:00
|
|
|
registration = user.registration
|
|
|
|
if not registration.participates:
|
2020-09-27 14:35:31 +00:00
|
|
|
raise PermissionDenied(_("You don't participate, so you can't create a team."))
|
2020-09-23 21:20:44 +00:00
|
|
|
elif registration.team:
|
2020-09-27 14:35:31 +00:00
|
|
|
raise PermissionDenied(_("You are already in a team."))
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
2020-09-23 21:20:44 +00:00
|
|
|
|
2020-09-27 14:35:31 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def form_valid(self, form):
|
2020-09-23 21:20:44 +00:00
|
|
|
ret = super().form_valid(form)
|
2020-09-27 14:35:31 +00:00
|
|
|
user = self.request.user
|
|
|
|
registration = user.registration
|
2020-09-23 21:20:44 +00:00
|
|
|
registration.team = form.instance
|
|
|
|
registration.save()
|
2020-10-09 11:49:09 +00:00
|
|
|
get_sympa_client().subscribe(user.email, f"equipe-{form.instance.trigram.lower()}", False,
|
|
|
|
f"{user.first_name} {user.last_name}")
|
2020-09-23 21:20:44 +00:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2020-09-24 12:58:35 +00:00
|
|
|
return reverse_lazy("participation:team_detail", args=(self.object.pk,))
|
2020-09-23 21:20:44 +00:00
|
|
|
|
2020-09-22 18:41:42 +00:00
|
|
|
|
|
|
|
class JoinTeamView(LoginRequiredMixin, FormView):
|
|
|
|
model = Team
|
|
|
|
form_class = JoinTeamForm
|
|
|
|
extra_context = dict(title=_("Join team"))
|
|
|
|
template_name = "participation/create_team.html"
|
2020-09-23 21:20:44 +00:00
|
|
|
|
2020-09-27 14:35:31 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
user = request.user
|
2020-09-23 21:20:44 +00:00
|
|
|
registration = user.registration
|
|
|
|
if not registration.participates:
|
2020-09-27 14:35:31 +00:00
|
|
|
raise PermissionDenied(_("You don't participate, so you can't create a team."))
|
2020-09-23 21:20:44 +00:00
|
|
|
elif registration.team:
|
2020-09-27 14:35:31 +00:00
|
|
|
raise PermissionDenied(_("You are already in a team."))
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
2020-09-23 21:20:44 +00:00
|
|
|
|
2020-09-27 14:35:31 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def form_valid(self, form):
|
2020-09-24 12:58:35 +00:00
|
|
|
self.object = form.instance
|
2020-09-23 21:20:44 +00:00
|
|
|
ret = super().form_valid(form)
|
2020-09-27 14:35:31 +00:00
|
|
|
user = self.request.user
|
|
|
|
registration = user.registration
|
2020-09-23 21:20:44 +00:00
|
|
|
registration.team = form.instance
|
|
|
|
registration.save()
|
2020-10-09 11:49:09 +00:00
|
|
|
get_sympa_client().subscribe(user.email, f"equipe-{form.instance.trigram.lower()}", False,
|
|
|
|
f"{user.first_name} {user.last_name}")
|
2020-09-23 21:20:44 +00:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2020-09-24 12:58:35 +00:00
|
|
|
return reverse_lazy("participation:team_detail", args=(self.object.pk,))
|
2020-09-24 08:21:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MyTeamDetailView(LoginRequiredMixin, RedirectView):
|
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
|
|
|
user = self.request.user
|
|
|
|
registration = user.registration
|
|
|
|
if registration.participates:
|
|
|
|
if registration.team:
|
|
|
|
return reverse_lazy("participation:team_detail", args=(registration.team_id,))
|
|
|
|
raise PermissionDenied(_("You are not in a team."))
|
|
|
|
raise PermissionDenied(_("You don't participate, so you don't have any team."))
|
|
|
|
|
|
|
|
|
|
|
|
class TeamDetailView(LoginRequiredMixin, DetailView):
|
|
|
|
model = Team
|
2020-09-24 09:15:54 +00:00
|
|
|
|
2020-09-27 14:35:31 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
user = request.user
|
2020-09-27 14:38:17 +00:00
|
|
|
if user.registration.is_admin or user.registration.participates and user.registration.team.pk == kwargs["pk"]:
|
2020-09-27 14:35:31 +00:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
raise PermissionDenied
|
|
|
|
|
2020-09-24 09:15:54 +00:00
|
|
|
|
|
|
|
class TeamUpdateView(LoginRequiredMixin, UpdateView):
|
|
|
|
model = Team
|
|
|
|
form_class = TeamForm
|
|
|
|
template_name = "participation/update_team.html"
|
|
|
|
|
2020-09-27 14:35:31 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
user = request.user
|
2020-09-27 14:38:17 +00:00
|
|
|
if user.registration.is_admin or user.registration.participates and user.registration.team.pk == kwargs["pk"]:
|
2020-09-27 14:35:31 +00:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
raise PermissionDenied
|
|
|
|
|
2020-09-24 09:15:54 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["participation_form"] = ParticipationForm(data=self.request.POST or None,
|
|
|
|
instance=self.object.participation)
|
|
|
|
return context
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def form_valid(self, form):
|
|
|
|
participation_form = ParticipationForm(data=self.request.POST or None, instance=self.object.participation)
|
|
|
|
if not participation_form.is_valid():
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
|
|
|
participation_form.save()
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy("participation:team_detail", args=(self.object.pk,))
|
2020-09-27 12:32:05 +00:00
|
|
|
|
|
|
|
|
2020-09-27 16:40:45 +00:00
|
|
|
class TeamAuthorizationsView(LoginRequiredMixin, DetailView):
|
|
|
|
model = Team
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
user = request.user
|
|
|
|
if user.registration.is_admin or user.registration.participates and user.registration.team.pk == kwargs["pk"]:
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
raise PermissionDenied
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
team = self.get_object()
|
|
|
|
output = BytesIO()
|
|
|
|
zf = ZipFile(output, "w")
|
|
|
|
for student in team.students.all():
|
|
|
|
magic = Magic(mime=True)
|
|
|
|
mime_type = magic.from_file("media/" + student.photo_authorization.name)
|
|
|
|
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
|
|
|
|
zf.write("media/" + student.photo_authorization.name,
|
|
|
|
_("Photo authorization of {student}.{ext}").format(student=str(student), ext=ext))
|
|
|
|
zf.close()
|
|
|
|
response = HttpResponse(content_type="application/zip")
|
|
|
|
response["Content-Disposition"] = "attachment; filename=\"{filename}\""\
|
|
|
|
.format(filename=_("Photo authorizations of team {trigram}.zip").format(trigram=team.trigram))
|
|
|
|
response.write(output.getvalue())
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2020-09-27 12:32:05 +00:00
|
|
|
class MyParticipationDetailView(LoginRequiredMixin, RedirectView):
|
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
|
|
|
user = self.request.user
|
|
|
|
registration = user.registration
|
|
|
|
if registration.participates:
|
|
|
|
if registration.team:
|
|
|
|
return reverse_lazy("participation:participation_detail", args=(registration.team.participation.id,))
|
|
|
|
raise PermissionDenied(_("You are not in a team."))
|
|
|
|
raise PermissionDenied(_("You don't participate, so you don't have any team."))
|
|
|
|
|
|
|
|
|
|
|
|
class ParticipationDetailView(LoginRequiredMixin, DetailView):
|
|
|
|
model = Participation
|
|
|
|
|
2020-09-27 14:35:31 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
user = request.user
|
2020-09-27 14:38:17 +00:00
|
|
|
if user.registration.is_admin or user.registration.participates\
|
|
|
|
and user.registration.team.participation.pk == kwargs["pk"]:
|
2020-09-27 14:35:31 +00:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
raise PermissionDenied
|
|
|
|
|
2020-09-27 12:32:05 +00:00
|
|
|
|
|
|
|
class UploadVideoView(LoginRequiredMixin, UpdateView):
|
|
|
|
model = Video
|
|
|
|
form_class = UploadVideoForm
|
|
|
|
template_name = "participation/upload_video.html"
|
|
|
|
|
2020-09-27 14:35:31 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
user = request.user
|
2020-09-27 14:38:17 +00:00
|
|
|
if user.registration.is_admin or user.registration.participates\
|
2020-09-27 14:52:52 +00:00
|
|
|
and user.registration.team.participation.pk == self.get_object().participation.pk:
|
2020-09-27 14:35:31 +00:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
raise PermissionDenied
|
|
|
|
|
2020-09-27 12:32:05 +00:00
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy("participation:participation_detail", args=(self.object.participation.pk,))
|