From 7d1c05cfd85042f00cd07fbb02d61820e4ffb84a Mon Sep 17 00:00:00 2001 From: James Graham Date: Sun, 25 Apr 2021 11:50:52 +0100 Subject: [PATCH] feat: add organisation relationships to network --- people/serializers.py | 13 +++++++++++++ people/static/js/network.js | 20 ++++++++++++++++++++ people/templates/people/network.html | 2 ++ people/views/network.py | 4 ++++ 4 files changed, 39 insertions(+) diff --git a/people/serializers.py b/people/serializers.py index 2303068..39f3578 100644 --- a/people/serializers.py +++ b/people/serializers.py @@ -36,3 +36,16 @@ class RelationshipSerializer(serializers.ModelSerializer): 'source', 'target', ] + + +class OrganisationRelationshipSerializer(serializers.ModelSerializer): + source = PersonSerializer() + target = OrganisationSerializer() + + class Meta: + model = models.OrganisationRelationship + fields = [ + 'pk', + 'source', + 'target', + ] diff --git a/people/static/js/network.js b/people/static/js/network.js index ea1a243..09714ed 100644 --- a/people/static/js/network.js +++ b/people/static/js/network.js @@ -103,6 +103,26 @@ function get_network() { } } + // Load organisation relationships and add to graph + relationship_set = JSON.parse(document.getElementById('organisation-relationship-set-data').textContent); + + for (var relationship of relationship_set) { + console.log(relationship) + try { + cy.add({ + group: 'edges', + data: { + id: 'organisation-relationship-' + relationship.pk.toString(), + source: 'person-' + relationship.source.pk.toString(), + target: 'organisation-' + relationship.target.pk.toString() + } + }) + } catch { + // Exception thrown if a node in the relationship does not exist + // This is probably because it's been filtered out + } + } + // Optimise graph layout var layout = cy.layout({ name: 'cose', diff --git a/people/templates/people/network.html b/people/templates/people/network.html index 60061c5..77bebd4 100644 --- a/people/templates/people/network.html +++ b/people/templates/people/network.html @@ -77,6 +77,8 @@ {{ relationship_set|json_script:'relationship-set-data' }} + {{ organisation_relationship_set|json_script:'organisation-relationship-set-data' }} + diff --git a/people/views/network.py b/people/views/network.py index cf81d61..74640e0 100644 --- a/people/views/network.py +++ b/people/views/network.py @@ -124,6 +124,10 @@ class NetworkView(LoginRequiredMixin, TemplateView): filter_relationships(all_forms['relationship'], relationship_at_date), many=True).data + context['organisation_relationship_set'] = serializers.OrganisationRelationshipSerializer( + models.OrganisationRelationship.objects.all(), many=True + ).data + logger.info('Found %d distinct relationships matching filters', len(context['relationship_set']))