From 9499e1052452325bcf0f742e907792071d278246 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 4 May 2020 22:27:45 +0200 Subject: [PATCH] My team --- apps/member/urls.py | 4 ++-- apps/member/views.py | 27 ++++++++++++++++++++++++++- apps/tournament/views.py | 5 +++-- templates/base.html | 2 +- templates/tournament/team_detail.html | 10 +++++++--- 5 files changed, 39 insertions(+), 9 deletions(-) diff --git a/apps/member/urls.py b/apps/member/urls.py index 90c8d91..5f5a89c 100644 --- a/apps/member/urls.py +++ b/apps/member/urls.py @@ -1,7 +1,7 @@ from django.urls import path from django.views.generic import RedirectView -from .views import CreateUserView, MyAccountView, UserDetailView,\ +from .views import CreateUserView, MyAccountView, UserDetailView, MyTeamView,\ ProfileListView, OrphanedProfileListView, OrganizersListView, ResetAdminView app_name = "member" @@ -12,7 +12,7 @@ urlpatterns = [ path("information//", UserDetailView.as_view(), name="information"), path("add-team/", RedirectView.as_view(pattern_name="index"), name="add_team"), path("join-team/", RedirectView.as_view(pattern_name="index"), name="join_team"), - path("my-team/", RedirectView.as_view(pattern_name="index"), name="my_team"), + path("my-team/", MyTeamView.as_view(), name="my_team"), path("my-team/update/", RedirectView.as_view(pattern_name="index"), name="update_my_team"), path("profiles/", ProfileListView.as_view(), name="all_profiles"), path("orphaned-profiles/", OrphanedProfileListView.as_view(), name="orphaned_profiles"), diff --git a/apps/member/views.py b/apps/member/views.py index 02767ed..222d6fc 100644 --- a/apps/member/views.py +++ b/apps/member/views.py @@ -9,7 +9,8 @@ from django.views import View from django.views.generic import CreateView, UpdateView, DetailView from django_tables2 import SingleTableView -from tournament.views import AdminMixin +from tournament.models import Team +from tournament.views import AdminMixin, TeamMixin from .forms import SignUpForm, TFJMUserForm from .models import TFJMUser, Document from .tables import UserTable @@ -64,6 +65,30 @@ class UserDetailView(LoginRequiredMixin, DetailView): return context +class MyTeamView(TeamMixin, DetailView): + model = Team + + def get_object(self, queryset=None): + return self.request.user.team + + def dispatch(self, request, *args, **kwargs): + if isinstance(request.user, AnonymousUser): + raise PermissionDenied + + team = self.get_object() + + if not request.user.participates or team is None: + raise PermissionDenied + return super().dispatch(request, *args, **kwargs) + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + + context["title"] = str(self.object) + + return context + + class DocumentView(LoginRequiredMixin, View): def get(self, request, *args, **kwargs): doc = Document.objects.get(file=self.kwargs["file"]) diff --git a/apps/tournament/views.py b/apps/tournament/views.py index 397b1a0..c0ee679 100644 --- a/apps/tournament/views.py +++ b/apps/tournament/views.py @@ -135,10 +135,11 @@ class TeamDetailView(LoginRequiredMixin, DetailView): class TeamUpdateView(LoginRequiredMixin, UpdateView): model = Team form_class = TeamForm - extra_context = dict(title=_("Udpate team"),) + extra_context = dict(title=_("Update team"),) def dispatch(self, request, *args, **kwargs): - if not request.user.admin and self.request.user not in self.get_object().tournament.organizers: + if not request.user.admin and self.request.user not in self.get_object().tournament.organizers.all() \ + and self.get_object() != self.request.user.team: raise PermissionDenied return super().dispatch(request, *args, **kwargs) diff --git a/templates/base.html b/templates/base.html index 979b4f4..9683977 100644 --- a/templates/base.html +++ b/templates/base.html @@ -143,7 +143,7 @@