Commit 93df343d authored by Dylann Cordel's avatar Dylann Cordel

render

parent 22a1cf1f
......@@ -18,5 +18,23 @@ class GraphvizPlugin(CMSPluginBase):
render_template = 'cms_graphviz/plugin.html'
cache = True
def save_model(self, request, obj, form, change):
"""
Given a model instance save it to the database.
"""
mobile_graph_image = form.cleaned_data.get('mobile_graph_image', None)
graph_image = form.cleaned_data.get('graph_image', None)
if mobile_graph_image or graph_image:
if not obj.pk:
obj.save()
pk = obj.pk
if mobile_graph_image:
obj.mobile_graph_image.save(mobile_graph_image.name % pk,
mobile_graph_image)
if graph_image:
obj.graph_image.save(graph_image.name % pk, graph_image, False)
return super(GraphvizPlugin, self).save_model(request, obj, form, change)
plugin_pool.register_plugin(GraphvizPlugin)
......@@ -50,8 +50,8 @@ class GraphvizForm(forms.ModelForm):
display_type=data['display_type'],
style=data['style'],
version=version)
except Exception:
self.add_error(field, _('Your graph seems invalid'))
except Exception as e:
self.add_error(field, _('Your graph seems invalid (%s)') % e)
return
display_type = data['display_type']
......@@ -67,19 +67,3 @@ class GraphvizForm(forms.ModelForm):
'graph-%s-%s.%s' % (version, '%d', fmt.lower()),
mime_type, stringio_obj.len, None)
self.cleaned_data[file_dest] = image_file
def save(self, commit=True):
if commit:
mobile_graph_image = self.cleaned_data.get('mobile_graph_image', None)
graph_image = self.cleaned_data.get('graph_image', None)
if mobile_graph_image or graph_image:
if self.instance.pk:
super(GraphvizForm, self).save(commit)
pk = self.instance.pk
if mobile_graph_image:
self.instance.mobile_graph_image.save(mobile_graph_image.name % pk,
mobile_graph_image)
if graph_image:
self.instance.graph_image.save(graph_image.name % pk, graph_image)
return super(GraphvizForm, self).save(commit)
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
from shutil import copyfile
from tempfile import mkstemp
from django.conf import settings
# from django.core.files import Image
from django.db import models
......@@ -22,7 +26,7 @@ class GraphvizPlugin(CMSPlugin):
DISPLAY_TYPE_CHOICES = (
(DISPLAY_TYPE_DOT, _('Au format DOT')),
(DISPLAY_TYPE_SVG, _('Image SVG')),
(DISPLAY_TYPE_PNG, _('Image PNG en base 64')),
(DISPLAY_TYPE_PNG, _('Image PNG')),
)
class Meta:
......@@ -76,26 +80,69 @@ class GraphvizPlugin(CMSPlugin):
g = AGraph(graph)
via_files = []
stylesheet = None
if style:
style = getattr(settings, 'CMS_GRAPHVIZ_STYLES', {}).get(style, None)
if 'graph_attr' in style:
for attr, value in style['graph_attr'].items():
if attr not in g.graph_attr:
g.graph_attr[attr] = value
if attr == 'stylesheet':
new_value = list(mkstemp(os.path.splitext(value[0])[-1]))
new_value.append(value[1])
via_files.append(new_value)
stylesheet = new_value
copyfile(value[0], new_value[1])
# value = os.path.basename(new_value[1])
value = new_value[1]
g.graph_attr[attr] = value
if 'node_attr' in style:
for attr, value in style['node_attr'].items():
if attr not in g.node_attr:
g.node_attr[attr] = value
g.node_attr[attr] = value
if attr == 'shape' and value and value != 'ellipse':
# fix bug : it adds shape="" which means ellipse...
g = AGraph(g.string().replace('shape=""', 'shape="'+ value +'"'))
if 'nodes' in style:
for node_id, attrs in style['nodes'].items():
if not g.has_node(node_id):
continue
node = g.get_node(node_id)
for attr, value in attrs.items():
if attr not in node.attr:
node.attr[attr] = value
if attr == 'image':
new_value = list(mkstemp(os.path.splitext(value[0])[-1]))
new_value.append(value[1])
via_files.append(new_value)
copyfile(value[0], new_value[1])
# value = os.path.basename(new_value[1])
value = new_value[1]
node.attr[attr] = value
if via_files:
dot_file = list(mkstemp('.dot'))
dot_file.append(None)
via_files.append(dot_file)
with open(dot_file[1], 'w') as f:
f.write(g.string())
g = AGraph(dot_file[1])
if display_type in (cls.DISPLAY_TYPE_PNG, cls.DISPLAY_TYPE_SVG):
return g.draw(prog='dot', format=display_type)
output = g.draw(prog='dot', format=display_type)
else:
output = graph
if via_files:
if display_type == cls.DISPLAY_TYPE_SVG and stylesheet:
from lxml import etree
root = etree.fromstring(str(output))
style = '<style type="text/css"><![CDATA[\n%s]]></style>'
with open(stylesheet[1]) as f:
style = etree.fromstring(style % f.read())
root.insert(0, style)
output = etree.tostring(root)
for fd, path, url in via_files:
if url and display_type == cls.DISPLAY_TYPE_SVG:
output = output.replace(path, url)
os.close(fd)
os.remove(path)
return output
@property
def graph(self):
......
......@@ -3,11 +3,9 @@
{% endif %}
<div class="graphviz">
{% if instance.display_type == instance.DISPLAY_TYPE_SVG %}
{% comment %}<img src="data:image/png;base64,{{ graph }}" alt="{{ instance.name }}" />{% endcomment %}
{{ instance.graph }}
{{ instance.graph.read|safe }}
{% elif instance.display_type == instance.DISPLAY_TYPE_PNG %}
{% comment %}<img src="data:image/png;base64,{{ graph }}" alt="{{ instance.name }}" />{% endcomment %}
{{ instance.graph }}
<img src="{{ instance.graph.url }}" alt="{{ instance.name }}" />
{% else %}
<pre class="dot">{{ instance.graph }}</pre>
{% endif %}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment