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 3 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 @@ -126,6 +126,7 @@ This is the list of the currently supported setting types you may need to use:
- `Setting.TYPE_TEXT`
- `Setting.TYPE_TIME`
- `Setting.TYPE_URL`
- `Setting.TYPE_JSON`

fabiocaccamo marked this conversation as resolved.
Show resolved Hide resolved
### Templates
You can retrieve settings in templates:
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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
django>=1.8.0
six>=1.9.0
jsonfield>=3.1.0
fabiocaccamo marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
requires=["django(>=1.8)"],
install_requires=[
"six >= 1.9.0, < 2.0.0",
"jsonfield>=3.0"
fabiocaccamo marked this conversation as resolved.
Show resolved Hide resolved
],
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand All @@ -81,8 +82,6 @@
"License :: OSI Approved :: MIT License",
"Natural Language :: English",
"Operating System :: OS Independent",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
fabiocaccamo marked this conversation as resolved.
Show resolved Hide resolved
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
Expand Down
8 changes: 5 additions & 3 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 @@ -62,8 +62,10 @@ def test_getter_setter(self):
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, '')
setting_json_value = Setting.get('TEST_SETTING_JSON')
self.assertEqual(setting_json_value, dict())

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 Down
2 changes: 0 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[tox]
envlist =
py27-{dj18,dj19,dj110,dj111}-{sqlite,postgres},
fabiocaccamo marked this conversation as resolved.
Show resolved Hide resolved
py36-{dj18,dj19,dj110,dj111,dj20,dj21,dj22,dj30,dj31,dj32}-{sqlite,postgres},
py37-{dj20,dj21,dj22,dj30,dj31,dj32}-{sqlite,postgres},
py38-{dj22,dj30,dj31,dj32}-{sqlite,postgres},
Expand All @@ -9,7 +8,6 @@ envlist =

[gh-actions]
python =
2.7: py27
fabiocaccamo marked this conversation as resolved.
Show resolved Hide resolved
3.6: py36
3.7: py37
3.8: py38
Expand Down