bornhack-website/src/teams/views.py

101 lines
3.8 KiB
Python
Raw Normal View History

from django.views.generic import ListView, DetailView
2017-05-07 15:55:31 +00:00
from django.views.generic.edit import UpdateView, FormView
from camps.mixins import CampViewMixin
2017-04-02 16:04:57 +00:00
from .models import Team, TeamMember
2017-05-07 15:55:31 +00:00
from .forms import ManageTeamForm
2017-04-02 16:04:57 +00:00
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import redirect
from django.contrib import messages
2017-05-23 19:21:47 +00:00
from django.http import Http404, HttpResponseRedirect
2017-05-23 19:21:47 +00:00
from django.views.generic.detail import SingleObjectMixin
2017-05-23 19:50:53 +00:00
from django.core.urlresolvers import reverse_lazy
class TeamListView(CampViewMixin, ListView):
template_name = "team_list.html"
2017-04-02 16:04:57 +00:00
model = Team
context_object_name = 'teams'
2017-05-21 14:05:31 +00:00
class TeamDetailView(CampViewMixin, DetailView, UpdateView, FormView):
template_name = "team_detail.html"
model = Team
2017-05-21 14:05:31 +00:00
form_class = ManageTeamForm
context_object_name = 'team'
2017-04-02 16:04:57 +00:00
class TeamJoinView(LoginRequiredMixin, CampViewMixin, UpdateView):
template_name = "team_join.html"
model = Team
fields = []
def get(self, request, *args, **kwargs):
if request.user in self.get_object().members.all():
messages.warning(request, "You are already a member of this team")
return redirect('team_list', camp_slug=self.camp.slug)
if not self.get_object().needs_members:
messages.warning(request, "This team does not need members right now")
return redirect('team_list', camp_slug=self.get_object().camp.slug)
return super().get(request, *args, **kwargs)
def form_valid(self, form):
TeamMember.objects.create(team=self.get_object(), user=self.request.user)
messages.success(self.request, "You request to join the team %s has been registered, thank you." % self.get_object().name)
return redirect('team_list', camp_slug=self.get_object().camp.slug)
class TeamLeaveView(LoginRequiredMixin, CampViewMixin, UpdateView):
template_name = "team_leave.html"
model = Team
fields = []
def get(self, request, *args, **kwargs):
if request.user not in self.get_object().members.all():
messages.warning(request, "You are not a member of this team")
return redirect('team_list', camp_slug=self.get_object().camp.slug)
return super().get(request, *args, **kwargs)
def form_valid(self, form):
TeamMember.objects.filter(team=self.get_object(), user=self.request.user).delete()
messages.success(self.request, "You are no longer a member of the team %s" % self.get_object().name)
return redirect('team_list', camp_slug=self.get_object().camp.slug)
2017-05-23 19:21:47 +00:00
class EnsureTeamResponsibleMixin(SingleObjectMixin):
model = TeamMember
def dispatch(self, request, *args, **kwargs):
2017-05-23 19:50:53 +00:00
if request.user not in self.get_object().team.responsible.all():
2017-05-23 19:21:47 +00:00
messages.error(request, 'No thanks')
return HttpResponseRedirect(reverse_lazy('team_detail', slug=self.get_object().team.slug))
return super().dispatch(
request, *args, **kwargs
)
class TeamMemberRemoveView(LoginRequiredMixin, EnsureTeamResponsibleMixin, UpdateView):
template_name = "teammember_remove.html"
model = TeamMember
fields = []
def form_valid(self, form):
form.instance.delete()
messages.success(self.request, "Team member removed")
return redirect('team_detail', camp_slug=form.instance.team.camp.slug, slug=form.instance.team.slug)
2017-05-23 19:50:53 +00:00
class TeamMemberApproveView(LoginRequiredMixin, EnsureTeamResponsibleMixin, UpdateView):
template_name = "teammember_approve.html"
model = TeamMember
fields = []
def form_valid(self, form):
form.instance.approved = True
form.instance.save()
messages.success(self.request, "Team member approved")
return redirect('team_detail', camp_slug=form.instance.team.camp.slug, slug=form.instance.team.slug)