diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-10-17 17:31:09 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-10-17 17:31:09 +0200 |
commit | 7811ea2ab5bc31c66228d02e84ac2208be56b5e8 (patch) | |
tree | faab2c98880088d074b0fd8a979f86af967e0736 /ishtar_common/models.py | |
parent | f9750f82ce0cf347491b2a8584811339fd8e52c6 (diff) | |
download | Ishtar-7811ea2ab5bc31c66228d02e84ac2208be56b5e8.tar.bz2 Ishtar-7811ea2ab5bc31c66228d02e84ac2208be56b5e8.zip |
JSON: add JSON fields to manage dynamic data schema - add table to administrate display of theses fields (refs #3077)
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r-- | ishtar_common/models.py | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 2904e51cd..8d4511809 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -35,6 +35,7 @@ import tempfile import time from django.conf import settings +from django.contrib.postgres.fields import JSONField from django.contrib.postgres.search import SearchVectorField, SearchVector from django.core.cache import cache from django.core.exceptions import ObjectDoesNotExist, ValidationError @@ -909,6 +910,55 @@ class BulkUpdatedItem(object): return transaction_id, False +class JsonDataSection(models.Model): + content_type = models.ForeignKey(ContentType) + name = models.CharField(_(u"Name"), max_length=200) + order = models.IntegerField(_(u"Order"), default=10) + + class Meta: + verbose_name = _(u"Json data - Menu") + verbose_name_plural = _(u"Json data - Menus") + ordering = ['name'] + + def __unicode__(self): + return u"{} - {}".format(self.content_type, self.name) + + +class JsonDataField(models.Model): + name = models.CharField(_(u"Name"), max_length=200) + content_type = models.ForeignKey(ContentType) + key = models.CharField( + _(u"Key"), max_length=200, + help_text=_(u"Value of the key in the JSON schema. For hierarchical " + u"key use \"__\" to explain it. For instance the key " + u"'my_subkey' with data such as {'my_key': {'my_subkey': " + u"'value'}} will be reached with my_key__my_subkey.")) + display = models.BooleanField(_(u"Display"), default=True) + order = models.IntegerField(_(u"Order"), default=10) + section = models.ForeignKey(JsonDataSection, blank=True, null=True) + + class Meta: + verbose_name = _(u"Json data - Field") + verbose_name_plural = _(u"Json data - Fields") + + def __unicode__(self): + return u"{} - {}".format(self.content_type, self.name) + + def clean(self): + if not self.section: + return + if self.section.content_type != self.content_type: + raise ValidationError( + _(u"Content type of the field and of the menu do not match")) + + +class JsonData(models.Model): + data = JSONField(null=True, blank=True) + + class Meta: + abstract = True + + class Imported(models.Model): imports = models.ManyToManyField( Import, blank=True, @@ -993,10 +1043,10 @@ class FullSearch(models.Model): return changed -class BaseHistorizedItem(FullSearch, Imported): +class BaseHistorizedItem(FullSearch, Imported, JsonData): """ Historized item with external ID management. - All historized items are searcheable + All historized items are searcheable and have a data json field """ IS_BASKET = False EXTERNAL_ID_KEY = '' |