* refactors team views * split views.py into users.py and teams.py in users app * refactors method headers for _user_has_permission() * adds method and class comments and documentation to base view classes
106 lines
3.2 KiB
Python
106 lines
3.2 KiB
Python
"""
|
|
Author: Michel Peltriaux
|
|
Created on: 05.11.25
|
|
|
|
"""
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from django.http import Http404, HttpRequest
|
|
from django.shortcuts import get_object_or_404, render
|
|
from django.urls import reverse
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from konova.contexts import BaseContext
|
|
from konova.utils.message_templates import TEAM_LEFT, TEAM_REMOVED, TEAM_EDITED, TEAM_ADDED
|
|
from konova.views.base import BaseModalFormView
|
|
from user.forms.modals.team import LeaveTeamModalForm, RemoveTeamModalForm, EditTeamModalForm, NewTeamModalForm
|
|
from user.forms.team import TeamDataForm
|
|
from user.models import Team
|
|
from user.views.users import UserBaseView
|
|
|
|
|
|
class TeamDetailModalView(LoginRequiredMixin, BaseModalFormView):
|
|
_FORM_CLS = TeamDataForm
|
|
_MODEL_CLS = Team
|
|
|
|
def _user_has_shared_access(self, user, **kwargs):
|
|
# No specific constraints
|
|
return True
|
|
|
|
def _user_has_permission(self, user, **kwargs):
|
|
# No specific constraints
|
|
return True
|
|
|
|
|
|
class TeamIndexView(LoginRequiredMixin, UserBaseView):
|
|
_TEMPLATE = "user/team/index.html"
|
|
_TAB_TITLE = _("Teams")
|
|
|
|
def get(self, request: HttpRequest):
|
|
user = request.user
|
|
context = {
|
|
"teams": user.shared_teams,
|
|
"tab_title": self._TAB_TITLE,
|
|
}
|
|
context = BaseContext(request, context).context
|
|
return render(request, self._TEMPLATE, context)
|
|
|
|
|
|
class BaseTeamView(LoginRequiredMixin, BaseModalFormView):
|
|
_REDIRECT_URL = "user:team-index"
|
|
_MODEL_CLS = Team
|
|
|
|
class Meta:
|
|
abstract = True
|
|
|
|
def _user_has_permission(self, user, **kwargs):
|
|
# Nothing to check here - just pass the test
|
|
return True
|
|
|
|
def _user_has_shared_access(self, user, **kwargs):
|
|
# Nothing to check here - just pass the test
|
|
return True
|
|
|
|
def _get_redirect_url(self, *args, **kwargs):
|
|
return reverse(self._REDIRECT_URL)
|
|
|
|
class NewTeamView(BaseTeamView):
|
|
_FORM_CLS = NewTeamModalForm
|
|
_MSG_SUCCESS = TEAM_ADDED
|
|
|
|
class EditTeamView(BaseTeamView):
|
|
_FORM_CLS = EditTeamModalForm
|
|
_MSG_SUCCESS = TEAM_EDITED
|
|
|
|
def _user_has_permission(self, user, **kwargs):
|
|
team = get_object_or_404(Team, id=kwargs.get("id"))
|
|
user_is_admin = team.is_user_admin(user)
|
|
if not user_is_admin:
|
|
# If user is not an admin, we act as if there is no such team on the database
|
|
raise Http404()
|
|
return user_is_admin
|
|
|
|
|
|
class RemoveTeamView(BaseTeamView):
|
|
_FORM_CLS = RemoveTeamModalForm
|
|
_MSG_SUCCESS = TEAM_REMOVED
|
|
|
|
def _user_has_permission(self, user, **kwargs):
|
|
team_id = kwargs.get("id")
|
|
team = get_object_or_404(Team, id=team_id)
|
|
user_is_admin = team.is_user_admin(user)
|
|
if not user_is_admin:
|
|
raise Http404()
|
|
return True
|
|
|
|
class LeaveTeamView(BaseTeamView):
|
|
_FORM_CLS = LeaveTeamModalForm
|
|
_MSG_SUCCESS = TEAM_LEFT
|
|
|
|
def _user_has_shared_access(self, user, **kwargs):
|
|
team_id = kwargs.get("id")
|
|
team = get_object_or_404(self._MODEL_CLS, id=team_id)
|
|
is_user_team_member = team.users.filter(id=user.id).exists()
|
|
if not is_user_team_member:
|
|
raise Http404()
|
|
return True
|