Skip to content
Snippets Groups Projects
Commit 309cd2fc authored by Konrad Mohrfeldt's avatar Konrad Mohrfeldt :koala:
Browse files

feat: add permission names to schema

This is helpful for applications like the dashboard that use static
typing and can use the `AnyPermission` type to catch typos and
missing/renamed permission names during the build phase.
parent 0a1fdf9d
No related branches found
No related tags found
No related merge requests found
Pipeline #8870 failed
from typing import Iterable, Tuple
from django.contrib.auth.models import Permission
def _generate_choices_description(choices: Iterable[Tuple[str, str]]):
def _gen():
......@@ -53,3 +55,21 @@ def fix_schedule_pk_type(result, generator, request, public):
parameter["schema"]["type"] = "integer"
break
return result
def _get_all_relevant_permission_names():
for perm in Permission.objects.all():
action, module_name, model_name = perm.natural_key()
if module_name not in {"program", "steering", "auth"}:
# we’re only interested in some permissions
continue
yield f"{module_name}.{action}"
def add_permission_type_to_schema(result, *args, **kwargs):
result["components"]["schemas"]["AnyPermission"] = {
"description": "Permissions used by steering.",
"enum": sorted(_get_all_relevant_permission_names()),
"type": "string",
}
return result
......@@ -104,6 +104,7 @@ SPECTACULAR_SETTINGS = {
"drf_spectacular.hooks.postprocess_schema_enums",
"steering.schema.add_enum_documentation",
"steering.schema.fix_schedule_pk_type",
"steering.schema.add_permission_type_to_schema",
"drf_spectacular.contrib.djangorestframework_camel_case.camelize_serializer_fields",
],
"VERSION": "1.0.0",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment