diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-09-03 18:10:25 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-09-03 18:10:25 +0200 | 
| commit | 2dbe0a1d95c9785ddcd6db3772f2224505fb47b6 (patch) | |
| tree | 2c2c143a4adbf21b5fac1e4521688a0a2bd9fba2 /archaeological_operations/models.py | |
| parent | 2c18fa15a97b59cd4d0065f1fd2629b7c9cb0623 (diff) | |
| download | Ishtar-2dbe0a1d95c9785ddcd6db3772f2224505fb47b6.tar.bz2 Ishtar-2dbe0a1d95c9785ddcd6db3772f2224505fb47b6.zip | |
Serializations: operations, sites, parcels
Diffstat (limited to 'archaeological_operations/models.py')
| -rw-r--r-- | archaeological_operations/models.py | 43 | 
1 files changed, 26 insertions, 17 deletions
| diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index f971981c0..c1f03368b 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -229,7 +229,7 @@ class ArchaeologicalSite(DocumentItem, BaseHistorizedItem, QRCodeItem,          ),          'top_operation': SearchAltName(              pgettext_lazy("key for text search", "top-operation"), -            'top_operation__cached_label__icontains' +            'top_operations__cached_label__icontains'          ),          'drassm_number': SearchAltName(              pgettext_lazy("key for text search", "numero-drassm"), @@ -254,7 +254,7 @@ class ArchaeologicalSite(DocumentItem, BaseHistorizedItem, QRCodeItem,                       'cached_remains']      DOWN_MODEL_UPDATE = ["context_records"] -    # objects = SiteManager() +    objects = SiteManager()      reference = models.CharField(_("Reference"), max_length=200, unique=True)      name = models.CharField(_("Name"), max_length=200, @@ -266,9 +266,6 @@ class ArchaeologicalSite(DocumentItem, BaseHistorizedItem, QRCodeItem,      towns = models.ManyToManyField(Town, verbose_name=_("Towns"),                                     related_name='sites', blank=True)      comment = models.TextField(_("Comment"), null=True, blank=True) -    top_operation = models.ForeignKey("Operation", blank=True, null=True, -                                      verbose_name=_("Top operation"), -                                      on_delete=models.SET_NULL)      locality_ngi = models.TextField(          _("National Geographic Institute locality"),          null=True, blank=True @@ -342,6 +339,12 @@ class ArchaeologicalSite(DocumentItem, BaseHistorizedItem, QRCodeItem,      def short_class_name(self):          return _("SITE") +    @property +    def top_operation(self): +        if self.top_operations.count(): +            return self.top_operations.all()[0] +        return +      def public_representation(self):          dct = super(ArchaeologicalSite, self).public_representation()          dct.update({ @@ -379,16 +382,16 @@ class ArchaeologicalSite(DocumentItem, BaseHistorizedItem, QRCodeItem,              'operations__context_record__base_finds__find__container__location__',              Warehouse._get_query_owns_dicts(ishtaruser)          ) | cls._construct_query_own( -            'top_operation__context_record__base_finds__find__container__responsible__', +            'top_operations__context_record__base_finds__find__container__responsible__',              Warehouse._get_query_owns_dicts(ishtaruser)          ) | cls._construct_query_own( -            'top_operation__context_record__base_finds__find__container__location__', +            'top_operations__context_record__base_finds__find__container__location__',              Warehouse._get_query_owns_dicts(ishtaruser)          ) | cls._construct_query_own(              'operations__', Operation._get_query_owns_dicts(ishtaruser,                                                              no_rel=True)          ) | cls._construct_query_own( -            'top_operation__', Operation._get_query_owns_dicts(ishtaruser) +            'top_operations__', Operation._get_query_owns_dicts(ishtaruser)          ) | cls._construct_query_own('', [              {'history_creator': ishtaruser.user_ptr}          ]) @@ -429,8 +432,8 @@ class ArchaeologicalSite(DocumentItem, BaseHistorizedItem, QRCodeItem,      def _generate_cached_periods(self):          return " & ".join([str(period) for period in self.periods.all()]) or "-" -    # def natural_key(self): -    #     return (self.reference, ) +    def natural_key(self): +        return (self.reference, )      @property      def external_id(self): @@ -466,7 +469,7 @@ class ArchaeologicalSite(DocumentItem, BaseHistorizedItem, QRCodeItem,          This cluster operation can be used to attach context records          which are only attached to the site.          """ -        if not self.top_operation: +        if not self.top_operations.count():              if not create:                  return              operation_type, created = OperationType.objects.get_or_create( @@ -479,20 +482,22 @@ class ArchaeologicalSite(DocumentItem, BaseHistorizedItem, QRCodeItem,              if self.towns.count():                  name += u' - ' + ", ".join([town.name                                               for town in self.towns.all()]) -            self.top_operation = Operation.objects.create( +            operation = Operation.objects.create(                  operation_type=operation_type,                  common_name=name,                  virtual_operation=True              ) -            self.skip_history_when_saving = True -            self.save() +            operation.top_sites.add(self) +        top_operation = self.top_operations.all()[0]          current_operations = dict(              [(ope.pk, ope) -             for ope in self.operations.exclude(pk=self.top_operation.pk).all() +             for ope in self.operations.exclude( +                pk=top_operation.pk +            ).all()               ]          )          q = RecordRelations.objects.filter( -            left_record=self.top_operation, +            left_record=top_operation,              relation_type__txt_idx='has_got'          )          for relation in q.all(): @@ -503,7 +508,7 @@ class ArchaeologicalSite(DocumentItem, BaseHistorizedItem, QRCodeItem,          rel_type = RelationType.get_cache('has_got')          for missing in current_operations:              RecordRelations.objects.create( -                left_record=self.top_operation, +                left_record=top_operation,                  right_record=current_operations[missing],                  relation_type=rel_type              ) @@ -998,6 +1003,10 @@ class Operation(ClosedItem, DocumentItem, BaseHistorizedItem, QRCodeItem,      archaeological_sites = models.ManyToManyField(          ArchaeologicalSite, verbose_name=_("Archaeological sites"),          blank=True, related_name='operations') +    top_sites = models.ManyToManyField( +        ArchaeologicalSite, +        verbose_name=_("Sites for which this operation is top operation"), +        related_name="top_operations", blank=True)      virtual_operation = models.BooleanField(          _("Virtual operation"),          default=False, help_text=_( | 
