Update role permission view to use AddRemoveView
Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
@@ -167,6 +167,7 @@
|
|||||||
* Update the role group list view to use the new AddRemoveView.
|
* Update the role group list view to use the new AddRemoveView.
|
||||||
* Commit the group event in conjunction with the role event
|
* Commit the group event in conjunction with the role event
|
||||||
when a group is added or remove from role.
|
when a group is added or remove from role.
|
||||||
|
* Update the role permission view to use the new AddRemoveView.
|
||||||
|
|
||||||
3.1.11 (2019-04-XX)
|
3.1.11 (2019-04-XX)
|
||||||
===================
|
===================
|
||||||
|
|||||||
@@ -199,6 +199,7 @@ Other changes
|
|||||||
* Update the role group list view to use the new AddRemoveView.
|
* Update the role group list view to use the new AddRemoveView.
|
||||||
* Commit the group event in conjunction with the role event
|
* Commit the group event in conjunction with the role event
|
||||||
when a group is added or remove from role.
|
when a group is added or remove from role.
|
||||||
|
* Update the role permission view to use the new AddRemoveView.
|
||||||
|
|
||||||
Removals
|
Removals
|
||||||
--------
|
--------
|
||||||
|
|||||||
@@ -147,6 +147,20 @@ class Role(models.Model):
|
|||||||
return (self.label,)
|
return (self.label,)
|
||||||
natural_key.dependencies = ['auth.Group', 'permissions.StoredPermission']
|
natural_key.dependencies = ['auth.Group', 'permissions.StoredPermission']
|
||||||
|
|
||||||
|
def permissions_add(self, queryset, _user=None):
|
||||||
|
with transaction.atomic():
|
||||||
|
event_role_edited.commit(
|
||||||
|
actor=_user, target=self
|
||||||
|
)
|
||||||
|
self.permissions.add(*queryset)
|
||||||
|
|
||||||
|
def permissions_remove(self, queryset, _user=None):
|
||||||
|
with transaction.atomic():
|
||||||
|
event_role_edited.commit(
|
||||||
|
actor=_user, target=self
|
||||||
|
)
|
||||||
|
self.permissions.remove(*queryset)
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
_user = kwargs.pop('_user', None)
|
_user = kwargs.pop('_user', None)
|
||||||
|
|
||||||
|
|||||||
@@ -99,72 +99,52 @@ class SetupRoleMembersView(AddRemoveView):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class SetupRolePermissionsView(AssignRemoveView):
|
class SetupRolePermissionsView(AddRemoveView):
|
||||||
|
action_add_method = 'permissions_add'
|
||||||
|
action_remove_method = 'permissions_remove'
|
||||||
grouped = True
|
grouped = True
|
||||||
left_list_title = _('Available permissions')
|
main_object_model = Role
|
||||||
right_list_title = _('Granted permissions')
|
main_object_permission = permission_role_edit
|
||||||
|
main_object_pk_url_kwarg = 'pk'
|
||||||
|
list_available_title = _('Available permissions')
|
||||||
|
list_added_title = _('Granted permissions')
|
||||||
|
related_field = 'permissions'
|
||||||
|
secondary_object_model = StoredPermission
|
||||||
|
|
||||||
@staticmethod
|
def generate_choices(self, queryset):
|
||||||
def generate_choices(entries):
|
namespaces_dictionary = {}
|
||||||
results = []
|
|
||||||
|
|
||||||
# Sort permissions by their translatable label
|
# Sort permissions by their translatable label
|
||||||
entries = sorted(
|
object_list = sorted(
|
||||||
entries, key=lambda permission: permission.volatile_permission.label
|
queryset, key=lambda permission: permission.volatile_permission.label
|
||||||
)
|
)
|
||||||
|
|
||||||
# Group permissions by namespace
|
# Group permissions by namespace
|
||||||
for namespace, permissions in itertools.groupby(entries, lambda entry: entry.namespace):
|
for permission in object_list:
|
||||||
permission_options = [
|
namespaces_dictionary.setdefault(
|
||||||
(force_text(permission.pk), permission) for permission in permissions
|
permission.volatile_permission.namespace.label,
|
||||||
]
|
[]
|
||||||
results.append(
|
)
|
||||||
(PermissionNamespace.get(namespace), permission_options)
|
namespaces_dictionary[permission.volatile_permission.namespace.label].append(
|
||||||
|
(permission.pk, force_text(permission))
|
||||||
)
|
)
|
||||||
|
|
||||||
return results
|
# Sort permissions by their translatable namespace label
|
||||||
|
return sorted(namespaces_dictionary.items())
|
||||||
|
|
||||||
def add(self, item):
|
def get_actions_extra_kwargs(self):
|
||||||
permission = get_object_or_404(klass=StoredPermission, pk=item)
|
return {'_user': self.request.user}
|
||||||
self.get_object().permissions.add(permission)
|
|
||||||
|
|
||||||
def dispatch(self, request, *args, **kwargs):
|
|
||||||
AccessControlList.objects.check_access(
|
|
||||||
permissions=(permission_role_edit,),
|
|
||||||
user=self.request.user, obj=self.get_object()
|
|
||||||
)
|
|
||||||
return super(SetupRolePermissionsView, self).dispatch(request, *args, **kwargs)
|
|
||||||
|
|
||||||
def get_extra_context(self):
|
def get_extra_context(self):
|
||||||
return {
|
return {
|
||||||
'object': self.get_object(),
|
'object': self.main_object,
|
||||||
'subtitle': _(
|
'subtitle': _(
|
||||||
'Permissions granted here will apply to the entire system '
|
'Permissions granted here will apply to the entire system '
|
||||||
'and all objects.'
|
'and all objects.'
|
||||||
),
|
),
|
||||||
'title': _('Permissions for role: %s') % self.get_object(),
|
'title': _('Permissions for role: %s') % self.main_object,
|
||||||
}
|
}
|
||||||
|
|
||||||
def get_object(self):
|
|
||||||
return get_object_or_404(klass=Role, pk=self.kwargs['pk'])
|
|
||||||
|
|
||||||
def left_list(self):
|
|
||||||
Permission.refresh()
|
|
||||||
|
|
||||||
return SetupRolePermissionsView.generate_choices(
|
|
||||||
entries=StoredPermission.objects.exclude(
|
|
||||||
id__in=self.get_object().permissions.values_list('pk', flat=True)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
def right_list(self):
|
|
||||||
return SetupRolePermissionsView.generate_choices(
|
|
||||||
entries=self.get_object().permissions.all()
|
|
||||||
)
|
|
||||||
|
|
||||||
def remove(self, item):
|
|
||||||
permission = get_object_or_404(klass=StoredPermission, pk=item)
|
|
||||||
self.get_object().permissions.remove(permission)
|
|
||||||
|
|
||||||
|
|
||||||
class RoleListView(SingleObjectListView):
|
class RoleListView(SingleObjectListView):
|
||||||
|
|||||||
Reference in New Issue
Block a user