Merge branch 'part-permissions' of https://github.com/SchrodingersGat/InvenTree into part-permissions

This commit is contained in:
Oliver Walters 2020-10-06 16:03:33 +11:00
commit 77a2b6de8b
3 changed files with 19 additions and 10 deletions

View File

@ -97,15 +97,12 @@ class RoleGroupAdmin(admin.ModelAdmin):
# Save inlines before model
# https://stackoverflow.com/a/14860703/12794913
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):
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):

View File

@ -155,8 +155,15 @@ class RuleSet(models.Model):
model=model
)
def __str__(self):
return self.name
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
def save(self, *args, **kwargs):
@ -331,6 +338,9 @@ def create_missing_rule_sets(sender, instance, **kwargs):
then we can now use these RuleSet values to update the
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)
update_group_roles(instance)

View File

@ -137,7 +137,8 @@ class RuleSetModelTest(TestCase):
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
self.assertEqual(group.permissions.count(), 3 * len(permission_set))
@ -151,7 +152,8 @@ class RuleSetModelTest(TestCase):
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
self.assertEqual(group.permissions.count(), 0)