simplify yml definitions to use wildcard syntax

This commit is contained in:
Les Begnaud 2019-12-11 10:14:38 -06:00 committed by Christian Mäder
parent cce4370d41
commit aa0d2a6e01
4 changed files with 25 additions and 36 deletions

View File

@ -7,19 +7,15 @@
# writers: # writers:
# users: # users:
# - writer # - writer
## specify explicit permission codenames or codename filter functions and filters to match on ## specify explicit permission codenames or include wildcard to match multiple permissions
# permissions: # permissions:
# - delete_device # - delete_device
# - delete_virtualmachine # - delete_virtualmachine
# - codename__startswith: # - add_*
# - add_ # - change_*
# - change_
# vm_managers: # vm_managers:
# - codename__endswith: # - *_virtualmachine
# - _virtualmachine
# device_managers: # device_managers:
# - codename__contains: # - *device*
# - device
# creators: # creators:
# - codename__startswith: # - add_*
# - add_

View File

@ -4,10 +4,9 @@
# password: reader # password: reader
# writer: # writer:
# password: writer # password: writer
## specify explicit permission codenames or codename filter functions and filters to match on ## specify explicit permission codenames or include wildcard to match multiple permissions
# permissions: # permissions:
# - delete_device # - delete_device
# - delete_virtualmachine # - delete_virtualmachine
# - codename__startswith: # - add_*
# - add_ # - change_*
# - change_

View File

@ -30,16 +30,13 @@ with file.open('r') as stream:
if yaml_permissions: if yaml_permissions:
permission_object.permissions.clear() permission_object.permissions.clear()
for yaml_permission in yaml_permissions: for yaml_permission in yaml_permissions:
if isinstance(yaml_permission,dict): if '*' in yaml_permission:
# assume this is the specific codename filter function instead of an exact codename permission_codename_function = 'codename__iregex'
permission_codename_function = list(yaml_permission.keys())[0] permission_codename = '^' + yaml_permission.replace('*','.*') + '$'
permission_codenames = yaml_permission[permission_codename_function]
else: else:
permission_codename_function = 'codename' permission_codename_function = 'codename'
permission_codenames = list({yaml_permission}) permission_codename = yaml_permission
# supports either one codename from the permissions list, or multiple codenames in a codename_function dict
for permission_codename in permission_codenames:
# supports non-unique permission codenames # supports non-unique permission codenames
for permission in eval('Permission.objects.filter(' + permission_codename_function + '=permission_codename)'): for permission in eval('Permission.objects.filter(' + permission_codename_function + '=permission_codename)'):
permission_object.permissions.add(permission) permission_object.permissions.add(permission)

View File

@ -29,16 +29,13 @@ with file.open('r') as stream:
if yaml_permissions: if yaml_permissions:
permission_object.permissions.clear() permission_object.permissions.clear()
for yaml_permission in yaml_permissions: for yaml_permission in yaml_permissions:
if isinstance(yaml_permission,dict): if '*' in yaml_permission:
# assume this is the specific codename filter function instead of an exact codename permission_codename_function = 'codename__iregex'
permission_codename_function = list(yaml_permission.keys())[0] permission_codename = '^' + yaml_permission.replace('*','.*') + '$'
permission_codenames = yaml_permission[permission_codename_function]
else: else:
permission_codename_function = 'codename' permission_codename_function = 'codename'
permission_codenames = list({yaml_permission}) permission_codename = yaml_permission
# supports either one codename from the permissions list, or multiple codenames in a codename_function dict
for permission_codename in permission_codenames:
# supports non-unique permission codenames # supports non-unique permission codenames
for permission in eval('Permission.objects.filter(' + permission_codename_function + '=permission_codename)'): for permission in eval('Permission.objects.filter(' + permission_codename_function + '=permission_codename)'):
permission_object.permissions.add(permission) permission_object.permissions.add(permission)