diff options
Diffstat (limited to 'ishtar_common/utils.py')
-rw-r--r-- | ishtar_common/utils.py | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 9eda2d22f..c6a4032f0 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -17,10 +17,11 @@ # See the file COPYING for details. +import datetime from functools import wraps +from itertools import chain import hashlib import random -import datetime from django import forms from django.conf import settings @@ -255,3 +256,36 @@ def create_slug(model, name, slug_attr='slug', max_length=100): slug = base_slug[:(max_length - 1 - len(str(idx)))] + "-" + str(idx) idx += 1 return final_slug + + +def get_all_field_names(model): + return list(set(chain.from_iterable( + (field.name, field.attname) if hasattr(field, 'attname') else ( + field.name,) + for field in model._meta.get_fields() + if not (field.many_to_one and field.related_model is None) + ))) + + +def get_all_related_m2m_objects_with_model(model): + return [ + (f, f.model if f.model != model else None) + for f in model._meta.get_fields(include_hidden=True) + if f.many_to_many and f.auto_created + ] + + +def get_all_related_many_to_many_objects(model): + return [ + f for f in model._meta.get_fields(include_hidden=True) + if f.many_to_many and f.auto_created + ] + + +def get_all_related_objects(model): + return [ + f for f in model._meta.get_fields() + if (f.one_to_many or f.one_to_one) + and f.auto_created and not f.concrete + ] + |