Commit f95f6b81 authored by Gabriel Detraz's avatar Gabriel Detraz

Merge branch 'foreign_origin' into 'master'

Foreign origin

See merge request nounous/re2o!24
parents f5de6180 566649ab
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
</Directory> </Directory>
Alias /static PATH/static_files Alias /static PATH/static_files
Alias /media PATH/media
WSGIScriptAlias / PATH/re2o/wsgi.py WSGIScriptAlias / PATH/re2o/wsgi.py
WSGIProcessGroup re2o WSGIProcessGroup re2o
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
</Directory> </Directory>
Alias /static PATH/static_files Alias /static PATH/static_files
Alias /media PATH/media
WSGIScriptAlias / PATH/re2o/wsgi.py WSGIScriptAlias / PATH/re2o/wsgi.py
WSGIProcessGroup re2o WSGIProcessGroup re2o
......
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2018-04-09 20:43
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('machines', '0076_auto_20180130_1623'),
]
operations = [
migrations.AlterField(
model_name='extension',
name='origin',
field=models.ForeignKey(blank=True, help_text='Enregistrement A associé à la zone', null=True, on_delete=django.db.models.deletion.PROTECT, to='machines.IpList'),
),
]
...@@ -484,7 +484,7 @@ class Extension(RevMixin, AclMixin, models.Model): ...@@ -484,7 +484,7 @@ class Extension(RevMixin, AclMixin, models.Model):
help_text="Nom de la zone, doit commencer par un point (.example.org)" help_text="Nom de la zone, doit commencer par un point (.example.org)"
) )
need_infra = models.BooleanField(default=False) need_infra = models.BooleanField(default=False)
origin = models.OneToOneField( origin = models.ForeignKey(
'IpList', 'IpList',
on_delete=models.PROTECT, on_delete=models.PROTECT,
blank=True, blank=True,
......
...@@ -175,7 +175,7 @@ class ExtensionSerializer(serializers.ModelSerializer): ...@@ -175,7 +175,7 @@ class ExtensionSerializer(serializers.ModelSerializer):
fields = ('name', 'origin', 'origin_v6', 'zone_entry', 'soa') fields = ('name', 'origin', 'origin_v6', 'zone_entry', 'soa')
def get_origin_ip(self, obj): def get_origin_ip(self, obj):
return obj.origin.ipv4 return getattr(obj.origin, 'ipv4', None)
def get_zone_name(self, obj): def get_zone_name(self, obj):
return str(obj.dns_entry) return str(obj.dns_entry)
......
...@@ -127,6 +127,8 @@ MODEL_NAME = { ...@@ -127,6 +127,8 @@ MODEL_NAME = {
'ConstructorSwitch' : topologie.models.ConstructorSwitch, 'ConstructorSwitch' : topologie.models.ConstructorSwitch,
'Port' : topologie.models.Port, 'Port' : topologie.models.Port,
'Room' : topologie.models.Room, 'Room' : topologie.models.Room,
'Building' : topologie.models.Building,
'SwitchBay' : topologie.models.SwitchBay,
# users # users
'User' : users.models.User, 'User' : users.models.User,
'Adherent' : users.models.Adherent, 'Adherent' : users.models.Adherent,
......
...@@ -220,10 +220,10 @@ class SortTable: ...@@ -220,10 +220,10 @@ class SortTable:
TOPOLOGIE_INDEX = { TOPOLOGIE_INDEX = {
'switch_dns': ['interface__domain__name'], 'switch_dns': ['interface__domain__name'],
'switch_ip': ['interface__ipv4__ipv4'], 'switch_ip': ['interface__ipv4__ipv4'],
'switch_loc': ['location'], 'switch_loc': ['switchbay__name'],
'switch_ports': ['number'], 'switch_ports': ['number'],
'switch_stack': ['stack__name'], 'switch_stack': ['stack__name'],
'default': ['location', 'stack', 'stack_member_id'] 'default': ['switchbay', 'stack', 'stack_member_id']
} }
TOPOLOGIE_INDEX_PORT = { TOPOLOGIE_INDEX_PORT = {
'port_port': ['port'], 'port_port': ['port'],
...@@ -238,6 +238,10 @@ class SortTable: ...@@ -238,6 +238,10 @@ class SortTable:
'room_name': ['name'], 'room_name': ['name'],
'default': ['name'] 'default': ['name']
} }
TOPOLOGIE_INDEX_BUILDING = {
'building_name': ['name'],
'default': ['name']
}
TOPOLOGIE_INDEX_BORNE = { TOPOLOGIE_INDEX_BORNE = {
'ap_name': ['interface__domain__name'], 'ap_name': ['interface__domain__name'],
'ap_ip': ['interface__ipv4__ipv4'], 'ap_ip': ['interface__ipv4__ipv4'],
...@@ -250,12 +254,17 @@ class SortTable: ...@@ -250,12 +254,17 @@ class SortTable:
'default': ['stack_id'], 'default': ['stack_id'],
} }
TOPOLOGIE_INDEX_MODEL_SWITCH = { TOPOLOGIE_INDEX_MODEL_SWITCH = {
'model_switch_name': ['reference'], 'model-switch_name': ['reference'],
'model_switch__contructor' : ['constructor__name'], 'model-switch_contructor' : ['constructor__name'],
'default': ['reference'], 'default': ['reference'],
} }
TOPOLOGIE_INDEX_SWITCH_BAY = {
'switch-bay_name': ['name'],
'switch-bay_building': ['building__name'],
'default': ['name'],
}
TOPOLOGIE_INDEX_CONSTRUCTOR_SWITCH = { TOPOLOGIE_INDEX_CONSTRUCTOR_SWITCH = {
'room_name': ['name'], 'constructor-switch_name': ['name'],
'default': ['name'], 'default': ['name'],
} }
LOGS_INDEX = { LOGS_INDEX = {
......
...@@ -85,6 +85,8 @@ HISTORY_BIND = { ...@@ -85,6 +85,8 @@ HISTORY_BIND = {
'modelswitch' : topologie.models.ModelSwitch, 'modelswitch' : topologie.models.ModelSwitch,
'constructorswitch' : topologie.models.ConstructorSwitch, 'constructorswitch' : topologie.models.ConstructorSwitch,
'accesspoint' : topologie.models.AccessPoint, 'accesspoint' : topologie.models.AccessPoint,
'switchbay' : topologie.models.SwitchBay,
'building' : topologie.models.Building,
}, },
'machines' : { 'machines' : {
'machine' : machines.models.Machine, 'machine' : machines.models.Machine,
......
...@@ -36,7 +36,9 @@ from .models import ( ...@@ -36,7 +36,9 @@ from .models import (
Stack, Stack,
ModelSwitch, ModelSwitch,
ConstructorSwitch, ConstructorSwitch,
AccessPoint AccessPoint,
SwitchBay,
Building
) )
...@@ -75,6 +77,16 @@ class ConstructorSwitchAdmin(VersionAdmin): ...@@ -75,6 +77,16 @@ class ConstructorSwitchAdmin(VersionAdmin):
pass pass
class SwitchBayAdmin(VersionAdmin):
"""Administration d'une baie de brassage"""
pass
class BuildingAdmin(VersionAdmin):
"""Administration d'un batiment"""
pass
admin.site.register(Port, PortAdmin) admin.site.register(Port, PortAdmin)
admin.site.register(AccessPoint, AccessPointAdmin) admin.site.register(AccessPoint, AccessPointAdmin)
admin.site.register(Room, RoomAdmin) admin.site.register(Room, RoomAdmin)
...@@ -82,3 +94,5 @@ admin.site.register(Switch, SwitchAdmin) ...@@ -82,3 +94,5 @@ admin.site.register(Switch, SwitchAdmin)
admin.site.register(Stack, StackAdmin) admin.site.register(Stack, StackAdmin)
admin.site.register(ModelSwitch, ModelSwitchAdmin) admin.site.register(ModelSwitch, ModelSwitchAdmin)
admin.site.register(ConstructorSwitch, ConstructorSwitchAdmin) admin.site.register(ConstructorSwitch, ConstructorSwitchAdmin)
admin.site.register(Building, BuildingAdmin)
admin.site.register(SwitchBay, SwitchBayAdmin)
...@@ -48,7 +48,9 @@ from .models import ( ...@@ -48,7 +48,9 @@ from .models import (
Stack, Stack,
ModelSwitch, ModelSwitch,
ConstructorSwitch, ConstructorSwitch,
AccessPoint AccessPoint,
SwitchBay,
Building,
) )
from re2o.mixins import FormRevMixin from re2o.mixins import FormRevMixin
...@@ -146,7 +148,7 @@ class NewSwitchForm(NewMachineForm): ...@@ -146,7 +148,7 @@ class NewSwitchForm(NewMachineForm):
"""Permet de créer un switch : emplacement, paramètres machine, """Permet de créer un switch : emplacement, paramètres machine,
membre d'un stack (option), nombre de ports (number)""" membre d'un stack (option), nombre de ports (number)"""
class Meta(EditSwitchForm.Meta): class Meta(EditSwitchForm.Meta):
fields = ['name', 'location', 'number', 'stack', 'stack_member_id'] fields = ['name', 'switchbay', 'number', 'stack', 'stack_member_id']
class EditRoomForm(FormRevMixin, ModelForm): class EditRoomForm(FormRevMixin, ModelForm):
...@@ -168,6 +170,8 @@ class CreatePortsForm(forms.Form): ...@@ -168,6 +170,8 @@ class CreatePortsForm(forms.Form):
class EditModelSwitchForm(FormRevMixin, ModelForm): class EditModelSwitchForm(FormRevMixin, ModelForm):
"""Permet d'éediter un modèle de switch : nom et constructeur""" """Permet d'éediter un modèle de switch : nom et constructeur"""
members = forms.ModelMultipleChoiceField(Switch.objects.all(), required=False)
class Meta: class Meta:
model = ModelSwitch model = ModelSwitch
fields = '__all__' fields = '__all__'
...@@ -175,6 +179,14 @@ class EditModelSwitchForm(FormRevMixin, ModelForm): ...@@ -175,6 +179,14 @@ class EditModelSwitchForm(FormRevMixin, ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__) prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(EditModelSwitchForm, self).__init__(*args, prefix=prefix, **kwargs) super(EditModelSwitchForm, self).__init__(*args, prefix=prefix, **kwargs)
instance = kwargs.get('instance', None)
if instance:
self.initial['members'] = Switch.objects.filter(model=instance)
def save(self, commit=True):
instance = super().save(commit)
instance.switch_set = self.cleaned_data['members']
return instance
class EditConstructorSwitchForm(FormRevMixin, ModelForm): class EditConstructorSwitchForm(FormRevMixin, ModelForm):
...@@ -186,3 +198,35 @@ class EditConstructorSwitchForm(FormRevMixin, ModelForm): ...@@ -186,3 +198,35 @@ class EditConstructorSwitchForm(FormRevMixin, ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__) prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(EditConstructorSwitchForm, self).__init__(*args, prefix=prefix, **kwargs) super(EditConstructorSwitchForm, self).__init__(*args, prefix=prefix, **kwargs)
class EditSwitchBayForm(FormRevMixin, ModelForm):
"""Permet d'éditer une baie de brassage"""
members = forms.ModelMultipleChoiceField(Switch.objects.all(), required=False)
class Meta:
model = SwitchBay
fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(EditSwitchBayForm, self).__init__(*args, prefix=prefix, **kwargs)
instance = kwargs.get('instance', None)
if instance:
self.initial['members'] = Switch.objects.filter(switchbay=instance)
def save(self, commit=True):
instance = super().save(commit)
instance.switch_set = self.cleaned_data['members']
return instance
class EditBuildingForm(FormRevMixin, ModelForm):
"""Permet d'éditer le batiment"""
class Meta:
model = Building
fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(EditBuildingForm, self).__init__(*args, prefix=prefix, **kwargs)
# -*- coding: utf-8 -*-
import sys
import json
import six
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from topologie.management.modelviz import ModelGraph, generate_dot
from django_extensions.management.utils import signalcommand
try:
import pygraphviz
HAS_PYGRAPHVIZ = True
except ImportError:
HAS_PYGRAPHVIZ = False
try:
try:
import pydotplus as pydot
except ImportError:
import pydot
HAS_PYDOT = True
except ImportError:
HAS_PYDOT = False
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."
can_import_settings = True
def __init__(self, *args, **kwargs):
"""Allow defaults for arguments to be set in settings.GRAPH_MODELS.
Each argument in self.arguments is a dict where the key is the
space-separated args and the value is our kwarg dict.
The default from settings is keyed as the long arg name with '--'
removed and any '-' replaced by '_'.
"""
self.arguments = {
'--pygraphviz': {
'action': 'store_true', 'dest': 'pygraphviz',
'help': 'Use PyGraphViz to generate the image.'},
'--pydot': {'action': 'store_true', 'dest': 'pydot',
'help': 'Use PyDot(Plus) to generate the image.'},
'--disable-fields -d': {
'action': 'store_true', 'dest': 'disable_fields',
'help': 'Do not show the class member fields'},
'--group-models -g': {
'action': 'store_true', 'dest': 'group_models',
'help': 'Group models together respective to their '
'application'},
'--all-applications -a': {
'action': 'store_true', 'dest': 'all_applications',
'help': 'Automatically include all applications from '
'INSTALLED_APPS'},
'--output -o': {
'action': 'store', 'dest': 'outputfile',
'help': 'Render output file. Type of output dependend on file '
'extensions. Use png or jpg to render graph to image.'},
'--layout -l': {
'action': 'store', 'dest': 'layout', 'default': 'dot',
'help': 'Layout to be used by GraphViz for visualization. '
'Layouts: circo dot fdp neato nop nop1 nop2 twopi'},
'--verbose-names -n': {
'action': 'store_true', 'dest': 'verbose_names',
'help': 'Use verbose_name of models and fields'},
'--language -L': {
'action': 'store', 'dest': 'language',
'help': 'Specify language used for verbose_name localization'},
'--exclude-columns -x': {
'action': 'store', 'dest': 'exclude_columns',
'help': 'Exclude specific column(s) from the graph. '
'Can also load exclude list from file.'},
'--exclude-models -X': {
'action': 'store', 'dest': 'exclude_models',
'help': 'Exclude specific model(s) from the graph. Can also '
'load exclude list from file. Wildcards (*) are allowed.'},
'--include-models -I': {
'action': 'store', 'dest': 'include_models',
'help': 'Restrict the graph to specified models. Wildcards '
'(*) are allowed.'},
'--inheritance -e': {
'action': 'store_true', 'dest': 'inheritance', 'default': True,
'help': 'Include inheritance arrows (default)'},
'--no-inheritance -E': {
'action': 'store_false', 'dest': 'inheritance',
'help': 'Do not include inheritance arrows'},
'--hide-relations-from-fields -R': {
'action': 'store_false', 'dest': 'relations_as_fields',
'default': True,
'help': 'Do not show relations as fields in the graph.'},
'--disable-sort-fields -S': {
'action': 'store_false', 'dest': 'sort_fields',
'default': True, 'help': 'Do not sort fields'},
'--json': {'action': 'store_true', 'dest': 'json',
'help': 'Output graph data as JSON'}
}
defaults = getattr(settings, 'GRAPH_MODELS', None)
if defaults:
for argument in self.arguments:
arg_split = argument.split(' ')
setting_opt = arg_split[0].lstrip('-').replace('-', '_')
if setting_opt in defaults:
self.arguments[argument]['default'] = defaults[setting_opt]
super(Command, self).__init__(*args, **kwargs)
def add_arguments(self, parser):
"""Unpack self.arguments for parser.add_arguments."""
parser.add_argument('app_label', nargs='*')
for argument in self.arguments:
parser.add_argument(*argument.split(' '),
**self.arguments[argument])
@signalcommand
def handle(self, *args, **options):
args = options['app_label']
if len(args) < 1 and not options['all_applications']:
raise CommandError("need one or more arguments for appname")
use_pygraphviz = options.get('pygraphviz', False)
use_pydot = options.get('pydot', False)
use_json = options.get('json', False)
if use_json and (use_pydot or use_pygraphviz):
raise CommandError("Cannot specify --json with --pydot or --pygraphviz")
cli_options = ' '.join(sys.argv[2:])
graph_models = ModelGraph(args, cli_options=cli_options, **options)
graph_models.generate_graph_data()
graph_data = graph_models.get_graph_data(as_json=use_json)
if use_json:
self.render_output_json(graph_data, **options)
return
dotdata = generate_dot(graph_data)
if not six.PY3:
dotdata = dotdata.encode('utf-8')
if options['outputfile']:
if not use_pygraphviz and not use_pydot:
if HAS_PYGRAPHVIZ:
use_pygraphviz = True
elif HAS_PYDOT:
use_pydot = True
if use_pygraphviz:
self.render_output_pygraphviz(dotdata, **options)
elif use_pydot:
self.render_output_pydot(dotdata, **options)
else:
raise CommandError("Neither pygraphviz nor pydotplus could be found to generate the image")
else:
self.print_output(dotdata)
def print_output(self, dotdata):
if six.PY3 and isinstance(dotdata, six.binary_type):
dotdata = dotdata.decode()
print(dotdata)
def render_output_json(self, graph_data, **kwargs):
output_file = kwargs.get('outputfile')
if output_file:
with open(output_file, 'wt') as json_output_f:
json.dump(graph_data, json_output_f)
else:
print(json.dumps(graph_data))
def render_output_pygraphviz(self, dotdata, **kwargs):
"""Renders the image using pygraphviz"""
if not HAS_PYGRAPHVIZ:
raise CommandError("You need to install pygraphviz python module")
version = pygraphviz.__version__.rstrip("-svn")
try:
if tuple(int(v) for v in version.split('.')) < (0, 36):
# HACK around old/broken AGraph before version 0.36 (ubuntu ships with this old version)
import tempfile
tmpfile = tempfile.NamedTemporaryFile()
tmpfile.write(dotdata)
tmpfile.seek(0)
dotdata = tmpfile.name
except ValueError:
pass
graph = pygraphviz.AGraph(dotdata)
graph.layout(prog=kwargs['layout'])
graph.draw(kwargs['outputfile'])
def render_output_pydot(self, dotdata, **kwargs):
"""Renders the image using pydot"""
if not HAS_PYDOT:
raise CommandError("You need to install pydot python module")
graph = pydot.graph_from_dot_data(dotdata)
if not graph:
raise CommandError("pydot returned an error")
if isinstance(graph, (list, tuple)):
if len(graph) > 1:
sys.stderr.write("Found more then one graph, rendering only the first one.\n")
graph = graph[0]
output_file = kwargs['outputfile']
formats = ['bmp', 'canon', 'cmap', 'cmapx', 'cmapx_np', 'dot', 'dia', 'emf',
'em', 'fplus', 'eps', 'fig', 'gd', 'gd2', 'gif', 'gv', 'imap',
'imap_np', 'ismap', 'jpe', 'jpeg', 'jpg', 'metafile', 'pdf',
'pic', 'plain', 'plain-ext', 'png', 'pov', 'ps', 'ps2', 'svg',
'svgz', 'tif', 'tiff', 'tk', 'vml', 'vmlz', 'vrml', 'wbmp', 'xdot']
ext = output_file[output_file.rfind('.') + 1:]
format = ext if ext in formats else 'raw'
graph.write(output_file, format=format)
# -*- coding: utf-8 -*-
"""
modelviz.py - DOT file generator for Django Models
Based on:
Django model to DOT (Graphviz) converter
by Antonio Cavedoni <antonio@cavedoni.org>
Adapted to be used with django-extensions
"""
import datetime
import os
import re
import six
from django.apps import apps
from django.db.models.fields.related import (
ForeignKey, ManyToManyField, OneToOneField, RelatedField,
)
from django.contrib.contenttypes.fields import GenericRelation
from django.template import Context, Template, loader
from django.utils.encoding import force_bytes, force_str
from django.utils.safestring import mark_safe
from django.utils.translation import activate as activate_language
__version__ = "1.1"
__license__ = "Python"
__author__ = "Bas van Oostveen <v.oostveen@gmail.com>",
__contributors__ = [
"Antonio Cavedoni <http://cavedoni.com/>"
"Stefano J. Attardi <http://attardi.org/>",
"limodou <http://www.donews.net/limodou/>",
"Carlo C8E Miron",
"Andre Campos <cahenan@gmail.com>",
"Justin Findlay <jfindlay@gmail.com>",
"Alexander Houben <alexander@houben.ch>",
"Joern Hees <gitdev@joernhees.de>",
"Kevin Cherepski <cherepski@gmail.com>",
"Jose Tomas Tocino <theom3ga@gmail.com>",
"Adam Dobrawy <naczelnik@jawnosc.tk>",
"Mikkel Munch Mortensen <https://www.detfalskested.dk/>",
"Andrzej Bistram <andrzej.bistram@gmail.com>",
]
def parse_file_or_list(arg):
if not arg:
return []
if isinstance(arg, (list, tuple, set)):
return arg
if ',' not in arg and os.path.isfile(arg):
return [e.strip() for e in open(arg).readlines()]
return [e.strip() for e in arg.split(',')]
class ModelGraph(object):
def __init__(self, app_labels, **kwargs):
self.graphs = []
self.cli_options = kwargs.get('cli_options', None)
self.disable_fields = kwargs.get('disable_fields', False)
self.include_models = parse_file_or_list(
kwargs.get('include_models', "")
)
self.all_applications = kwargs.get('all_applications', False)
self.use_subgraph = kwargs.get('group_models', False)
self.verbose_names = kwargs.get('verbose_names', False)
self.inheritance = kwargs.get('inheritance', True)
self.relations_as_fields = kwargs.get("relations_as_fields", True)
self.sort_fields = kwargs.get("sort_fields", True)
self.language = kwargs.get('language', None)
if self.language is not None:
activate_language(self.language)
self.exclude_columns = parse_file_or_list(
kwargs.get('exclude_columns', "")
)
self.exclude_models = parse_file_or_list(
kwargs.get('exclude_models', "")
)
if self.all_applications:
self.app_labels = [app.label for app in apps.get_app_configs()]
else:
self.app_labels = app_labels
def generate_graph_data(self):
self.process_apps()
nodes = []
for graph in self.graphs:
nodes.extend([e['name'] for e in graph['models']])
for graph in self.graphs:
for model in graph['models']:
for relation in model['relations']:
if relation is not None:
if relation['target'] in nodes:
relation['needs_node'] = False
def get_graph_data(self, as_json=False):
now = datetime.datetime.now()
graph_data = {
'created_at': now.strftime("%Y-%m-%d %H:%M"),
'cli_options': self.cli_options,
'disable_fields': self.disable_fields,
'use_subgraph': self.use_subgraph,
}
if as_json:
graph_data['graphs'] = [context.flatten() for context in self.graphs]
else:
graph_data['graphs'] = self.graphs
return graph_data
def add_attributes(self, field, abstract_fields):
if self.verbose_names and field.verbose_name:
label = force_bytes(field.verbose_name)
if label.islower():
label = label.capitalize()
else:
label = field.name
t = type(field).__name__
if isinstance(field, (OneToOneField, ForeignKey)):
remote_field = field.remote_field if hasattr(field, 'remote_field') else field.rel # Remove me after Django 1.8 is unsupported
t += " ({0})".format(remote_field.field_name)
# TODO: ManyToManyField, GenericRelation
return {
'name': field.name,
'label': label,
'type': t,
'blank': field.blank,
'abstract': field in abstract_fields,
'relation': isinstance(field, RelatedField),
'primary_key': field.primary_key,
}
def add_relation(self, field, model, extras=""):
if self.verbose_names and field.verbose_name:
label = force_bytes(field.verbose_name)
if label.islower():
label = label.capitalize()
else:
label = field.name
# show related field name
if hasattr(field, 'related_query_name'):
related_query_name = field.related_query_name()
if self.verbose_names and related_query_name.islower():
related_query_name = related_query_name.replace('_', ' ').capitalize()
label = '{} ({})'.format(label, force_str(related_query_name))
# handle self-relationships and lazy-relationships
remote_field = field.remote_field if hasattr(field, 'remote_field') else field.rel # Remove me after Django 1.8 is unsupported
remote_field_model = remote_field.model if hasattr(remote_field, 'model') else remote_field.to # Remove me after Django 1.8 is unsupported
if isinstance(remote_field_model, six.string_types):
if remote_field_model == 'self':
target_model = field.model
else:
if '.' in remote_field_model:
app_label, model_name = remote_field_model.split('.', 1)
else:
app_label = field.model._meta.app_label
model_name = remote_field_model
target_model = apps.get_model(app_label, model_name)
else:
target_model = remote_field_model
_rel = self.get_relation_context(target_model, field, label, extras)
if _rel not in model['relations'] and self.use_model(_rel['target']):
return _rel