From be94567f09995e58418b47b053a78a1b3b10217a Mon Sep 17 00:00:00 2001 From: SilviaAmAm Date: Wed, 8 Nov 2023 16:15:38 +0100 Subject: [PATCH 1/2] :bug: [open-formulieren/open-forms#3435] Add label to token exchange configuration --- .../0002_tokenexchangeconfiguration_label.py | 22 +++++++++++++++++++ token_exchange/models.py | 17 ++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 token_exchange/migrations/0002_tokenexchangeconfiguration_label.py diff --git a/token_exchange/migrations/0002_tokenexchangeconfiguration_label.py b/token_exchange/migrations/0002_tokenexchangeconfiguration_label.py new file mode 100644 index 0000000..96cd0bb --- /dev/null +++ b/token_exchange/migrations/0002_tokenexchangeconfiguration_label.py @@ -0,0 +1,22 @@ +# Generated by Django 3.2.21 on 2023-11-08 15:02 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("token_exchange", "0001_initial"), + ] + + operations = [ + migrations.AddField( + model_name="tokenexchangeconfiguration", + name="label", + field=models.CharField( + blank=True, + help_text="The label of this token exchange configuration", + max_length=250, + verbose_name="label", + ), + ), + ] diff --git a/token_exchange/models.py b/token_exchange/models.py index 9c96ed6..3d255e6 100644 --- a/token_exchange/models.py +++ b/token_exchange/models.py @@ -3,6 +3,12 @@ class TokenExchangeConfiguration(models.Model): + label = models.CharField( + verbose_name=_("label"), + help_text=_("The label of this token exchange configuration"), + max_length=250, + blank=True, + ) service = models.OneToOneField( to="zgw_consumers.Service", verbose_name=_("service"), @@ -20,3 +26,14 @@ class TokenExchangeConfiguration(models.Model): class Meta: verbose_name = _("Token exchange plugin configuration") + + def save(self, *args, **kwargs) -> None: + if not self.label and (self.service and self.service.label): + self.label = self.service.label + return super().save(*args, **kwargs) + + def __str__(self) -> str: + if not self.label: + return super().__str__() + + return self.label From 0aeaade5cc27e764a0d98e7e6fb8c016a41827a6 Mon Sep 17 00:00:00 2001 From: SilviaAmAm Date: Wed, 8 Nov 2023 16:16:16 +0100 Subject: [PATCH 2/2] :white_check_mark: [open-formulieren/open-forms#3435] Test label configuration --- token_exchange/tests/test_models.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 token_exchange/tests/test_models.py diff --git a/token_exchange/tests/test_models.py b/token_exchange/tests/test_models.py new file mode 100644 index 0000000..5cb05ba --- /dev/null +++ b/token_exchange/tests/test_models.py @@ -0,0 +1,24 @@ +from django.test import TestCase + +from .factories import TokenExchangeConfigurationFactory + + +class TokenExchangeConfigurationTests(TestCase): + def test_string_method_with_service_label(self): + config = TokenExchangeConfigurationFactory.create(service__label="A label") + + self.assertEqual("A label", str(config)) + + def test_string_method_withou_service_label(self): + config = TokenExchangeConfigurationFactory.create() + + self.assertEqual( + f"TokenExchangeConfiguration object ({config.pk})", str(config) + ) + + def test_save_does_not_overwrite_label(self): + config = TokenExchangeConfigurationFactory.create( + label="A config label", service__label="A service label" + ) + + self.assertEqual("A config label", str(config))