From 4b37c1963b34cb9d92f9ada7bd785e1f495558c1 Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Fri, 6 May 2022 22:29:30 -0700 Subject: [PATCH] skeleton frontend_settings api endpoint --- src/documents/serialisers.py | 19 +++++++++++++++++++ src/documents/views.py | 35 +++++++++++++++++++++++++++++++++++ src/paperless/urls.py | 6 ++++++ 3 files changed, 60 insertions(+) diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index a257a1dd6..0339db88e 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -11,6 +11,7 @@ from . import bulk_edit from .models import Correspondent from .models import Document from .models import DocumentType +from .models import FrontendSettings from .models import MatchingModel from .models import SavedView from .models import SavedViewFilterRule @@ -498,3 +499,21 @@ class BulkDownloadSerializer(DocumentListSerializer): "bzip2": zipfile.ZIP_BZIP2, "lzma": zipfile.ZIP_LZMA, }[compression] + + +class FrontendSettingsViewSerializer(serializers.ModelSerializer): + class Meta: + model = FrontendSettings + depth = 1 + fields = [ + "id", + "settings", + ] + + def update(self, instance, validated_data): + super(FrontendSettingsViewSerializer, self).update(instance, validated_data) + return instance + + def create(self, validated_data): + frontend_settings = FrontendSettings.objects.create(**validated_data) + return frontend_settings diff --git a/src/documents/views.py b/src/documents/views.py index 5d179da02..8b548eed7 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -11,6 +11,7 @@ from unicodedata import normalize from urllib.parse import quote from django.conf import settings +from django.contrib.auth.models import User from django.db.models import Case from django.db.models import Count from django.db.models import IntegerField @@ -70,6 +71,7 @@ from .serialisers import CorrespondentSerializer from .serialisers import DocumentListSerializer from .serialisers import DocumentSerializer from .serialisers import DocumentTypeSerializer +from .serialisers import FrontendSettingsViewSerializer from .serialisers import PostDocumentSerializer from .serialisers import SavedViewSerializer from .serialisers import TagSerializer @@ -715,3 +717,36 @@ class RemoteVersionView(GenericAPIView): "feature_is_set": feature_is_set, }, ) + + +class FrontendSettingsView(GenericAPIView): + + permission_classes = (IsAuthenticated,) + serializer_class = FrontendSettingsViewSerializer + + def get(self, request, format=None): + serializer = self.get_serializer(data=request.data) + serializer.is_valid(raise_exception=True) + + user = User.objects.get(pk=request.user.id) + settings = [] + if hasattr(user, "frontend_settings"): + settings = user.frontend_settings.settings + return Response( + { + "user_id": user.id, + "settings": settings, + }, + ) + + def post(self, request, format=None): + serializer = self.get_serializer(data=request.data) + serializer.is_valid(raise_exception=True) + + serializer.save(user=self.request.user) + + return Response( + { + "success": True, + }, + ) diff --git a/src/paperless/urls.py b/src/paperless/urls.py index 833788cce..0ad9d072f 100644 --- a/src/paperless/urls.py +++ b/src/paperless/urls.py @@ -11,6 +11,7 @@ from documents.views import BulkDownloadView from documents.views import BulkEditView from documents.views import CorrespondentViewSet from documents.views import DocumentTypeViewSet +from documents.views import FrontendSettingsView from documents.views import IndexView from documents.views import LogViewSet from documents.views import PostDocumentView @@ -78,6 +79,11 @@ urlpatterns = [ RemoteVersionView.as_view(), name="remoteversion", ), + re_path( + r"^frontend_settings/", + FrontendSettingsView.as_view(), + name="frontend_settings", + ), path("token/", views.obtain_auth_token), ] + api_router.urls,