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
|
|
|
|
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 09:15:54 +00:00
|
|
|
from django.views.generic import CreateView, FormView, DetailView, RedirectView, UpdateView
|
2020-09-21 13:41:55 +00:00
|
|
|
|
2020-09-24 09:15:54 +00:00
|
|
|
from participation.forms import TeamForm, JoinTeamForm, ParticipationForm
|
2020-09-22 18:41:42 +00:00
|
|
|
from participation.models import Team
|
|
|
|
|
|
|
|
|
|
|
|
class CreateTeamView(LoginRequiredMixin, CreateView):
|
|
|
|
model = Team
|
|
|
|
form_class = TeamForm
|
|
|
|
extra_context = dict(title=_("Create team"))
|
|
|
|
template_name = "participation/create_team.html"
|
|
|
|
|
2020-09-23 21:20:44 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def form_valid(self, form):
|
|
|
|
user = self.request.user
|
|
|
|
registration = user.registration
|
|
|
|
if not registration.participates:
|
|
|
|
form.add_error(None, _("You don't participate, so you can't create a team."))
|
|
|
|
return self.form_invalid(form)
|
|
|
|
elif registration.team:
|
|
|
|
form.add_error(None, _("You are already in a team."))
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
|
|
|
ret = super().form_valid(form)
|
|
|
|
registration.team = form.instance
|
|
|
|
registration.save()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy("index")
|
|
|
|
|
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
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def form_valid(self, form):
|
|
|
|
user = self.request.user
|
|
|
|
registration = user.registration
|
|
|
|
if not registration.participates:
|
|
|
|
form.add_error(None, _("You don't participate, so you can't create a team."))
|
|
|
|
return self.form_invalid(form)
|
|
|
|
elif registration.team:
|
|
|
|
form.add_error(None, _("You are already in a team."))
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
|
|
|
ret = super().form_valid(form)
|
|
|
|
registration.team = form.instance
|
|
|
|
registration.save()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy("index")
|
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
|
|
|
|
|
|
|
|
|
|
|
class TeamUpdateView(LoginRequiredMixin, UpdateView):
|
|
|
|
model = Team
|
|
|
|
form_class = TeamForm
|
|
|
|
template_name = "participation/update_team.html"
|
|
|
|
|
|
|
|
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,))
|