From 6785bfc8af7ac7dcc79622817dca2939a47ae973 Mon Sep 17 00:00:00 2001 From: r-xyz <100710244+r-xyz@users.noreply.github.com> Date: Mon, 9 Dec 2024 21:40:44 +0100 Subject: [PATCH] Add `user_documents` to API. Closes #262. --- NEMO/serializers.py | 15 +++++++++++++++ NEMO/urls.py | 1 + NEMO/views/api.py | 9 +++++++++ 3 files changed, 25 insertions(+) diff --git a/NEMO/serializers.py b/NEMO/serializers.py index 9385be4d..74f24dca 100644 --- a/NEMO/serializers.py +++ b/NEMO/serializers.py @@ -54,6 +54,7 @@ TrainingSession, UsageEvent, User, + UserDocuments, ) @@ -152,6 +153,8 @@ class Meta: class UserSerializer(FlexFieldsSerializerMixin, ModelSerializer): + user_documents = serializers.PrimaryKeyRelatedField(many=True, read_only=True) + class Meta: model = User exclude = ["preferences"] @@ -159,6 +162,7 @@ class Meta: "projects": ("NEMO.serializers.ProjectSerializer", {"many": True}), "managed_projects": ("NEMO.serializers.ProjectSerializer", {"many": True}), "groups": ("NEMO.serializers.GroupSerializer", {"many": True}), + "user_documents": ("NEMO.serializers.UserDocumentSerializer", {"many": True}), "user_permissions": ("NEMO.serializers.PermissionSerializer", {"many": True}), } @@ -177,6 +181,17 @@ class Meta: fields = "__all__" +class UserDocumentSerializer(FlexFieldsSerializerMixin, ModelSerializer): + user = PrimaryKeyRelatedField(many=False, queryset=User.objects.all(), allow_null=True, required=False) + + class Meta: + model = UserDocuments + fields = "__all__" + expandable_fields = { + "user": ("NEMO.serializers.UserSerializer", {"many": False, "read_only": True}), + } + + class ProjectSerializer(FlexFieldsSerializerMixin, ModelSerializer): principal_investigators = PrimaryKeyRelatedField( source="manager_set", many=True, queryset=User.objects.all(), allow_null=True, required=False diff --git a/NEMO/urls.py b/NEMO/urls.py index 8ded3aeb..12409dda 100644 --- a/NEMO/urls.py +++ b/NEMO/urls.py @@ -110,6 +110,7 @@ def sort_urls(url_path): router.register(r"training_sessions", api.TrainingSessionViewSet) router.register(r"usage_events", api.UsageEventViewSet) router.register(r"users", api.UserViewSet) +router.register(r"user_documents", api.UserDocumentsViewSet) router.registry.sort(key=sort_urls) reservation_item_types = f'(?P{"|".join(ReservationItemType.values())})' diff --git a/NEMO/views/api.py b/NEMO/views/api.py index 9f9eb215..d38ed3a7 100644 --- a/NEMO/views/api.py +++ b/NEMO/views/api.py @@ -42,6 +42,7 @@ TrainingSession, UsageEvent, User, + UserDocuments, ) from NEMO.rest_pagination import NEMOPageNumberPagination from NEMO.serializers import ( @@ -82,6 +83,7 @@ TrainingSessionSerializer, UsageEventSerializer, UserSerializer, + UserDocumentSerializer, ) from NEMO.typing import QuerySetType from NEMO.utilities import export_format_datetime, remove_duplicates @@ -213,6 +215,13 @@ class UserViewSet(ModelViewSet): } +class UserDocumentsViewSet(ModelViewSet): + filename = "user_documents" + queryset = UserDocuments.objects.all() + serializer_class = UserDocumentSerializer + filterset_fields = {"id": key_filters, "user": key_filters, "name": string_filters, "display_order": number_filters} + + class ProjectDisciplineViewSet(ModelViewSet): filename = "project_disciplines" queryset = ProjectDiscipline.objects.all()