bornhack-website/src/teams/views.py

178 lines
6.6 KiB
Python
Raw Normal View History

from django.views.generic import ListView, DetailView
2017-11-24 21:06:23 +00:00
from django.views.generic.edit import CreateView, UpdateView, FormView
from camps.mixins import CampViewMixin
2017-11-23 22:09:14 +00:00
from .models import Team, TeamMember, TeamTask
2017-05-07 15:55:31 +00:00
from .forms import ManageTeamForm
from .email import add_added_membership_email, add_removed_membership_email
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
2017-08-13 09:43:28 +00:00
from profiles.models import Profile
2017-05-23 20:28:21 +00:00
import logging
logger = logging.getLogger("bornhack.%s" % __name__)
2017-11-24 21:19:17 +00:00
class EnsureTeamResponsibleMixin(object):
def dispatch(self, request, *args, **kwargs):
2017-11-24 21:19:17 +00:00
self.team = Team.objects.get(slug=kwargs['team_slug'], camp=self.camp)
if request.user not in self.team.responsible.all():
messages.error(request, 'No thanks')
2017-11-24 21:19:17 +00:00
return redirect('teams:detail', camp_slug=self.camp.slug, team_slug=self.team.slug)
return super().dispatch(
request, *args, **kwargs
)
class TeamListView(CampViewMixin, ListView):
template_name = "team_list.html"
2017-04-02 16:04:57 +00:00
model = Team
context_object_name = 'teams'
class TeamDetailView(CampViewMixin, DetailView):
template_name = "team_detail.html"
context_object_name = 'team'
model = Team
2017-11-24 21:06:23 +00:00
slug_url_kwarg = 'team_slug'
class TeamManageView(CampViewMixin, EnsureTeamResponsibleMixin, UpdateView):
model = Team
template_name = "team_manage.html"
fields = ['description', 'needs_members']
2017-11-24 21:06:23 +00:00
slug_url_kwarg = 'team_slug'
def get_success_url(self):
2017-11-24 21:06:23 +00:00
return reverse_lazy('teams:detail', kwargs={'camp_slug': self.camp.slug, 'slug': self.get_object().slug})
2017-04-02 16:04:57 +00:00
class TeamJoinView(LoginRequiredMixin, CampViewMixin, UpdateView):
template_name = "team_join.html"
model = Team
fields = []
2017-11-24 21:06:23 +00:00
slug_url_kwarg = 'team_slug'
2017-04-02 16:04:57 +00:00
def get(self, request, *args, **kwargs):
2017-08-13 09:43:28 +00:00
if not Profile.objects.get(user=request.user).description:
messages.warning(
request,
"Please fill the description in your profile before joining a team"
)
2017-11-23 22:09:14 +00:00
return redirect('teams:list', camp_slug=self.camp.slug)
2017-08-13 09:43:28 +00:00
2017-04-02 16:04:57 +00:00
if request.user in self.get_object().members.all():
messages.warning(request, "You are already a member of this team")
2017-11-23 22:09:14 +00:00
return redirect('teams:list', camp_slug=self.camp.slug)
2017-04-02 16:04:57 +00:00
if not self.get_object().needs_members:
messages.warning(request, "This team does not need members right now")
2017-11-23 22:09:14 +00:00
return redirect('teams:list', camp_slug=self.get_object().camp.slug)
2017-04-02 16:04:57 +00:00
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)
2017-11-23 22:09:14 +00:00
return redirect('teams:list', camp_slug=self.get_object().camp.slug)
2017-04-02 16:04:57 +00:00
class TeamLeaveView(LoginRequiredMixin, CampViewMixin, UpdateView):
template_name = "team_leave.html"
model = Team
fields = []
2017-11-24 21:06:23 +00:00
slug_url_kwarg = 'team_slug'
2017-04-02 16:04:57 +00:00
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")
2017-11-23 22:09:14 +00:00
return redirect('teams:list', camp_slug=self.get_object().camp.slug)
2017-04-02 16:04:57 +00:00
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)
2017-11-23 22:09:14 +00:00
return redirect('teams:list', camp_slug=self.get_object().camp.slug)
2017-05-23 19:21:47 +00:00
class EnsureTeamMemberResponsibleMixin(SingleObjectMixin):
2017-05-23 19:21:47 +00:00
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')
2017-11-24 21:06:23 +00:00
return redirect('teams:detail', camp_slug=self.get_object().team.camp.slug, team_slug=self.get_object().team.slug)
2017-05-23 19:21:47 +00:00
return super().dispatch(
request, *args, **kwargs
)
class TeamMemberRemoveView(LoginRequiredMixin, CampViewMixin, EnsureTeamMemberResponsibleMixin, UpdateView):
2017-05-23 19:21:47 +00:00
template_name = "teammember_remove.html"
model = TeamMember
fields = []
def form_valid(self, form):
form.instance.delete()
if add_removed_membership_email(form.instance):
messages.success(self.request, "Team member removed")
else:
messages.success(self.request, "Team member removed (unable to add email to outgoing queue).")
logger.error(
'Unable to add removed email to outgoing queue for teammember: {}'.format(form.instance)
)
2017-11-24 21:06:23 +00:00
return redirect('teams:detail', camp_slug=self.camp.slug, team_slug=form.instance.team.slug)
2017-05-23 19:50:53 +00:00
class TeamMemberApproveView(LoginRequiredMixin, CampViewMixin, EnsureTeamMemberResponsibleMixin, UpdateView):
2017-05-23 19:50:53 +00:00
template_name = "teammember_approve.html"
model = TeamMember
fields = []
def form_valid(self, form):
form.instance.approved = True
form.instance.save()
if add_added_membership_email(form.instance):
messages.success(self.request, "Team member approved")
else:
messages.success(self.request, "Team member removed (unable to add email to outgoing queue).")
logger.error(
'Unable to add approved email to outgoing queue for teammember: {}'.format(form.instance)
)
2017-11-24 21:06:23 +00:00
return redirect('teams:detail', camp_slug=self.camp.slug, team_slug=form.instance.team.slug)
2017-11-23 22:09:14 +00:00
class TaskDetailView(CampViewMixin, DetailView):
template_name = "task_detail.html"
context_object_name = "task"
model = TeamTask
2017-11-24 21:06:23 +00:00
class TaskCreateView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibleMixin, CreateView):
model = TeamTask
template_name = "task_create.html"
fields = ['name', 'description']
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(**kwargs)
context['team'] = self.team
return context
def get_success_url(self):
2017-11-24 21:19:17 +00:00
return reverse_lazy(
'task_detail',
kwargs={
'camp_slug': self.camp.slug,
'team_slug': self.team.slug,
'slug': self.get_object().slug
}
)
2017-11-24 21:06:23 +00:00