Skip to content
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

Delete zds-client usage #89

Merged
merged 10 commits into from
Mar 26, 2024
34 changes: 34 additions & 0 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.test import Client
from django.urls import reverse


def test_oas_fields_enabled(admin_client: Client, settings):
settings.ZGW_CONSUMERS_IGNORE_OAS_FIELDS = False
url = reverse("admin:zgw_consumers_service_add")

response = admin_client.get(url)

form = response.context["adminform"]

# django 3.2
if hasattr(form, "form"):
form = form.form

assert "oas" in form.fields
assert "oas_file" in form.fields


def test_oas_fields_disabled(admin_client: Client, settings):
settings.ZGW_CONSUMERS_IGNORE_OAS_FIELDS = True
url = reverse("admin:zgw_consumers_service_add")

response = admin_client.get(url)

form = response.context["adminform"]

# django 3.2
if hasattr(form, "form"):
form = form.form

assert "oas" not in form.fields
assert "oas_file" not in form.fields
78 changes: 78 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from django.core.exceptions import ValidationError

import pytest

from zgw_consumers.constants import APITypes, AuthTypes
from zgw_consumers.models import Service


def test_model_validation_with_oas_fields_enabled_none_provided(settings):
settings.ZGW_CONSUMERS_IGNORE_OAS_FIELDS = False
service = Service(
label="test",
api_type=APITypes.orc,
api_root="https://example.com/api/",
auth_type=AuthTypes.no_auth,
oas="",
oas_file="",
)

with pytest.raises(ValidationError) as exc_context:
service.clean()

error_dict = exc_context.value.error_dict
assert "oas" in error_dict
assert "oas_file" in error_dict


def test_model_validation_with_oas_fields_enabled_both_provided(settings):
settings.ZGW_CONSUMERS_IGNORE_OAS_FIELDS = False
service = Service(
label="test",
api_type=APITypes.orc,
api_root="https://example.com/api/",
auth_type=AuthTypes.no_auth,
oas="https://example.com/api/schema.json",
oas_file="schema.json",
)

with pytest.raises(ValidationError) as exc_context:
service.clean()

error_dict = exc_context.value.error_dict
assert "oas" in error_dict
assert "oas_file" in error_dict


def test_model_validation_with_oas_fields_disabled_none_provided(settings):
settings.ZGW_CONSUMERS_IGNORE_OAS_FIELDS = True
service = Service(
label="test",
api_type=APITypes.orc,
api_root="https://example.com/api/",
auth_type=AuthTypes.no_auth,
oas="",
oas_file="",
)

try:
service.clean()
except ValidationError:
pytest.fail("OAS fields should be ignored")


def test_model_validation_with_oas_fields_disabled_both_provided(settings):
settings.ZGW_CONSUMERS_IGNORE_OAS_FIELDS = True
service = Service(
label="test",
api_type=APITypes.orc,
api_root="https://example.com/api/",
auth_type=AuthTypes.no_auth,
oas="https://example.com/api/schema.json",
oas_file="schema.json",
)

try:
service.clean()
except ValidationError:
pytest.fail("OAS fields should be ignored")
11 changes: 11 additions & 0 deletions zgw_consumers/admin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from django.contrib import admin
from django.db import models
from django.http import HttpRequest

from privates.admin import PrivateMediaMixin
from solo.admin import SingletonModelAdmin

from .admin_fields import get_nlx_field
from .models.services import NLXConfig, Service
from .settings import get_setting
from .widgets import NoDownloadPrivateFileWidget


Expand All @@ -14,6 +17,14 @@ class ServiceAdmin(admin.ModelAdmin):
list_filter = ("api_type", "auth_type")
search_fields = ("label", "api_root", "nlx", "uuid")

def get_fields(self, request: HttpRequest, obj: models.Model | None = None):
fields = super().get_fields(request, obj=obj)
if get_setting("ZGW_CONSUMERS_IGNORE_OAS_FIELDS"):
assert isinstance(fields, list)
fields.remove("oas")
fields.remove("oas_file")
return fields
sergei-maertens marked this conversation as resolved.
Show resolved Hide resolved

def formfield_for_dbfield(self, db_field, request, **kwargs):
if db_field.name == "nlx":
return get_nlx_field(db_field, request, **kwargs)
Expand Down
24 changes: 24 additions & 0 deletions zgw_consumers/models/abstract.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from django.core.exceptions import ValidationError
from django.core.validators import FileExtensionValidator
from django.db import models
from django.utils.translation import gettext_lazy as _

from ..settings import get_setting


class Service(models.Model):
label = models.CharField(_("label"), max_length=100)
Expand All @@ -24,3 +27,24 @@ class RestAPIService(Service):

class Meta:
abstract = True

def clean(self):
super().clean()

if get_setting("ZGW_CONSUMERS_IGNORE_OAS_FIELDS"):
return

if self.oas and self.oas_file:
raise ValidationError(
{
"oas": _("Set either oas or oas_file, not both"),
"oas_file": _("Set either oas or oas_file, not both"),
}
)
elif not self.oas and not self.oas_file:
raise ValidationError(
{
"oas": _("Set either oas or oas_file"),
"oas_file": _("Set either oas or oas_file"),
}
)
15 changes: 0 additions & 15 deletions zgw_consumers/models/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,6 @@ def clean(self):
{"header_key": _("If header_value is set, header_key must also be set")}
)

if self.oas and self.oas_file:
raise ValidationError(
{
"oas": _("Set either oas or oas_file, not both"),
"oas_file": _("Set either oas or oas_file, not both"),
}
)
elif not self.oas and not self.oas_file:
raise ValidationError(
{
"oas": _("Set either oas or oas_file"),
"oas_file": _("Set either oas or oas_file"),
}
)

@deprecated(
"The `build_client` method is deprecated and will be removed in the next major release. "
"Instead, use the new `ape_pie.APIClient` or `zgw_consumers.nlx.NLXClient`.",
Expand Down
13 changes: 13 additions & 0 deletions zgw_consumers/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@
Deprecated.
"""

ZGW_CONSUMERS_IGNORE_OAS_FIELDS = False
"""
When enabled, the OAS URL/file field uploads are excluded from the admin.

The OAS information is only relevant for legacy usage of zds-client, which exposes
operations based on the operations in the API. This didn't work well in practice, so
the requirement of OpenAPI specs was dropped. Enable this if you are not using legacy/
deprecated features.

This is a temporary setting that will be removed in 1.0 when zds-client support will be
dropped entirely.
"""


def get_setting(name: str):
default = globals()[name]
Expand Down