mirror of
https://github.com/Findus23/RPGnotes.git
synced 2024-09-19 15:43:45 +02:00
more nodes
This commit is contained in:
parent
a171427e10
commit
ca8e97bd96
5 changed files with 67 additions and 16 deletions
|
@ -78,3 +78,7 @@ class Character(NameSlugModel, DescriptionModel, AliasModel, HistoryModel):
|
||||||
if self.token_image:
|
if self.token_image:
|
||||||
return self.token_image
|
return self.token_image
|
||||||
return self.larger_image
|
return self.larger_image
|
||||||
|
|
||||||
|
@property
|
||||||
|
def graphkey(self):
|
||||||
|
return f"cha{self.pk}"
|
||||||
|
|
|
@ -16,3 +16,7 @@ class Faction(NameSlugModel, DescriptionModel, AliasModel, HistoryModel):
|
||||||
|
|
||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse('factiondetail', args=[self.slug])
|
return reverse('factiondetail', args=[self.slug])
|
||||||
|
|
||||||
|
@property
|
||||||
|
def graphkey(self):
|
||||||
|
return f"fac{self.pk}"
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
{% extends "tenantbase.jinja" %}
|
{% extends "tenantbase.jinja" %}
|
||||||
|
|
||||||
|
{% block title %}Graph{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>Graph</h1>
|
<h1>Graph</h1>
|
||||||
<div id="graph"></div>
|
<div id="graph"></div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block extra_js %}
|
{% block extra_js %}
|
||||||
|
|
|
@ -1,12 +1,41 @@
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
from django.db import connection
|
||||||
from django.http import JsonResponse, HttpRequest, HttpResponse
|
from django.http import JsonResponse, HttpRequest, HttpResponse
|
||||||
from django.views.generic import TemplateView
|
from django.views.generic import TemplateView
|
||||||
|
|
||||||
|
from characters.models import Character
|
||||||
|
from factions.models import Faction
|
||||||
from locations.models import Location
|
from locations.models import Location
|
||||||
from notes.models import Note
|
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:
|
# class Graph:
|
||||||
|
@ -18,21 +47,29 @@ class GraphView(TemplateView):
|
||||||
|
|
||||||
|
|
||||||
def get_graph(request: HttpRequest) -> HttpResponse:
|
def get_graph(request: HttpRequest) -> HttpResponse:
|
||||||
nodes = []
|
g = Graph()
|
||||||
edges = []
|
for loc in list(Location.objects.all()) + list(Note.objects.all()):
|
||||||
for loc in list(Location.objects.all())+list(Note.objects.all()):
|
g.add_node(loc)
|
||||||
nodes.append({
|
|
||||||
"key": loc.graphkey,
|
|
||||||
"attributes": {"label": loc.name}
|
|
||||||
})
|
|
||||||
if loc.parent:
|
if loc.parent:
|
||||||
edges.append({
|
g.add_edge(loc, loc.parent)
|
||||||
"source": loc.graphkey,
|
for faction in Faction.objects.all():
|
||||||
"target": loc.parent.graphkey
|
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({
|
return JsonResponse({
|
||||||
"attributes": {},
|
"attributes": {},
|
||||||
"nodes": nodes,
|
"nodes": g.nodes,
|
||||||
"edges": edges
|
"edges": g.edges
|
||||||
})
|
})
|
||||||
|
|
|
@ -34,3 +34,7 @@ class TenantUser(UserProfile):
|
||||||
@property
|
@property
|
||||||
def username(self):
|
def username(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def graphkey(self):
|
||||||
|
return f"use{self.pk}"
|
||||||
|
|
Loading…
Reference in a new issue