#101 Team sharing tests
* adds tests for team sharing * extends the API for team sharing support * adds shared_teams property shortcut for ShareableObjectMixin * adds full support for team-based sharing to all views and functions * simplifies ShareModalForm * adds/updates translations
This commit is contained in:
@@ -9,7 +9,7 @@ from dal import autocomplete
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
|
||||
from konova.utils.message_templates import DEDUCTION_ADDED, REVOCATION_ADDED, DEDUCTION_REMOVED, DEDUCTION_EDITED, \
|
||||
REVOCATION_EDITED
|
||||
REVOCATION_EDITED, ENTRY_REMOVE_MISSING_PERMISSION
|
||||
from user.models import User, Team
|
||||
from user.models import UserActionLogEntry
|
||||
from django.db import transaction
|
||||
@@ -37,7 +37,7 @@ class ShareModalForm(BaseModalForm):
|
||||
}
|
||||
)
|
||||
)
|
||||
team_select = forms.ModelMultipleChoiceField(
|
||||
teams = forms.ModelMultipleChoiceField(
|
||||
label=_("Add team to share with"),
|
||||
label_suffix="",
|
||||
help_text=_("Multiple selection possible - You can only select teams which do not already have access."),
|
||||
@@ -51,7 +51,7 @@ class ShareModalForm(BaseModalForm):
|
||||
},
|
||||
),
|
||||
)
|
||||
user_select = forms.ModelMultipleChoiceField(
|
||||
users = forms.ModelMultipleChoiceField(
|
||||
label=_("Add user to share with"),
|
||||
label_suffix="",
|
||||
help_text=_("Multiple selection possible - You can only select users which do not already have access. Enter the full username."),
|
||||
@@ -63,21 +63,8 @@ class ShareModalForm(BaseModalForm):
|
||||
"data-placeholder": _("Click for selection"),
|
||||
"data-minimum-input-length": 3,
|
||||
},
|
||||
forward=["users"]
|
||||
),
|
||||
)
|
||||
users = forms.MultipleChoiceField(
|
||||
label=_("Shared with"),
|
||||
label_suffix="",
|
||||
required=True,
|
||||
help_text=_("Remove check to remove access for this user"),
|
||||
widget=forms.CheckboxSelectMultiple(
|
||||
attrs={
|
||||
"class": "list-unstyled",
|
||||
}
|
||||
),
|
||||
choices=[]
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
@@ -91,6 +78,48 @@ class ShareModalForm(BaseModalForm):
|
||||
|
||||
self._init_fields()
|
||||
|
||||
def _user_team_valid(self):
|
||||
""" Checks whether users and teams have been removed by the user and if the user is allowed to do so or not
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
users = self.cleaned_data.get("users", User.objects.none())
|
||||
teams = self.cleaned_data.get("teams", Team.objects.none())
|
||||
|
||||
_is_valid = True
|
||||
if is_default_group_only(self.user):
|
||||
shared_users = self.instance.shared_users
|
||||
shared_teams = self.instance.shared_teams
|
||||
|
||||
shared_users_are_removed = not set(shared_users).issubset(users)
|
||||
shared_teams_are_removed = not set(shared_teams).issubset(teams)
|
||||
|
||||
if shared_users_are_removed:
|
||||
self.add_error(
|
||||
"users",
|
||||
ENTRY_REMOVE_MISSING_PERMISSION
|
||||
)
|
||||
_is_valid = False
|
||||
if shared_teams_are_removed:
|
||||
self.add_error(
|
||||
"teams",
|
||||
ENTRY_REMOVE_MISSING_PERMISSION
|
||||
)
|
||||
_is_valid = False
|
||||
return _is_valid
|
||||
|
||||
def is_valid(self):
|
||||
""" Extended validity check
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
super_valid = super().is_valid()
|
||||
user_team_valid = self._user_team_valid()
|
||||
_is_valid = super_valid and user_team_valid
|
||||
return _is_valid
|
||||
|
||||
def _init_fields(self):
|
||||
""" Wraps initializing of fields
|
||||
|
||||
@@ -105,39 +134,12 @@ class ShareModalForm(BaseModalForm):
|
||||
self.share_link
|
||||
)
|
||||
|
||||
# Initialize users field
|
||||
# Disable field if user is not in registration or conservation group
|
||||
if is_default_group_only(self.request.user):
|
||||
self.disable_form_field("users")
|
||||
|
||||
self._add_user_choices_to_field()
|
||||
self._add_teams_to_field()
|
||||
|
||||
def _add_teams_to_field(self):
|
||||
form_data = {
|
||||
"team_select": self.instance.teams.all()
|
||||
"teams": self.instance.teams.all(),
|
||||
"users": self.instance.users.all(),
|
||||
}
|
||||
self.load_initial_data(form_data)
|
||||
|
||||
def _add_user_choices_to_field(self):
|
||||
""" Transforms the instance's sharing users into a list for the form field
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
users = self.instance.users.all()
|
||||
choices = []
|
||||
for n in users:
|
||||
choices.append(
|
||||
(n.id, n.username)
|
||||
)
|
||||
self.fields["users"].choices = choices
|
||||
u_ids = list(users.values_list("id", flat=True))
|
||||
self.initialize_form_field(
|
||||
"users",
|
||||
u_ids
|
||||
)
|
||||
|
||||
def save(self):
|
||||
self.instance.update_sharing_user(self)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user