def test_permission_group_form_updates_members(regular_user): with replace_modules([ARestrictedTestModule]): modules = [m for m in get_modules()] test_module = modules[0] module_permissions = test_module.get_required_permissions() assert module_permissions group = get_default_permission_group() PermissionGroupForm(instance=group, prefix=None) assert not group.permissions.all() assert not group.user_set.all() data = { "name": "New Name", "members": [force_text(regular_user.pk)], } for permission in ARestrictedTestModule().get_required_permissions(): data["perm:%s" % permission] = permission form = PermissionGroupForm(instance=group, prefix=None, data=data) form.save() assert group.name == "New Name" assert set(module_permissions) == get_permissions_from_group(group) assert regular_user in group.user_set.all() form = PermissionGroupForm(instance=group, prefix=None, data={"name": "Name"}) form.save() assert not group.permissions.all() assert not group.user_set.all()
def __init__(self, *args, **kwargs): super(PermissionGroupForm, self).__init__(*args, **kwargs) self.fields["name"].help_text = _("The permission group name.") initial_members = self._get_initial_members() members_field = Select2MultipleField( model=get_user_model(), initial=[member.pk for member in initial_members], required=False, label=_("Members"), help_text=_("Set the users that belong to this permission group.") ) members_field.widget.choices = [(member.pk, force_text(member)) for member in initial_members] self.fields["members"] = members_field initial_permissions = list(get_permissions_from_group(self.instance.pk)) if self.instance.pk else [] self.admin_modules = self._get_module_choices() for admin_module in self.admin_modules: all_permissions_granted = True partial_permissions_granted = False admin_module.required_permissions_fields = [] admin_module.per_view_permissions_fields = [] for required_permission in admin_module.get_required_permissions(): field_id = "perm:{}".format(required_permission) self.fields[field_id] = forms.BooleanField( required=False, label=required_permission, initial=(required_permission in initial_permissions) ) admin_module.required_permissions_fields.append(field_id) if required_permission in initial_permissions: partial_permissions_granted = True else: all_permissions_granted = False extra_permissions = ( list(get_permissions_from_urls(admin_module.get_urls())) + list(admin_module.get_extra_permissions()) ) for permission in extra_permissions: field_id = "perm:{}".format(permission) self.fields[field_id] = forms.BooleanField( required=False, label=permission, initial=(permission in initial_permissions) ) admin_module.per_view_permissions_fields.append(field_id) if permission in initial_permissions: partial_permissions_granted = True else: all_permissions_granted = False admin_module.all_permissions_granted = all_permissions_granted admin_module.partial_permissions_granted = False if all_permissions_granted else partial_permissions_granted
def __init__(self, *args, **kwargs): super(PermissionGroupForm, self).__init__(*args, **kwargs) self.fields["name"].help_text = _("The Permission Group name.") initial_members = self._get_initial_members() members_field = Select2MultipleField( model=get_user_model(), initial=[member.pk for member in initial_members], required=False, label=_("Members"), help_text=_("Set the users that belong to this Permission Group."), ) members_field.widget.choices = [(member.pk, force_text(member)) for member in initial_members] self.fields["members"] = members_field initial_permissions = list(get_permissions_from_group( self.instance.pk)) if self.instance.pk else [] self.admin_modules = self._get_module_choices() for admin_module in self.admin_modules: all_permissions_granted = True partial_permissions_granted = False admin_module.required_permissions_fields = [] admin_module.per_view_permissions_fields = [] for required_permission in admin_module.get_required_permissions(): field_id = "perm:{}".format(required_permission) self.fields[field_id] = forms.BooleanField( required=False, label=required_permission, initial=(required_permission in initial_permissions)) admin_module.required_permissions_fields.append(field_id) if required_permission in initial_permissions: partial_permissions_granted = True else: all_permissions_granted = False extra_permissions = list( get_permissions_from_urls(admin_module.get_urls())) + list( admin_module.get_extra_permissions()) for permission in extra_permissions: field_id = "perm:{}".format(permission) self.fields[field_id] = forms.BooleanField( required=False, label=permission, initial=(permission in initial_permissions)) admin_module.per_view_permissions_fields.append(field_id) if permission in initial_permissions: partial_permissions_granted = True else: all_permissions_granted = False admin_module.all_permissions_granted = all_permissions_granted admin_module.partial_permissions_granted = False if all_permissions_granted else partial_permissions_granted
def __init__(self, *args, **kwargs): super(PermissionGroupForm, self).__init__(*args, **kwargs) self.fields["name"].help_text = _("The Permission Group name.") initial_permissions = list(get_permissions_from_group( self.instance.pk)) if self.instance.pk else [] self.admin_modules = self._get_module_choices() for admin_module in self.admin_modules: all_permissions_granted = True partial_permissions_granted = False admin_module.required_permissions_fields = [] admin_module.per_view_permissions_fields = [] help_texts = admin_module.get_permissions_help_texts() for required_permission in admin_module.get_required_permissions(): field_id = "perm:{}".format(required_permission) self.fields[field_id] = forms.BooleanField( required=False, label=required_permission, initial=(required_permission in initial_permissions), help_text=help_texts.get(required_permission), ) admin_module.required_permissions_fields.append(field_id) if required_permission in initial_permissions: partial_permissions_granted = True else: all_permissions_granted = False extra_permissions = list( get_permissions_from_urls(admin_module.get_urls())) + list( admin_module.get_extra_permissions()) for permission in extra_permissions: field_id = "perm:{}".format(permission) self.fields[field_id] = forms.BooleanField( required=False, label=permission, initial=(permission in initial_permissions), help_text=help_texts.get(permission), ) admin_module.per_view_permissions_fields.append(field_id) if permission in initial_permissions: partial_permissions_granted = True else: all_permissions_granted = False admin_module.all_permissions_granted = all_permissions_granted admin_module.partial_permissions_granted = False if all_permissions_granted else partial_permissions_granted