2011-07-13 17:35:51 +08:00
|
|
|
import inspect
|
|
|
|
import os
|
2015-01-28 20:35:27 +08:00
|
|
|
from importlib import import_module
|
2011-07-13 17:35:51 +08:00
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.conf import settings
|
2013-11-05 18:49:16 +08:00
|
|
|
from django.contrib import admin
|
2005-10-18 13:12:45 +08:00
|
|
|
from django.contrib.admin.views.decorators import staff_member_required
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.contrib.admindocs import utils
|
2016-05-30 23:14:00 +08:00
|
|
|
from django.contrib.admindocs.utils import (
|
|
|
|
replace_named_groups, replace_unnamed_groups,
|
|
|
|
)
|
2015-01-12 03:16:28 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db import models
|
2008-07-22 11:24:09 +08:00
|
|
|
from django.http import Http404
|
2014-11-23 04:43:13 +08:00
|
|
|
from django.template.engine import Engine
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import get_mod_func, get_resolver, get_urlconf, reverse
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.decorators import method_decorator
|
2014-10-31 03:45:01 +08:00
|
|
|
from django.utils.inspect import (
|
|
|
|
func_accepts_kwargs, func_accepts_var_args, func_has_no_args,
|
|
|
|
get_func_full_args,
|
|
|
|
)
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import gettext as _
|
2013-11-21 15:48:29 +08:00
|
|
|
from django.views.generic import TemplateView
|
2005-07-16 01:09:01 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
# Exclude methods starting with these strings from documentation
|
|
|
|
MODEL_METHODS_EXCLUDE = ('_', 'add_', 'delete', 'save', 'set_')
|
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2013-11-21 15:48:29 +08:00
|
|
|
class BaseAdminDocsView(TemplateView):
|
|
|
|
"""
|
|
|
|
Base view for admindocs views.
|
|
|
|
"""
|
|
|
|
@method_decorator(staff_member_required)
|
2015-01-01 05:25:00 +08:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2013-11-21 15:48:29 +08:00
|
|
|
if not utils.docutils_is_available:
|
|
|
|
# Display an error message for people without docutils
|
|
|
|
self.template_name = 'admin_doc/missing_docutils.html'
|
2015-01-01 05:25:00 +08:00
|
|
|
return self.render_to_response(admin.site.each_context(request))
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|
2013-11-21 15:48:29 +08:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2015-12-30 23:51:16 +08:00
|
|
|
kwargs.update({'root_path': reverse('admin:index')})
|
2015-01-01 05:25:00 +08:00
|
|
|
kwargs.update(admin.site.each_context(self.request))
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_context_data(**kwargs)
|
2013-11-21 15:48:29 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-21 15:48:29 +08:00
|
|
|
class BookmarkletsView(BaseAdminDocsView):
|
|
|
|
template_name = 'admin_doc/bookmarklets.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
context = super().get_context_data(**kwargs)
|
2013-11-21 15:48:29 +08:00
|
|
|
context.update({
|
|
|
|
'admin_url': "%s://%s%s" % (
|
|
|
|
self.request.scheme, self.request.get_host(), context['root_path'])
|
2009-07-01 02:40:29 +08:00
|
|
|
})
|
2013-11-21 15:48:29 +08:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class TemplateTagIndexView(BaseAdminDocsView):
|
|
|
|
template_name = 'admin_doc/template_tag_index.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
tags = []
|
2015-05-09 04:10:36 +08:00
|
|
|
try:
|
|
|
|
engine = Engine.get_default()
|
|
|
|
except ImproperlyConfigured:
|
|
|
|
# Non-trivial TEMPLATES settings aren't supported (#24125).
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
app_libs = sorted(engine.template_libraries.items())
|
|
|
|
builtin_libs = [('', lib) for lib in engine.template_builtins]
|
|
|
|
for module_name, library in builtin_libs + app_libs:
|
|
|
|
for tag_name, tag_func in library.tags.items():
|
|
|
|
title, body, metadata = utils.parse_docstring(tag_func.__doc__)
|
|
|
|
if title:
|
|
|
|
title = utils.parse_rst(title, 'tag', _('tag:') + tag_name)
|
|
|
|
if body:
|
|
|
|
body = utils.parse_rst(body, 'tag', _('tag:') + tag_name)
|
|
|
|
for key in metadata:
|
|
|
|
metadata[key] = utils.parse_rst(metadata[key], 'tag', _('tag:') + tag_name)
|
2013-11-21 15:48:29 +08:00
|
|
|
tag_library = module_name.split('.')[-1]
|
2015-05-09 04:10:36 +08:00
|
|
|
tags.append({
|
|
|
|
'name': tag_name,
|
|
|
|
'title': title,
|
|
|
|
'body': body,
|
|
|
|
'meta': metadata,
|
|
|
|
'library': tag_library,
|
|
|
|
})
|
2013-11-21 15:48:29 +08:00
|
|
|
kwargs.update({'tags': tags})
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_context_data(**kwargs)
|
2013-11-21 15:48:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TemplateFilterIndexView(BaseAdminDocsView):
|
|
|
|
template_name = 'admin_doc/template_filter_index.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
filters = []
|
2015-05-09 04:10:36 +08:00
|
|
|
try:
|
|
|
|
engine = Engine.get_default()
|
|
|
|
except ImproperlyConfigured:
|
|
|
|
# Non-trivial TEMPLATES settings aren't supported (#24125).
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
app_libs = sorted(engine.template_libraries.items())
|
|
|
|
builtin_libs = [('', lib) for lib in engine.template_builtins]
|
|
|
|
for module_name, library in builtin_libs + app_libs:
|
|
|
|
for filter_name, filter_func in library.filters.items():
|
|
|
|
title, body, metadata = utils.parse_docstring(filter_func.__doc__)
|
|
|
|
if title:
|
|
|
|
title = utils.parse_rst(title, 'filter', _('filter:') + filter_name)
|
|
|
|
if body:
|
|
|
|
body = utils.parse_rst(body, 'filter', _('filter:') + filter_name)
|
|
|
|
for key in metadata:
|
|
|
|
metadata[key] = utils.parse_rst(metadata[key], 'filter', _('filter:') + filter_name)
|
2013-11-21 15:48:29 +08:00
|
|
|
tag_library = module_name.split('.')[-1]
|
2015-05-09 04:10:36 +08:00
|
|
|
filters.append({
|
|
|
|
'name': filter_name,
|
|
|
|
'title': title,
|
|
|
|
'body': body,
|
|
|
|
'meta': metadata,
|
|
|
|
'library': tag_library,
|
|
|
|
})
|
2013-11-21 15:48:29 +08:00
|
|
|
kwargs.update({'filters': filters})
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_context_data(**kwargs)
|
2013-11-21 15:48:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ViewIndexView(BaseAdminDocsView):
|
|
|
|
template_name = 'admin_doc/view_index.html'
|
|
|
|
|
2016-08-13 02:07:24 +08:00
|
|
|
@staticmethod
|
|
|
|
def _get_full_name(func):
|
|
|
|
mod_name = func.__module__
|
2016-12-01 18:38:01 +08:00
|
|
|
return '%s.%s' % (mod_name, func.__qualname__)
|
2016-08-13 02:07:24 +08:00
|
|
|
|
2013-11-21 15:48:29 +08:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
views = []
|
2013-11-05 17:16:27 +08:00
|
|
|
urlconf = import_module(settings.ROOT_URLCONF)
|
|
|
|
view_functions = extract_views_from_urlpatterns(urlconf.urlpatterns)
|
|
|
|
for (func, regex, namespace, name) in view_functions:
|
|
|
|
views.append({
|
2016-08-13 02:07:24 +08:00
|
|
|
'full_name': self._get_full_name(func),
|
2013-11-05 17:16:27 +08:00
|
|
|
'url': simplify_regex(regex),
|
|
|
|
'url_name': ':'.join((namespace or []) + (name and [name] or [])),
|
|
|
|
'namespace': ':'.join((namespace or [])),
|
|
|
|
'name': name,
|
|
|
|
})
|
2013-11-21 15:48:29 +08:00
|
|
|
kwargs.update({'views': views})
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_context_data(**kwargs)
|
2013-11-21 15:48:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ViewDetailView(BaseAdminDocsView):
|
|
|
|
template_name = 'admin_doc/view_detail.html'
|
|
|
|
|
2016-08-13 02:07:24 +08:00
|
|
|
@staticmethod
|
|
|
|
def _get_view_func(view):
|
2015-12-30 23:51:16 +08:00
|
|
|
urlconf = get_urlconf()
|
|
|
|
if get_resolver(urlconf)._is_callback(view):
|
|
|
|
mod, func = get_mod_func(view)
|
2016-08-13 02:07:24 +08:00
|
|
|
try:
|
|
|
|
# Separate the module and function, e.g.
|
|
|
|
# 'mymodule.views.myview' -> 'mymodule.views', 'myview').
|
|
|
|
return getattr(import_module(mod), func)
|
|
|
|
except ImportError:
|
|
|
|
# Import may fail because view contains a class name, e.g.
|
|
|
|
# 'mymodule.views.ViewContainer.my_view', so mod takes the form
|
|
|
|
# 'mymodule.views.ViewContainer'. Parse it again to separate
|
|
|
|
# the module and class.
|
|
|
|
mod, klass = get_mod_func(mod)
|
|
|
|
return getattr(getattr(import_module(mod), klass), func)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
view = self.kwargs['view']
|
|
|
|
view_func = self._get_view_func(view)
|
|
|
|
if view_func is None:
|
2013-11-21 15:48:29 +08:00
|
|
|
raise Http404
|
|
|
|
title, body, metadata = utils.parse_docstring(view_func.__doc__)
|
|
|
|
if title:
|
|
|
|
title = utils.parse_rst(title, 'view', _('view:') + view)
|
|
|
|
if body:
|
|
|
|
body = utils.parse_rst(body, 'view', _('view:') + view)
|
|
|
|
for key in metadata:
|
|
|
|
metadata[key] = utils.parse_rst(metadata[key], 'model', _('view:') + view)
|
|
|
|
kwargs.update({
|
|
|
|
'name': view,
|
|
|
|
'summary': title,
|
|
|
|
'body': body,
|
|
|
|
'meta': metadata,
|
2009-07-01 02:40:29 +08:00
|
|
|
})
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_context_data(**kwargs)
|
2009-07-01 02:40:29 +08:00
|
|
|
|
2013-11-21 15:48:29 +08:00
|
|
|
|
|
|
|
class ModelIndexView(BaseAdminDocsView):
|
|
|
|
template_name = 'admin_doc/model_index.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2013-12-24 19:25:17 +08:00
|
|
|
m_list = [m._meta for m in apps.get_models()]
|
2013-11-21 15:48:29 +08:00
|
|
|
kwargs.update({'models': m_list})
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_context_data(**kwargs)
|
2013-11-21 15:48:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ModelDetailView(BaseAdminDocsView):
|
|
|
|
template_name = 'admin_doc/model_detail.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2014-11-04 03:16:28 +08:00
|
|
|
model_name = self.kwargs['model_name']
|
2013-11-21 15:48:29 +08:00
|
|
|
# Get the model class.
|
|
|
|
try:
|
2014-01-26 19:57:08 +08:00
|
|
|
app_config = apps.get_app_config(self.kwargs['app_label'])
|
2013-12-14 18:11:52 +08:00
|
|
|
except LookupError:
|
|
|
|
raise Http404(_("App %(app_label)r not found") % self.kwargs)
|
2013-12-28 21:55:54 +08:00
|
|
|
try:
|
2014-11-04 03:16:28 +08:00
|
|
|
model = app_config.get_model(model_name)
|
2013-12-28 21:55:54 +08:00
|
|
|
except LookupError:
|
2013-12-14 18:11:52 +08:00
|
|
|
raise Http404(_("Model %(model_name)r not found in app %(app_label)r") % self.kwargs)
|
2013-11-21 15:48:29 +08:00
|
|
|
|
|
|
|
opts = model._meta
|
|
|
|
|
2014-11-04 03:16:28 +08:00
|
|
|
title, body, metadata = utils.parse_docstring(model.__doc__)
|
|
|
|
if title:
|
|
|
|
title = utils.parse_rst(title, 'model', _('model:') + model_name)
|
|
|
|
if body:
|
|
|
|
body = utils.parse_rst(body, 'model', _('model:') + model_name)
|
|
|
|
|
2013-11-21 15:48:29 +08:00
|
|
|
# Gather fields/field descriptions.
|
|
|
|
fields = []
|
|
|
|
for field in opts.fields:
|
|
|
|
# ForeignKey is a special case since the field will actually be a
|
|
|
|
# descriptor that returns the other object
|
|
|
|
if isinstance(field, models.ForeignKey):
|
2015-02-26 22:19:17 +08:00
|
|
|
data_type = field.remote_field.model.__name__
|
|
|
|
app_label = field.remote_field.model._meta.app_label
|
2013-11-21 15:48:29 +08:00
|
|
|
verbose = utils.parse_rst(
|
|
|
|
(_("the related `%(app_label)s.%(data_type)s` object") % {
|
|
|
|
'app_label': app_label, 'data_type': data_type,
|
|
|
|
}),
|
|
|
|
'model',
|
|
|
|
_('model:') + data_type,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
data_type = get_readable_field_data_type(field)
|
|
|
|
verbose = field.verbose_name
|
2005-07-13 09:25:57 +08:00
|
|
|
fields.append({
|
2013-11-21 15:48:29 +08:00
|
|
|
'name': field.name,
|
|
|
|
'data_type': data_type,
|
2014-10-31 03:45:01 +08:00
|
|
|
'verbose': verbose or '',
|
2013-11-21 15:48:29 +08:00
|
|
|
'help_text': field.help_text,
|
2005-07-13 09:25:57 +08:00
|
|
|
})
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-21 15:48:29 +08:00
|
|
|
# Gather many-to-many fields.
|
|
|
|
for field in opts.many_to_many:
|
2015-02-26 22:19:17 +08:00
|
|
|
data_type = field.remote_field.model.__name__
|
|
|
|
app_label = field.remote_field.model._meta.app_label
|
2014-09-04 20:15:09 +08:00
|
|
|
verbose = _("related `%(app_label)s.%(object_name)s` objects") % {
|
|
|
|
'app_label': app_label,
|
|
|
|
'object_name': data_type,
|
|
|
|
}
|
2013-11-21 15:48:29 +08:00
|
|
|
fields.append({
|
|
|
|
'name': "%s.all" % field.name,
|
|
|
|
"data_type": 'List',
|
|
|
|
'verbose': utils.parse_rst(_("all %s") % verbose, 'model', _('model:') + opts.model_name),
|
|
|
|
})
|
|
|
|
fields.append({
|
|
|
|
'name': "%s.count" % field.name,
|
|
|
|
'data_type': 'Integer',
|
|
|
|
'verbose': utils.parse_rst(_("number of %s") % verbose, 'model', _('model:') + opts.model_name),
|
|
|
|
})
|
|
|
|
|
2014-10-31 03:45:01 +08:00
|
|
|
methods = []
|
2013-11-21 15:48:29 +08:00
|
|
|
# Gather model methods.
|
|
|
|
for func_name, func in model.__dict__.items():
|
2014-10-31 03:45:01 +08:00
|
|
|
if inspect.isfunction(func):
|
2013-11-21 15:48:29 +08:00
|
|
|
try:
|
|
|
|
for exclude in MODEL_METHODS_EXCLUDE:
|
|
|
|
if func_name.startswith(exclude):
|
|
|
|
raise StopIteration
|
|
|
|
except StopIteration:
|
|
|
|
continue
|
|
|
|
verbose = func.__doc__
|
|
|
|
if verbose:
|
|
|
|
verbose = utils.parse_rst(utils.trim_docstring(verbose), 'model', _('model:') + opts.model_name)
|
2014-10-31 03:45:01 +08:00
|
|
|
# If a method has no arguments, show it as a 'field', otherwise
|
|
|
|
# as a 'method with arguments'.
|
|
|
|
if func_has_no_args(func) and not func_accepts_kwargs(func) and not func_accepts_var_args(func):
|
|
|
|
fields.append({
|
|
|
|
'name': func_name,
|
|
|
|
'data_type': get_return_data_type(func_name),
|
|
|
|
'verbose': verbose or '',
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
arguments = get_func_full_args(func)
|
|
|
|
# Join arguments with ', ' and in case of default value,
|
|
|
|
# join it with '='. Use repr() so that strings will be
|
|
|
|
# correctly displayed.
|
|
|
|
print_arguments = ', '.join([
|
|
|
|
'='.join(list(arg_el[:1]) + [repr(el) for el in arg_el[1:]])
|
|
|
|
for arg_el in arguments
|
|
|
|
])
|
|
|
|
methods.append({
|
|
|
|
'name': func_name,
|
|
|
|
'arguments': print_arguments,
|
|
|
|
'verbose': verbose or '',
|
|
|
|
})
|
2013-11-21 15:48:29 +08:00
|
|
|
|
|
|
|
# Gather related objects
|
2015-01-07 08:16:35 +08:00
|
|
|
for rel in opts.related_objects:
|
2014-09-04 20:15:09 +08:00
|
|
|
verbose = _("related `%(app_label)s.%(object_name)s` objects") % {
|
2015-04-11 17:07:13 +08:00
|
|
|
'app_label': rel.related_model._meta.app_label,
|
|
|
|
'object_name': rel.related_model._meta.object_name,
|
2014-09-04 20:15:09 +08:00
|
|
|
}
|
2013-11-21 15:48:29 +08:00
|
|
|
accessor = rel.get_accessor_name()
|
|
|
|
fields.append({
|
|
|
|
'name': "%s.all" % accessor,
|
|
|
|
'data_type': 'List',
|
|
|
|
'verbose': utils.parse_rst(_("all %s") % verbose, 'model', _('model:') + opts.model_name),
|
|
|
|
})
|
|
|
|
fields.append({
|
|
|
|
'name': "%s.count" % accessor,
|
|
|
|
'data_type': 'Integer',
|
|
|
|
'verbose': utils.parse_rst(_("number of %s") % verbose, 'model', _('model:') + opts.model_name),
|
|
|
|
})
|
|
|
|
kwargs.update({
|
|
|
|
'name': '%s.%s' % (opts.app_label, opts.object_name),
|
2014-11-04 03:16:28 +08:00
|
|
|
'summary': title,
|
|
|
|
'description': body,
|
2013-11-21 15:48:29 +08:00
|
|
|
'fields': fields,
|
2014-10-31 03:45:01 +08:00
|
|
|
'methods': methods,
|
2006-05-02 09:31:56 +08:00
|
|
|
})
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_context_data(**kwargs)
|
2013-11-21 15:48:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TemplateDetailView(BaseAdminDocsView):
|
|
|
|
template_name = 'admin_doc/template_detail.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
template = self.kwargs['template']
|
|
|
|
templates = []
|
2015-01-12 03:16:28 +08:00
|
|
|
try:
|
|
|
|
default_engine = Engine.get_default()
|
|
|
|
except ImproperlyConfigured:
|
|
|
|
# Non-trivial TEMPLATES settings aren't supported (#24125).
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# This doesn't account for template loaders (#24128).
|
|
|
|
for index, directory in enumerate(default_engine.dirs):
|
|
|
|
template_file = os.path.join(directory, template)
|
2016-08-05 07:45:14 +08:00
|
|
|
if os.path.exists(template_file):
|
|
|
|
with open(template_file) as f:
|
|
|
|
template_contents = f.read()
|
|
|
|
else:
|
|
|
|
template_contents = ''
|
2015-01-12 03:16:28 +08:00
|
|
|
templates.append({
|
|
|
|
'file': template_file,
|
|
|
|
'exists': os.path.exists(template_file),
|
2016-08-05 07:45:14 +08:00
|
|
|
'contents': template_contents,
|
2015-01-12 03:16:28 +08:00
|
|
|
'order': index,
|
|
|
|
})
|
2013-11-21 15:48:29 +08:00
|
|
|
kwargs.update({
|
|
|
|
'name': template,
|
|
|
|
'templates': templates,
|
2006-05-02 09:31:56 +08:00
|
|
|
})
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_context_data(**kwargs)
|
2013-11-21 15:48:29 +08:00
|
|
|
|
2005-08-06 05:24:24 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
####################
|
|
|
|
# Helper functions #
|
|
|
|
####################
|
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def get_return_data_type(func_name):
|
|
|
|
"""Return a somewhat-helpful data type given a function name"""
|
|
|
|
if func_name.startswith('get_'):
|
|
|
|
if func_name.endswith('_list'):
|
|
|
|
return 'List'
|
|
|
|
elif func_name.endswith('_count'):
|
|
|
|
return 'Integer'
|
|
|
|
return ''
|
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2005-07-16 01:09:01 +08:00
|
|
|
def get_readable_field_data_type(field):
|
2017-01-25 04:31:57 +08:00
|
|
|
"""
|
|
|
|
Return the description for a given field type, if it exists. Fields'
|
|
|
|
descriptions can contain format strings, which will be interpolated with
|
|
|
|
the values of field.__dict__ before being output.
|
|
|
|
"""
|
2009-12-17 02:13:34 +08:00
|
|
|
return field.description % field.__dict__
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2013-11-05 17:16:27 +08:00
|
|
|
def extract_views_from_urlpatterns(urlpatterns, base='', namespace=None):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
Return a list of views from a list of urlpatterns.
|
2005-07-16 01:09:01 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
Each object in the returned list is a two-tuple: (view_func, regex)
|
|
|
|
"""
|
|
|
|
views = []
|
|
|
|
for p in urlpatterns:
|
2011-08-12 22:15:56 +08:00
|
|
|
if hasattr(p, 'url_patterns'):
|
2006-09-01 05:35:36 +08:00
|
|
|
try:
|
|
|
|
patterns = p.url_patterns
|
|
|
|
except ImportError:
|
|
|
|
continue
|
2013-11-05 17:16:27 +08:00
|
|
|
views.extend(extract_views_from_urlpatterns(
|
|
|
|
patterns,
|
|
|
|
base + p.regex.pattern,
|
|
|
|
(namespace or []) + (p.namespace and [p.namespace] or [])
|
|
|
|
))
|
2011-08-12 22:15:56 +08:00
|
|
|
elif hasattr(p, 'callback'):
|
|
|
|
try:
|
2013-11-05 17:16:27 +08:00
|
|
|
views.append((p.callback, base + p.regex.pattern,
|
|
|
|
namespace, p.name))
|
2011-08-12 22:15:56 +08:00
|
|
|
except ViewDoesNotExist:
|
|
|
|
continue
|
2005-07-13 09:25:57 +08:00
|
|
|
else:
|
2010-01-11 02:36:20 +08:00
|
|
|
raise TypeError(_("%s does not appear to be a urlpattern object") % p)
|
2005-07-13 09:25:57 +08:00
|
|
|
return views
|
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def simplify_regex(pattern):
|
2016-09-17 00:15:00 +08:00
|
|
|
r"""
|
2016-05-30 21:44:33 +08:00
|
|
|
Clean up urlpattern regexes into something more readable by humans. For
|
|
|
|
example, turn "^(?P<sport_slug>\w+)/athletes/(?P<athlete_slug>\w+)/$"
|
|
|
|
into "/<sport_slug>/athletes/<athlete_slug>/".
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2016-05-30 23:14:00 +08:00
|
|
|
pattern = replace_named_groups(pattern)
|
|
|
|
pattern = replace_unnamed_groups(pattern)
|
2006-05-02 09:31:56 +08:00
|
|
|
# clean up any outstanding regex-y characters.
|
2017-01-27 07:36:34 +08:00
|
|
|
pattern = pattern.replace('^', '').replace('$', '').replace('?', '')
|
2005-07-13 09:25:57 +08:00
|
|
|
if not pattern.startswith('/'):
|
|
|
|
pattern = '/' + pattern
|
2005-07-19 04:35:51 +08:00
|
|
|
return pattern
|