Files
social-graph/backend/graph/services.py
T
gitrusprusandCursor 4eb4c145b6 Add JWT auth with per-user data isolation and account settings.
Users can register, log in, and manage profile/password in a personal account page; server data is scoped by owner across contacts, maps, tags, and import.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-28 21:10:16 +03:00

113 lines
3.5 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):
return {
'id': contact.id,
'label': contact.name,
'title': '\n'.join(filter(None, [contact.organization, contact.position, contact.email])),
'group': contact.organization or 'default',
}
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 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': ''}
memberships = list(
NetworkMapMembership.objects.filter(map_id=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
]
allowed_ids = {m.contact_id for m in memberships}
nodes = [node_from_membership(m) for m in memberships]
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,
}