Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graph_models style per app #1848

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion django_extensions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
VERSION = (3, 2, 4, 'DEV')
VERSION = (3, 2, 5, 'DEV')


def get_version(version):
Expand Down
31 changes: 30 additions & 1 deletion django_extensions/management/commands/graph_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@
HAS_PYDOT = False


def retheme(graph_data, app_style={}):
if isinstance(app_style, str):
if os.path.exists(app_style):
try:
with open(app_style, 'rt') as f:
app_style = json.load(f)
except Exception as e:
print(f"Invalid app style file {app_style}")
raise Exception(e)
else:
return graph_data

for gc in graph_data["graphs"]:
for g in gc:
if "name" in g:
for m in g["models"]:
app_name = g['app_name']
if app_name in app_style:
m["style"] = app_style[app_name]
return graph_data


class Command(BaseCommand):
help = "Creates a GraphViz dot file for the specified app names. You can pass multiple app names and they will all be combined into a single model. Output is usually directed to a dot file."

Expand All @@ -44,6 +66,12 @@ def __init__(self, *args, **kwargs):
--disable-fields can be set in settings.GRAPH_MODELS['disable_fields'].
"""
self.arguments = {
'--app-style': {
'action': 'store',
'help': 'Path to style json to configure the style per app',
'dest': 'app-style',
'default': ".app-style.json"#dict(),
},
'--pygraphviz': {
'action': 'store_true',
'default': False,
Expand Down Expand Up @@ -106,7 +134,7 @@ def __init__(self, *args, **kwargs):
'--theme -t': {
'action': 'store',
'dest': 'theme',
'default': 'django2018',
'default': 'django2018style',
'help': 'Theme to use. Supplied are \'original\' and \'django2018\'. You can create your own by creating dot templates in \'django_extentions/graph_models/themename/\' template directory.',
},
'--verbose-names -n': {
Expand Down Expand Up @@ -272,6 +300,7 @@ def handle(self, *args, **options):
template_name = os.path.join('django_extensions', 'graph_models', theme, 'digraph.dot')
template = loader.get_template(template_name)

graph_data = retheme(graph_data, app_style=options['app-style'])
dotdata = generate_dot(graph_data, template=template)

if output == "pygraphviz":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% block digraph %}digraph model_graph {
// Dotfile by Django-Extensions graph_models
// Created: {{ created_at }}
{% if cli_options %}// Cli Options: {{ cli_options }}{% endif %}

{% block digraph_options %}fontname = "Roboto"
fontsize = 8
splines = true
rankdir = "{{ rankdir }}"{% endblock %}

node [{% block node_options %}
fontname = "Roboto"
fontsize = 8
shape = "plaintext"
{% endblock %}]

edge [{% block edge_options %}
fontname = "Roboto"
fontsize = 8
{% endblock %}]

// Labels
{% block labels %}{% for graph in graphs %}{% include "django_extensions/graph_models/django2018style/label.dot" %}{% endfor %}{% endblock %}

// Relations
{% block relations %}{% for graph in graphs %}{% include "django_extensions/graph_models/django2018style/relation.dot" %}{% endfor %}{% endblock %}
}{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{% load indent_text %}{% if use_subgraph %} subgraph {{ graph.cluster_app_name }} {
label=<
<TABLE BORDER="0" CELLBORDER="0" CELLSPACING="0">
<TR><TD COLSPAN="2" CELLPADDING="4" ALIGN="CENTER">
<FONT FACE="Roboto" COLOR="Black" POINT-SIZE="10">
<B>{{ graph.app_name }}</B>
</FONT>
</TD></TR>
</TABLE>
>
color=olivedrab4
style="rounded"{% endif %}
{% indentby 2 if use_subgraph %}{% for model in graph.models %}
{{ model.app_name }}_{{ model.name }} [label=<
<TABLE BGCOLOR="white" BORDER="1" CELLBORDER="0" CELLSPACING="0">
<TR><TD COLSPAN="2" CELLPADDING="5" ALIGN="CENTER"
{% if model.style.bg%}
BGCOLOR="{{model.style.bg}}"
{%else%}
BGCOLOR="#1b563f"
{%endif%}
>
<FONT FACE="Roboto" COLOR="white" POINT-SIZE="10"><B>
{{ model.label }}{% if model.abstracts %}<BR/>&lt;<FONT FACE="Roboto"><I>{{ model.abstracts|join:"," }}</I></FONT>&gt;{% endif %}
</B></FONT></TD></TR>
{% if not disable_fields %}{% for field in model.fields %}
{% if disable_abstract_fields and field.abstract %}
{% else %}
<TR><TD ALIGN="LEFT" BORDER="0">
<FONT {% if not field.primary_key and field.blank %}COLOR="#7B7B7B" {% endif %}FACE="Roboto">{% if field.abstract %}<I>{% endif %}{% if field.relation or field.primary_key %}<B>{% endif %}{{ field.label }}{% if field.relation or field.primary_key %}</B>{% endif %}{% if field.abstract %}</I>{% endif %}</FONT>
</TD><TD ALIGN="LEFT">
<FONT {% if not field.primary_key and field.blank %}COLOR="#7B7B7B" {% endif %}FACE="Roboto">{% if field.abstract %}<I>{% endif %}{% if field.relation or field.primary_key %}<B>{% endif %}{{ field.type }}{% if field.relation or field.primary_key %}</B>{% endif %}{% if field.abstract %}</I>{% endif %}</FONT>
</TD></TR>
{% endif %}
{% endfor %}{% endif %}
</TABLE>
>]
{% endfor %}{% endindentby %}
{% if use_subgraph %} }{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% for model in graph.models %}{% for relation in model.relations %}{% if relation.needs_node %} {{ relation.target_app }}_{{ relation.target }} [label=<
<TABLE BGCOLOR="white" BORDER="0" CELLBORDER="0" CELLSPACING="0">
<TR><TD COLSPAN="2" CELLPADDING="4" ALIGN="CENTER" BGCOLOR="#1b563f">
<FONT FACE="Roboto" POINT-SIZE="12" COLOR="white">{{ relation.target }}</FONT>
</TD></TR>
</TABLE>
>]{% endif %}
{{ model.app_name }}_{{ model.name }} -> {{ relation.target_app }}_{{ relation.target }}
[label=" {{ relation.label }}"] {{ relation.arrows }};
{% endfor %}{% endfor %}