def check_bulk_value_per mission self field value if field in status i

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def check_bulk_value_permission(self, field, value):
if field in ('status', 'importance'):
# Check that status or importance are global or belong to the current user's primary organization
model = TaskStatus if field == 'status' else TaskImportance
try:
obj = model.objects.get(pk=value)
if obj.organization is None or obj.organization == self.request.primary_organization:
return True
except (ObjectDoesNotExist, ValueError):
pass
elif field in ('assignee', 'assignor'):
# If the bulk action is performed for a sub matter, we're checking that assignee/assignor are collaborators
# in that sub matter. If the action is performed across several sub matters, we restrict assignee/assignors
# only to the memberes of the current user's primary organization
try:
user = User.objects.get(pk=value)
except (ObjectDoesNotExist, ValueError):
pass
else:
sub_matter_id = self.request.query_params.get('sub_matter')
if sub_matter_id:
try:
sub_matter = SubMatter.objects.get(pk=sub_matter_id)
user_organizations = OrganizationMembership.objects.filter(user=user).values('organization')
return Collaborator.objects.filter(sub_matter=sub_matter,
organization__in=user_organizations).exists()
except (ObjectDoesNotExist, ValueError):
pass
else:
return self.request.primary_organization.members.filter(pk=value).exists()
return False