diff --git a/tests/test_validation.py b/tests/test_validation.py index ec07110..23f4fd6 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -2,11 +2,11 @@ import pytest -from zgw_consumers.models.validators import IsNotUrlValidator, StartWithValidator +from zgw_consumers.models.validators import NonUrlValidator, PrefixValidator -def test_start_with_validator_return_value_false(): - validator = StartWithValidator(prefix="/", return_value=False) +def test_start_with_validator_starts_with_false(): + validator = PrefixValidator(prefix="/", starts_with=False) assert validator.__call__("no_leading_slash") is None @@ -16,8 +16,8 @@ def test_start_with_validator_return_value_false(): assert "The given value cannot start with '/'" in exc_context.value -def test_start_with_validator_return_value_true(): - validator = StartWithValidator(prefix="/") +def test_start_with_validator_starts_with_true(): + validator = PrefixValidator(prefix="/") with pytest.raises(ValidationError) as exc_context: validator.__call__("no_leading_slash") @@ -28,11 +28,11 @@ def test_start_with_validator_return_value_true(): def test_is_not_url_validator(): - validator = IsNotUrlValidator() + validator = NonUrlValidator() assert validator.__call__("some random text") is None with pytest.raises(ValidationError) as exc_context: assert validator.__call__("http://www.example.com") - assert "String cannot be a URL" in exc_context.value + assert "Value cannot be a URL" in exc_context.value diff --git a/zgw_consumers/admin.py b/zgw_consumers/admin.py index 8c0f194..4331d09 100644 --- a/zgw_consumers/admin.py +++ b/zgw_consumers/admin.py @@ -1,6 +1,7 @@ from django.contrib import admin from django.db import models from django.http import HttpRequest +from django.utils.translation import gettext_lazy as _ from privates.admin import PrivateMediaMixin from solo.admin import SingletonModelAdmin @@ -18,7 +19,7 @@ class ServiceAdmin(admin.ModelAdmin): search_fields = ("label", "api_root", "nlx", "uuid") readonly_fields = ("get_connection_check",) - @admin.display(description="Connection Check") + @admin.display(description=_("Connection check status code")) def get_connection_check(self, obj): return obj.connection_check diff --git a/zgw_consumers/migrations/0021_service_api_connection_check_path.py b/zgw_consumers/migrations/0021_service_api_connection_check_path.py index 104acbb..c17c4bf 100644 --- a/zgw_consumers/migrations/0021_service_api_connection_check_path.py +++ b/zgw_consumers/migrations/0021_service_api_connection_check_path.py @@ -17,13 +17,13 @@ class Migration(migrations.Migration): name="api_connection_check_path", field=models.CharField( blank=True, - help_text="An optional API endpoint which will be used to check if the API is configured correctly and is currently up or down. This field is only used for in the admin's 'Connection check' field.", + help_text="An optional API endpoint which will be used to check if the API is configured correctly and is currently up or down. This field is only used for the admin's 'Connection check status code' field.", max_length=255, validators=[ - zgw_consumers.models.validators.StartWithValidator( - prefix="/", return_value=False + zgw_consumers.models.validators.PrefixValidator( + prefix="/", starts_with=False ), - zgw_consumers.models.validators.IsNotUrlValidator(), + zgw_consumers.models.validators.NonUrlValidator(), ], verbose_name="connection check endpoint", ), diff --git a/zgw_consumers/models/services.py b/zgw_consumers/models/services.py index 98b3fba..12040b3 100644 --- a/zgw_consumers/models/services.py +++ b/zgw_consumers/models/services.py @@ -21,7 +21,7 @@ from ..constants import APITypes, AuthTypes, NLXDirectories from .abstract import RestAPIService -from .validators import IsNotUrlValidator, StartWithValidator +from .validators import NonUrlValidator, PrefixValidator logger = logging.getLogger(__name__) @@ -37,12 +37,12 @@ class Service(RestAPIService): _("connection check endpoint"), help_text=_( "An optional API endpoint which will be used to check if the API is configured correctly and " - "is currently up or down. This field is only used for in the admin's 'Connection check' field." + "is currently up or down. This field is only used for the admin's 'Connection check status code' field." ), max_length=255, validators=[ - StartWithValidator(prefix="/", return_value=False), - IsNotUrlValidator(), + PrefixValidator(prefix="/", starts_with=False), + NonUrlValidator(), ], blank=True, ) diff --git a/zgw_consumers/models/validators.py b/zgw_consumers/models/validators.py index 02685b1..b9724f2 100644 --- a/zgw_consumers/models/validators.py +++ b/zgw_consumers/models/validators.py @@ -5,7 +5,7 @@ @deconstructible -class StartWithValidator: +class PrefixValidator: code = "invalid" def __init__( @@ -13,10 +13,10 @@ def __init__( prefix: str, message: str = None, code: str = None, - return_value: bool = True, + starts_with: bool = True, ): self.prefix = prefix - self.return_value = return_value + self.starts_with = starts_with if code is not None: self.code = code @@ -27,12 +27,12 @@ def __init__( self.message = _( "The given value {must_or_cannot} start with '{prefix}'" ).format( - must_or_cannot="must" if self.return_value else "cannot", + must_or_cannot="must" if self.starts_with else "cannot", prefix=self.prefix, ) def __call__(self, value: str) -> bool: - if not value.startswith(self.prefix) == self.return_value: + if value.startswith(self.prefix) != self.starts_with: raise ValidationError(self.message, code=self.code, params={"value": value}) def __eq__(self, other): @@ -41,13 +41,13 @@ def __eq__(self, other): and self.prefix == other.prefix and (self.message == other.message) and (self.code == other.code) - and (self.return_value == other.return_value) + and (self.starts_with == other.starts_with) ) @deconstructible -class IsNotUrlValidator(URLValidator): - message = _("String cannot be a URL") +class NonUrlValidator(URLValidator): + message = _("Value cannot be a URL") def __call__(self, value): try: