Switch from DictField to JSONField

This commit is contained in:
Yohan Boniface 2019-04-07 10:40:30 +02:00
parent addc67549e
commit 7e38a08fb8
3 changed files with 21 additions and 45 deletions

View file

@ -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'),
),
]

View file

@ -8,8 +8,8 @@ from django.utils.translation import ugettext_lazy as _
from django.core.signing import Signer from django.core.signing import Signer
from django.template.defaultfilters import slugify from django.template.defaultfilters import slugify
from django.core.files.base import File from django.core.files.base import File
from django.contrib.postgres.fields import JSONField
from .fields import DictField
from .managers import PublicManager from .managers import PublicManager
@ -137,7 +137,7 @@ class Map(NamedModel):
editors = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, verbose_name=_("editors")) 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")) 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")) 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() objects = models.Manager()
public = PublicManager() public = PublicManager()

View file

@ -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