# API Refactoring sharing
* refactors team and user sharing by splitting into more maintainable blocks of code
This commit is contained in:
@@ -309,22 +309,36 @@ class AbstractModelShareAPIView(AbstractAPIView):
|
|||||||
raise ValueError("Shared user list must not be empty!")
|
raise ValueError("Shared user list must not be empty!")
|
||||||
new_teams = content.get("teams", [])
|
new_teams = content.get("teams", [])
|
||||||
|
|
||||||
|
self.__process_user_sharing(new_users, obj)
|
||||||
|
self.__process_team_sharing(new_teams, obj)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def __process_user_sharing(self, user_list: list, obj):
|
||||||
|
""" Processes API sharing for user payload
|
||||||
|
|
||||||
|
Args:
|
||||||
|
user_list (list): A list of users to share the obj with
|
||||||
|
obj (BaseObject): The shareable object
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
# Eliminate duplicates
|
# Eliminate duplicates
|
||||||
new_users = list(dict.fromkeys(new_users))
|
new_users = list(dict.fromkeys(user_list))
|
||||||
new_teams = list(dict.fromkeys(new_teams))
|
|
||||||
|
|
||||||
# Make sure each of these names exist as a user
|
# Make sure each of these names exist as a user
|
||||||
new_users_objs = []
|
new_users_objs = []
|
||||||
for user in new_users:
|
for user in new_users:
|
||||||
new_users_objs.append(User.objects.get(username=user))
|
try:
|
||||||
|
user_obj = User.objects.get(username=user)
|
||||||
# Make sure each of these names exist as a user
|
except User.DoesNotExist:
|
||||||
new_teams_objs = []
|
raise AssertionError(f"User with username {user} does not exist")
|
||||||
for team_name in new_teams:
|
new_users_objs.append(user_obj)
|
||||||
new_teams_objs.append(Team.objects.get(name=team_name))
|
|
||||||
|
|
||||||
if self.user.is_default_group_only():
|
if self.user.is_default_group_only():
|
||||||
# Default only users are not allowed to remove other users from having access. They can only add new ones!
|
# Default only users are not allowed to remove other users from having access. They can only add new ones!
|
||||||
|
# So we need to keep the ones that already have access from being removed!
|
||||||
new_users_to_be_added = User.objects.filter(
|
new_users_to_be_added = User.objects.filter(
|
||||||
username__in=new_users
|
username__in=new_users
|
||||||
).exclude(
|
).exclude(
|
||||||
@@ -332,6 +346,33 @@ class AbstractModelShareAPIView(AbstractAPIView):
|
|||||||
)
|
)
|
||||||
new_users_objs = obj.shared_users.union(new_users_to_be_added)
|
new_users_objs = obj.shared_users.union(new_users_to_be_added)
|
||||||
|
|
||||||
|
obj.share_with_user_list(new_users_objs)
|
||||||
|
|
||||||
|
def __process_team_sharing(self, team_list: list, obj):
|
||||||
|
""" Processes API sharing for team payload
|
||||||
|
|
||||||
|
Args:
|
||||||
|
team_list (list): A list of teams to share the obj with
|
||||||
|
obj (BaseObject): The shareable object
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
# Eliminate duplicates
|
||||||
|
new_teams = list(dict.fromkeys(team_list))
|
||||||
|
|
||||||
|
# Make sure each of these names exist as a team
|
||||||
|
new_teams_objs = []
|
||||||
|
for team_name in new_teams:
|
||||||
|
try:
|
||||||
|
team_obj = Team.objects.get(name=team_name)
|
||||||
|
except Team.DoesNotExist:
|
||||||
|
raise AssertionError(f"Team {team_name} does not exist!")
|
||||||
|
new_teams_objs.append(team_obj)
|
||||||
|
|
||||||
|
if self.user.is_default_group_only():
|
||||||
|
# Default only users are not allowed to remove other users from having access. They can only add new ones!
|
||||||
|
# So we need to keep the ones that already have access from being removed!
|
||||||
new_teams_to_be_added = Team.objects.filter(
|
new_teams_to_be_added = Team.objects.filter(
|
||||||
name__in=new_teams
|
name__in=new_teams
|
||||||
).exclude(
|
).exclude(
|
||||||
@@ -339,10 +380,7 @@ class AbstractModelShareAPIView(AbstractAPIView):
|
|||||||
)
|
)
|
||||||
new_teams_objs = obj.shared_teams.union(new_teams_to_be_added)
|
new_teams_objs = obj.shared_teams.union(new_teams_to_be_added)
|
||||||
|
|
||||||
obj.share_with_user_list(new_users_objs)
|
|
||||||
obj.share_with_team_list(new_teams_objs)
|
obj.share_with_team_list(new_teams_objs)
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
class InterventionAPIShareView(AbstractModelShareAPIView):
|
class InterventionAPIShareView(AbstractModelShareAPIView):
|
||||||
model = Intervention
|
model = Intervention
|
||||||
|
|||||||
Reference in New Issue
Block a user