Merge branch 'master' into part-permissions

This commit is contained in:
Oliver 2020-10-06 12:35:45 +11:00 committed by GitHub
commit e076470305
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 10 deletions

View File

@ -97,15 +97,12 @@ class RoleGroupAdmin(admin.ModelAdmin):
# Save inlines before model # Save inlines before model
# https://stackoverflow.com/a/14860703/12794913 # https://stackoverflow.com/a/14860703/12794913
def save_model(self, request, obj, form, change): def save_model(self, request, obj, form, change):
if obj is not None:
# Save model immediately only if in 'Add role' view
super().save_model(request, obj, form, change)
else:
pass # don't actually save the parent instance pass # don't actually save the parent instance
def save_formset(self, request, form, formset, change): def save_formset(self, request, form, formset, change):
formset.save() # this will save the children formset.save() # this will save the children
form.instance.save() # form.instance is the parent # update_fields is required to trigger permissions update
form.instance.save(update_fields=['name']) # form.instance is the parent
class InvenTreeUserAdmin(UserAdmin): class InvenTreeUserAdmin(UserAdmin):

View File

@ -155,7 +155,14 @@ class RuleSet(models.Model):
model=model model=model
) )
def __str__(self): def __str__(self, debug=False):
""" Ruleset string representation """
if debug:
# Makes debugging easier
return f'{str(self.group).ljust(15)}: {self.name.title().ljust(15)} | ' \
f'v: {str(self.can_view).ljust(5)} | a: {str(self.can_add).ljust(5)} | ' \
f'c: {str(self.can_change).ljust(5)} | d: {str(self.can_delete).ljust(5)}'
else:
return self.name return self.name
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
@ -327,6 +334,9 @@ def create_missing_rule_sets(sender, instance, **kwargs):
then we can now use these RuleSet values to update the then we can now use these RuleSet values to update the
group permissions. group permissions.
""" """
created = kwargs.get('created', False)
# To trigger the group permissions update: update_fields should not be None
update_fields = kwargs.get('update_fields', None)
created = kwargs.get('created', False) created = kwargs.get('created', False)
# To trigger the group permissions update: update_fields should not be None # To trigger the group permissions update: update_fields should not be None

View File

@ -137,7 +137,8 @@ class RuleSetModelTest(TestCase):
rule.save() rule.save()
group.save() # update_fields is required to trigger permissions update
group.save(update_fields=['name'])
# There should now be three permissions for each rule set # There should now be three permissions for each rule set
self.assertEqual(group.permissions.count(), 3 * len(permission_set)) self.assertEqual(group.permissions.count(), 3 * len(permission_set))
@ -151,7 +152,8 @@ class RuleSetModelTest(TestCase):
rule.save() rule.save()
group.save() # update_fields is required to trigger permissions update
group.save(update_fields=['name'])
# There should now not be any permissions assigned to this group # There should now not be any permissions assigned to this group
self.assertEqual(group.permissions.count(), 0) self.assertEqual(group.permissions.count(), 0)