diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-10-28 14:13:31 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-10-28 14:13:31 +0200 | 
| commit | 516a0a55be0a5dd4b3d1849ab5b7e150be1c9d7f (patch) | |
| tree | 2c527257e8df950c22520da165c35935185ece22 /ishtar_common/models.py | |
| parent | 606e7a781722b671f98135f72149f7b290f320bd (diff) | |
| download | Ishtar-516a0a55be0a5dd4b3d1849ab5b7e150be1c9d7f.tar.bz2 Ishtar-516a0a55be0a5dd4b3d1849ab5b7e150be1c9d7f.zip | |
Use lazy model evaluation for fixed type constraints
Diffstat (limited to 'ishtar_common/models.py')
| -rw-r--r-- | ishtar_common/models.py | 54 | 
1 files changed, 54 insertions, 0 deletions
| diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 3453a53c5..814387bb4 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -43,6 +43,7 @@ from django.db.utils import DatabaseError  from django.db.models import Q, Max, Count  from django.db.models.base import ModelBase  from django.db.models.signals import post_save, pre_delete, post_delete +from django.utils.functional import lazy  from django.utils.translation import ugettext_lazy as _, ugettext, \      pgettext_lazy @@ -334,6 +335,51 @@ class GeneralType(Cached, models.Model):          return self.label      @classmethod +    def get_or_create(cls, slug, label=''): +        """ +        Get or create a new item. + +        :param slug: textual id +        :param label: label for initialization if the item doesn't exist (not +        mandatory) + +        :return: instancied item of the base class +        """ + +        item = cls.get_cache(slug) +        if item: +            return item +        item, created = cls.objects.get_or_create( +                txt_idx=slug, defaults={'label': label}) +        return item + +    @classmethod +    def get_or_create_pk(cls, slug): +        """ +        Get an id from a slug. Create the associated item if needed. + +        :param slug: textual id + +        :return: the id of the item +        """ +        return cls.get_or_create(slug).pk + +    @classmethod +    def get_or_create_pks(cls, slugs): +        """ +        Get and merge a list of ids from a slug list. Create the associated +        items if needed. + +        :param slugs: textual ids + +        :return: string with ids separated by "_" +        """ +        items = [] +        for slug in slugs: +            items.append(str(cls.get_or_create(slug).pk)) +        return "_".join(items) + +    @classmethod      def get_help(cls, dct={}, exclude=[]):          help_text = cls.HELP_TEXT          c_rank = -1 @@ -1534,6 +1580,10 @@ class OrganizationType(GeneralType):  post_save.connect(post_save_cache, sender=OrganizationType)  post_delete.connect(post_save_cache, sender=OrganizationType) +organization_type_lazy = lazy(OrganizationType.get_or_create, OrganizationType) +organization_type_pk_lazy = lazy(OrganizationType.get_or_create_pk, int) +organization_type_pks_lazy = lazy(OrganizationType.get_or_create_pks, unicode) +  IMPORTER_CLASSES = {}  IMPORTER_CLASSES.update({ @@ -2324,6 +2374,10 @@ class PersonType(GeneralType):  post_save.connect(post_save_cache, sender=PersonType)  post_delete.connect(post_save_cache, sender=PersonType) +person_type_lazy = lazy(PersonType.get_or_create_pk, PersonType) +person_type_pk_lazy = lazy(PersonType.get_or_create_pk, int) +person_type_pks_lazy = lazy(PersonType.get_or_create_pks, unicode) +  class TitleType(GeneralType):      class Meta: | 
