Skip to content

Commit

Permalink
✨ [#38] added uuid field to documents model, changed the api lookup f…
Browse files Browse the repository at this point in the history
…ield from identifier to uuid and added filter to keep the functionality of lookup documents based on identifier
  • Loading branch information
bart-maykin committed Oct 25, 2024
1 parent 6996f3e commit fa9a101
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/woo_publications/api/filters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django import forms

from django_filters.rest_framework import Filter
from django_filters.rest_framework import filters


class URLFilter(Filter):
class URLFilter(filters.Filter):
field_class = forms.URLField
25 changes: 17 additions & 8 deletions src/woo_publications/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ paths:
The display name of the user performing the action, to make them recognizable.
required: true
- in: query
name: identifier
schema:
type: string
description: Search the document based on the identifier field.
- name: page
required: false
in: query
Expand All @@ -51,10 +56,10 @@ paths:
- in: query
name: publicatie
schema:
type: string
format: uuid
description: Search the document based on the unique identifier that represents
a publication.
type: integer
description: 'Search the document based on the unique identifier (UUID) that
represents a publication. <sup><sub>Disclaimer: disregard the documented
type `integer` the correct type is `UUID`.</sub></sup>'
- in: query
name: sorteer
schema:
Expand Down Expand Up @@ -88,7 +93,7 @@ paths:
schema:
$ref: '#/components/schemas/PaginatedDocumentList'
description: ''
/api/v1/documenten/{identifier}:
/api/v1/documenten/{uuid}:
get:
operationId: documentenRetrieve
description: Retrieve a specific document.
Expand Down Expand Up @@ -121,11 +126,10 @@ paths:
The display name of the user performing the action, to make them recognizable.
required: true
- in: path
name: identifier
name: uuid
schema:
type: string
title: Identificatie
description: De (primaire) unieke identificatie.
format: uuid
required: true
tags:
- Documenten
Expand Down Expand Up @@ -748,6 +752,10 @@ components:
Document:
type: object
properties:
uuid:
type: string
format: uuid
readOnly: true
identifier:
type: string
title: Identificatie
Expand Down Expand Up @@ -800,6 +808,7 @@ components:
- officieleTitel
- publicatie
- registratiedatum
- uuid
InformationCategory:
type: object
properties:
Expand Down
14 changes: 10 additions & 4 deletions src/woo_publications/publications/api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@


class DocumentFilterSet(FilterSet):
publicatie = filters.UUIDFilter(
field_name="publicatie__uuid",
lookup_expr="exact",
# TODO: change this filter to custom named filter with `@extend_schema_field(UUID)` once bug is fixed in drf-spectacular
publicatie = filters.ModelChoiceFilter(
queryset=Publication.objects.all(),
to_field_name="uuid",
help_text=_(
"Search the document based on the unique identifier that represents a publication."
"Search the document based on the unique identifier (UUID) that represents a publication. "
"<sup><sub>Disclaimer: disregard the documented type `integer` the correct type is `UUID`.</sub></sup>"
),
)
identifier = filters.CharFilter(
help_text="Search the document based on the identifier field.",
)
sorteer = filters.OrderingFilter(
help_text=_("Order on."),
fields=(
Expand All @@ -26,6 +31,7 @@ class Meta:
model = Document
fields = (
"publicatie",
"identifier",
"sorteer",
)

Expand Down
6 changes: 4 additions & 2 deletions src/woo_publications/publications/api/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@


class DocumentSerializer(serializers.ModelSerializer):
publicatie = serializers.UUIDField(
source="publicatie.uuid",
publicatie = serializers.SlugRelatedField(
queryset=Publication.objects.all(),
slug_field="uuid",
help_text=_("The unique identifier of the publication."),
)

class Meta: # type: ignore
model = Document
fields = (
"uuid",
"identifier",
"publicatie",
"officiele_titel",
Expand Down
2 changes: 1 addition & 1 deletion src/woo_publications/publications/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DocumentViewSet(AuditTrailViewSetMixin, viewsets.ReadOnlyModelViewSet):
queryset = Document.objects.order_by("-creatiedatum")
serializer_class = DocumentSerializer
filterset_class = DocumentFilterSet
lookup_field = "identifier"
lookup_field = "uuid"


@extend_schema(tags=["Publicaties"])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 4.2.16 on 2024-10-25 07:38

from django.db import migrations, models
import uuid


class Migration(migrations.Migration):

dependencies = [
(
"publications",
"0003_document_document_service_document_document_uuid_and_more",
),
]

operations = [
migrations.AddField(
model_name="document",
name="uuid",
field=models.UUIDField(
default=uuid.uuid4, editable=False, unique=True, verbose_name="UUID"
),
),
migrations.AlterField(
model_name="document",
name="identifier",
field=models.CharField(
help_text="The (primary) unique identifier.",
max_length=255,
verbose_name="identifier",
),
),
]
7 changes: 6 additions & 1 deletion src/woo_publications/publications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def __str__(self):


class Document(models.Model):
uuid = models.UUIDField(
_("UUID"),
unique=True,
default=uuid.uuid4,
editable=False,
)
publicatie = models.ForeignKey(
Publication,
verbose_name=_("publication"),
Expand Down Expand Up @@ -79,7 +85,6 @@ class Document(models.Model):
_("identifier"),
help_text=_("The (primary) unique identifier."),
max_length=255,
unique=True,
)
officiele_titel = models.CharField(
_("official title"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def test_detail_logging(self):
)
detail_url = reverse(
"api:document-detail",
kwargs={"identifier": str(document.identifier)},
kwargs={"uuid": str(document.uuid)},
)

response = self.client.get(detail_url, headers=AUDIT_HEADERS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_403_when_audit_headers_are_missing(self):
self.client.force_authenticate(user=user)
list_endpoint = reverse("api:document-list")
detail_endpoint = reverse(
"api:document-detail", kwargs={"identifier": str(uuid4())}
"api:document-detail", kwargs={"uuid": str(uuid4())}
)

with self.subTest(action="list"):
Expand All @@ -39,7 +39,7 @@ def test_403_when_audit_headers_are_missing(self):
def test_list_documents(self):
publication, publication2 = PublicationFactory.create_batch(2)
with freeze_time("2024-09-25T12:30:00-00:00"):
DocumentFactory.create(
document = DocumentFactory.create(
publicatie=publication,
identifier="document-1",
officiele_titel="title one",
Expand All @@ -48,7 +48,7 @@ def test_list_documents(self):
creatiedatum="2024-01-01",
)
with freeze_time("2024-09-24T12:00:00-00:00"):
DocumentFactory.create(
document2 = DocumentFactory.create(
publicatie=publication2,
identifier="document-2",
officiele_titel="title two",
Expand All @@ -65,6 +65,7 @@ def test_list_documents(self):

with self.subTest("first_item_in_response_with_expected_data"):
expected_second_item_data = {
"uuid": str(document2.uuid),
"identifier": "document-2",
"publicatie": str(publication2.uuid),
"officieleTitel": "title two",
Expand All @@ -81,6 +82,7 @@ def test_list_documents(self):

with self.subTest("second_item_in_response_with_expected_data"):
expected_first_item_data = {
"uuid": str(document.uuid),
"identifier": "document-1",
"publicatie": str(publication.uuid),
"officieleTitel": "title one",
Expand All @@ -98,7 +100,7 @@ def test_list_documents(self):
def test_list_documents_filter_order(self):
publication, publication2 = PublicationFactory.create_batch(2)
with freeze_time("2024-09-25T12:30:00-00:00"):
DocumentFactory.create(
document = DocumentFactory.create(
publicatie=publication,
identifier="document-1",
officiele_titel="title one",
Expand All @@ -107,7 +109,7 @@ def test_list_documents_filter_order(self):
creatiedatum="2024-01-01",
)
with freeze_time("2024-09-24T12:00:00-00:00"):
DocumentFactory.create(
document2 = DocumentFactory.create(
publicatie=publication2,
identifier="document-2",
officiele_titel="title two",
Expand All @@ -117,6 +119,7 @@ def test_list_documents_filter_order(self):
)

expected_first_item_data = {
"uuid": str(document.uuid),
"identifier": "document-1",
"publicatie": str(publication.uuid),
"officieleTitel": "title one",
Expand All @@ -129,6 +132,7 @@ def test_list_documents_filter_order(self):
"registratiedatum": "2024-09-25T14:30:00+02:00",
}
expected_second_item_data = {
"uuid": str(document2.uuid),
"identifier": "document-2",
"publicatie": str(publication2.uuid),
"officieleTitel": "title two",
Expand Down Expand Up @@ -231,7 +235,7 @@ def test_list_documents_filter_order(self):
def test_list_document_publication_filter(self):
publication, publication2 = PublicationFactory.create_batch(2)
with freeze_time("2024-09-25T12:30:00-00:00"):
DocumentFactory.create(
document = DocumentFactory.create(
publicatie=publication,
identifier="document-1",
officiele_titel="title one",
Expand All @@ -250,6 +254,7 @@ def test_list_document_publication_filter(self):
)

expected_first_item_data = {
"uuid": str(document.uuid),
"identifier": "document-1",
"publicatie": str(publication.uuid),
"officieleTitel": "title one",
Expand All @@ -275,10 +280,59 @@ def test_list_document_publication_filter(self):
self.assertEqual(data["count"], 1)
self.assertEqual(data["results"][0], expected_first_item_data)

def test_list_document_identifier_filter(self):
publication, publication2 = PublicationFactory.create_batch(2)
with freeze_time("2024-09-25T12:30:00-00:00"):
document = DocumentFactory.create(
publicatie=publication,
identifier="document-1",
officiele_titel="title one",
verkorte_titel="one",
omschrijving="Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
creatiedatum="2024-01-01",
)
with freeze_time("2024-09-24T12:00:00-00:00"):
DocumentFactory.create(
publicatie=publication2,
identifier="document-2",
officiele_titel="title two",
verkorte_titel="two",
omschrijving="Vestibulum eros nulla, tincidunt sed est non, facilisis mollis urna.",
creatiedatum="2024-02-02",
)

expected_first_item_data = {
"uuid": str(document.uuid),
"identifier": "document-1",
"publicatie": str(publication.uuid),
"officieleTitel": "title one",
"verkorteTitel": "one",
"omschrijving": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"creatiedatum": "2024-01-01",
"bestandsformaat": "unknown",
"bestandsnaam": "unknown.bin",
"bestandsomvang": 0,
"registratiedatum": "2024-09-25T14:30:00+02:00",
}

response = self.client.get(
reverse("api:document-list"),
{"identifier": "document-1"},
headers=AUDIT_HEADERS,
)

self.assertEqual(response.status_code, status.HTTP_200_OK)

data = response.json()

self.assertEqual(data["count"], 1)
self.assertEqual(data["results"][0], expected_first_item_data)


def test_detail_document(self):
publication = PublicationFactory.create()
with freeze_time("2024-09-25T12:30:00-00:00"):
DocumentFactory.create(
document = DocumentFactory.create(
publicatie=publication,
identifier="document-1",
officiele_titel="title one",
Expand All @@ -288,7 +342,7 @@ def test_detail_document(self):
)
detail_url = reverse(
"api:document-detail",
kwargs={"identifier": "document-1"},
kwargs={"uuid": str(document.uuid)},
)

response = self.client.get(detail_url, headers=AUDIT_HEADERS)
Expand All @@ -297,6 +351,7 @@ def test_detail_document(self):

data = response.json()
expected_data = {
"uuid": str(document.uuid),
"identifier": "document-1",
"publicatie": str(publication.uuid),
"officieleTitel": "title one",
Expand Down

0 comments on commit fa9a101

Please sign in to comment.