Skip to content

Commit

Permalink
✨ created put/patch action for document api endpoint to update metada…
Browse files Browse the repository at this point in the history
…ta fields, created specific serializer for the update and dynamically fill in the read_only_fields excepted the fields we want to edit.
  • Loading branch information
bart-maykin authored and sergei-maertens committed Nov 18, 2024
1 parent dc0acfb commit ece2bbd
Show file tree
Hide file tree
Showing 3 changed files with 329 additions and 0 deletions.
280 changes: 280 additions & 0 deletions src/woo_publications/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,112 @@ paths:
schema:
$ref: '#/components/schemas/Document'
description: ''
put:
operationId: documentenUpdate
description: Update the metadata of a specific document.
summary: Update the metadata of a specific document.
parameters:
- in: header
name: Audit-Remarks
schema:
type: string
description: |2
Any additional information describing the action performed by the user.
required: true
- in: header
name: Audit-User-ID
schema:
type: string
description: |2
The system identifier that uniquely identifies the user performing the action.
Ideally, this is obtained from some Identity and Access Management infrastructure.
With OpenID Connect, this would typically be the `sub` claim.
required: true
- in: header
name: Audit-User-Representation
schema:
type: string
description: |2
The display name of the user performing the action, to make them recognizable.
required: true
- in: path
name: uuid
schema:
type: string
format: uuid
required: true
tags:
- Documenten
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/DocumentUpdate'
security:
- tokenAuth: []
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/DocumentUpdate'
description: ''
patch:
operationId: documentenPartialUpdate
description: Update the metadata of a specific document partially.
summary: Update the metadata of a specific document partially.
parameters:
- in: header
name: Audit-Remarks
schema:
type: string
description: |2
Any additional information describing the action performed by the user.
required: true
- in: header
name: Audit-User-ID
schema:
type: string
description: |2
The system identifier that uniquely identifies the user performing the action.
Ideally, this is obtained from some Identity and Access Management infrastructure.
With OpenID Connect, this would typically be the `sub` claim.
required: true
- in: header
name: Audit-User-Representation
schema:
type: string
description: |2
The display name of the user performing the action, to make them recognizable.
required: true
- in: path
name: uuid
schema:
type: string
format: uuid
required: true
tags:
- Documenten
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/PatchedDocumentUpdate'
security:
- tokenAuth: []
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/DocumentUpdate'
description: ''
/api/v1/documenten/{uuid}/bestandsdelen/{partUuid}:
put:
operationId: documentenBestandsdelenUpdate
Expand Down Expand Up @@ -1450,6 +1556,99 @@ components:
Documents API.
required:
- documentUploadVoltooid
DocumentUpdate:
type: object
properties:
uuid:
type: string
format: uuid
readOnly: true
identifier:
type: string
readOnly: true
title: Identificatie
description: De (primaire) unieke identificatie.
publicatie:
type: string
format: uuid
readOnly: true
description: De unieke identificatie van de publicatie.
officieleTitel:
type: string
title: Officiële titel
maxLength: 255
verkorteTitel:
type: string
maxLength: 255
omschrijving:
type: string
publicatiestatus:
allOf:
- $ref: '#/components/schemas/PublicatiestatusEnum'
title: Status
creatiedatum:
type: string
format: date
readOnly: true
description: De datum waarop het document ontstaan is. Niet te verwarren
met de registratiedatum - de creatiedatum valt typisch *voor* de registratiedatum.
bestandsformaat:
type: string
readOnly: true
description: TODO - placeholder accepting anything, in the future this will
be validated against a reference value list.
bestandsnaam:
type: string
readOnly: true
description: Bestandsnaam 'op de harde schijf' van het document, bijvoorbeeld
'gelakt-verslag.pdf'.
bestandsomvang:
type: integer
readOnly: true
description: Bestandsgrootte op de harde schijf, in bytes.
registratiedatum:
type: string
format: date-time
readOnly: true
title: Geregistreerd op
description: System timestamp reflecting when the document was registered
in the database. Not to be confused with the creation date of the document,
which is usually *before* the registration date.
laatstGewijzigdDatum:
type: string
format: date-time
readOnly: true
title: Laatst gewijzigd op
description: System timestamp reflecting when the document was last modified
in the database.
bestandsdelen:
type: array
items:
$ref: '#/components/schemas/FilePart'
readOnly: true
nullable: true
title: file parts
description: The expected file parts/chunks to upload the file contents.
These are derived from the specified total file size (`bestandsomvang`)
in the document create body.
documenthandelingen:
allOf:
- $ref: '#/components/schemas/DocumentAction'
readOnly: true
description: The document actions of this document, currently only one action
will be used per document.
required:
- bestandsdelen
- bestandsformaat
- bestandsnaam
- bestandsomvang
- creatiedatum
- documenthandelingen
- identifier
- laatstGewijzigdDatum
- publicatie
- registratiedatum
- uuid
Eigenaar:
type: object
properties:
Expand Down Expand Up @@ -1716,6 +1915,87 @@ components:
type: array
items:
$ref: '#/components/schemas/Theme'
PatchedDocumentUpdate:
type: object
properties:
uuid:
type: string
format: uuid
readOnly: true
identifier:
type: string
readOnly: true
title: Identificatie
description: De (primaire) unieke identificatie.
publicatie:
type: string
format: uuid
readOnly: true
description: De unieke identificatie van de publicatie.
officieleTitel:
type: string
title: Officiële titel
maxLength: 255
verkorteTitel:
type: string
maxLength: 255
omschrijving:
type: string
publicatiestatus:
allOf:
- $ref: '#/components/schemas/PublicatiestatusEnum'
title: Status
creatiedatum:
type: string
format: date
readOnly: true
description: De datum waarop het document ontstaan is. Niet te verwarren
met de registratiedatum - de creatiedatum valt typisch *voor* de registratiedatum.
bestandsformaat:
type: string
readOnly: true
description: TODO - placeholder accepting anything, in the future this will
be validated against a reference value list.
bestandsnaam:
type: string
readOnly: true
description: Bestandsnaam 'op de harde schijf' van het document, bijvoorbeeld
'gelakt-verslag.pdf'.
bestandsomvang:
type: integer
readOnly: true
description: Bestandsgrootte op de harde schijf, in bytes.
registratiedatum:
type: string
format: date-time
readOnly: true
title: Geregistreerd op
description: System timestamp reflecting when the document was registered
in the database. Not to be confused with the creation date of the document,
which is usually *before* the registration date.
laatstGewijzigdDatum:
type: string
format: date-time
readOnly: true
title: Laatst gewijzigd op
description: System timestamp reflecting when the document was last modified
in the database.
bestandsdelen:
type: array
items:
$ref: '#/components/schemas/FilePart'
readOnly: true
nullable: true
title: file parts
description: The expected file parts/chunks to upload the file contents.
These are derived from the specified total file size (`bestandsomvang`)
in the document create body.
documenthandelingen:
allOf:
- $ref: '#/components/schemas/DocumentAction'
readOnly: true
description: The document actions of this document, currently only one action
will be used per document.
PatchedOrganisation:
type: object
properties:
Expand Down
33 changes: 33 additions & 0 deletions src/woo_publications/publications/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,39 @@ def validate_publicatiestatus(
return value


class DocumentUpdateSerializer(DocumentSerializer):
publicatie = serializers.SlugRelatedField(
slug_field="uuid",
help_text=_("The unique identifier of the publication."),
read_only=True,
)
documenthandelingen = DocumentActionSerializer(
help_text=_(
"The document actions of this document, currently only one action will be used per document."
),
read_only=True,
)

class Meta(DocumentSerializer.Meta):
fields = DocumentSerializer.Meta.fields
read_only_fields = [
field
for field in DocumentSerializer.Meta.fields
if field
not in (
"officiele_titel",
"verkorte_titel",
"omschrijving",
"publicatiestatus",
)
]
extra_kwargs = {
"officiele_titel": {
"required": False,
}
}


class EigenaarSerializer(serializers.Serializer):
weergave_naam = serializers.CharField(
source="display_name",
Expand Down
16 changes: 16 additions & 0 deletions src/woo_publications/publications/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from .serializers import (
DocumentSerializer,
DocumentStatusSerializer,
DocumentUpdateSerializer,
FilePartSerializer,
PublicationSerializer,
)
Expand All @@ -54,6 +55,14 @@
summary=_("Retrieve a specific document."),
description=_("Retrieve a specific document."),
),
update=extend_schema(
summary=_("Update the metadata of a specific document."),
description=_("Update the metadata of a specific document."),
),
partial_update=extend_schema(
summary=_("Update the metadata of a specific document partially."),
description=_("Update the metadata of a specific document partially."),
),
create=extend_schema(
summary=_("Register a document's metadata."),
description=_(
Expand All @@ -75,6 +84,7 @@
class DocumentViewSet(
AuditTrailViewSetMixin,
mixins.CreateModelMixin,
mixins.UpdateModelMixin,
viewsets.ReadOnlyModelViewSet,
):
queryset = Document.objects.order_by("-creatiedatum")
Expand All @@ -97,6 +107,12 @@ def perform_create(self, serializer):
build_absolute_uri=self.request.build_absolute_uri,
)

def get_serializer_class(self):
action = getattr(self, "action", None)
if action in ["update", "partial_update"]:
return DocumentUpdateSerializer
return super().get_serializer_class()

@extend_schema(
summary=_("Upload file part"),
description=_(
Expand Down

0 comments on commit ece2bbd

Please sign in to comment.