Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for JSONField #19

Merged
merged 6 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,6 @@ dmypy.json

# Pyre type checker
.pyre/

# PyCharm
.idea
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ This is the list of the currently supported setting types you may need to use:
- `Setting.TYPE_FLOAT`
- `Setting.TYPE_IMAGE`
- `Setting.TYPE_INT`
- `Setting.TYPE_JSON`
- `Setting.TYPE_STRING`
- `Setting.TYPE_TEXT`
- `Setting.TYPE_TIME`
Expand Down
2 changes: 1 addition & 1 deletion extra_settings/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SettingAdmin(admin.ModelAdmin):
value_fields_names = (
'value_bool', 'value_date', 'value_datetime', 'value_decimal', 'value_duration',
'value_email', 'value_file', 'value_float', 'value_image',
'value_int', 'value_string', 'value_text', 'value_time', 'value_url',
'value_int', 'value_json', 'value_string', 'value_text', 'value_time', 'value_url',
)
search_fields = ('name', )
list_display = ('name', 'value_type', ) + value_fields_names + ('description', )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.0.1 on 2022-01-21 01:29

from django.db import migrations, models
import jsonfield.fields


class Migration(migrations.Migration):

dependencies = [
('extra_settings', '0004_setting_description'),
]

operations = [
migrations.AddField(
model_name='setting',
name='value_json',
field=jsonfield.fields.JSONField(blank=True, default=dict, verbose_name='Value'),
),
migrations.AlterField(
model_name='setting',
name='value_type',
field=models.CharField(choices=[('bool', 'bool'), ('date', 'date'), ('datetime', 'datetime'), ('decimal', 'decimal'), ('duration', 'duration'), ('email', 'email'), ('file', 'file'), ('float', 'float'), ('image', 'image'), ('int', 'int'), ('json', 'json'), ('string', 'string'), ('text', 'text'), ('time', 'time'), ('url', 'url')], max_length=20, verbose_name='Type'),
),
]
7 changes: 5 additions & 2 deletions extra_settings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from django.conf import settings
from django.db import models
from jsonfield import JSONField

if django.VERSION < (2, 0):
from django.utils.encoding import force_text as force_str
else:
Expand Down Expand Up @@ -67,7 +69,7 @@ def get(cls, name, default=None):
# TYPE_HTML = 'html'
TYPE_IMAGE = 'image'
TYPE_INT = 'int'
# TYPE_JSON = 'json' # TODO
TYPE_JSON = 'json'
TYPE_STRING = 'string'
TYPE_TEXT = 'text'
TYPE_TIME = 'time'
Expand All @@ -87,7 +89,7 @@ def get(cls, name, default=None):
# (TYPE_HTML, TYPE_HTML, ),
(TYPE_IMAGE, TYPE_IMAGE, ),
(TYPE_INT, TYPE_INT, ),
# (TYPE_JSON, TYPE_JSON, ),
(TYPE_JSON, TYPE_JSON, ),
(TYPE_STRING, TYPE_STRING, ),
(TYPE_TEXT, TYPE_TEXT, ),
(TYPE_TIME, TYPE_TIME, ),
Expand All @@ -109,6 +111,7 @@ def get(cls, name, default=None):
value_float = models.FloatField(blank=True, default=0.0, verbose_name=_('Value'))
value_image = models.FileField(blank=True, upload_to=fields.upload_to_images, verbose_name=_('Value'))
value_int = models.IntegerField(blank=True, default=0, verbose_name=_('Value'))
value_json = JSONField(blank=True, default=dict, verbose_name=_('Value'))
value_string = models.CharField(blank=True, max_length=50, verbose_name=_('Value'))
value_text = models.TextField(blank=True, verbose_name=_('Value'))
value_time = models.TimeField(blank=True, null=True, verbose_name=_('Value'))
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
django>=1.8.0
six>=1.9.0
jsonfield == 2.0.2; python_version <= "2.7"
jsonfield>=3.0.0; python_version >= "3.6"
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
requires=["django(>=1.8)"],
install_requires=[
"six >= 1.9.0, < 2.0.0",
'jsonfield == 2.0.2; python_version <= "2.7"',
'jsonfield >= 3.0.0; python_version >= "3.6"'
],
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand Down
31 changes: 21 additions & 10 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def setUp(self):
# Setting(name='TEST_SETTING_HTML', value_type=Setting.TYPE_HTML),
Setting(name='TEST_SETTING_IMAGE', value_type=Setting.TYPE_IMAGE),
Setting(name='TEST_SETTING_INT', value_type=Setting.TYPE_INT),
# Setting(name='TEST_SETTING_JSON', value_type=Setting.TYPE_JSON),
Setting(name='TEST_SETTING_JSON', value_type=Setting.TYPE_JSON),
Setting(name='TEST_SETTING_STRING', value_type=Setting.TYPE_STRING),
Setting(name='TEST_SETTING_TEXT', value_type=Setting.TYPE_TEXT),
Setting(name='TEST_SETTING_TIME', value_type=Setting.TYPE_TIME),
Expand Down Expand Up @@ -54,16 +54,16 @@ def test_create_setting(self):

def test_getter_setter(self):
setting_obj, setting_created = Setting.objects.get_or_create(
name='TEST_GETTER_SETTER', defaults={ 'value_type':Setting.TYPE_STRING })
name='TEST_GETTER_SETTER', defaults={'value_type': Setting.TYPE_STRING})
self.assertEqual(setting_obj.value, '')
setting_obj.value = 'string value'
setting_obj.save()
setting_obj = Setting.objects.get(name='TEST_GETTER_SETTER')
self.assertEqual(setting_obj.value, 'string value')

def test_get_with_valid_name(self):
setting_value = Setting.get('TEST_SETTING_STRING')
self.assertEqual(setting_value, '')
setting_string_value = Setting.get('TEST_SETTING_STRING')
self.assertEqual(setting_string_value, '')

fabiocaccamo marked this conversation as resolved.
Show resolved Hide resolved
def test_get_with_valid_name_and_default_value(self):
setting_value = Setting.get('TEST_SETTING_STRING', default='default string value')
Expand All @@ -87,7 +87,7 @@ def test_get_num_queries(self):

def test_cache_updated_on_model_delete(self):
setting_obj, setting_created = Setting.objects.get_or_create(
name='TEST_GETTER_SETTER', defaults={ 'value_type':Setting.TYPE_STRING })
name='TEST_GETTER_SETTER', defaults={'value_type': Setting.TYPE_STRING})
setting_obj.value = 'string value'
setting_obj.save()
self.assertEqual(Setting.get('TEST_GETTER_SETTER'), 'string value')
Expand All @@ -96,7 +96,7 @@ def test_cache_updated_on_model_delete(self):

def test_cache_updated_on_model_name_changed(self):
setting_obj, setting_created = Setting.objects.get_or_create(
name='TEST_GETTER_SETTER', defaults={ 'value_type':Setting.TYPE_STRING })
name='TEST_GETTER_SETTER', defaults={'value_type': Setting.TYPE_STRING})
setting_obj.value = 'string value'
setting_obj.save()
self.assertEqual(Setting.get('TEST_GETTER_SETTER'), 'string value')
Expand All @@ -106,12 +106,23 @@ def test_cache_updated_on_model_name_changed(self):
self.assertEqual(Setting.get('TEST_GETTER_SETTER_RENAMED'), 'string value')

def test_repr(self):
setting_obj, setting_created=Setting.objects.get_or_create(
setting_obj, setting_created = Setting.objects.get_or_create(
name='PACKAGE_NAME',
defaults={
'value_type':Setting.TYPE_STRING,
'value_string':'django-extra-settings',
'value_type': Setting.TYPE_STRING,
'value_string': 'django-extra-settings',
})
setting_repr='{} [{}]'.format(
setting_repr = '{} [{}]'.format(
setting_obj.name, setting_obj.value_type)
self.assertEqual('{0}'.format(setting_obj), setting_repr)

def test_setting_type_json(self):
# getter & setter
setting_obj, setting_created = Setting.objects.get_or_create(
name='TEST_SETTING_JSON', defaults={'value_type': Setting.TYPE_JSON}
)
self.assertEqual(setting_obj.value, dict())
setting_obj.value = {"level": "L2", "role": "Admin"}
setting_obj.save()
setting_obj = Setting.objects.get(name='TEST_SETTING_JSON')
self.assertEqual(setting_obj.value, {"level": "L2", "role": "Admin"})