vererbung von teams implementiert
This commit is contained in:
parent
baf9727a37
commit
ef7f93884f
@ -6,12 +6,45 @@ from django.urls import reverse_lazy
|
|||||||
from .models import Event, Training, Game, EventParticipation, GameResult
|
from .models import Event, Training, Game, EventParticipation, GameResult
|
||||||
from .forms import EventForm, TrainingForm, GameForm, OpenGameForm, GameResultForm
|
from .forms import EventForm, TrainingForm, GameForm, OpenGameForm, GameResultForm
|
||||||
from accounts.models import CustomUser # Import CustomUser for manage_participation view
|
from accounts.models import CustomUser # Import CustomUser for manage_participation view
|
||||||
|
from clubs.models import Team
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
def select_event_type(request):
|
def select_event_type(request):
|
||||||
return render(request, 'calendars/select_event_type.html')
|
return render(request, 'calendars/select_event_type.html')
|
||||||
|
|
||||||
|
def get_all_child_teams(parent_team):
|
||||||
|
"""
|
||||||
|
Iteratively gets all child teams for a given team.
|
||||||
|
"""
|
||||||
|
children = []
|
||||||
|
teams_to_check = list(parent_team.child_teams.all())
|
||||||
|
while teams_to_check:
|
||||||
|
team = teams_to_check.pop(0)
|
||||||
|
if team not in children:
|
||||||
|
children.append(team)
|
||||||
|
teams_to_check.extend(list(team.child_teams.all()))
|
||||||
|
return children
|
||||||
|
|
||||||
|
class ManageableTeamsMixin:
|
||||||
|
def get_form(self, form_class=None):
|
||||||
|
form = super().get_form(form_class)
|
||||||
|
user = self.request.user
|
||||||
|
if not user.is_superuser:
|
||||||
|
coached_teams_qs = user.coached_teams.all()
|
||||||
|
|
||||||
|
expanded_coached_teams = list(coached_teams_qs)
|
||||||
|
for team in coached_teams_qs:
|
||||||
|
expanded_coached_teams.extend(get_all_child_teams(team))
|
||||||
|
|
||||||
|
assisted_teams_qs = user.assisted_teams.all()
|
||||||
|
|
||||||
|
# Combine and get unique teams
|
||||||
|
managed_teams_ids = {team.id for team in expanded_coached_teams} | {team.id for team in assisted_teams_qs}
|
||||||
|
|
||||||
|
form.fields['team'].queryset = Team.objects.filter(id__in=managed_teams_ids)
|
||||||
|
return form
|
||||||
|
|
||||||
class CoachCheckMixin(UserPassesTestMixin):
|
class CoachCheckMixin(UserPassesTestMixin):
|
||||||
def test_func(self):
|
def test_func(self):
|
||||||
user = self.request.user
|
user = self.request.user
|
||||||
@ -20,55 +53,24 @@ class CoachCheckMixin(UserPassesTestMixin):
|
|||||||
team = self.get_object().team
|
team = self.get_object().team
|
||||||
return user == team.head_coach or user in team.assistant_coaches.all()
|
return user == team.head_coach or user in team.assistant_coaches.all()
|
||||||
|
|
||||||
class EventCreateView(LoginRequiredMixin, CreateView):
|
class EventCreateView(LoginRequiredMixin, ManageableTeamsMixin, CreateView):
|
||||||
model = Event
|
model = Event
|
||||||
form_class = EventForm
|
form_class = EventForm
|
||||||
template_name = 'calendars/event_form.html'
|
template_name = 'calendars/event_form.html'
|
||||||
success_url = reverse_lazy('dashboard')
|
success_url = reverse_lazy('dashboard')
|
||||||
|
|
||||||
def get_form(self, form_class=None):
|
class TrainingCreateView(LoginRequiredMixin, ManageableTeamsMixin, CreateView):
|
||||||
form = super().get_form(form_class)
|
|
||||||
user = self.request.user
|
|
||||||
if not user.is_superuser:
|
|
||||||
coached_teams = user.coached_teams.all()
|
|
||||||
assisted_teams = user.assisted_teams.all()
|
|
||||||
teams = coached_teams | assisted_teams
|
|
||||||
form.fields['team'].queryset = teams
|
|
||||||
return form
|
|
||||||
|
|
||||||
class TrainingCreateView(LoginRequiredMixin, CreateView):
|
|
||||||
model = Training
|
model = Training
|
||||||
form_class = TrainingForm
|
form_class = TrainingForm
|
||||||
template_name = 'calendars/event_form.html'
|
template_name = 'calendars/event_form.html'
|
||||||
success_url = reverse_lazy('dashboard')
|
success_url = reverse_lazy('dashboard')
|
||||||
|
|
||||||
def get_form(self, form_class=None):
|
class GameCreateView(LoginRequiredMixin, ManageableTeamsMixin, CreateView):
|
||||||
form = super().get_form(form_class)
|
|
||||||
user = self.request.user
|
|
||||||
print("DEBUG: "+str(user.coached_teams.all()))
|
|
||||||
if not user.is_superuser:
|
|
||||||
coached_teams = user.coached_teams.all()
|
|
||||||
assisted_teams = user.assisted_teams.all()
|
|
||||||
teams = coached_teams | assisted_teams
|
|
||||||
form.fields['team'].queryset = teams
|
|
||||||
return form
|
|
||||||
|
|
||||||
class GameCreateView(LoginRequiredMixin, CreateView):
|
|
||||||
model = Game
|
model = Game
|
||||||
form_class = GameForm
|
form_class = GameForm
|
||||||
template_name = 'calendars/event_form.html'
|
template_name = 'calendars/event_form.html'
|
||||||
success_url = reverse_lazy('dashboard')
|
success_url = reverse_lazy('dashboard')
|
||||||
|
|
||||||
def get_form(self, form_class=None):
|
|
||||||
form = super().get_form(form_class)
|
|
||||||
user = self.request.user
|
|
||||||
if not user.is_superuser:
|
|
||||||
coached_teams = user.coached_teams.all()
|
|
||||||
assisted_teams = user.assisted_teams.all()
|
|
||||||
teams = coached_teams | assisted_teams
|
|
||||||
form.fields['team'].queryset = teams
|
|
||||||
return form
|
|
||||||
|
|
||||||
class EventUpdateView(LoginRequiredMixin, CoachCheckMixin, UpdateView):
|
class EventUpdateView(LoginRequiredMixin, CoachCheckMixin, UpdateView):
|
||||||
model = Event
|
model = Event
|
||||||
template_name = 'calendars/event_form.html'
|
template_name = 'calendars/event_form.html'
|
||||||
|
|||||||
@ -6,6 +6,19 @@ from django.db.models import Q
|
|||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
def get_all_child_teams(parent_team):
|
||||||
|
"""
|
||||||
|
Iteratively gets all child teams for a given team.
|
||||||
|
"""
|
||||||
|
children = []
|
||||||
|
teams_to_check = list(parent_team.child_teams.all())
|
||||||
|
while teams_to_check:
|
||||||
|
team = teams_to_check.pop(0)
|
||||||
|
if team not in children:
|
||||||
|
children.append(team)
|
||||||
|
teams_to_check.extend(list(team.child_teams.all()))
|
||||||
|
return children
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def dashboard(request):
|
def dashboard(request):
|
||||||
user = request.user
|
user = request.user
|
||||||
@ -16,10 +29,18 @@ def dashboard(request):
|
|||||||
player_teams = []
|
player_teams = []
|
||||||
if hasattr(user, 'team') and user.team:
|
if hasattr(user, 'team') and user.team:
|
||||||
player_teams = [user.team]
|
player_teams = [user.team]
|
||||||
coached_teams = user.coached_teams.all()
|
|
||||||
|
coached_teams_qs = user.coached_teams.all()
|
||||||
|
|
||||||
|
# A head coach also manages all child teams
|
||||||
|
expanded_coached_teams = list(coached_teams_qs)
|
||||||
|
for team in coached_teams_qs:
|
||||||
|
expanded_coached_teams.extend(get_all_child_teams(team))
|
||||||
|
|
||||||
assisted_teams = user.assisted_teams.all()
|
assisted_teams = user.assisted_teams.all()
|
||||||
|
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
all_teams = list(set(chain(player_teams, coached_teams, assisted_teams)))
|
all_teams = list(set(chain(player_teams, expanded_coached_teams, assisted_teams)))
|
||||||
|
|
||||||
now = timezone.now()
|
now = timezone.now()
|
||||||
three_hours_ago = now - datetime.timedelta(hours=3)
|
three_hours_ago = now - datetime.timedelta(hours=3)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user