summaryrefslogtreecommitdiff
path: root/ishtar_common/utils.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2019-07-09 19:02:09 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2019-07-09 19:02:09 +0200
commit909471a6e58df80fbcb289d8bfcc2a1d6b4db2b6 (patch)
tree2c77235acc68c317b1788ad0285784752876bf5f /ishtar_common/utils.py
parentadcd9fa8abff676b6d9e27fb75b16a1ae1d557b3 (diff)
downloadIshtar-909471a6e58df80fbcb289d8bfcc2a1d6b4db2b6.tar.bz2
Ishtar-909471a6e58df80fbcb289d8bfcc2a1d6b4db2b6.zip
Graph generation: manage link - display raw SVG - scale SVG - manage reopening in a specific tab
Diffstat (limited to 'ishtar_common/utils.py')
-rw-r--r--ishtar_common/utils.py46
1 files changed, 38 insertions, 8 deletions
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py
index eb8414ced..43603b2e1 100644
--- a/ishtar_common/utils.py
+++ b/ishtar_common/utils.py
@@ -36,6 +36,7 @@ import six
import subprocess
import sys
import tempfile
+import xmltodict
import zipfile
from django import forms
@@ -1224,15 +1225,22 @@ def generate_relation_graph(obj, highlight_current=True,
above_relations, equal_relations, styles = get_relations_for_graph(
rel_model, obj.pk, render_above=render_above,
render_bellow=render_bellow, full=full)
- if not above_relations and not equal_relations:
- obj.relation_image = None
- obj.save()
- return
# generate dotfile
dot_str = "digraph relations {\nnode [shape=box];\n"
rel_str = ""
described = []
+ if not above_relations and not equal_relations:
+ rel_str += "subgraph NoDir {\nedge [dir=none,style=dashed];\n"
+ style = 'label="{}"'.format(obj.relation_label)
+ if highlight_current:
+ style += ',style=filled,fillcolor="#C6C0C0"'
+ dot_str += u'item{}[{},href="{}"];\n'.format(
+ obj.pk, style,
+ reverse('display-item',
+ args=[model.SLUG, obj.pk])
+ )
+ rel_str += "}\n"
for list, directed in ((above_relations, True),
(equal_relations, False)):
if directed:
@@ -1246,14 +1254,20 @@ def generate_relation_graph(obj, highlight_current=True,
style = 'label="{}"'.format(left.relation_label)
if left.pk == obj.pk and highlight_current:
style += ',style=filled,fillcolor="#C6C0C0"'
- dot_str += u'item{}[{}];\n'.format(left.pk, style)
+ dot_str += u'item{}[{},href="{}"];\n'.format(
+ left.pk, style,
+ reverse('display-item', args=[model.SLUG, left.pk])
+ )
if right_pk not in described:
described.append(right_pk)
right = model.objects.get(pk=right_pk)
style = 'label="{}"'.format(right.relation_label)
if right.pk == obj.pk and highlight_current:
style += ',style=filled,fillcolor="#C6C0C0"'
- dot_str += u'item{}[{}];\n'.format(right.pk, style)
+ dot_str += u'item{}[{},href="{}"];\n'.format(
+ right.pk, style,
+ reverse('display-item', args=[model.SLUG, right.pk])
+ )
if not directed: # on the same level
rel_str += u"{{rank = same; item{}; item{};}}\n".format(
left_pk, right_pk)
@@ -1270,9 +1284,9 @@ def generate_relation_graph(obj, highlight_current=True,
dot_file.write(dot_str)
if not render_above:
- suffix = "_above"
- elif not render_bellow:
suffix = "_bellow"
+ elif not render_bellow:
+ suffix = "_above"
else:
suffix = ""
@@ -1293,6 +1307,22 @@ def generate_relation_graph(obj, highlight_current=True,
with open(svg_tmp_name, "w") as svg_file:
popen = subprocess.Popen(args, stdout=svg_file)
popen.wait()
+
+ # scale image if necessary
+ with open(svg_tmp_name, "r") as svg_file:
+ doc = xmltodict.parse(svg_file.read())
+ width = doc["svg"]["@width"]
+ if width.endswith("pt"):
+ width = float(width[:-2])
+ if width > 600:
+ doc["svg"].pop("@height")
+ doc["svg"].pop("@width")
+
+ doc["svg"]["@preserveAspectRatio"] = "xMinYMin meet"
+
+ with open(svg_tmp_name, "w") as svg_file:
+ svg_file.write(xmltodict.unparse(doc))
+
with open(svg_tmp_name, "r") as svg_file:
django_file = File(svg_file)
attr = "relation_image" + suffix