summaryrefslogtreecommitdiff
path: root/archaeological_operations/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'archaeological_operations/models.py')
-rw-r--r--archaeological_operations/models.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py
index 60bdd5f2d..7d0efe34c 100644
--- a/archaeological_operations/models.py
+++ b/archaeological_operations/models.py
@@ -104,6 +104,9 @@ class ArchaeologicalSite(BaseHistorizedItem):
blank=True)
towns = models.ManyToManyField(Town, verbose_name=_(u"Towns"),
related_name='sites', blank=True)
+ top_operation = models.ForeignKey("Operation", blank=True, null=True,
+ verbose_name=_(u"Top operation"),
+ on_delete=models.SET_NULL)
class Meta:
verbose_name = _(u"Archaeological site")
@@ -142,6 +145,55 @@ class ArchaeologicalSite(BaseHistorizedItem):
return [u"{} ({})".format(town.name, town.numero_insee) for town in
self.towns.all()]
+ def create_or_update_top_operation(self, create=False):
+ """
+ Create a virtual operation to associate with the site.
+ A cluster operation is created for site with many operation.
+ This cluster operation can be used to attach context records
+ which are only attached to the site.
+ """
+ if not self.top_operation:
+ if not create:
+ return
+ operation_type, created = OperationType.objects.get_or_create(
+ txt_idx='unknown',
+ defaults={'label': _(u"Unknown"), 'available': True,
+ 'order': 999})
+ name = unicode(
+ _(u"Virtual operation of site: {}")
+ ).format(self.reference)
+ if self.towns.count():
+ name += u' - ' + u", ".join([town.name
+ for town in self.towns.all()])
+ self.top_operation = Operation.objects.create(
+ operation_type=operation_type,
+ common_name=name,
+ virtual_operation=True
+ )
+ self.skip_history_when_saving = True
+ self.save()
+ current_operations = dict(
+ [(ope.pk, ope)
+ for ope in self.operations.exclude(pk=self.top_operation.pk).all()
+ ]
+ )
+ q = RecordRelations.objects.filter(
+ left_record=self.top_operation,
+ relation_type__txt_idx='has_got'
+ )
+ for relation in q.all():
+ if relation.right_record.pk not in current_operations:
+ relation.delete()
+ else:
+ current_operations.pop(relation.right_record.pk)
+ rel_type = RelationType.get_cache('has_got')
+ for missing in current_operations:
+ RecordRelations.objects.create(
+ left_record=self.top_operation,
+ right_record=current_operations[missing],
+ relation_type=rel_type
+ )
+
def get_values_town_related(item, prefix, values):
values[prefix + 'parcellist'] = item.render_parcels()