118 lines
3.6 KiB
Python
118 lines
3.6 KiB
Python
from contacts.models import Contact, Relation, NetworkMap, NetworkMapMembership
|
|
from core.access import use_jwt_auth
|
|
|
|
|
|
def _contacts_qs(user):
|
|
qs = Contact.objects.all()
|
|
if use_jwt_auth():
|
|
if user and user.is_authenticated:
|
|
return qs.filter(owner=user)
|
|
return qs.none()
|
|
return qs
|
|
|
|
|
|
def _relations_qs(user):
|
|
qs = Relation.objects.select_related('source', 'target').all()
|
|
if use_jwt_auth():
|
|
if user and user.is_authenticated:
|
|
return qs.filter(owner=user)
|
|
return qs.none()
|
|
return qs
|
|
|
|
|
|
def _network_maps_qs(user):
|
|
qs = NetworkMap.objects.select_related('map_type').all()
|
|
if use_jwt_auth():
|
|
if user and user.is_authenticated:
|
|
return qs.filter(owner=user)
|
|
return qs.none()
|
|
return qs
|
|
|
|
|
|
def node_from_contact(contact, defaults=None):
|
|
defaults = defaults or {}
|
|
return {
|
|
'id': contact.id,
|
|
'label': contact.name,
|
|
'title': '\n'.join(filter(None, [contact.organization, contact.position, contact.email])),
|
|
'group': contact.organization or 'default',
|
|
**defaults,
|
|
}
|
|
|
|
|
|
def node_from_membership(membership):
|
|
contact = membership.contact
|
|
return node_from_contact(contact, {
|
|
'life_sphere': membership.life_sphere,
|
|
'network_circle': membership.network_circle,
|
|
'importance': membership.importance,
|
|
'conflict_involvement': membership.conflict_involvement,
|
|
'map_angle': membership.map_angle,
|
|
'map_radius_ratio': membership.map_radius_ratio,
|
|
'membership_id': membership.id,
|
|
})
|
|
|
|
|
|
def nodes_for_network_map(network_map, user):
|
|
memberships = list(
|
|
NetworkMapMembership.objects.filter(map_id=network_map.id)
|
|
.select_related('contact', 'map')
|
|
.order_by('contact__name')
|
|
)
|
|
if use_jwt_auth() and user and user.is_authenticated:
|
|
memberships = [
|
|
m for m in memberships
|
|
if m.map.owner_id == user.id and m.contact.owner_id == user.id
|
|
]
|
|
|
|
return [node_from_membership(m) for m in memberships]
|
|
|
|
|
|
def edge_from_relation(relation):
|
|
return {
|
|
'id': relation.id,
|
|
'from': relation.source_id,
|
|
'to': relation.target_id,
|
|
'label': relation.get_relation_type_display(),
|
|
'title': relation.description or relation.get_relation_type_display(),
|
|
'relation_type': relation.relation_type,
|
|
'interaction_intensity': relation.interaction_intensity,
|
|
}
|
|
|
|
|
|
def build_full_graph(user=None):
|
|
contacts = _contacts_qs(user)
|
|
nodes = [node_from_contact(c) for c in contacts]
|
|
relations = _relations_qs(user)
|
|
edges = [edge_from_relation(r) for r in relations]
|
|
return {'nodes': nodes, 'edges': edges}
|
|
|
|
|
|
def build_network_map_graph(map_id=None, user=None):
|
|
maps_qs = _network_maps_qs(user)
|
|
|
|
if not map_id:
|
|
default_map = maps_qs.order_by('id').first()
|
|
if not default_map:
|
|
return {'nodes': [], 'edges': [], 'conflictology': False, 'conflict_subject': ''}
|
|
map_id = default_map.id
|
|
|
|
network_map = maps_qs.filter(pk=map_id).first()
|
|
if not network_map:
|
|
return {'nodes': [], 'edges': [], 'conflictology': False, 'conflict_subject': ''}
|
|
|
|
nodes = nodes_for_network_map(network_map, user)
|
|
allowed_ids = {n['id'] for n in nodes}
|
|
relations = _relations_qs(user)
|
|
edges = [
|
|
edge_from_relation(r)
|
|
for r in relations
|
|
if r.source_id in allowed_ids and r.target_id in allowed_ids
|
|
]
|
|
return {
|
|
'nodes': nodes,
|
|
'edges': edges,
|
|
'conflictology': network_map.map_type.conflictology_enabled,
|
|
'conflict_subject': network_map.conflict_subject or network_map.name,
|
|
}
|