-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from open-zaak/feature/external-documents-api
Add support for external documents API
- Loading branch information
Showing
22 changed files
with
473 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from django_loose_fk.drf import FKOrURLField | ||
from rest_framework.fields import empty | ||
from rest_framework.reverse import reverse | ||
|
||
from ..models import EnkelvoudigInformatieObjectCanonical | ||
|
||
|
||
class EnkelvoudigInformatieObjectField(FKOrURLField): | ||
""" | ||
Custom field to construct the url for models that have a ForeignKey to | ||
`EnkelvoudigInformatieObject` | ||
Needed because the canonical `EnkelvoudigInformatieObjectCanonical` no longer stores | ||
the uuid, but the `EnkelvoudigInformatieObject`s related to it do | ||
store the uuid | ||
""" | ||
|
||
def to_representation(self, value): | ||
if not isinstance(value, EnkelvoudigInformatieObjectCanonical): | ||
return super().to_representation(value) | ||
|
||
value = value.latest_version | ||
return reverse( | ||
"enkelvoudiginformatieobject-detail", | ||
kwargs={"uuid": value.uuid}, | ||
request=self.context.get("request"), | ||
) | ||
|
||
def run_validation(self, data=empty): | ||
value = super().run_validation(data=data) | ||
if value.pk: | ||
return value.canonical | ||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,63 @@ | ||
import uuid | ||
from datetime import date | ||
from typing import Dict, Union | ||
|
||
from django.conf import settings | ||
from django.utils import timezone | ||
|
||
from vng_api_common.tests import get_operation_url as _get_operation_url | ||
|
||
from openzaak.components.zaken.api.serializers.utils import _get_oio_endpoint | ||
|
||
JsonValue = Union[str, None, int, float] | ||
|
||
|
||
def get_operation_url(operation, **kwargs): | ||
return _get_operation_url( | ||
operation, spec_path=settings.SPEC_URL["documenten"], **kwargs | ||
) | ||
|
||
|
||
def get_eio_response(url: str, **overrides) -> Dict[str, JsonValue]: | ||
eio_type = ( | ||
f"https://external.catalogus.nl/api/v1/informatieobjecttypen/{uuid.uuid4()}" | ||
) | ||
eio = { | ||
"url": url, | ||
"identificatie": "DOCUMENT-00001", | ||
"bronorganisatie": "272618196", | ||
"creatiedatum": date.today().isoformat(), | ||
"titel": "some titel", | ||
"auteur": "some auteur", | ||
"status": "", | ||
"formaat": "some formaat", | ||
"taal": "nld", | ||
"beginRegistratie": timezone.now().isoformat().replace("+00:00", "Z"), | ||
"versie": 1, | ||
"bestandsnaam": "", | ||
"inhoud": f"{url}/download?versie=1", | ||
"bestandsomvang": 100, | ||
"link": "", | ||
"beschrijving": "", | ||
"ontvangstdatum": None, | ||
"verzenddatum": None, | ||
"ondertekening": {"soort": "", "datum": None}, | ||
"indicatieGebruiksrecht": None, | ||
"vertrouwelijkheidaanduiding": "openbaar", | ||
"integriteit": {"algoritme": "", "waarde": "", "datum": None}, | ||
"informatieobjecttype": eio_type, | ||
"locked": False, | ||
} | ||
eio.update(**overrides) | ||
return eio | ||
|
||
|
||
def get_oio_response(io_url: str, object_url: str) -> Dict[str, JsonValue]: | ||
url = f"{_get_oio_endpoint(io_url)}/{uuid.uuid4()}" | ||
oio = { | ||
"url": url, | ||
"informatieobject": io_url, | ||
"object": object_url, | ||
"objectType": "zaak", | ||
} | ||
return oio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import logging | ||
|
||
import requests | ||
from vng_api_common.models import APICredential | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def _get_oio_endpoint(io_url: str) -> str: | ||
""" | ||
Build the OIO endpoint from the EIO URL. | ||
.. todo: TODO: clean this mess up - ideally this would use | ||
gemma_zds_client.Client.from_url() & fetch the URL from the associated | ||
API spec, but that requires mocking out the api spec fetch + setting up | ||
the schema in the mock. A refactor in gemma-zds-client for this is | ||
suitable. | ||
""" | ||
start = io_url.split("enkelvoudiginformatieobjecten")[0] | ||
url = f"{start}objectinformatieobjecten" | ||
return url | ||
|
||
|
||
def create_remote_oio(io_url: str, zaak_url: str) -> dict: | ||
client_auth = APICredential.get_auth(io_url) | ||
if client_auth is None: | ||
logger.warning("Missing credentials for %s", io_url) | ||
|
||
url = _get_oio_endpoint(io_url) | ||
headers = client_auth.credentials() if client_auth else {} | ||
body = {"informatieobject": io_url, "object": zaak_url, "objectType": "zaak"} | ||
|
||
response = requests.post(url, json=body, headers=headers) | ||
response.raise_for_status() | ||
return response.json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from urllib.parse import urlparse | ||
|
||
from django.db.models.base import ModelBase | ||
|
||
from vng_api_common.utils import get_resource_for_path | ||
|
||
from openzaak.components.catalogi.models import InformatieObjectType | ||
from openzaak.loaders import AuthorizedRequestsLoader | ||
|
||
|
||
class EIOLoader(AuthorizedRequestsLoader): | ||
""" | ||
Load the EIO directly instead of going through EIOCanonical. | ||
""" | ||
|
||
def load(self, url: str, model: ModelBase): | ||
from openzaak.components.documenten.models import ( | ||
EnkelvoudigInformatieObject, | ||
EnkelvoudigInformatieObjectCanonical, | ||
) | ||
|
||
if model is EnkelvoudigInformatieObjectCanonical: | ||
model = EnkelvoudigInformatieObject | ||
|
||
if model is InformatieObjectType: | ||
return self.resolve_io_type(url) | ||
|
||
return super().load(url, model=model) | ||
|
||
def resolve_io_type(self, url: str): | ||
try: | ||
return get_resource_for_path(urlparse(url).path) | ||
except InformatieObjectType.DoesNotExist: | ||
return super().load(url, model=InformatieObjectType) |
Oops, something went wrong.