urls.py ----------
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^model_graph/$','<my_project>.<my_app>.views.model_graph'),
)
<my_app>/views.py ----------
from django.http import HttpResponse
from subprocess import Popen,PIPE
from <my_project>.utils.modelviz import generate_dot
def model_graph(request):
options = dict((str(key),val) for key,val in request.GET.iteritems())
if 'app_labels' in options:
app_labels = options['app_labels']
del options['app_labels']
if 'image_type' in options:
image_type = options['image_type'][0]
del options['image_type']
dot = Popen(['dot','-T%s' % image_type],stdin=PIPE,stdout=PIPE)
content = generate_dot(app_labels,**options)
if image_type == 'svg' : image_type = 'svg+xml'
response = HttpResponse(mimetype='image/%s' % image_type)
dot.stdin.write(content)
dot.stdin.close()
response.write(dot.stdout.read())
dot.stdout.close()
return response
Comments