1
0
Fork 0
mirror of https://github.com/Findus23/RPGnotes.git synced 2024-09-19 15:43:45 +02:00

more nodes

This commit is contained in:
Lukas Winkler 2022-11-19 22:46:41 +01:00
parent a171427e10
commit ca8e97bd96
Signed by: lukas
GPG key ID: 54DE4D798D244853
5 changed files with 67 additions and 16 deletions

View file

@ -78,3 +78,7 @@ class Character(NameSlugModel, DescriptionModel, AliasModel, HistoryModel):
if self.token_image:
return self.token_image
return self.larger_image
@property
def graphkey(self):
return f"cha{self.pk}"

View file

@ -16,3 +16,7 @@ class Faction(NameSlugModel, DescriptionModel, AliasModel, HistoryModel):
def get_absolute_url(self):
return reverse('factiondetail', args=[self.slug])
@property
def graphkey(self):
return f"fac{self.pk}"

View file

@ -1,8 +1,10 @@
{% extends "tenantbase.jinja" %}
{% block title %}Graph{% endblock %}
{% block content %}
<h1>Graph</h1>
<div id="graph"></div>
<div id="graph"></div>
{% endblock %}
{% block extra_js %}

View file

@ -1,12 +1,41 @@
from typing import Union
from django.db import connection
from django.http import JsonResponse, HttpRequest, HttpResponse
from django.views.generic import TemplateView
from characters.models import Character
from factions.models import Faction
from locations.models import Location
from notes.models import Note
from users.models import TenantUser
GraphModelEl = Union[Location, Note, Character, Faction]
class Graph:
def __init__(self):
self.nodes = []
self.edges = []
def add_node(self, el: GraphModelEl, label: str = None):
if label is None:
label = el.name
self.nodes.append({
"key": el.graphkey,
"attributes": {
"label": label,
"size": 20
}
})
def add_edge(self, source: GraphModelEl, target: GraphModelEl):
self.edges.append({
"source": source.graphkey,
"target": target.graphkey
})
# @dataclass
# class Node:
#
#
# class Graph:
@ -18,21 +47,29 @@ class GraphView(TemplateView):
def get_graph(request: HttpRequest) -> HttpResponse:
nodes = []
edges = []
for loc in list(Location.objects.all())+list(Note.objects.all()):
nodes.append({
"key": loc.graphkey,
"attributes": {"label": loc.name}
})
g = Graph()
for loc in list(Location.objects.all()) + list(Note.objects.all()):
g.add_node(loc)
if loc.parent:
edges.append({
"source": loc.graphkey,
"target": loc.parent.graphkey
})
g.add_edge(loc, loc.parent)
for faction in Faction.objects.all():
g.add_node(faction, faction.name)
for user in TenantUser.objects \
.filter(tenants=connection.get_tenant()) \
.exclude(pk__in=[1, 2]):
g.add_node(user, user.name)
for char in Character.objects.all():
g.add_node(char)
if char.location:
g.add_edge(char, char.location)
if char.faction:
g.add_edge(char, char.faction)
if char.player:
g.add_edge(char, char.player)
return JsonResponse({
"attributes": {},
"nodes": nodes,
"edges": edges
"nodes": g.nodes,
"edges": g.edges
})

View file

@ -34,3 +34,7 @@ class TenantUser(UserProfile):
@property
def username(self):
return self.name
@property
def graphkey(self):
return f"use{self.pk}"