Moved parts/admin/doc.py to contrib/admin/utils.py, where it belongs. Also caught docutils ImportError in a more controlled way

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1463 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-11-27 17:20:43 +00:00
parent f237936a87
commit 2e7e19e0f2
3 changed files with 30 additions and 30 deletions

View File

@ -1,14 +1,17 @@
""" "Misc. utility functions/classes for admin documentation generator."
Misc. utility functions/classes for documentation generator
"""
import re import re
from email.Parser import HeaderParser from email.Parser import HeaderParser
from email.Errors import HeaderParseError from email.Errors import HeaderParseError
import docutils.core
import docutils.nodes
import docutils.parsers.rst.roles
from urlparse import urljoin from urlparse import urljoin
try:
import docutils.core
import docutils.nodes
import docutils.parsers.rst.roles
except ImportError:
docutils_is_available = False
else:
docutils_is_available = True
def trim_docstring(docstring): def trim_docstring(docstring):
""" """

View File

@ -6,17 +6,14 @@ from django.models.core import sites
from django.core.extensions import DjangoContext, render_to_response from django.core.extensions import DjangoContext, render_to_response
from django.core.exceptions import Http404, ViewDoesNotExist from django.core.exceptions import Http404, ViewDoesNotExist
from django.core import template, urlresolvers from django.core import template, urlresolvers
try: from django.contrib.admin import utils
from django.parts.admin import doc
except ImportError:
doc = None
import inspect, os, re import inspect, os, re
# Exclude methods starting with these strings from documentation # Exclude methods starting with these strings from documentation
MODEL_METHODS_EXCLUDE = ('_', 'add_', 'delete', 'save', 'set_') MODEL_METHODS_EXCLUDE = ('_', 'add_', 'delete', 'save', 'set_')
def doc_index(request): def doc_index(request):
if not doc: if not utils.docutils_is_available:
return missing_docutils_page(request) return missing_docutils_page(request)
return render_to_response('admin_doc/index', context_instance=DjangoContext(request)) return render_to_response('admin_doc/index', context_instance=DjangoContext(request))
doc_index = staff_member_required(doc_index) doc_index = staff_member_required(doc_index)
@ -28,7 +25,7 @@ def bookmarklets(request):
bookmarklets = staff_member_required(bookmarklets) bookmarklets = staff_member_required(bookmarklets)
def template_tag_index(request): def template_tag_index(request):
if not doc: if not utils.docutils_is_available:
return missing_docutils_page(request) return missing_docutils_page(request)
load_all_installed_template_libraries() load_all_installed_template_libraries()
@ -36,13 +33,13 @@ def template_tag_index(request):
tags = [] tags = []
for module_name, library in template.libraries.items(): for module_name, library in template.libraries.items():
for tag_name, tag_func in library.tags.items(): for tag_name, tag_func in library.tags.items():
title, body, metadata = doc.parse_docstring(tag_func.__doc__) title, body, metadata = utils.parse_docstring(tag_func.__doc__)
if title: if title:
title = doc.parse_rst(title, 'tag', 'tag:' + tag_name) title = utils.parse_rst(title, 'tag', 'tag:' + tag_name)
if body: if body:
body = doc.parse_rst(body, 'tag', 'tag:' + tag_name) body = utils.parse_rst(body, 'tag', 'tag:' + tag_name)
for key in metadata: for key in metadata:
metadata[key] = doc.parse_rst(metadata[key], 'tag', 'tag:' + tag_name) metadata[key] = utils.parse_rst(metadata[key], 'tag', 'tag:' + tag_name)
if library in template.builtins: if library in template.builtins:
tag_library = None tag_library = None
else: else:
@ -59,7 +56,7 @@ def template_tag_index(request):
template_tag_index = staff_member_required(template_tag_index) template_tag_index = staff_member_required(template_tag_index)
def template_filter_index(request): def template_filter_index(request):
if not doc: if not utils.docutils_is_available:
return missing_docutils_page(request) return missing_docutils_page(request)
load_all_installed_template_libraries() load_all_installed_template_libraries()
@ -67,13 +64,13 @@ def template_filter_index(request):
filters = [] filters = []
for module_name, library in template.libraries.items(): for module_name, library in template.libraries.items():
for filter_name, filter_func in library.filters.items(): for filter_name, filter_func in library.filters.items():
title, body, metadata = doc.parse_docstring(filter_func.__doc__) title, body, metadata = utils.parse_docstring(filter_func.__doc__)
if title: if title:
title = doc.parse_rst(title, 'filter', 'filter:' + filter_name) title = utils.parse_rst(title, 'filter', 'filter:' + filter_name)
if body: if body:
body = doc.parse_rst(body, 'filter', 'filter:' + filter_name) body = utils.parse_rst(body, 'filter', 'filter:' + filter_name)
for key in metadata: for key in metadata:
metadata[key] = doc.parse_rst(metadata[key], 'filter', 'filter:' + filter_name) metadata[key] = utils.parse_rst(metadata[key], 'filter', 'filter:' + filter_name)
if library in template.builtins: if library in template.builtins:
tag_library = None tag_library = None
else: else:
@ -89,7 +86,7 @@ def template_filter_index(request):
template_filter_index = staff_member_required(template_filter_index) template_filter_index = staff_member_required(template_filter_index)
def view_index(request): def view_index(request):
if not doc: if not utils.docutils_is_available:
return missing_docutils_page(request) return missing_docutils_page(request)
views = [] views = []
@ -109,7 +106,7 @@ def view_index(request):
view_index = staff_member_required(view_index) view_index = staff_member_required(view_index)
def view_detail(request, view): def view_detail(request, view):
if not doc: if not utils.docutils_is_available:
return missing_docutils_page(request) return missing_docutils_page(request)
mod, func = urlresolvers.get_mod_func(view) mod, func = urlresolvers.get_mod_func(view)
@ -117,13 +114,13 @@ def view_detail(request, view):
view_func = getattr(__import__(mod, '', '', ['']), func) view_func = getattr(__import__(mod, '', '', ['']), func)
except (ImportError, AttributeError): except (ImportError, AttributeError):
raise Http404 raise Http404
title, body, metadata = doc.parse_docstring(view_func.__doc__) title, body, metadata = utils.parse_docstring(view_func.__doc__)
if title: if title:
title = doc.parse_rst(title, 'view', 'view:' + view) title = utils.parse_rst(title, 'view', 'view:' + view)
if body: if body:
body = doc.parse_rst(body, 'view', 'view:' + view) body = utils.parse_rst(body, 'view', 'view:' + view)
for key in metadata: for key in metadata:
metadata[key] = doc.parse_rst(metadata[key], 'model', 'view:' + view) metadata[key] = utils.parse_rst(metadata[key], 'model', 'view:' + view)
return render_to_response('admin_doc/view_detail', { return render_to_response('admin_doc/view_detail', {
'name': view, 'name': view,
'summary': title, 'summary': title,
@ -133,7 +130,7 @@ def view_detail(request, view):
view_detail = staff_member_required(view_detail) view_detail = staff_member_required(view_detail)
def model_index(request): def model_index(request):
if not doc: if not utils.docutils_is_available:
return missing_docutils_page(request) return missing_docutils_page(request)
models = [] models = []
@ -149,7 +146,7 @@ def model_index(request):
model_index = staff_member_required(model_index) model_index = staff_member_required(model_index)
def model_detail(request, model): def model_detail(request, model):
if not doc: if not utils.docutils_is_available:
return missing_docutils_page(request) return missing_docutils_page(request)
try: try:
@ -177,7 +174,7 @@ def model_detail(request, model):
continue continue
verbose = func.__doc__ verbose = func.__doc__
if verbose: if verbose:
verbose = doc.parse_rst(doc.trim_docstring(verbose), 'model', 'model:' + opts.module_name) verbose = utils.parse_rst(utils.trim_docstring(verbose), 'model', 'model:' + opts.module_name)
fields.append({ fields.append({
'name': func_name, 'name': func_name,
'data_type': get_return_data_type(func_name), 'data_type': get_return_data_type(func_name),