#38 User requests
* implements 3) "Extend sharing on direct adding of users"
This commit is contained in:
@@ -16,10 +16,9 @@ from compensation.models import EcoAccount, EcoAccountDeduction
|
||||
from intervention.inputs import TextToClipboardInput
|
||||
from intervention.models import Revocation, RevocationDocument, Intervention
|
||||
from konova.forms import BaseModalForm
|
||||
from konova.settings import ZB_GROUP, ETS_GROUP
|
||||
from konova.utils.general import format_german_float
|
||||
from konova.utils.messenger import Messenger
|
||||
from konova.utils.user_checks import in_group
|
||||
from konova.utils.user_checks import is_default_group_only
|
||||
from user.models import UserActionLogEntry, UserAction
|
||||
|
||||
|
||||
@@ -36,6 +35,21 @@ class ShareInterventionModalForm(BaseModalForm):
|
||||
}
|
||||
)
|
||||
)
|
||||
user_select = 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"),
|
||||
required=False,
|
||||
queryset=User.objects.all(),
|
||||
widget=autocomplete.ModelSelect2Multiple(
|
||||
url="share-user-autocomplete",
|
||||
attrs={
|
||||
"data-placeholder": _("Click for selection"),
|
||||
"data-minimum-input-length": 3,
|
||||
},
|
||||
forward=["users"]
|
||||
),
|
||||
)
|
||||
users = forms.MultipleChoiceField(
|
||||
label=_("Shared with"),
|
||||
label_suffix="",
|
||||
@@ -78,28 +92,39 @@ class ShareInterventionModalForm(BaseModalForm):
|
||||
)
|
||||
|
||||
# Initialize users field
|
||||
# Remove field if user is not in registration or conservation group
|
||||
if not in_group(self.request.user, ZB_GROUP) and not in_group(self.request.user, ETS_GROUP):
|
||||
del self.fields["users"]
|
||||
else:
|
||||
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
|
||||
# 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()
|
||||
|
||||
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):
|
||||
accessing_users = User.objects.filter(
|
||||
id__in=self.cleaned_data["users"]
|
||||
still_accessing_users = self.cleaned_data["users"]
|
||||
new_accessing_users = list(self.cleaned_data["user_select"].values_list("id", flat=True))
|
||||
accessing_users = still_accessing_users + new_accessing_users
|
||||
users = User.objects.filter(
|
||||
id__in=accessing_users
|
||||
)
|
||||
self.instance.share_with_list(accessing_users)
|
||||
self.instance.share_with_list(users)
|
||||
|
||||
|
||||
class NewRevocationModalForm(BaseModalForm):
|
||||
|
||||
Reference in New Issue
Block a user