diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-11-15 16:37:42 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-12-12 12:20:59 +0100 |
commit | e3ce9c77d36db4510076b677ec7ce1e0021a51f8 (patch) | |
tree | edb1258d180c35e73a10ab7120c69d81b3925d44 /ishtar_common/models_common.py | |
parent | 905dfbb0ebb91af7b7acaae256306f178863c57f (diff) | |
download | Ishtar-e3ce9c77d36db4510076b677ec7ce1e0021a51f8.tar.bz2 Ishtar-e3ce9c77d36db4510076b677ec7ce1e0021a51f8.zip |
Syndication - serialization - display sheet operation
Diffstat (limited to 'ishtar_common/models_common.py')
-rw-r--r-- | ishtar_common/models_common.py | 49 |
1 files changed, 47 insertions, 2 deletions
diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index 58eac91e2..e55a21e0c 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -9,6 +9,7 @@ import copy from collections import OrderedDict import datetime import json +import locale import logging import os import pyqrcode @@ -39,7 +40,8 @@ from django.db.models.signals import post_save, post_delete, m2m_changed from django.template.defaultfilters import slugify from django.utils.safestring import SafeText, mark_safe from django.utils.translation import activate, deactivate -from ishtar_common.utils import ugettext_lazy as _, pgettext_lazy, get_image_path +from ishtar_common.utils import ugettext_lazy as _, pgettext_lazy, get_image_path, \ + human_date from simple_history.models import HistoricalRecords as BaseHistoricalRecords from simple_history.signals import ( post_create_historical_record, @@ -2536,6 +2538,10 @@ class DocumentItem: ), } + def documents_list(self) -> list: + Document = apps.get_model("ishtar_common", "Document") + return self.get_associated_main_item_list("documents", Document) + def public_representation(self): images = [] if getattr(self, "main_image", None): @@ -3247,6 +3253,9 @@ class MainItem(ShortMenuItem): QUICK_ACTIONS = [] SERIALIZE_EXCLUDE = ["search_vector"] + SERIALIZE_PROPERTIES = ["external_id", "multi_polygon_geojson", "point_2d_geojson"] + SERIALIZE_CALL = {} + SERIALIZE_DATES = [] def full_serialize(self) -> dict: """ @@ -3268,20 +3277,56 @@ class MainItem(ShortMenuItem): elif field.many_to_many: values = getattr(self, field.name) if values.count(): - values = [str(v) for v in values] + values = [str(v) for v in values.all()] else: values = [] full_result[field.name] = values else: serialize_fields.append(field.name) + result = json.loads(serialize( "json", [self], fields=serialize_fields )) full_result.update(result[0]["fields"]) + for prop in self.SERIALIZE_PROPERTIES: + if prop not in full_result: + full_result[prop] = getattr(self, prop) + if "point_2d_geojson" in full_result: + full_result["point_2d"] = True + if "multi_polygon_geojson" in full_result: + full_result["multi_polygon"] = True + for prop in self.SERIALIZE_DATES: + dt = getattr(self, prop) or "" + if dt: + dt = human_date(dt) + full_result[prop] = dt + for k in self.SERIALIZE_CALL: + full_result[k] = getattr(self, self.SERIALIZE_CALL[k])() + full_result["SLUG"] = self.SLUG + full_result["pk"] = f"external_{self.pk}" + full_result["id"] = f"external_{self.id}" return full_result + def get_associated_main_item_list(self, attr, model) -> list: + items = getattr(self, attr) + if not items.count(): + return [] + lst = [] + table_cols = model.TABLE_COLS + if callable(table_cols): + table_cols = table_cols() + for colname in table_cols: + if colname in model.COL_LABELS: + lst.append(str(model.COL_LABELS[colname])) + else: + lst.append(model._meta.get_field(colname).verbose_name) + lst = [lst] + for values in items.values_list(*table_cols): + lst.append(["-" if v is None else v for v in values]) + return lst + @classmethod def class_verbose_name(cls): return cls._meta.verbose_name |