diff options
Diffstat (limited to 'ishtar_common/utils.py')
| -rw-r--r-- | ishtar_common/utils.py | 67 | 
1 files changed, 67 insertions, 0 deletions
| diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index f1e2e4b96..c6a4032f0 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -17,7 +17,9 @@  # See the file COPYING for details. +import datetime  from functools import wraps +from itertools import chain  import hashlib  import random @@ -31,6 +33,24 @@ from django.utils.translation import ugettext_lazy as _, ugettext  from django.template.defaultfilters import slugify +class BColors: +    """ +    Bash colors. Don't forget to finish your colored string with ENDC. +    """ +    HEADER = '\033[95m' +    OKBLUE = '\033[94m' +    OKGREEN = '\033[92m' +    WARNING = '\033[93m' +    FAIL = '\033[91m' +    ENDC = '\033[0m' +    BOLD = '\033[1m' +    UNDERLINE = '\033[4m' + + +def get_current_year(): +    return datetime.datetime.now().year + +  def get_cache(cls, extra_args=[]):      cache_key = u"{}-{}-{}".format(          settings.PROJECT_SLUG, cls._meta.app_label, cls.__name__) @@ -222,3 +242,50 @@ def post_save_point(sender, **kwargs):          instance.skip_history_when_saving = True          instance.save()      return + + +def create_slug(model, name, slug_attr='slug', max_length=100): +    base_slug = slugify(name) +    slug = base_slug[:max_length] +    final_slug = None +    idx = 1 +    while not final_slug: +        if slug and not model.objects.filter(**{slug_attr:slug}).exists(): +            final_slug = slug +            break +        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 +    ] + | 
