-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/writable api #20
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b2a48d9
:heavy_plus_sign: add drf-nested-routers
annashamray 2988c6f
:sparkles: https://github.com/maykinmedia/objects-api/issues/95 -- ad…
annashamray 3140157
:sparkles: make API endpoints writable
annashamray 643b083
:sparkle: generate version number for objecttypes
annashamray b7df151
:sparkles: retrieve object_type when creating version
annashamray 090ea59
:white_check_mark: Ref. https://github.com/maykinmedia/objects-api/is…
annashamray 167a00c
:sparkles: add versions/<version>/publish endpoint
annashamray 39b78c2
:white_check_mark: test publish endpoint
annashamray 50f3e8f
:sparkles: add validators to API since it's writable now
annashamray 48ba19b
:white_check_mark: test input validation
annashamray dc47659
:memo: update OAS with writable endpoints
annashamray bb0499f
:ok_hand: process PR feedback - remove /publish endpoint
annashamray df9184d
:ok_hand: process PR feedback
annashamray File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,38 @@ | ||
from django.core.exceptions import ValidationError | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from rest_framework import serializers | ||
|
||
from objecttypes.core.constants import ObjectVersionStatus | ||
from objecttypes.core.utils import check_json_schema | ||
|
||
|
||
class VersionUpdateValidator: | ||
message = _("Only draft versions can be changed") | ||
code = "non-draft-version-update" | ||
|
||
def set_context(self, serializer): | ||
""" | ||
This hook is called by the serializer instance, | ||
prior to the validation call being made. | ||
""" | ||
# Determine the existing instance, if this is an update operation. | ||
self.instance = getattr(serializer, "instance", None) | ||
self.request = serializer.context["request"] | ||
|
||
def __call__(self, attrs): | ||
if not self.instance: | ||
return | ||
|
||
if self.instance.status != ObjectVersionStatus.draft: | ||
raise serializers.ValidationError(self.message, code=self.code) | ||
|
||
|
||
class JsonSchemaValidator: | ||
code = "invalid-json-schema" | ||
|
||
def __call__(self, value): | ||
try: | ||
check_json_schema(value) | ||
except ValidationError as exc: | ||
raise serializers.ValidationError(exc.args[0], code=self.code) from exc |
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,13 +1,53 @@ | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from rest_framework import viewsets | ||
from rest_framework.exceptions import ValidationError | ||
from rest_framework.settings import api_settings | ||
from vng_api_common.viewsets import NestedViewSetMixin | ||
|
||
from objecttypes.core.models import ObjectType | ||
from objecttypes.core.constants import ObjectVersionStatus | ||
from objecttypes.core.models import ObjectType, ObjectVersion | ||
|
||
from .filters import ObjectTypeFilterSet | ||
from .serializers import ObjectTypeSerializer | ||
from .serializers import ObjectTypeSerializer, ObjectVersionSerializer | ||
|
||
|
||
class ObjectTypeViewSet(viewsets.ReadOnlyModelViewSet): | ||
class ObjectTypeViewSet(viewsets.ModelViewSet): | ||
queryset = ObjectType.objects.prefetch_related("versions").order_by("-pk") | ||
serializer_class = ObjectTypeSerializer | ||
lookup_field = "uuid" | ||
filterset_class = ObjectTypeFilterSet | ||
|
||
def perform_destroy(self, instance): | ||
if instance.versions.exists(): | ||
raise ValidationError( | ||
{ | ||
api_settings.NON_FIELD_ERRORS_KEY: [ | ||
_( | ||
"All related versions should be destroyed before destroying the objecttype" | ||
) | ||
] | ||
}, | ||
code="pending-versions", | ||
) | ||
|
||
super().perform_destroy(instance) | ||
|
||
|
||
class ObjectVersionViewSet(NestedViewSetMixin, viewsets.ModelViewSet): | ||
queryset = ObjectVersion.objects.order_by("object_type", "-version") | ||
serializer_class = ObjectVersionSerializer | ||
lookup_field = "version" | ||
|
||
def perform_destroy(self, instance): | ||
if instance.status != ObjectVersionStatus.draft: | ||
raise ValidationError( | ||
{ | ||
api_settings.NON_FIELD_ERRORS_KEY: [ | ||
_("Only draft versions can be destroyed") | ||
] | ||
}, | ||
code="non-draft-version-destroy", | ||
) | ||
|
||
super().perform_destroy(instance) |
20 changes: 20 additions & 0 deletions
20
src/objecttypes/core/migrations/0013_auto_20201118_1251.py
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,20 @@ | ||
# Generated by Django 2.2.12 on 2020-11-18 11:51 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("core", "0012_auto_20201112_1123"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="objectversion", | ||
name="version", | ||
field=models.PositiveSmallIntegerField( | ||
help_text="Integer version of the OBJECTTYPE", verbose_name="version" | ||
), | ||
), | ||
] |
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,12 @@ | ||
from django.core.exceptions import ValidationError | ||
|
||
from jsonschema.exceptions import SchemaError | ||
from jsonschema.validators import validator_for | ||
|
||
|
||
def check_json_schema(json_schema: dict): | ||
schema_validator = validator_for(json_schema) | ||
try: | ||
schema_validator.check_schema(json_schema) | ||
except SchemaError as exc: | ||
raise ValidationError(exc.args[0]) from exc |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just realized that this change breaks compatibility with Objects API validator. But I like that it's consistent with Zaken API now.
@joeribekker Should we display all versions data here? Or I can make a small change in Objects API
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this as well. I don't see the need to expand this property by default.
If you manually saw that this breaks Objects API validation, then please change yes, BUT it also means its a blind spot in our CI-setup. You can make an issue for that :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, we don't have integration tests and we don't even have
docker-compose
with two APIs now. Perhaps we can set up a separate repo with integration tests?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it, but lets make an issue for now so it won't be missed again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maykinmedia/objects-api#102