summaryrefslogtreecommitdiff
path: root/ishtar_common/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/utils.py')
-rw-r--r--ishtar_common/utils.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py
index 1d3c4d143..3abc0ddd1 100644
--- a/ishtar_common/utils.py
+++ b/ishtar_common/utils.py
@@ -1026,6 +1026,54 @@ def get_field_labels_from_path(model, path):
return labels
+def get_columns_from_class(cls, table_cols_attr="TABLE_COLS", dict_col_labels=True):
+ """
+ Get table columns and table label from a model
+ :param table_cols_attr: "TABLE_COLS" if not specified
+ :param dict_col_labels: (default: True) if set to False return list matching
+ with table_cols list
+ :return: (table_cols, table_col_labels)
+ """
+ col_labels = {}
+ slug = getattr(cls, "SLUG", None)
+ if hasattr(cls, "COL_LABELS"):
+ col_labels = cls.COL_LABELS
+ if slug in settings.COL_LABELS:
+ col_labels.update(settings.COL_LABELS[slug])
+ tb_key = (slug, table_cols_attr)
+ if tb_key in settings.TABLE_COLS:
+ table_cols = settings.TABLE_COLS[tb_key]
+ else:
+ table_cols = getattr(cls, table_cols_attr)
+ if callable(table_cols):
+ table_cols = table_cols()
+ if dict_col_labels:
+ return table_cols, col_labels
+ table_cols_label = []
+ for c in table_cols:
+ if c in col_labels:
+ table_cols_label.append(str(col_labels[c]))
+ else:
+ field_verbose_name, field_name = "", ""
+ field = cls
+ keys = c.split("__")
+ if "." in c:
+ keys = c.split(".")
+ for key in keys:
+ if hasattr(field, "remote_field") and field.remote_field:
+ field = field.remote_field.model
+ try:
+ field = field._meta.get_field(key)
+ field_verbose_name = field.verbose_name
+ except (fields.FieldDoesNotExist, AttributeError):
+ if hasattr(field, key + "_lbl"):
+ field_verbose_name = getattr(field, key + "_lbl")
+ else:
+ continue
+ table_cols_label.append(str(field_verbose_name))
+ return table_cols, table_cols_label
+
+
def create_default_areas(models=None, verbose=False):
# can be used on migrations if models are provided
if not models: