Example #1
0
class BesluitInformatieObjectSerializer(serializers.HyperlinkedModelSerializer):
    informatieobject = EnkelvoudigInformatieObjectField(
        validators=[
            LooseFkIsImmutableValidator(instance_path="canonical"),
            LooseFkResourceValidator(
                "EnkelvoudigInformatieObject", settings.DRC_API_SPEC
            ),
        ],
        max_length=1000,
        min_length=1,
        help_text=get_help_text(
            "besluiten.BesluitInformatieObject", "informatieobject"
        ),
    )

    class Meta:
        model = BesluitInformatieObject
        fields = ("url", "informatieobject", "besluit")
        validators = [
            UniqueTogetherValidator(
                queryset=BesluitInformatieObject.objects.all(),
                fields=["besluit", "informatieobject"],
            ),
            ObjecttypeInformatieobjecttypeRelationValidator("besluit", "besluittype"),
        ]
        extra_kwargs = {
            "url": {"lookup_field": "uuid"},
            "besluit": {"lookup_field": "uuid", "validators": [IsImmutableValidator()]},
        }

    def create(self, validated_data):
        with transaction.atomic():
            bio = super().create(validated_data)

        # local FK or CMIS - nothing to do -> our signals create the OIO
        if bio.informatieobject.pk or settings.CMIS_ENABLED:
            return bio

        # we know that we got valid URLs in the initial data
        io_url = self.initial_data["informatieobject"]
        besluit_url = self.initial_data["besluit"]

        # manual transaction management - documents API checks that the BIO
        # exists, so that transaction must be committed.
        # If it fails in any other way, we need to handle that by rolling back
        # the BIO creation.
        try:
            response = create_remote_oio(io_url, besluit_url, "besluit")
        except Exception as exception:
            bio.delete()
            raise serializers.ValidationError(
                {
                    "informatieobject": _(
                        "Could not create remote relation: {exception}"
                    ).format(exception=exception)
                },
                code="pending-relations",
            )
        else:
            bio._objectinformatieobject_url = response["url"]
            bio.save()
        return bio

    def run_validators(self, value):
        """
        Add read_only fields with defaults to value before running validators.
        """
        # In the case CMIS is enabled, we need to filter on the URL and not the canonical object
        if value.get("informatieobject") is not None and settings.CMIS_ENABLED:
            value["informatieobject"] = self.initial_data.get("informatieobject")

        return super().run_validators(value)
Example #2
0
class BesluitInformatieObjectSerializer(serializers.HyperlinkedModelSerializer
                                        ):
    informatieobject = EnkelvoudigInformatieObjectField(
        validators=[
            LooseFkIsImmutableValidator(instance_path="canonical"),
            LooseFkResourceValidator("EnkelvoudigInformatieObject",
                                     settings.DRC_API_SPEC),
        ],
        max_length=1000,
        min_length=1,
        help_text=get_help_text("besluiten.BesluitInformatieObject",
                                "informatieobject"),
    )

    class Meta:
        model = BesluitInformatieObject
        fields = ("url", "informatieobject", "besluit")
        validators = [
            UniqueTogetherValidator(
                queryset=BesluitInformatieObject.objects.all(),
                fields=["besluit", "informatieobject"],
            ),
            ObjecttypeInformatieobjecttypeRelationValidator(
                "besluit", "besluittype"),
        ]
        extra_kwargs = {
            "url": {
                "lookup_field": "uuid"
            },
            "besluit": {
                "lookup_field": "uuid",
                "validators": [IsImmutableValidator()]
            },
        }

    def create(self, validated_data):
        with transaction.atomic():
            bio = super().create(validated_data)

        # local FK - nothing to do -> our signals create the OIO
        if bio.informatieobject.pk:
            return bio

        # we know that we got valid URLs in the initial data
        io_url = self.initial_data["informatieobject"]
        besluit_url = self.initial_data["besluit"]

        # manual transaction management - documents API checks that the BIO
        # exists, so that transaction must be committed.
        # If it fails in any other way, we need to handle that by rolling back
        # the BIO creation.
        try:
            response = create_remote_oio(io_url, besluit_url, "besluit")
        except Exception as exception:
            bio.delete()
            raise serializers.ValidationError(
                _("Could not create remote relation: {exception}"),
                params={"exception": exception},
            )
        else:
            bio._objectinformatieobject_url = response["url"]
            bio.save()
        return bio
Example #3
0
class ZaakInformatieObjectSerializer(serializers.HyperlinkedModelSerializer):
    aard_relatie_weergave = serializers.ChoiceField(
        source="get_aard_relatie_display",
        read_only=True,
        choices=[(force_text(value), key) for key, value in RelatieAarden.choices],
    )
    informatieobject = EnkelvoudigInformatieObjectField(
        validators=[
            LooseFkIsImmutableValidator(instance_path="canonical"),
            LooseFkResourceValidator(
                "EnkelvoudigInformatieObject", settings.DRC_API_SPEC
            ),
        ],
        max_length=1000,
        min_length=1,
        help_text=get_help_text("zaken.ZaakInformatieObject", "informatieobject"),
    )

    class Meta:
        model = ZaakInformatieObject
        fields = (
            "url",
            "uuid",
            "informatieobject",
            "zaak",
            "aard_relatie_weergave",
            "titel",
            "beschrijving",
            "registratiedatum",
        )
        validators = [
            UniqueTogetherValidator(
                queryset=ZaakInformatieObject.objects.all(),
                fields=["zaak", "informatieobject"],
            ),
            ObjecttypeInformatieobjecttypeRelationValidator(),
        ]
        extra_kwargs = {
            "url": {"lookup_field": "uuid"},
            "uuid": {"read_only": True},
            "zaak": {"lookup_field": "uuid", "validators": [IsImmutableValidator()]},
        }

    def create(self, validated_data):
        with transaction.atomic():
            zio = super().create(validated_data)

        # local FK - nothing to do -> our signals create the OIO
        if zio.informatieobject.pk:
            return zio

        # we know that we got valid URLs in the initial data
        io_url = self.initial_data["informatieobject"]
        zaak_url = self.initial_data["zaak"]

        # manual transaction management - documents API checks that the ZIO
        # exists, so that transaction must be committed.
        # If it fails in any other way, we need to handle that by rolling back
        # the ZIO creation.
        try:
            response = create_remote_oio(io_url, zaak_url)
        except Exception as exception:
            zio.delete()
            raise serializers.ValidationError(
                {
                    "informatieobject": _(
                        "Could not create remote relation: {exception}"
                    ).format(exception)
                },
                code="pending-relations",
            )
        else:
            zio._objectinformatieobject_url = response["url"]
            zio.save()

        return zio
Example #4
0
class ZaakInformatieObjectSerializer(serializers.HyperlinkedModelSerializer):
    aard_relatie_weergave = serializers.ChoiceField(
        source="get_aard_relatie_display",
        read_only=True,
        choices=[(force_text(value), key)
                 for key, value in RelatieAarden.choices],
    )
    informatieobject = EnkelvoudigInformatieObjectField(
        validators=[
            LooseFkIsImmutableValidator(instance_path="canonical"),
            LooseFkResourceValidator("EnkelvoudigInformatieObject",
                                     settings.DRC_API_SPEC),
        ],
        max_length=1000,
        min_length=1,
        help_text=get_help_text("zaken.ZaakInformatieObject",
                                "informatieobject"),
    )

    class Meta:
        model = ZaakInformatieObject
        fields = (
            "url",
            "uuid",
            "informatieobject",
            "zaak",
            "aard_relatie_weergave",
            "titel",
            "beschrijving",
            "registratiedatum",
        )
        validators = [
            UniqueTogetherValidator(
                queryset=ZaakInformatieObject.objects.all(),
                fields=["zaak", "informatieobject"],
            ),
            ObjecttypeInformatieobjecttypeRelationValidator(),
        ]
        extra_kwargs = {
            "url": {
                "lookup_field": "uuid"
            },
            "uuid": {
                "read_only": True
            },
            "zaak": {
                "lookup_field": "uuid",
                "validators": [IsImmutableValidator()]
            },
        }

    def create(self, validated_data):
        with transaction.atomic():
            zio = super().create(validated_data)

        # we now expect to be in autocommit mode, i.e. - the transaction before has been
        # committed to the database as well. This makes it so that the remote DRC can
        # actually retrieve this ZIO we just created, to validate that we did indeed
        # create the relation information on our end.
        # XXX: would be nice to change the standard to rely on notifications for this
        # sync-machinery

        # Can't actually make the assertion because tests run in atomic transactions
        # assert (
        #     transaction.get_autocommit()
        # ), "Expected to be in autocommit mode at this point"

        # local FK or CMIS - nothing to do -> our signals create the OIO
        if zio.informatieobject.pk or settings.CMIS_ENABLED:
            return zio

        # we know that we got valid URLs in the initial data
        io_url = self.initial_data["informatieobject"]
        zaak_url = self.initial_data["zaak"]

        # manual transaction management - documents API checks that the ZIO
        # exists, so that transaction must be committed.
        # If it fails in any other way, we need to handle that by rolling back
        # the ZIO creation.
        try:
            response = create_remote_oio(io_url, zaak_url)
        except Exception as exception:
            zio.delete()
            raise serializers.ValidationError(
                {
                    "informatieobject":
                    _("Could not create remote relation: {exception}").format(
                        exception=exception)
                },
                code="pending-relations",
            )
        else:
            zio._objectinformatieobject_url = response["url"]
            zio.save()

        return zio

    def run_validators(self, value):
        """
        Add read_only fields with defaults to value before running validators.
        """
        # In the case CMIS is enabled, we need to filter on the URL and not the canonical object
        if value.get("informatieobject") is not None and settings.CMIS_ENABLED:
            value["informatieobject"] = self.initial_data.get(
                "informatieobject")

        return super().run_validators(value)