Skip to content

Commit

Permalink
[#328] get specific object version (#429)
Browse files Browse the repository at this point in the history
* [#328] add object-history-detail endpoint

* [#328] update oas spec

* [#328] update path parameter documentation

* [#328] apply formatting

---------

Co-authored-by: Sonny Bakker <[email protected]>
  • Loading branch information
SonnyBA and Sonny Bakker authored Sep 16, 2024
1 parent 590495f commit 4f0789d
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 1 deletion.
45 changes: 45 additions & 0 deletions src/objects/api/v2/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,51 @@ paths:
responses:
'204':
description: No response body
/objects/{uuid}/{index}:
get:
operationId: object_history_detail
description: Retrieve the specified OBJECT given an UUID and INDEX.
parameters:
- in: header
name: Accept-Crs
schema:
type: string
enum:
- EPSG:4326
description: 'The desired ''Coordinate Reference System'' (CRS) of the response
data. According to the GeoJSON spec, WGS84 is the default (EPSG: 4326 is
the same as WGS84).'
- in: path
name: index
schema:
type: number
required: true
- in: path
name: uuid
schema:
type: string
format: uuid
required: true
tags:
- objects
security:
- tokenAuth: []
responses:
'200':
headers:
Content-Crs:
schema:
type: string
enum:
- EPSG:4326
description: 'The ''Coordinate Reference System'' (CRS) of the request
data. According to the GeoJSON spec, WGS84 is the default (EPSG: 4326
is the same as WGS84).'
content:
application/json:
schema:
$ref: '#/components/schemas/HistoryRecord'
description: OK
/objects/{uuid}/history:
get:
operationId: object_history
Expand Down
35 changes: 34 additions & 1 deletion src/objects/api/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from django.conf import settings
from django.db import models

from drf_spectacular.utils import extend_schema, extend_schema_view
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import OpenApiParameter, extend_schema, extend_schema_view
from rest_framework import mixins, viewsets
from rest_framework.decorators import action
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response
from vng_api_common.filters import Backend as FilterBackend
from vng_api_common.search import SearchMixin
Expand Down Expand Up @@ -126,6 +128,37 @@ def history(self, request, uuid=None):
serializer = self.get_serializer(records, many=True)
return Response(serializer.data)

@extend_schema(
description="Retrieve the specified OBJECT given an UUID and INDEX.",
responses={"200": HistoryRecordSerializer()},
parameters=[
OpenApiParameter(
name="index",
location=OpenApiParameter.PATH,
required=True,
type=OpenApiTypes.NUMBER,
),
OpenApiParameter(
name="uuid",
location=OpenApiParameter.PATH,
required=True,
type=OpenApiTypes.UUID,
),
],
)
@action(
detail=True,
methods=["get"],
url_path=r"(?P<index>\d+)",
serializer_class=HistoryRecordSerializer,
)
def history_detail(self, request, uuid=None, index=None):
"""Retrieve a RECORD of an OBJECT."""
queryset = self.get_queryset()
record = get_object_or_404(queryset, object__uuid=uuid, index=index)
serializer = self.get_serializer(record)
return Response(serializer.data)

@extend_schema(
description="Perform a (geo) search on OBJECTs.",
request=ObjectSearchSerializer,
Expand Down
62 changes: 62 additions & 0 deletions src/objects/tests/v2/test_object_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,68 @@ def test_retrieve_object(self, m):
},
)

def test_retrieve_by_index(self, m):
record1 = ObjectRecordFactory.create(
object__object_type=self.object_type,
start_at=date(2020, 1, 1),
geometry="POINT (4.910649523925713 52.37240093589432)",
index=1,
)

object = record1.object

record2 = ObjectRecordFactory.create(
object=object, start_at=date.today(), correct=record1, index=2
)

with self.subTest(record=record1):
url = reverse("object-history-detail", args=[object.uuid, 1])

response = self.client.get(url)

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

data = response.json()

self.assertEqual(
data,
{
"index": 1,
"typeVersion": record1.version,
"data": record1.data,
"geometry": json.loads(record1.geometry.json),
"startAt": record1.start_at.isoformat(),
"endAt": record2.start_at.isoformat(),
"registrationAt": record1.registration_at.isoformat(),
"correctionFor": None,
"correctedBy": 2,
},
)

with self.subTest(record=record2):
url = reverse("object-history-detail", args=[object.uuid, 2])

response = self.client.get(url)

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

data = response.json()

self.assertEqual(
data,
{
"index": 2,
"typeVersion": record2.version,
"data": record2.data,
"geometry": json.loads(record2.geometry.json),
"startAt": record2.start_at.isoformat(),
"endAt": None,
"registrationAt": record2.registration_at.isoformat(),
"correctionFor": 1,
"correctedBy": None,
},
)

def test_create_object(self, m):
mock_service_oas_get(m, OBJECT_TYPES_API, "objecttypes")
m.get(
Expand Down

0 comments on commit 4f0789d

Please sign in to comment.