diff options
| -rw-r--r-- | archaeological_operations/models.py | 4 | ||||
| -rw-r--r-- | archaeological_warehouse/models.py | 10 | ||||
| -rw-r--r-- | ishtar_common/models.py | 52 | 
3 files changed, 62 insertions, 4 deletions
| diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index d3a839558..a6b6daf71 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -85,7 +85,7 @@ post_save.connect(post_save_cache, sender=ReportState)  post_delete.connect(post_save_cache, sender=ReportState) -class SiteManager(models.Manager): +class SiteManager(models.GeoManager):      def get_by_natural_key(self, txt_idx):          return self.get(reference=txt_idx) @@ -389,7 +389,7 @@ class ClosedItem(object):          return {'date': date, 'user': user} -class OperationManager(models.Manager): +class OperationManager(models.GeoManager):      def get_by_natural_key(self, txt_idx):          return self.get(code_patriarche=txt_idx) diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py index 03397161a..280890c83 100644 --- a/archaeological_warehouse/models.py +++ b/archaeological_warehouse/models.py @@ -30,7 +30,7 @@ from django.utils.translation import ugettext_lazy as _, pgettext_lazy, \  from ishtar_common.data_importer import post_importer_action  from ishtar_common.models import Document, GeneralType, get_external_id, \      LightHistorizedItem, OwnPerms, Address, Person, post_save_cache, \ -    ImageModel, DashboardFormItem +    ImageModel, DashboardFormItem, ExternalIdManager  from ishtar_common.utils import cached_label_changed @@ -69,6 +69,7 @@ class Warehouse(Address, DashboardFormItem, OwnPerms):              activate(language_code)              EXTRA_REQUEST_KEYS[unicode(v[0])] = v[1]              deactivate() +    objects = ExternalIdManager()      name = models.CharField(_(u"Name"), max_length=200)      warehouse_type = models.ForeignKey(WarehouseType, @@ -102,6 +103,9 @@ class Warehouse(Address, DashboardFormItem, OwnPerms):      def __unicode__(self):          return self.name +    def naturel_key(self): +        return (self.external_id, ) +      def _get_base_image_path(self):          return u"{}/{}".format(self.SLUG, self.external_id) @@ -356,6 +360,7 @@ class Container(LightHistorizedItem, ImageModel):              activate(language_code)              EXTRA_REQUEST_KEYS[unicode(v[0])] = v[1]              deactivate() +    objects = ExternalIdManager()      # fields      location = models.ForeignKey( @@ -397,6 +402,9 @@ class Container(LightHistorizedItem, ImageModel):          lbl = u"{} ({})".format(self.reference, self.container_type)          return lbl +    def natural_key(self): +        return (self.external_id, ) +      def _generate_cached_label(self):          items = [self.reference, self.precise_location]          cached_label = u" | ".join(items) diff --git a/ishtar_common/models.py b/ishtar_common/models.py index a73ba8155..c9fc8eec4 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -441,7 +441,7 @@ def post_save_cache(sender, **kwargs):      sender.refresh_cache() -class ExternalIdManager(models.Manager): +class ExternalIdManager(models.GeoManager):      def get_by_natural_key(self, external_id):          return self.get(external_id=external_id) @@ -2604,6 +2604,11 @@ organization_type_pk_lazy = lazy(OrganizationType.get_or_create_pk, unicode)  organization_type_pks_lazy = lazy(OrganizationType.get_or_create_pks, unicode) +class OrganizationManager(models.Manager): +    def get_by_natural_key(self, name, organization_type): +        return self.get(name=name, organization_type__txt_idx=organization_type) + +  class Organization(Address, Merge, OwnPerms, ValueGetter):      TABLE_COLS = ('name', 'organization_type', 'town')      SHOW_URL = 'show-organization' @@ -2628,6 +2633,7 @@ class Organization(Address, Merge, OwnPerms, ValueGetter):              activate(language_code)              EXTRA_REQUEST_KEYS[unicode(v[0])] = v[1]              deactivate() +    objects = OrganizationManager()      # fields      name = models.CharField(_(u"Name"), max_length=500) @@ -2653,6 +2659,8 @@ class Organization(Address, Merge, OwnPerms, ValueGetter):              return self.name          return u"{} - {}".format(self.organization_type,                                   self.town or "") +    def natural_key(self): +        return (self.name, self.organization_type.txt_idx)      def __unicode__(self):          if self.cached_label: @@ -2715,6 +2723,19 @@ post_save.connect(post_save_cache, sender=TitleType)  post_delete.connect(post_save_cache, sender=TitleType) +class PersonManager(models.Manager): +    def get_by_natural_key( +            self, name, surname, attached_to_name, +            attached_to_organization_type): +        q = {"name": name, "surname": surname} +        if attached_to_name: +            q['attached_to__name'] = attached_to_name +        if attached_to_organization_type: +            q['attached_to__organization_type__txt_idx'] = \ +                attached_to_organization_type +        return self.get(**q) + +  class Person(Address, Merge, OwnPerms, ValueGetter):      _prefix = 'person_'      TYPE = ( @@ -2775,6 +2796,7 @@ class Person(Address, Merge, OwnPerms, ValueGetter):              activate(language_code)              EXTRA_REQUEST_KEYS[unicode(v[0])] = v[1]              deactivate() +    objects = PersonManager()      # fields      old_title = models.CharField(_(u"Title"), max_length=100, choices=TYPE, @@ -2811,6 +2833,12 @@ class Person(Address, Merge, OwnPerms, ValueGetter):              ("delete_own_person", u"Can delete own Person"),          ) +    def natural_key(self): +        if not self.attached_to: +            return (self.name, self.surname, '', '') +        return (self.name, self.surname, self.attached_to.name, +                self.attached_to.organization_type.txt_idx) +      @property      def full_title(self):          return u" ".join( @@ -3299,6 +3327,20 @@ post_save.connect(post_save_cache, sender=AuthorType)  post_delete.connect(post_save_cache, sender=AuthorType) +class AuthorManager(models.Manager): +    def get_by_natural_key( +            self, name, surname, attached_to_name, +            attached_to_organization_type, author_type): +        q = {"name": name, "surname": surname, +             "author_type__txt_idx": author_type} +        if attached_to_name: +            q['attached_to__name'] = attached_to_name +        if attached_to_organization_type: +            q['attached_to__organization_type__txt_idx'] = \ +                attached_to_organization_type +        return self.get(**q) + +  class Author(FullSearch):      PARENT_SEARCH_VECTORS = ['person']      SLUG = "author" @@ -3308,6 +3350,7 @@ class Author(FullSearch):      author_type = models.ForeignKey(AuthorType, verbose_name=_(u"Author type"))      cached_label = models.TextField(_(u"Cached name"), null=True, blank=True,                                      db_index=True) +    objects = AuthorManager()      class Meta:          verbose_name = _(u"Author") @@ -3327,6 +3370,9 @@ class Author(FullSearch):          self.save()          return self.cached_label +    def natural_key(self): +        return self.person.natural_key() + (self.author_type.txt_idx, ) +      def _generate_cached_label(self):          return unicode(self.person) + settings.JOINT + \              unicode(self.author_type) @@ -3476,6 +3522,7 @@ class Document(OwnPerms, ImageModel, FullSearch, Imported):              activate(language_code)              EXTRA_REQUEST_KEYS[unicode(v[0])] = v[1]              deactivate() +    objects = ExternalIdManager()      title = models.TextField(_(u"Title"), blank=True, default='')      associated_file = models.FileField( @@ -3543,6 +3590,9 @@ class Document(OwnPerms, ImageModel, FullSearch, Imported):      def __unicode__(self):          return self.title +    def natural_key(self): +        return (self.external_id, ) +      """      @property      def code(self): | 
