summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ishtar_common/utils.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py
index 0c4d217d6..ceaa7d27a 100644
--- a/ishtar_common/utils.py
+++ b/ishtar_common/utils.py
@@ -30,6 +30,7 @@ import tempfile
from django import forms
from django.conf import settings
+from django.contrib.contenttypes.models import ContentType
from django.contrib.gis.geos import GEOSGeometry
from django.contrib.sessions.backends.db import SessionStore
from django.core.cache import cache
@@ -687,3 +688,37 @@ def generate_relation_graph(obj, debug=False):
return
shutil.rmtree(tempdir)
+
+def create_default_json_fields(model):
+ """
+ Create default json field configuration in existing database
+ :param model: model concerned
+ """
+ from ishtar_common.models import JsonDataField
+
+ def _get_keys(data, current_path=""):
+ keys = []
+ for key in data.keys():
+ if type(data[key]) == dict:
+ keys += _get_keys(data[key], current_path + key + "__")
+ continue
+ keys.append(current_path + key)
+ return keys
+
+ keys = []
+ for item in model.objects.all():
+ for key in _get_keys(item.data):
+ if key not in keys:
+ keys.append(key)
+
+ content_type = ContentType.objects.get_for_model(model)
+ for key in keys:
+ JsonDataField.objects.get_or_create(
+ content_type=content_type, key=key,
+ defaults={
+ 'name': u" ".join(key.split('__')).capitalize(),
+ 'value_type': 'T',
+ 'display': False
+ }
+ )
+