From b55ed9db1ac5e19e122abf4a1da5788ad14c7d86 Mon Sep 17 00:00:00 2001 From: Andre Anjos <andre.anjos@idiap.ch> Date: Thu, 30 Jun 2016 18:19:37 +0200 Subject: [PATCH] [teams] Closes issue #406 --- beat/web/team/api.py | 7 ++++++- beat/web/team/models.py | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/beat/web/team/api.py b/beat/web/team/api.py index a2d94bb52..a4f4a02d8 100644 --- a/beat/web/team/api.py +++ b/beat/web/team/api.py @@ -42,7 +42,7 @@ from .serializers import TeamUpdateSerializer from .models import Team from .permissions import IsOwner, HasPrivacyLevel -from ..common.responses import BadRequestResponse +from ..common.responses import BadRequestResponse, ForbiddenResponse from ..common.mixins import CommonContextMixin @@ -117,6 +117,11 @@ class TeamDetailView(CommonContextMixin, generics.RetrieveUpdateDestroyAPIView): def delete(self, request, owner_name, team_name): team = self.get_queryset() + + # Check that the team can still be deleted + if not(team.deletable()): + return ForbiddenResponse("The team isn't deletable (it has been used to share %d objects with its members)" % team.total_shares()) + team.delete() return Response(status=status.HTTP_204_NO_CONTENT) diff --git a/beat/web/team/models.py b/beat/web/team/models.py index 69bd56e0e..b1c86908e 100644 --- a/beat/web/team/models.py +++ b/beat/web/team/models.py @@ -93,6 +93,32 @@ class Team(models.Model): return (self.owner.username, self.name) + def total_shares(self): + + used_at = 0 + used_at += self.shared_algorithms.count() + used_at += self.shared_databases.count() + used_at += self.shared_dataformats.count() + used_at += self.shared_environment.count() + used_at += self.shared_experiments.count() + used_at += self.shared_librarys.count() + used_at += self.shared_plotterparameters.count() + used_at += self.shared_plotters.count() + used_at += self.shared_searchs.count() + used_at += self.shared_toolchains.count() + used_at += self.usable_algorithm.count() + used_at += self.usable_library.count() + used_at += self.usable_plotterparameters.count() + used_at += self.usable_plotters.count() + + return used_at + + + def deletable(self): + + return not self.total_shares() + + #_____ Overrides __________ def __unicode__(self): -- GitLab