diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-01-02 10:18:44 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-01-11 17:30:46 +0100 |
commit | 35f9cbb4d247efe038bbc3507bd8c8c056735ca8 (patch) | |
tree | e5d0f38c898f24c420702682ded8d12ada2dbc3e /ishtar_common | |
parent | be8538ec1cf68dc5dcffa927558181b2e7e6225e (diff) | |
download | Ishtar-35f9cbb4d247efe038bbc3507bd8c8c056735ca8.tar.bz2 Ishtar-35f9cbb4d247efe038bbc3507bd8c8c056735ca8.zip |
Work on M2M historization
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/models.py | 19 | ||||
-rw-r--r-- | ishtar_common/templates/ishtar/blocks/window_field_flex_historized_multiple.html | 8 | ||||
-rw-r--r-- | ishtar_common/templatetags/window_field.py | 29 | ||||
-rw-r--r-- | ishtar_common/utils.py | 18 |
4 files changed, 73 insertions, 1 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index e1318ef62..2915c4997 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -69,7 +69,7 @@ from ishtar_common.models_imports import ImporterModel, ImporterType, \ Import, TargetKeyGroup from ishtar_common.utils import get_cache, disable_for_loaddata, create_slug, \ get_all_field_names, merge_tsvectors, cached_label_changed, \ - generate_relation_graph + generate_relation_graph, HISTORY_M2M_SPLIT __all__ = [ 'ImporterModel', 'ImporterType', 'ImporterDefault', 'ImporterDefaultValues', @@ -507,6 +507,21 @@ class GeneralType(Cached, models.Model): def natural_key(self): return (self.txt_idx,) + def history_compress(self): + return self.txt_idx + + @classmethod + def history_decompress(cls, value=""): + if not value: + value = "" + res = [] + for txt_idx in value.split(HISTORY_M2M_SPLIT): + try: + res.append(cls.objects.get(txt_idx=txt_idx)) + except cls.DoesNotExist: + continue + return res + @property def explicit_label(self): return u"{} ({})".format(self.label, self._meta.verbose_name) @@ -1433,6 +1448,8 @@ class BaseHistorizedItem(DocumentItem, FullSearch, Imported, JsonData, IS_BASKET = False EXTERNAL_ID_KEY = '' EXTERNAL_ID_DEPENDENCIES = [] + HISTORICAL_M2M = [] + history_modifier = models.ForeignKey( User, related_name='+', on_delete=models.SET_NULL, verbose_name=_(u"Last editor"), blank=True, null=True) diff --git a/ishtar_common/templates/ishtar/blocks/window_field_flex_historized_multiple.html b/ishtar_common/templates/ishtar/blocks/window_field_flex_historized_multiple.html new file mode 100644 index 000000000..a68bd0cb4 --- /dev/null +++ b/ishtar_common/templates/ishtar/blocks/window_field_flex_historized_multiple.html @@ -0,0 +1,8 @@ +{% load i18n %}{% if data %} +<dl class="col-12 {% if size == 2 %}col-lg-6{% else %}col-md-6 col-lg-4{% endif %} d-flex flex-wrap row"> + <dt class="col-5">{% trans caption %}</dt> + <dd class="col-7">{% for d in data %} + {% if forloop.counter0 %} ; {% endif %}{{ d }} + {% endfor %}</dd> +</dl> +{% endif %} diff --git a/ishtar_common/templatetags/window_field.py b/ishtar_common/templatetags/window_field.py index a5bae3b72..30a711ed9 100644 --- a/ishtar_common/templatetags/window_field.py +++ b/ishtar_common/templatetags/window_field.py @@ -95,6 +95,27 @@ def field_multiple(caption, data, li=False, size=None): return {'caption': caption, 'data': data, 'li': li, "size": size} +from django.template import Library, loader, Context + + +@register.simple_tag +def field_multiple_obj(caption, item, attr, li=False, size=None): + data = getattr(item, attr) + if not hasattr(item, '_step') or not hasattr(item, 'historical_' + attr): + t = loader.get_template('ishtar/blocks/window_field_flex_multiple.html') + return t.render( + {'caption': caption, 'data': data, 'li': li, "size": size} + ) + rel_model = data.model + data = getattr(item, 'historical_' + attr) + data = rel_model.history_decompress(data) + t = loader.get_template( + 'ishtar/blocks/window_field_flex_historized_multiple.html') + return t.render( + {'caption': caption, 'data': data, 'li': li, "size": size} + ) + + @register.inclusion_tag('ishtar/blocks/window_field_multiple.html') def field_li_multiple(caption, data): return field_multiple(caption, data, li=True) @@ -108,6 +129,14 @@ def field_flex_multiple(caption, data, small=False): return field_multiple(caption, data, size=size) +@register.simple_tag +def field_flex_multiple_obj(caption, item, attr, small=False): + size = None + if small: + size = 2 + return field_multiple_obj(caption, item, attr, size=size) + + @register.inclusion_tag('ishtar/blocks/window_field_flex_multiple_full.html') def field_flex_multiple_full(caption, data, small=False): size = None diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index ff67fc470..94e71c655 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -929,3 +929,21 @@ def get_urls_for_model(model, views, own=False, autocomplete=False, ] return urls + + +HISTORY_M2M_SPLIT = u"*||*" + + +def m2m_historization_changed(sender, **kwargs): + obj = kwargs.get('instance', None) + if not obj: + return + for attr in obj.HISTORICAL_M2M: + values = [] + for value in getattr(obj, attr).all(): + if not hasattr(value, "history_compress"): + continue + values.append(value.history_compress()) + setattr(obj, 'historical_' + attr, HISTORY_M2M_SPLIT.join(values)) + obj.skip_history_when_saving = True + obj.save() |