#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
###############################################################################
# #
# Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.web module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
# or FITNESS FOR A PARTICULAR PURPOSE. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
from django.contrib.auth.models import User
from django.db.models import Q
from django.shortcuts import get_object_or_404
from rest_framework import exceptions as drf_exceptions
from rest_framework import generics
from rest_framework import permissions
from rest_framework.response import Response
from rest_framework.reverse import reverse
from ..common.mixins import CommonContextMixin
from .models import Team
from .permissions import HasPrivacyLevel
from .permissions import IsOwner
from .serializers import FullTeamSerializer
from .serializers import SimpleTeamSerializer
from .serializers import TeamCreationSerializer
from .serializers import TeamUpdateSerializer
# ----------------------------------------------------------
[docs]class UserTeamListView(CommonContextMixin, generics.ListCreateAPIView):
"""
Lists the team from a user and create new teams
"""
model = Team
serializer_class = SimpleTeamSerializer
writing_serializer_class = TeamCreationSerializer
permission_classes = [permissions.IsAuthenticated]
[docs] def get_serializer(self, *args, **kwargs):
if self.request.method == "POST":
self.serializer_class = self.writing_serializer_class
return super().get_serializer(*args, **kwargs)
[docs] def list(self, request, owner_name):
owner = get_object_or_404(User, username=owner_name)
if request.user == owner:
queryset = Team.objects.filter(owner=owner)
else:
queryset = Team.objects.filter(
Q(owner=owner),
Q(privacy_level=Team.PUBLIC)
| Q(privacy_level=Team.MEMBERS, members=request.user),
).distinct()
serializer = self.get_serializer(
queryset, many=True, context={"user": request.user}
)
return Response(serializer.data)
# ----------------------------------------------------------
[docs]class TeamDetailView(CommonContextMixin, generics.RetrieveUpdateDestroyAPIView):
"""
This view provides the details of a team
"""
model = Team
serializer_class = FullTeamSerializer
writing_serializer_class = TeamUpdateSerializer
[docs] def get_permissions(self):
self.permission_classes = [permissions.IsAuthenticatedOrReadOnly]
if self.request.method == "GET":
self.permission_classes.append(HasPrivacyLevel)
else:
self.permission_classes.append(IsOwner)
return super().get_permissions()
[docs] def get_object(self):
owner_name = self.kwargs.get("owner_name")
team_name = self.kwargs.get("team_name")
team = get_object_or_404(Team, owner__username=owner_name, name=team_name)
self.check_object_permissions(self.request, team)
return team
[docs] def get_serializer(self, *args, **kwargs):
if self.request.method in ["PUT", "PATCH"]:
self.serializer_class = self.writing_serializer_class
return super().get_serializer(*args, **kwargs)
[docs] def get_serializer_context(self):
context = super().get_serializer_context()
context["user"] = self.request.user
return context
[docs] def update(self, request, owner_name, team_name):
team = self.get_object()
self.check_object_permissions(request, team)
serializer = self.writing_serializer_class(
team, data=request.data, partial=True
)
serializer.is_valid(raise_exception=True)
db_object = serializer.save()
result = {
"name": db_object.name,
"url": reverse(
"api_teams:team_info", args=[db_object.owner.username, db_object.name]
),
}
response = Response(result, status=200)
response["Location"] = result["url"]
return response
# ----------------------------------------------------------
[docs]class TeamListView(CommonContextMixin, generics.ListAPIView):
"""
List all teams of the caller
"""
model = Team
serializer_class = SimpleTeamSerializer
permission_classes = [permissions.IsAuthenticated]
[docs] def get_queryset(self):
return self.model.objects.filter(owner=self.request.user)