diff --git a/src/dowc/api/serializers.py b/src/dowc/api/serializers.py index ab26b0c..a4a487a 100644 --- a/src/dowc/api/serializers.py +++ b/src/dowc/api/serializers.py @@ -22,6 +22,13 @@ class DocumentFileSerializer(serializers.ModelSerializer): "The URL that opens the MS Office WebDAV client on the local machine." ) ) + zaak = serializers.URLField( + help_text=_( + "URL-reference to ZAAK if the DocumentFile is associated with a ZAAK." + ), + required=False, + allow_blank=True, + ) class Meta: model = DocumentFile @@ -32,6 +39,7 @@ class Meta: "uuid", "info_url", "unversioned_url", + "zaak", ) extra_kwargs = { "drc_url": { @@ -206,6 +214,24 @@ class Meta: } +class StatusSerializer(serializers.Serializer): + zaak = serializers.URLField( + help_text=_( + "URL-reference to ZAAK if the DocumentFile is associated with a ZAAK." + ), + required=False, + allow_blank=True, + ) + documents = serializers.ListField( + child=serializers.CharField( + required=False, + help_text=_("URL-reference to document in DRC API."), + ), + required=False, + allow_empty=True, + ) + + class SupportedFileExtensionsSerializer(serializers.Serializer): extensions = serializers.ListField( child=serializers.CharField(required=True, help_text=_("File extension.")), diff --git a/src/dowc/api/tests/test_documentfile_api.py b/src/dowc/api/tests/test_documentfile_api.py index d5ae7b0..0912fe6 100644 --- a/src/dowc/api/tests/test_documentfile_api.py +++ b/src/dowc/api/tests/test_documentfile_api.py @@ -480,10 +480,12 @@ def test_retrieve_documentfiles_on_url(self, m): ) # Retrieve documentfiles with this data - data = [ - {"document": "http://some-unversioned-url.com/1"}, - {"document": "http://some-unversioned-url.com/2"}, - ] + data = { + "documents": [ + "http://some-unversioned-url.com/1", + "http://some-unversioned-url.com/2", + ] + } # Call get on list response = self.client.post(reverse_lazy("documentfile-status"), data=data) @@ -508,6 +510,67 @@ def test_retrieve_documentfiles_on_url(self, m): ], ) + def test_retrieve_documentfiles_on_zaak(self, m): + mock_service_oas_get(m, self.DRC_URL, "drc") + + # Two documentfiles purpose to write + df1 = DocumentFileFactory.create( + unversioned_url="http://some-unversioned-url.com/1", + purpose=DocFileTypes.write, + zaak="http://some-zaak.nl/", + ) + df2 = DocumentFileFactory.create( + unversioned_url="http://some-unversioned-url.com/2", + purpose=DocFileTypes.write, + zaak="http://some-other-zaak.nl/", + ) + + # Retrieve documentfiles with this data + data = {"zaak": "http://some-other-zaak.nl/"} + + # Call get on list + response = self.client.post(reverse_lazy("documentfile-status"), data=data) + + # Check response status + self.assertEqual(response.status_code, status.HTTP_200_OK) + + results = response.json() + + # Expecting 1 documentfiles + self.assertEqual( + results, + [ + { + "document": "http://some-unversioned-url.com/2", + "uuid": str(df2.uuid), + }, + ], + ) + + def test_retrieve_documentfiles_no_zaak_no_urls(self, m): + mock_service_oas_get(m, self.DRC_URL, "drc") + + # Two documentfiles purpose to write + df1 = DocumentFileFactory.create( + unversioned_url="http://some-unversioned-url.com/1", + purpose=DocFileTypes.write, + zaak="http://some-zaak.nl/", + ) + + # Retrieve documentfiles with this data + data = {} + + # Call get on list + response = self.client.post(reverse_lazy("documentfile-status"), data=data) + + # Check response status + self.assertEqual(response.status_code, status.HTTP_200_OK) + + results = response.json() + + # Expecting 1 documentfiles + self.assertEqual(results, []) + def test_retrieve_documentfiles_on_url_empty_array(self, m): mock_service_oas_get(m, self.DRC_URL, "drc") @@ -522,7 +585,7 @@ def test_retrieve_documentfiles_on_url_empty_array(self, m): ) # Retrieve documentfiles with this data - data = [] + data = dict() # Call get on list response = self.client.post(reverse_lazy("documentfile-status"), data=data) @@ -532,7 +595,7 @@ def test_retrieve_documentfiles_on_url_empty_array(self, m): results = response.json() - # Expecting 2 documentfiles + # Expecting 0 documentfiles self.assertEqual( results, [], @@ -552,10 +615,12 @@ def test_application_token(self, m): ) # Retrieve documentfiles with this data - data = [ - {"document": "http://some-unversioned-url.com/1"}, - {"document": "http://some-unversioned-url.com/2"}, - ] + data = { + "documents": [ + "http://some-unversioned-url.com/1", + "http://some-unversioned-url.com/2", + ] + } self.client.logout() token = ApplicationTokenFactory.create() diff --git a/src/dowc/api/viewsets.py b/src/dowc/api/viewsets.py index 25a1ae5..e3182c6 100644 --- a/src/dowc/api/viewsets.py +++ b/src/dowc/api/viewsets.py @@ -20,6 +20,7 @@ from .serializers import ( DocumentFileSerializer, DocumentStatusSerializer, + StatusSerializer, UnlockedDocumentSerializer, ) @@ -140,7 +141,7 @@ def perform_destroy(self, instance): @extend_schema( summary=_("Retrieve open documents."), - request=DocumentStatusSerializer, + request=StatusSerializer, responses={200: DocumentStatusSerializer}, ) @action( @@ -148,14 +149,18 @@ def perform_destroy(self, instance): detail=False, ) def status(self, request, *args, **kwargs): - serializer = DocumentStatusSerializer(data=request.data, many=True) + serializer = StatusSerializer(data=request.data) serializer.is_valid(raise_exception=True) - if not serializer.data: - return Response([]) - queryset = self.get_queryset().filter( - purpose=DocFileTypes.write, - unversioned_url__in=[url["document"] for url in serializer.data], - ) + filters = dict() + if docs := serializer.data.get("documents"): + filters["unversioned_url__in"] = docs + if zaak := serializer.data.get("zaak"): + filters["zaak"] = zaak + + if not filters: + return Response(list()) + + queryset = self.get_queryset().filter(purpose=DocFileTypes.write, **filters) serializer = DocumentStatusSerializer(queryset, many=True) return Response(serializer.data) diff --git a/src/dowc/core/migrations/0013_documentfile_zaak.py b/src/dowc/core/migrations/0013_documentfile_zaak.py new file mode 100644 index 0000000..d85c873 --- /dev/null +++ b/src/dowc/core/migrations/0013_documentfile_zaak.py @@ -0,0 +1,22 @@ +# Generated by Django 3.2.12 on 2023-12-20 20:59 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("core", "0012_alter_documentfile_purpose"), + ] + + operations = [ + migrations.AddField( + model_name="documentfile", + name="zaak", + field=models.URLField( + blank=True, + default="", + help_text="URL-reference to ZAAK if associated to one.", + ), + ), + ] diff --git a/src/dowc/core/models.py b/src/dowc/core/models.py index 22fd925..4b5e45b 100644 --- a/src/dowc/core/models.py +++ b/src/dowc/core/models.py @@ -195,6 +195,11 @@ class DocumentFile(models.Model): info_url = models.URLField( default="", help_text=_("Points to the origin of the document's usage.") ) + zaak = models.URLField( + help_text=_("URL-reference to ZAAK if associated to one."), + default="", + blank=True, + ) api_document: Optional[Document] = None