Skip to content

Commit 04e6c5b

Browse files
authored
Merge pull request #59 from maykinmedia/issue/58-setup-config-example-uuid
🐛 [#58] Add example for UUID field in directive
2 parents 24e287e + e37276b commit 04e6c5b

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

django_setup_configuration/documentation/setup_config_example.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
get_args,
1717
get_origin,
1818
)
19+
from uuid import UUID
1920

21+
from django.core.serializers.json import DjangoJSONEncoder
2022
from django.utils.functional import Promise
2123

2224
import ruamel.yaml
@@ -123,6 +125,8 @@ def _insert_example_with_comments(
123125
"""
124126
if isinstance(example, str) and "\n" in example:
125127
example = LiteralScalarString(example)
128+
if isinstance(example, UUID):
129+
example = str(example)
126130

127131
example_data[field_name] = example
128132
example_data.yaml_set_comment_before_after_key(field_name, before="\n")
@@ -140,14 +144,14 @@ def _insert_example_with_comments(
140144
_yaml_set_wrapped_comment(
141145
example_data,
142146
field_name,
143-
f"POSSIBLE VALUES: {json.dumps(values)}",
147+
f"POSSIBLE VALUES: {json.dumps(values, cls=DjangoJSONEncoder)}",
144148
80,
145149
indent=depth * 2,
146150
)
147151

148152
if (default := _get_default_from_field_info(field_info)) is not PydanticUndefined:
149153
if not (isinstance(example, str) and "\n" in example):
150-
default = json.dumps(default)
154+
default = json.dumps(default, cls=DjangoJSONEncoder)
151155

152156
example_data.yaml_set_comment_before_after_key(
153157
field_name, f"DEFAULT VALUE: {default}", indent=depth * 2
@@ -343,6 +347,7 @@ def _generate_basic_example(field_type: Any, field_info: FieldInfo) -> Any:
343347
float: 123.45,
344348
list: [],
345349
dict: {},
350+
UUID: "02907e89-1ba8-43e9-a86c-d0534d461316",
346351
}
347352
return example_map.get(field_type, NO_EXAMPLE)
348353

testapp/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from uuid import UUID
2+
13
from django.contrib.postgres.fields import ArrayField
24
from django.db import models
35
from django.utils.functional import lazy
@@ -14,6 +16,10 @@ class DjangoModel(models.Model):
1416
int_with_default = models.IntegerField(default=42)
1517
nullable_int = models.IntegerField(null=True)
1618
nullable_int_with_default = models.IntegerField(default=42)
19+
uuid_field = models.UUIDField()
20+
uuid_field_with_default = models.UUIDField(
21+
default=UUID("125a77ef-d158-4bea-b036-8dcdbdde428d")
22+
)
1723

1824
nullable_str = models.CharField(null=True, blank=False, max_length=1)
1925
nullable_and_blank_str = models.CharField(null=True, blank=False, max_length=1)

tests/test_documentation.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from docutils.frontend import get_default_settings
99
from docutils.parsers.rst import Parser, directives
1010
from docutils.utils import new_document
11-
from pydantic import Field, ValidationError
11+
from pydantic import UUID4, Field, ValidationError
1212

1313
from django_setup_configuration.configuration import BaseConfigurationStep
1414
from django_setup_configuration.documentation.setup_config_example import (
@@ -69,6 +69,7 @@ class ConfigModel(ConfigurationModel):
6969
sequence_of_primitives: list[int] = Field()
7070
literal: Literal["foo", "bar", "bar"] = Field()
7171
literal_block_scalar: str = Field(default='{\n "foo":"bar",\n "bar":"baz"\n}')
72+
uuid_field: UUID4 = Field()
7273

7374
class Meta:
7475
django_model_refs = {
@@ -81,6 +82,7 @@ class Meta:
8182
"str_with_localized_default",
8283
"int_with_lazy_default",
8384
"blank_str",
85+
"uuid_field_with_default",
8486
)
8587
}
8688
extra_kwargs = {
@@ -225,6 +227,9 @@ def test_directive_output(register_directive, docutils_document):
225227
"bar":"baz"
226228
}
227229
230+
# REQUIRED: true
231+
uuid_field: 02907e89-1ba8-43e9-a86c-d0534d461316
232+
228233
# REQUIRED: true
229234
required_int: 1234
230235
@@ -274,6 +279,10 @@ def test_directive_output(register_directive, docutils_document):
274279
# DEFAULT VALUE: ""
275280
# REQUIRED: false
276281
blank_str: example_string
282+
283+
# DEFAULT VALUE: "125a77ef-d158-4bea-b036-8dcdbdde428d"
284+
# REQUIRED: false
285+
uuid_field_with_default: 125a77ef-d158-4bea-b036-8dcdbdde428d
277286
"""
278287
)
279288

0 commit comments

Comments
 (0)