2021-12-28 17:43:30 +03:00
|
|
|
import re
|
2022-03-03 12:39:16 +03:00
|
|
|
from typing import Iterable, List
|
2022-01-18 19:56:57 +03:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
config = settings.PLUGINS_CONFIG['netbox_interface_sync']
|
2021-12-28 20:32:33 +03:00
|
|
|
|
2021-04-20 10:22:26 +03:00
|
|
|
|
|
|
|
def split(s):
|
2021-12-28 20:32:33 +03:00
|
|
|
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)
|
|
|
|
|
2021-12-27 19:37:56 +03:00
|
|
|
|
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()]
|
2021-12-27 19:37:56 +03:00
|
|
|
|
|
|
|
|
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).
|
2021-12-27 19:37:56 +03:00
|
|
|
|
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}')
|
2021-12-27 19:37:56 +03:00
|
|
|
|
2022-03-03 12:39:16 +03:00
|
|
|
return permissions
|