From 7e38a08fb89447b77302d1aabaa57bb96f223a00 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Sun, 7 Apr 2019 10:40:30 +0200 Subject: [PATCH] Switch from DictField to JSONField --- umap/migrations/0006_auto_20190407_0719.py | 19 ++++++++++ umap/models.py | 4 +- umap/tests/test_fields.py | 43 ---------------------- 3 files changed, 21 insertions(+), 45 deletions(-) create mode 100644 umap/migrations/0006_auto_20190407_0719.py delete mode 100644 umap/tests/test_fields.py diff --git a/umap/migrations/0006_auto_20190407_0719.py b/umap/migrations/0006_auto_20190407_0719.py new file mode 100644 index 00000000..baa93ca0 --- /dev/null +++ b/umap/migrations/0006_auto_20190407_0719.py @@ -0,0 +1,19 @@ +# Generated by Django 2.1.5 on 2019-04-07 07:19 + +import django.contrib.postgres.fields.jsonb +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('umap', '0005_remove_map_tilelayer'), + ] + + operations = [ + migrations.AlterField( + model_name='map', + name='settings', + field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict, null=True, verbose_name='settings'), + ), + ] diff --git a/umap/models.py b/umap/models.py index 7bdd3be5..11e6d667 100644 --- a/umap/models.py +++ b/umap/models.py @@ -8,8 +8,8 @@ from django.utils.translation import ugettext_lazy as _ from django.core.signing import Signer from django.template.defaultfilters import slugify from django.core.files.base import File +from django.contrib.postgres.fields import JSONField -from .fields import DictField from .managers import PublicManager @@ -137,7 +137,7 @@ class Map(NamedModel): editors = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, verbose_name=_("editors")) edit_status = models.SmallIntegerField(choices=EDIT_STATUS, default=OWNER, verbose_name=_("edit status")) share_status = models.SmallIntegerField(choices=SHARE_STATUS, default=PUBLIC, verbose_name=_("share status")) - settings = DictField(blank=True, null=True, verbose_name=_("settings")) + settings = JSONField(blank=True, null=True, verbose_name=_("settings"), default=dict) objects = models.Manager() public = PublicManager() diff --git a/umap/tests/test_fields.py b/umap/tests/test_fields.py deleted file mode 100644 index 4d744cd3..00000000 --- a/umap/tests/test_fields.py +++ /dev/null @@ -1,43 +0,0 @@ -import json - -import pytest - -from umap.models import Map - -pytestmark = pytest.mark.django_db - - -def test_can_use_dict(map): - d = {'locateControl': True} - map.settings = d - map.save() - assert Map.objects.get(pk=map.pk).settings == d - - -def test_can_set_item(map): - d = {'locateControl': True} - map.settings = d - map.save() - map_inst = Map.objects.get(pk=map.pk) - map_inst.settings['color'] = 'DarkGreen' - assert map_inst.settings['locateControl'] is True - - -def test_should_return_a_dict_if_none(map): - map.settings = None - map.save() - assert Map.objects.get(pk=map.pk).settings == {} - - -def test_should_not_double_dumps(map): - map.settings = '{"locate": true}' - map.save() - assert Map.objects.get(pk=map.pk).settings == {'locate': True} - - -def test_value_to_string(map): - d = {'locateControl': True} - map.settings = d - map.save() - field = Map._meta.get_field('settings') - assert json.loads(field.value_to_string(map)) == d