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 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))