diff --git a/intervention/forms/modalForms.py b/intervention/forms/modalForms.py index f246a598..4d9abec4 100644 --- a/intervention/forms/modalForms.py +++ b/intervention/forms/modalForms.py @@ -10,16 +10,14 @@ from django.contrib.auth.models import User from django.db import transaction from django import forms from django.urls import reverse -from django.utils.translation import gettext_lazy as _ +from django.utils.translation import gettext_lazy as _ -from compensation.models import EcoAccount, EcoAccountDeduction +from compensation.models import EcoAccount from intervention.inputs import TextToClipboardInput -from intervention.models import Revocation, RevocationDocument, Intervention, InterventionDocument +from intervention.models import Intervention, InterventionDocument from konova.forms import BaseModalForm, NewDocumentForm from konova.utils.general import format_german_float -from konova.utils.messenger import Messenger from konova.utils.user_checks import is_default_group_only -from user.models import UserActionLogEntry, UserAction class ShareInterventionModalForm(BaseModalForm): @@ -118,13 +116,7 @@ class ShareInterventionModalForm(BaseModalForm): ) def save(self): - 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(users) + self.instance.update_sharing_user(self) class NewRevocationModalForm(BaseModalForm): diff --git a/intervention/models/intervention.py b/intervention/models/intervention.py index 64949559..e75c215f 100644 --- a/intervention/models/intervention.py +++ b/intervention/models/intervention.py @@ -284,6 +284,25 @@ class Intervention(BaseObject, ShareableObjectMixin, RecordableObjectMixin, Chec ) return deduction + def update_sharing_user(self, form): + """ Adds a new user with shared access to the intervention + + Args: + form (ShareInterventionModalForm): The form holding the data + + Returns: + + """ + form_data = form.cleaned_data + + keep_accessing_users = form_data["users"] + new_accessing_users = list(form_data["user_select"].values_list("id", flat=True)) + accessing_users = keep_accessing_users + new_accessing_users + users = User.objects.filter( + id__in=accessing_users + ) + self.share_with_list(users) + class InterventionDocument(AbstractDocument): """ diff --git a/konova/models/object.py b/konova/models/object.py index ef07b33e..d62130cc 100644 --- a/konova/models/object.py +++ b/konova/models/object.py @@ -135,45 +135,6 @@ class BaseObject(BaseResource): ) self.log.add(user_action) - def is_shared_with(self, user: User): - """ Access check - - Checks whether a given user has access to this object - - Args: - user (): - - Returns: - - """ - if isinstance(self, ShareableObjectMixin): - return self.users.filter(id=user.id) - else: - return User.objects.none() - - def share_with(self, user: User): - """ Adds user to list of shared access users - - Args: - user (User): The user to be added to the object - - Returns: - - """ - if not self.is_shared_with(user): - self.users.add(user) - - def share_with_list(self, user_list: list): - """ Sets the list of shared access users - - Args: - user_list (list): The users to be added to the object - - Returns: - - """ - self.users.set(user_list) - def generate_new_identifier(self) -> str: """ Generates a new identifier for the intervention object @@ -398,4 +359,40 @@ class ShareableObjectMixin(models.Model): self.generate_access_token(make_unique, rec_depth) else: self.access_token = token - self.save() \ No newline at end of file + self.save() + + def is_shared_with(self, user: User): + """ Access check + + Checks whether a given user has access to this object + + Args: + user (): + + Returns: + + """ + return self.users.filter(id=user.id) + + def share_with(self, user: User): + """ Adds user to list of shared access users + + Args: + user (User): The user to be added to the object + + Returns: + + """ + if not self.is_shared_with(user): + self.users.add(user) + + def share_with_list(self, user_list: list): + """ Sets the list of shared access users + + Args: + user_list (list): The users to be added to the object + + Returns: + + """ + self.users.set(user_list)