Refactoring

* moves updating of shared access users into Intervention
This commit is contained in:
mpeltriaux 2021-11-16 12:51:10 +01:00
parent d8f0db6bd6
commit 6ad387daa3
3 changed files with 60 additions and 52 deletions

View File

@ -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):

View File

@ -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):
"""

View File

@ -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
@ -399,3 +360,39 @@ class ShareableObjectMixin(models.Model):
else:
self.access_token = token
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)