-
-
Notifications
You must be signed in to change notification settings - Fork 167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lint, test, fix mongo helper partial permissions #4776
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
# coding: utf-8 | ||
from __future__ import annotations | ||
|
||
import json | ||
from collections import defaultdict | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
from django.db import transaction | ||
from django.contrib.auth.models import Permission, User | ||
from django.db import transaction | ||
from django.urls import Resolver404 | ||
from django.utils.translation import gettext as t | ||
from rest_framework import serializers | ||
|
@@ -27,24 +28,22 @@ | |
) | ||
from kpi.utils.urls import absolute_resolve | ||
|
||
|
||
ASSIGN_OWNER_ERROR_MESSAGE = "Owner's permissions cannot be assigned explicitly" | ||
|
||
|
||
class AssetPermissionAssignmentSerializer(serializers.ModelSerializer): | ||
|
||
url = serializers.SerializerMethodField() | ||
user = RelativePrefixHyperlinkedRelatedField( | ||
view_name='user-detail', | ||
lookup_field='username', | ||
queryset=User.objects.all(), | ||
style={'base_template': 'input.html'} # Render as a simple text box | ||
style={'base_template': 'input.html'}, # Render as a simple text box | ||
) | ||
permission = RelativePrefixHyperlinkedRelatedField( | ||
view_name='permission-detail', | ||
lookup_field='codename', | ||
queryset=Permission.objects.all(), | ||
style={'base_template': 'input.html'} # Render as a simple text box | ||
style={'base_template': 'input.html'}, # Render as a simple text box | ||
) | ||
partial_permissions = serializers.SerializerMethodField() | ||
label = serializers.SerializerMethodField() | ||
|
@@ -99,26 +98,28 @@ def get_partial_permissions(self, object_permission): | |
# TODO: optimize `asset.get_partial_perms()` so it doesn't execute | ||
# a new query for each assignment | ||
partial_perms = asset.get_partial_perms( | ||
object_permission.user_id, with_filters=True) | ||
object_permission.user_id, with_filters=True | ||
) | ||
if not partial_perms: | ||
return None | ||
|
||
if partial_perms: | ||
hyperlinked_partial_perms = [] | ||
for perm_codename, filters in partial_perms.items(): | ||
url = self.__get_permission_hyperlink(perm_codename) | ||
hyperlinked_partial_perms.append({ | ||
'url': url, | ||
'filters': filters | ||
}) | ||
hyperlinked_partial_perms.append( | ||
{'url': url, 'filters': filters} | ||
) | ||
return hyperlinked_partial_perms | ||
return None | ||
|
||
def get_url(self, object_permission): | ||
asset_uid = self.context.get('asset_uid') | ||
return reverse('asset-permission-assignment-detail', | ||
args=(asset_uid, object_permission.uid), | ||
request=self.context.get('request', None)) | ||
return reverse( | ||
'asset-permission-assignment-detail', | ||
args=(asset_uid, object_permission.uid), | ||
request=self.context.get('request', None), | ||
) | ||
|
||
def validate(self, attrs): | ||
# Because `partial_permissions` is a `SerializerMethodField`, | ||
|
@@ -145,16 +146,16 @@ def validate_partial_permissions(self, attrs): | |
return attrs | ||
|
||
def _invalid_partial_permissions(message): | ||
raise serializers.ValidationError( | ||
{'partial_permissions': message} | ||
) | ||
raise serializers.ValidationError({'partial_permissions': message}) | ||
|
||
request = self.context['request'] | ||
partial_permissions = None | ||
|
||
if isinstance(request.data, dict): # for a single assignment | ||
partial_permissions = request.data.get('partial_permissions') | ||
elif self.context.get('partial_permissions'): # injected during bulk assignment | ||
elif self.context.get( | ||
'partial_permissions' | ||
): # injected during bulk assignment | ||
partial_permissions = self.context.get('partial_permissions') | ||
|
||
if not partial_permissions: | ||
|
@@ -166,12 +167,12 @@ def _invalid_partial_permissions(message): | |
|
||
partial_permissions_attr = defaultdict(list) | ||
|
||
for partial_permission, filters_ in \ | ||
self.__get_partial_permissions_generator(partial_permissions): | ||
for ( | ||
partial_permission, | ||
filters_, | ||
) in self.__get_partial_permissions_generator(partial_permissions): | ||
try: | ||
resolver_match = absolute_resolve( | ||
partial_permission.get('url') | ||
) | ||
resolver_match = absolute_resolve(partial_permission.get('url')) | ||
except (TypeError, Resolver404): | ||
_invalid_partial_permissions(t('Invalid `url`')) | ||
|
||
|
@@ -181,8 +182,9 @@ def _invalid_partial_permissions(message): | |
_invalid_partial_permissions(t('Invalid `url`')) | ||
|
||
# Permission must valid and must be assignable. | ||
if not self._validate_permission(codename, | ||
SUFFIX_SUBMISSIONS_PERMS): | ||
if not self._validate_permission( | ||
codename, SUFFIX_SUBMISSIONS_PERMS | ||
): | ||
_invalid_partial_permissions(t('Invalid `url`')) | ||
|
||
# No need to validate Mongo syntax, query will fail | ||
|
@@ -249,11 +251,11 @@ def _validate_permission(self, codename, suffix=None): | |
""" | ||
return ( | ||
# DONOTMERGE abusive to the database server? | ||
codename in Asset.objects.only('asset_type').get( | ||
uid=self.context['asset_uid'] | ||
).get_assignable_permissions( | ||
with_partial=True | ||
) and (suffix is None or codename.endswith(suffix)) | ||
codename | ||
in Asset.objects.only('asset_type') | ||
.get(uid=self.context['asset_uid']) | ||
.get_assignable_permissions(with_partial=True) | ||
and (suffix is None or codename.endswith(suffix)) | ||
) | ||
|
||
def __get_partial_permissions_generator(self, partial_permissions): | ||
|
@@ -275,13 +277,14 @@ def __get_permission_hyperlink(self, codename): | |
:param codename: str | ||
:return: str. url | ||
""" | ||
return reverse('permission-detail', | ||
args=(codename,), | ||
request=self.context.get('request', None)) | ||
return reverse( | ||
'permission-detail', | ||
args=(codename,), | ||
request=self.context.get('request', None), | ||
) | ||
|
||
|
||
class PartialPermissionField(serializers.Field): | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💔 again. |
||
default_error_messages = { | ||
'invalid': t('Not a valid list.'), | ||
'blank': t('This field may not be blank.'), | ||
|
@@ -301,7 +304,6 @@ def to_representation(self, value): | |
|
||
|
||
class PermissionAssignmentSerializer(serializers.Serializer): | ||
|
||
user = serializers.CharField() | ||
permission = serializers.CharField() | ||
partial_permissions = PartialPermissionField() | ||
|
@@ -318,11 +320,15 @@ class AssetBulkInsertPermissionSerializer(serializers.Serializer): | |
Warning: If less queries are sent to DB, it consumes more CPU and memory. | ||
The bigger the assignments are, the bigger the resources footprint will be. | ||
""" | ||
|
||
assignments = serializers.ListField(child=PermissionAssignmentSerializer()) | ||
|
||
@dataclass(frozen=True) | ||
class PermissionAssignment: | ||
""" A more-explicit alternative to a simple tuple """ | ||
""" | ||
A more-explicit alternative to a simple tuple | ||
""" | ||
|
||
user_pk: int | ||
permission_codename: str | ||
partial_permissions_json: Optional[str] = None | ||
|
@@ -355,7 +361,7 @@ def create(self, validated_data): | |
partial_perms = json.loads(addition.partial_permissions_json) | ||
else: | ||
partial_perms = None | ||
perm = asset.assign_perm( | ||
asset.assign_perm( | ||
user_obj=user_pk_to_obj_cache[addition.user_pk], | ||
perm=addition.permission_codename, | ||
partial_perms=partial_perms, | ||
|
@@ -448,8 +454,7 @@ def get_set_of_incoming_assignments( | |
# Expand to include implied partial permissions | ||
if incoming_permission.codename == PERM_PARTIAL_SUBMISSIONS: | ||
partial_permissions = json.dumps( | ||
AssetUserPartialPermission\ | ||
.update_partial_perms_to_include_implied( | ||
AssetUserPartialPermission.update_partial_perms_to_include_implied( | ||
asset, incoming_assignment['partial_permissions'] | ||
), | ||
sort_keys=True, | ||
|
@@ -503,6 +508,11 @@ def validate(self, attrs): | |
username = self._get_arg_from_url('username', user_url) | ||
username_to_url[username] = user_url | ||
for partial_assignment in assignment.get('partial_permissions', []): | ||
if 'filters' not in partial_assignment: | ||
# Instead of this, we should validate using DRF | ||
raise serializers.ValidationError( | ||
'Permission assignment must contain filters' | ||
) | ||
partial_codename = self._get_arg_from_url( | ||
'codename', partial_assignment['url'] | ||
) | ||
|
@@ -562,7 +572,7 @@ def validate(self, attrs): | |
].codename | ||
assignment_with_objects['partial_permissions'][ | ||
partial_codename | ||
] = partial_assignment['filters'] | ||
] = partial_assignment.get('filters') | ||
assignments_with_objects.append(assignment_with_objects) | ||
|
||
attrs['assignments'] = assignments_with_objects | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💔 . Your linter is going to be crazy on all my code. I do add an extra line in purpose.
IMHO, It's more compliant with separated blocks of docstring and variables declaration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It happens several times in this PR. I would keep the extra line at the beginning of classes (and methods).