mirror of
https://gitlab.com/animath/si/plateforme.git
synced 2025-07-05 09:54:13 +02:00
Validate teams
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import random
|
||||
import zipfile
|
||||
from datetime import datetime
|
||||
from io import BytesIO
|
||||
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
@ -21,21 +22,21 @@ from .tables import TournamentTable, TeamTable, SolutionTable, SynthesisTable
|
||||
|
||||
class AdminMixin(LoginRequiredMixin):
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
if not request.user.admin:
|
||||
if not request.user.is_authenticated or not request.user.admin:
|
||||
raise PermissionDenied
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
|
||||
class OrgaMixin(LoginRequiredMixin):
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
if not request.user.organizes:
|
||||
if not request.user.is_authenticated or not request.user.organizes:
|
||||
raise PermissionDenied
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
|
||||
class TeamMixin(LoginRequiredMixin):
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
if not request.user.team:
|
||||
if not request.user.is_authenticated or not request.user.team:
|
||||
raise PermissionDenied
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
@ -105,12 +106,14 @@ class TeamDetailView(LoginRequiredMixin, DetailView):
|
||||
model = Team
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
if not request.user.admin and self.request.user not in self.get_object().tournament.organizers.all()\
|
||||
and self.get_object() != request.user.team:
|
||||
if not request.user.is_authenticated or \
|
||||
(not request.user.admin and self.request.user not in self.get_object().tournament.organizers.all()
|
||||
and self.get_object() != request.user.team):
|
||||
raise PermissionDenied
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
print(request.POST)
|
||||
team = self.get_object()
|
||||
if "zip" in request.POST:
|
||||
solutions = team.solutions.all()
|
||||
@ -128,13 +131,28 @@ class TeamDetailView(LoginRequiredMixin, DetailView):
|
||||
.format(_("Solutions for team {team}.zip")
|
||||
.format(team=str(team)).replace(" ", "%20"))
|
||||
return resp
|
||||
elif "leave" in request.POST:
|
||||
elif "leave" in request.POST and request.user.participates:
|
||||
request.user.team = None
|
||||
request.user.save()
|
||||
if not team.users.exists():
|
||||
team.delete()
|
||||
return redirect('tournament:detail', pk=team.tournament.pk)
|
||||
elif "delete" in request.POST:
|
||||
elif "request_validation" in request.POST and request.user.participates:
|
||||
team.validation_status = "1waiting"
|
||||
team.save()
|
||||
# TODO Send mail
|
||||
return redirect('tournament:team_detail', pk=team.pk)
|
||||
elif "validate" in request.POST and request.user.organizes:
|
||||
team.validation_status = "2valid"
|
||||
team.save()
|
||||
# TODO Send mail
|
||||
return redirect('tournament:team_detail', pk=team.pk)
|
||||
elif "invalidate" in request.POST and request.user.organizes:
|
||||
team.validation_status = "0invalid"
|
||||
team.save()
|
||||
# TODO Send mail
|
||||
return redirect('tournament:team_detail', pk=team.pk)
|
||||
elif "delete" in request.POST and request.user.organizes:
|
||||
team.delete()
|
||||
return redirect('tournament:detail', pk=team.tournament.pk)
|
||||
|
||||
@ -204,12 +222,18 @@ class SolutionsView(TeamMixin, BaseFormView, SingleTableView):
|
||||
solution = form.instance
|
||||
solution.team = self.request.user.team
|
||||
solution.final = solution.team.selected_for_final
|
||||
|
||||
if datetime.now() > solution.tournament.date_solutions:
|
||||
form.add_error('file', _("You can't publish your solution anymore. Deadline: {date:%m-%d-%Y %h:%M}.")
|
||||
.format(date=solution.tournament.date_solutions))
|
||||
return super().form_invalid(form)
|
||||
|
||||
prev_sol = Solution.objects.filter(problem=solution.problem, team=solution.team, final=solution.final)
|
||||
for sol in prev_sol.all():
|
||||
sol.delete()
|
||||
alphabet = "0123456789abcdefghijklmnopqrstuvwxyz0123456789"
|
||||
id = ""
|
||||
for _ in range(64):
|
||||
for i in range(64):
|
||||
id += random.choice(alphabet)
|
||||
solution.file.name = id
|
||||
solution.save()
|
||||
@ -302,13 +326,26 @@ class SynthesesView(TeamMixin, BaseFormView, SingleTableView):
|
||||
synthesis = form.instance
|
||||
synthesis.team = self.request.user.team
|
||||
synthesis.final = synthesis.team.selected_for_final
|
||||
|
||||
if synthesis.round == '1' and datetime.now() > synthesis.tournament.date_syntheses:
|
||||
form.add_error('file', _("You can't publish your synthesis anymore for the first round."
|
||||
" Deadline: {date:%m-%d-%Y %h:%M}.")
|
||||
.format(date=synthesis.tournament.date_syntheses))
|
||||
return super().form_invalid(form)
|
||||
|
||||
if synthesis.round == '2' and datetime.now() > synthesis.tournament.date_syntheses_2:
|
||||
form.add_error('file', _("You can't publish your synthesis anymore for the second round."
|
||||
" Deadline: {date:%m-%d-%Y %h:%M}.")
|
||||
.format(date=synthesis.tournament.date_syntheses_2))
|
||||
return super().form_invalid(form)
|
||||
|
||||
prev_syn = Synthesis.objects.filter(team=synthesis.team, round=synthesis.round, source=synthesis.source,
|
||||
final=synthesis.final)
|
||||
for syn in prev_syn.all():
|
||||
syn.delete()
|
||||
alphabet = "0123456789abcdefghijklmnopqrstuvwxyz0123456789"
|
||||
id = ""
|
||||
for _ in range(64):
|
||||
for i in range(64):
|
||||
id += random.choice(alphabet)
|
||||
synthesis.file.name = id
|
||||
synthesis.save()
|
||||
|
Reference in New Issue
Block a user