netbox-plugin-interface-sync/netbox_interface_sync/utils.py

40 lines
1.0 KiB
Python
Raw Normal View History

import re
2022-03-03 12:39:16 +03:00
from typing import Iterable, List
from django.conf import settings
config = settings.PLUGINS_CONFIG['netbox_interface_sync']
2021-04-20 10:22:26 +03:00
def split(s):
for x, y in re.findall(r"(\d*)(\D*)", s):
yield "", int(x or "0")
2021-04-20 10:22:26 +03:00
yield y, 0
def natural_keys(c):
return tuple(split(c))
def human_sorted(iterable: Iterable):
return sorted(iterable, key=natural_keys)
2022-03-03 12:39:16 +03:00
def make_integer_list(lst: List[str]):
return [int(i) for i in lst if i.isdigit()]
2022-03-03 12:39:16 +03:00
def get_permissions_for_model(model, actions: Iterable[str]) -> List[str]:
"""
Resolve a list of permissions for a given model (or instance).
2022-03-03 12:39:16 +03:00
:param model: A model or instance
:param actions: List of actions: view, add, change, or delete
"""
permissions = []
for action in actions:
if action not in ("view", "add", "change", "delete"):
raise ValueError(f"Unsupported action: {action}")
permissions.append(f'{model._meta.app_label}.{action}_{model._meta.model_name}')
2022-03-03 12:39:16 +03:00
return permissions