From 9f8f172e7e18c9d8fffc5d973f269a50a975d34e Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 5 Jan 2017 18:19:06 +0100 Subject: Better management of context record relations tables (refs #3347) --- archaeological_context_records/models.py | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'archaeological_context_records/models.py') diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index a0ba6c379..51a239230 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -431,6 +431,44 @@ class RecordRelations(GeneralRecordRelations, models.Model): post_delete.connect(post_delete_record_relation, sender=RecordRelations) +class RecordRelationView(models.Model): + """ + CREATE VIEW record_relations AS + SELECT DISTINCT right_record_id as id, + right_record_id, + left_record_id, + relation_type_id + FROM archaeological_context_records_recordrelations; + + -- deactivate deletion + CREATE RULE record_relations_del AS ON DELETE TO record_relations + DO INSTEAD DELETE FROM record_relations where id=NULL; + """ + TABLE_COLS = [ + "relation_type", + "right_record__label", "right_record__unit", "right_record__parcel", + "right_record__datings__period", "right_record__description"] + COL_LABELS = { + "relation_type": _(u"Relation type"), + "right_record__label": _(u"ID"), + "right_record__unit": _(u"Unit"), + "right_record__parcel": _(u"Parcel"), + "right_record__description": _(u"Description"), + "right_record__datings__period": _(u"Periods") + } + left_record = models.ForeignKey(ContextRecord, related_name='+') + right_record = models.ForeignKey(ContextRecord, related_name='+') + relation_type = models.ForeignKey(RelationType, related_name='+') + + class Meta: + managed = False + db_table = 'record_relations' + unique_together = ('id', 'right_record') + + def __unicode__(self): + return u"{} \"{}\"".format(self.relation_type, self.right_record) + + class ContextRecordSource(Source): SHOW_URL = 'show-contextrecordsource' MODIFY_URL = 'record_source_modify' -- cgit v1.2.3