def get_api_perms(request): """ Return all API perms for @groups and users. Format: {resource: {create/read/update/delete: [users, @groups]}} """ perms = {} ret = get_url_with_resource(request) for obj in models.ResourcePermission.objects.all(): name = URL_ARG_RE.sub(r'{\1}', obj.resource.name) if name not in ret: continue url = ret[name] if read_permission_for_all() and obj.permission.name == 'read': members_list = ['@all'] else: members_list = get_users_and_groups(obj) perms.setdefault(name, OrderedDict()).setdefault(obj.permission.name, members_list) perms.setdefault(name, OrderedDict()).setdefault('url', url) # sort groups and users for resource in perms: for perm in perms[resource]: if not isinstance(perms[resource][perm], set): # sort only lists with groups and users, skip 'url' continue perms[resource][perm] = sorted(perms[resource][perm]) result = OrderedDict(sorted(perms.items())) return result
def get_api_perms(request): """ Return all API perms for @groups and users. Format: {resource: {create/read/update/delete: [users, @groups]}} """ perms = {} ret = get_url_with_resource(request) for obj in models.ResourcePermission.objects.all(): name = URL_ARG_RE.sub(r'{\1}', obj.resource.name) if name not in ret: continue url = ret[name] if read_permission_for_all() and obj.permission.name == 'read': members_list = ['@all'] else: members_list = get_users_and_groups(obj) perms.setdefault(name, OrderedDict()).setdefault(obj.permission.name, members_list) perms.setdefault(name, OrderedDict()).setdefault('url', url) # sort groups and users for resource in perms: for perm in perms[resource]: if not isinstance(perms[resource][perm], set): # sort only lists with groups and users, skip 'url' continue perms[resource][perm] = sorted(perms[resource][perm]) result = OrderedDict(sorted(perms.items())) return result
def get_url_with_resource(request): api_root_dict = OrderedDict() viewsets = {} ret = OrderedDict() list_name = router.routes[0].name for prefix, viewset, basename in router.registry: api_root_dict[prefix] = list_name.format(basename=basename) viewsets[prefix] = viewset sorted_api_root_dict = OrderedDict(sorted(api_root_dict.items())) for key, url_name in sorted_api_root_dict.items(): name = URL_ARG_RE.sub(r'{\1}', key) ret[name] = None urlargs = [_get_arg_value(arg[0]) for arg in URL_ARG_RE.findall(key)] for getter in [_get_list_url, _get_nested_list_url, _get_detail_url]: try: if getter == _get_list_url: ret[name] = urldecode( getter(url_name, viewsets[key], request)) else: ret[name] = urldecode( getter(url_name, viewsets[key], request, urlargs)) break except NoReverseMatch: continue return ret
def get_url_with_resource(request): api_root_dict = OrderedDict() viewsets = {} ret = OrderedDict() list_name = router.routes[0].name for prefix, viewset, basename in router.registry: api_root_dict[prefix] = list_name.format(basename=basename) viewsets[prefix] = viewset sorted_api_root_dict = OrderedDict(sorted(api_root_dict.items())) for key, url_name in sorted_api_root_dict.items(): name = URL_ARG_RE.sub(r'{\1}', key) ret[name] = None urlargs = [_get_arg_value(arg[0]) for arg in URL_ARG_RE.findall(key)] for getter in [_get_list_url, _get_nested_list_url, _get_detail_url]: try: if getter == _get_list_url: ret[name] = urldecode(getter(url_name, viewsets[key], request)) else: ret[name] = urldecode(getter(url_name, viewsets[key], request, urlargs)) break except NoReverseMatch: continue return ret