diff options
Diffstat (limited to 'archaeological_finds/models_finds.py')
| -rw-r--r-- | archaeological_finds/models_finds.py | 101 | 
1 files changed, 78 insertions, 23 deletions
| diff --git a/archaeological_finds/models_finds.py b/archaeological_finds/models_finds.py index c54fd1ed9..d33933264 100644 --- a/archaeological_finds/models_finds.py +++ b/archaeological_finds/models_finds.py @@ -31,13 +31,12 @@ from ishtar_common.utils import cached_label_changed, post_save_point  from ishtar_common.models import GeneralType, ImageModel, BaseHistorizedItem, \      ShortMenuItem, LightHistorizedItem, HistoricalRecords, OwnPerms, Source, \ -    Person, Basket, get_external_id, post_save_cache +    Person, Basket, get_external_id, post_save_cache, ValueGetter  from archaeological_operations.models import AdministrativeAct  from archaeological_context_records.models import ContextRecord, Dating  from ishtar_common.models import PRIVATE_FIELDS, SpatialReferenceSystem -from archaeological_warehouse.models import Container, Collection  class MaterialType(GeneralType): @@ -192,6 +191,7 @@ class BaseFind(BaseHistorizedItem, OwnPerms):          help_text=_(u"Cached value - do not edit"))      history = HistoricalRecords()      RELATED_POST_PROCESS = ['find'] +    CACHED_LABELS = ['cache_short_id', 'cache_complete_id']      class Meta:          verbose_name = _(u"Base find") @@ -254,6 +254,9 @@ class BaseFind(BaseHistorizedItem, OwnPerms):                       ).format(self.index))          return settings.JOINT.join(c_id) +    def _generate_cache_complete_id(self): +        return self.complete_id() +      def short_id(self):          # OPE|FIND_index          c_id = [self._ope_code()] @@ -261,6 +264,9 @@ class BaseFind(BaseHistorizedItem, OwnPerms):                       ).format(self.index))          return settings.JOINT.join(c_id) +    def _generate_cache_short_id(self): +        return self.short_id() +      def full_label(self):          return self._real_label() or self._temp_label() or u"" @@ -323,7 +329,8 @@ class BaseFind(BaseHistorizedItem, OwnPerms):          return returned      @classmethod -    def cached_label_bulk_update(cls, operation_id=None, parcel_id=None): +    def cached_label_bulk_update(cls, operation_id=None, parcel_id=None, +                                 context_record_id=None):          if operation_id:              filters = """              INNER JOIN archaeological_context_records_contextrecord acr @@ -338,6 +345,12 @@ class BaseFind(BaseHistorizedItem, OwnPerms):              """              args = [int(parcel_id)]              kwargs = {'parcel_id': parcel_id} +        elif context_record_id: +            filters = """ +            WHERE mybf.context_record_id = %s +            """ +            args = [int(context_record_id)] +            kwargs = {'context_record_id': context_record_id}          else:              return @@ -477,7 +490,8 @@ class FBulkView(object):      """ -class Find(BaseHistorizedItem, ImageModel, OwnPerms, ShortMenuItem): +class Find(ValueGetter, BaseHistorizedItem, ImageModel, OwnPerms, +           ShortMenuItem):      CHECK_DICT = dict(CHECK_CHOICES)      SHOW_URL = 'show-find'      SLUG = 'find' @@ -623,7 +637,8 @@ class Find(BaseHistorizedItem, ImageModel, OwnPerms, ShortMenuItem):      datings = models.ManyToManyField(Dating, verbose_name=_(u"Dating"),                                       related_name='find')      container = models.ForeignKey( -        Container, verbose_name=_(u"Container"), blank=True, null=True, +        "archaeological_warehouse.Container", verbose_name=_(u"Container"), +        blank=True, null=True,          related_name='finds', on_delete=models.SET_NULL)      is_complete = models.NullBooleanField(_(u"Is complete?"), blank=True,                                            null=True) @@ -657,8 +672,8 @@ class Find(BaseHistorizedItem, ImageModel, OwnPerms, ShortMenuItem):      estimated_value = models.FloatField(_(u"Estimated value"), blank=True,                                          null=True)      collection = models.ForeignKey( -        Collection, verbose_name=_(u"Collection"), blank=True, null=True, -        related_name='finds', on_delete=models.SET_NULL) +        "archaeological_warehouse.Collection", verbose_name=_(u"Collection"), +        blank=True, null=True, related_name='finds', on_delete=models.SET_NULL)      cached_label = models.TextField(_(u"Cached name"), null=True, blank=True)      history = HistoricalRecords()      BASKET_MODEL = FindBasket @@ -847,16 +862,20 @@ class Find(BaseHistorizedItem, ImageModel, OwnPerms, ShortMenuItem):      def duplicate(self, user):          model = self.__class__ -        # base fields -        table_cols = [field.name for field in model._meta.fields -                      if field.name not in PRIVATE_FIELDS or -                      field.name == 'order'] -        dct = dict([(attr, getattr(self, attr)) for attr in -                    table_cols]) -        dct['order'] += 1 -        dct['history_modifier'] = user -        new = self.__class__(**dct) -        new.save() + +        new = model.objects.get(pk=self.pk) + +        for field in model._meta.fields: +            # pk is in PRIVATE_FIELDS so: new.pk = None and a new +            # item will be created on save +            if field.name in PRIVATE_FIELDS: +                setattr(new, field.name, None) +        new.order = self.order + 1 +        new.history_order = user +        new.image.name = self.image.name +        # force_copy is necessary to not regenerate a thumb and resize +        # again the image +        new.save(force_copy=True)          # m2m fields          m2m = [field.name for field in model._meta.many_to_many @@ -868,11 +887,14 @@ class Find(BaseHistorizedItem, ImageModel, OwnPerms, ShortMenuItem):      @classmethod      def get_query_owns(cls, user): -        return Q(base_finds__context_record__operation__scientist=user. -                 ishtaruser.person) | \ -            Q(base_finds__context_record__operation__in_charge=user. -              ishtaruser.person) | \ -            Q(history_creator=user) +        return (Q(base_finds__context_record__operation__scientist=user. +                 ishtaruser.person) | +                Q(base_finds__context_record__operation__in_charge=user. +                  ishtaruser.person) | +                Q(base_finds__context_record__operation__collaborators__pk=user. +                  ishtaruser.person.pk) | +                Q(history_creator=user)) \ +           & Q(base_finds__context_record__operation__end_date__isnull=True)      @classmethod      def get_owns(cls, user, menu_filtr=None, limit=None, @@ -891,7 +913,8 @@ class Find(BaseHistorizedItem, ImageModel, OwnPerms, ShortMenuItem):          return unicode(self)      @classmethod -    def cached_label_bulk_update(cls, operation_id=None, parcel_id=None): +    def cached_label_bulk_update(cls, operation_id=None, parcel_id=None, +                                 context_record_id=None):          if operation_id:              filters = """              INNER JOIN find_first_base_find myfbf @@ -912,6 +935,14 @@ class Find(BaseHistorizedItem, ImageModel, OwnPerms, ShortMenuItem):              ON acr.parcel_id = %s AND acr.id = mybf.context_record_id              """              args = [int(parcel_id)] +        elif context_record_id: +            filters = """ +            INNER JOIN find_first_base_find myfbf +                ON myfbf.find_id = myf.id +            INNER JOIN archaeological_finds_basefind mybf +                ON myfbf.basefind_id = mybf.id AND mybf.context_record_id = %s +            """ +            args = [int(context_record_id)]          else:              return @@ -1068,6 +1099,18 @@ class FindSource(Source):      class Meta:          verbose_name = _(u"Find documentation")          verbose_name_plural = _(u"Find documentations") +        permissions = ( +            ("view_findsource", +             ugettext(u"Can view all Find sources")), +            ("view_own_findsource", +             ugettext(u"Can view own Find source")), +            ("add_own_findsource", +             ugettext(u"Can add own Find source")), +            ("change_own_findsource", +             ugettext(u"Can change own Find source")), +            ("delete_own_findsource", +             ugettext(u"Can delete own Find source")), +        )      find = models.ForeignKey(Find, verbose_name=_(u"Find"),                               related_name="source") @@ -1075,6 +1118,18 @@ class FindSource(Source):      def owner(self):          return self.find +    @classmethod +    def get_query_owns(cls, user): +        return (Q(find__base_finds__context_record__operation__scientist=user. +                  ishtaruser.person) | +                Q(find__base_finds__context_record__operation__in_charge=user. +                  ishtaruser.person) | +                Q( +            find__base_finds__context_record__operation__collaborators__pk=user. +            ishtaruser.person.pk)) \ +               & Q( +            find__base_finds__context_record__operation__end_date__isnull=True) +  class Property(LightHistorizedItem):      find = models.ForeignKey(Find, verbose_name=_(u"Find")) | 
