Skip to content

Commit

Permalink
fixes default attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
saxix committed Jul 2, 2024
1 parent 2bca167 commit e9979aa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
20 changes: 13 additions & 7 deletions src/hope_flex_fields/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
}


def get_default_attrs():
return DEFAULT_ATTRS


class TestForm(forms.Form):
fieldset = None

Expand All @@ -39,7 +43,7 @@ class FieldDefinition(models.Model):
name = models.CharField(max_length=255, unique=True)
description = models.TextField(max_length=500, blank=True, null=True, default="")
field_type = StrategyClassField(registry=field_registry)
attrs = models.JSONField(default=DEFAULT_ATTRS, blank=True)
attrs = models.JSONField(default=get_default_attrs, blank=True)
regex = RegexField(blank=True, null=True, validators=[RegexValidator()])
validation = models.TextField(blank=True, null=True)
objects = FieldDefinitionManager()
Expand All @@ -59,17 +63,19 @@ def clean(self):
raise ValidationError(e)

def set_default_arguments(self):
if not isinstance(self.attrs, dict):
self.attrs = {}
stored = self.attrs
if self.attrs in [None, "null", ""]:
self.attrs = DEFAULT_ATTRS
elif isinstance(self.attrs, str):
self.attrs = DEFAULT_ATTRS

sig: inspect.Signature = inspect.signature(self.field_type)
defaults = {
merged = {
k.name: k.default
for __, k in sig.parameters.items()
if k.default not in [inspect.Signature.empty]
}
defaults.update(**stored)
self.attrs = defaults
merged.update(**self.attrs)
self.attrs = merged

@property
def required(self):
Expand Down
21 changes: 20 additions & 1 deletion tests/admin/test_admin_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from django.urls import reverse

import pytest
from strategy_field.utils import fqn

from hope_flex_fields.models import FieldDefinition

pytestmark = [pytest.mark.admin, pytest.mark.smoke, pytest.mark.django_db]

Expand All @@ -19,7 +22,7 @@ def record(db):
return fd1


def test_fieldset_test(app, record):
def test_field_test(app, record):
url = reverse("admin:hope_flex_fields_fielddefinition_test", args=[record.pk])
res = app.get(url)
res.forms["test"]["IntField"] = ""
Expand All @@ -31,3 +34,19 @@ def test_fieldset_test(app, record):
res = res.forms["test"].submit()
messages = [s.message for s in res.context["messages"]]
assert messages == ["Valid"]


def test_fields_create(app, record):
url = reverse("admin:hope_flex_fields_fielddefinition_add")
res = app.get(url)
res.form["name"] = "Int"
res.form["field_type"] = fqn(forms.ChoiceField)
res = res.form.submit()
assert res.status_code == 302
obj: FieldDefinition = FieldDefinition.objects.get(name="int")
assert obj.attrs == {
"choices": [],
"required": False,
"label": None,
"help_text": "",
}
2 changes: 1 addition & 1 deletion tests/extra/demoapp/demo/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@


urlpatterns = [
path("admin/", admin.site.urls),
path("api/", include(router.urls)),
path("", admin.site.urls),
]
1 change: 1 addition & 0 deletions tests/extra/testutils/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class SuperUserFactory(UserFactory):
class FieldDefinitionFactory(AutoRegisterModelFactory):
name = factory.Sequence(lambda d: "FieldDefinition-%s" % d)
field_type = factory.fuzzy.FuzzyChoice([forms.CharField, forms.IntegerField])
attrs = {}

class Meta:
model = FieldDefinition
Expand Down

0 comments on commit e9979aa

Please sign in to comment.