Skip to content

Commit

Permalink
Merge pull request #14 from open-formulieren/fix/3435-add-model-str-m…
Browse files Browse the repository at this point in the history
…ethod

Add string method to the token exchange config method
  • Loading branch information
sergei-maertens authored Nov 8, 2023
2 parents 68181a3 + 0aeaade commit 147fa32
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
22 changes: 22 additions & 0 deletions token_exchange/migrations/0002_tokenexchangeconfiguration_label.py
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
17 changes: 17 additions & 0 deletions token_exchange/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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
24 changes: 24 additions & 0 deletions token_exchange/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 147fa32

Please sign in to comment.