1 from django
import template
, templatetags
2 from django
.template
import RequestContext
3 from django
.conf
import settings
4 from django
.contrib
.admin
.views
.decorators
import staff_member_required
5 from django
.db
import models
6 from django
.shortcuts
import render_to_response
7 from django
.core
.exceptions
import ImproperlyConfigured
, ViewDoesNotExist
8 from django
.http
import Http404
9 from django
.core
import urlresolvers
10 from django
.contrib
.admindocs
import utils
11 from django
.contrib
.sites
.models
import Site
12 from django
.utils
.translation
import ugettext
as _
13 from django
.utils
.safestring
import mark_safe
14 import inspect
, os
, re
16 # Exclude methods starting with these strings from documentation
17 MODEL_METHODS_EXCLUDE
= ('_', 'add_', 'delete', 'save', 'set_')
19 class GenericSite(object):
20 domain
= 'example.com'
24 from django
.contrib
import admin
26 return urlresolvers
.reverse(admin
.site
.root
, args
=[''])
27 except urlresolvers
.NoReverseMatch
:
28 return getattr(settings
, "ADMIN_SITE_ROOT_URL", "/admin/")
30 def doc_index(request
):
31 if not utils
.docutils_is_available
:
32 return missing_docutils_page(request
)
33 return render_to_response('admin_doc/index.html', {
34 'root_path': get_root_path(),
35 }, context_instance
=RequestContext(request
))
36 doc_index
= staff_member_required(doc_index
)
38 def bookmarklets(request
):
39 admin_root
= get_root_path()
40 return render_to_response('admin_doc/bookmarklets.html', {
41 'root_path': admin_root
,
42 'admin_url': mark_safe("%s://%s%s" % (request
.is_secure() and 'https' or 'http', request
.get_host(), admin_root
)),
43 }, context_instance
=RequestContext(request
))
44 bookmarklets
= staff_member_required(bookmarklets
)
46 def template_tag_index(request
):
47 if not utils
.docutils_is_available
:
48 return missing_docutils_page(request
)
50 load_all_installed_template_libraries()
53 for module_name
, library
in template
.libraries
.items():
54 for tag_name
, tag_func
in library
.tags
.items():
55 title
, body
, metadata
= utils
.parse_docstring(tag_func
.__doc
__)
57 title
= utils
.parse_rst(title
, 'tag', _('tag:') + tag_name
)
59 body
= utils
.parse_rst(body
, 'tag', _('tag:') + tag_name
)
61 metadata
[key
] = utils
.parse_rst(metadata
[key
], 'tag', _('tag:') + tag_name
)
62 if library
in template
.builtins
:
65 tag_library
= module_name
.split('.')[-1]
71 'library': tag_library
,
73 return render_to_response('admin_doc/template_tag_index.html', {
74 'root_path': get_root_path(),
76 }, context_instance
=RequestContext(request
))
77 template_tag_index
= staff_member_required(template_tag_index
)
79 def template_filter_index(request
):
80 if not utils
.docutils_is_available
:
81 return missing_docutils_page(request
)
83 load_all_installed_template_libraries()
86 for module_name
, library
in template
.libraries
.items():
87 for filter_name
, filter_func
in library
.filters
.items():
88 title
, body
, metadata
= utils
.parse_docstring(filter_func
.__doc
__)
90 title
= utils
.parse_rst(title
, 'filter', _('filter:') + filter_name
)
92 body
= utils
.parse_rst(body
, 'filter', _('filter:') + filter_name
)
94 metadata
[key
] = utils
.parse_rst(metadata
[key
], 'filter', _('filter:') + filter_name
)
95 if library
in template
.builtins
:
98 tag_library
= module_name
.split('.')[-1]
104 'library': tag_library
,
106 return render_to_response('admin_doc/template_filter_index.html', {
107 'root_path': get_root_path(),
109 }, context_instance
=RequestContext(request
))
110 template_filter_index
= staff_member_required(template_filter_index
)
112 def view_index(request
):
113 if not utils
.docutils_is_available
:
114 return missing_docutils_page(request
)
116 if settings
.ADMIN_FOR
:
117 settings_modules
= [__import__(m
, {}, {}, ['']) for m
in settings
.ADMIN_FOR
]
119 settings_modules
= [settings
]
122 for settings_mod
in settings_modules
:
123 urlconf
= __import__(settings_mod
.ROOT_URLCONF
, {}, {}, [''])
124 view_functions
= extract_views_from_urlpatterns(urlconf
.urlpatterns
)
125 if Site
._meta
.installed
:
126 site_obj
= Site
.objects
.get(pk
=settings_mod
.SITE_ID
)
128 site_obj
= GenericSite()
129 for (func
, regex
) in view_functions
:
131 'name': func
.__name
__,
132 'module': func
.__module
__,
133 'site_id': settings_mod
.SITE_ID
,
135 'url': simplify_regex(regex
),
137 return render_to_response('admin_doc/view_index.html', {
138 'root_path': get_root_path(),
140 }, context_instance
=RequestContext(request
))
141 view_index
= staff_member_required(view_index
)
143 def view_detail(request
, view
):
144 if not utils
.docutils_is_available
:
145 return missing_docutils_page(request
)
147 mod
, func
= urlresolvers
.get_mod_func(view
)
149 view_func
= getattr(__import__(mod
, {}, {}, ['']), func
)
150 except (ImportError, AttributeError):
152 title
, body
, metadata
= utils
.parse_docstring(view_func
.__doc
__)
154 title
= utils
.parse_rst(title
, 'view', _('view:') + view
)
156 body
= utils
.parse_rst(body
, 'view', _('view:') + view
)
158 metadata
[key
] = utils
.parse_rst(metadata
[key
], 'model', _('view:') + view
)
159 return render_to_response('admin_doc/view_detail.html', {
160 'root_path': get_root_path(),
165 }, context_instance
=RequestContext(request
))
166 view_detail
= staff_member_required(view_detail
)
168 def model_index(request
):
169 if not utils
.docutils_is_available
:
170 return missing_docutils_page(request
)
171 m_list
= [m
._meta
for m
in models
.get_models()]
172 return render_to_response('admin_doc/model_index.html', {
173 'root_path': get_root_path(),
175 }, context_instance
=RequestContext(request
))
176 model_index
= staff_member_required(model_index
)
178 def model_detail(request
, app_label
, model_name
):
179 if not utils
.docutils_is_available
:
180 return missing_docutils_page(request
)
182 # Get the model class.
184 app_mod
= models
.get_app(app_label
)
185 except ImproperlyConfigured
:
186 raise Http404
, _("App %r not found") % app_label
188 for m
in models
.get_models(app_mod
):
189 if m
._meta
.object_name
.lower() == model_name
:
193 raise Http404
, _("Model %(model_name)r not found in app %(app_label)r") % {'model_name': model_name
, 'app_label': app_label
}
197 # Gather fields/field descriptions.
199 for field
in opts
.fields
:
200 # ForeignKey is a special case since the field will actually be a
201 # descriptor that returns the other object
202 if isinstance(field
, models
.ForeignKey
):
203 data_type
= related_object_name
= field
.rel
.to
.__name
__
204 app_label
= field
.rel
.to
._meta
.app_label
205 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
)
207 data_type
= get_readable_field_data_type(field
)
208 verbose
= field
.verbose_name
211 'data_type': data_type
,
213 'help_text': field
.help_text
,
216 # Gather model methods.
217 for func_name
, func
in model
.__dict
__.items():
218 if (inspect
.isfunction(func
) and len(inspect
.getargspec(func
)[0]) == 1):
220 for exclude
in MODEL_METHODS_EXCLUDE
:
221 if func_name
.startswith(exclude
):
223 except StopIteration:
225 verbose
= func
.__doc
__
227 verbose
= utils
.parse_rst(utils
.trim_docstring(verbose
), 'model', _('model:') + opts
.module_name
)
230 'data_type': get_return_data_type(func_name
),
234 # Gather related objects
235 for rel
in opts
.get_all_related_objects():
236 verbose
= _("related `%(app_label)s.%(object_name)s` objects") % {'app_label': rel
.opts
.app_label
, 'object_name': rel
.opts
.object_name
}
237 accessor
= rel
.get_accessor_name()
239 'name' : "%s.all" % accessor
,
240 'data_type' : 'List',
241 'verbose' : utils
.parse_rst(_("all %s") % verbose
, 'model', _('model:') + opts
.module_name
),
244 'name' : "%s.count" % accessor
,
245 'data_type' : 'Integer',
246 'verbose' : utils
.parse_rst(_("number of %s") % verbose
, 'model', _('model:') + opts
.module_name
),
248 return render_to_response('admin_doc/model_detail.html', {
249 'root_path': get_root_path(),
250 'name': '%s.%s' % (opts
.app_label
, opts
.object_name
),
251 'summary': _("Fields on %s objects") % opts
.object_name
,
252 'description': model
.__doc
__,
254 }, context_instance
=RequestContext(request
))
255 model_detail
= staff_member_required(model_detail
)
257 def template_detail(request
, template
):
259 for site_settings_module
in settings
.ADMIN_FOR
:
260 settings_mod
= __import__(site_settings_module
, {}, {}, [''])
261 if Site
._meta
.installed
:
262 site_obj
= Site
.objects
.get(pk
=settings_mod
.SITE_ID
)
264 site_obj
= GenericSite()
265 for dir in settings_mod
.TEMPLATE_DIRS
:
266 template_file
= os
.path
.join(dir, "%s.html" % template
)
268 'file': template_file
,
269 'exists': os
.path
.exists(template_file
),
270 'contents': lambda: os
.path
.exists(template_file
) and open(template_file
).read() or '',
271 'site_id': settings_mod
.SITE_ID
,
273 'order': list(settings_mod
.TEMPLATE_DIRS
).index(dir),
275 return render_to_response('admin_doc/template_detail.html', {
276 'root_path': get_root_path(),
278 'templates': templates
,
279 }, context_instance
=RequestContext(request
))
280 template_detail
= staff_member_required(template_detail
)
286 def missing_docutils_page(request
):
287 """Display an error message for people without docutils"""
288 return render_to_response('admin_doc/missing_docutils.html')
290 def load_all_installed_template_libraries():
291 # Load/register all template tag libraries from installed apps.
292 for e
in templatetags
.__path
__:
293 libraries
= [os
.path
.splitext(p
)[0] for p
in os
.listdir(e
) if p
.endswith('.py') and p
[0].isalpha()]
294 for library_name
in libraries
:
296 lib
= template
.get_library("django.templatetags.%s" % library_name
.split('.')[-1])
297 except template
.InvalidTemplateLibrary
:
300 def get_return_data_type(func_name
):
301 """Return a somewhat-helpful data type given a function name"""
302 if func_name
.startswith('get_'):
303 if func_name
.endswith('_list'):
305 elif func_name
.endswith('_count'):
309 # Maps Field objects to their human-readable data types, as strings.
310 # Column-type strings can contain format strings; they'll be interpolated
311 # against the values of Field.__dict__ before being output.
312 # If a column type is set to None, it won't be included in the output.
313 DATA_TYPE_MAPPING
= {
314 'AutoField' : _('Integer'),
315 'BooleanField' : _('Boolean (Either True or False)'),
316 'CharField' : _('String (up to %(max_length)s)'),
317 'CommaSeparatedIntegerField': _('Comma-separated integers'),
318 'DateField' : _('Date (without time)'),
319 'DateTimeField' : _('Date (with time)'),
320 'DecimalField' : _('Decimal number'),
321 'EmailField' : _('E-mail address'),
322 'FileField' : _('File path'),
323 'FilePathField' : _('File path'),
324 'FloatField' : _('Floating point number'),
325 'ForeignKey' : _('Integer'),
326 'ImageField' : _('File path'),
327 'IntegerField' : _('Integer'),
328 'IPAddressField' : _('IP address'),
329 'ManyToManyField' : '',
330 'NullBooleanField' : _('Boolean (Either True, False or None)'),
331 'OneToOneField' : _('Relation to parent model'),
332 'PhoneNumberField' : _('Phone number'),
333 'PositiveIntegerField' : _('Integer'),
334 'PositiveSmallIntegerField' : _('Integer'),
335 'SlugField' : _('String (up to %(max_length)s)'),
336 'SmallIntegerField' : _('Integer'),
337 'TextField' : _('Text'),
338 'TimeField' : _('Time'),
339 'URLField' : _('URL'),
340 'USStateField' : _('U.S. state (two uppercase letters)'),
341 'XMLField' : _('XML text'),
344 def get_readable_field_data_type(field
):
345 return DATA_TYPE_MAPPING
[field
.get_internal_type()] % field
.__dict
__
347 def extract_views_from_urlpatterns(urlpatterns
, base
=''):
349 Return a list of views from a list of urlpatterns.
351 Each object in the returned list is a two-tuple: (view_func, regex)
354 for p
in urlpatterns
:
355 if hasattr(p
, '_get_callback'):
357 views
.append((p
._get
_callback
(), base
+ p
.regex
.pattern
))
358 except ViewDoesNotExist
:
360 elif hasattr(p
, '_get_url_patterns'):
362 patterns
= p
.url_patterns
365 views
.extend(extract_views_from_urlpatterns(patterns
, base
+ p
.regex
.pattern
))
367 raise TypeError, _("%s does not appear to be a urlpattern object") % p
370 named_group_matcher
= re
.compile(r
'\(\?P(<\w+>).+?\)')
371 non_named_group_matcher
= re
.compile(r
'\(.*?\)')
373 def simplify_regex(pattern
):
375 Clean up urlpattern regexes into something somewhat readable by Mere Humans:
376 turns something like "^(?P<sport_slug>\w+)/athletes/(?P<athlete_slug>\w+)/$"
377 into "<sport_slug>/athletes/<athlete_slug>/"
379 # handle named groups first
380 pattern
= named_group_matcher
.sub(lambda m
: m
.group(1), pattern
)
382 # handle non-named groups
383 pattern
= non_named_group_matcher
.sub("<var>", pattern
)
385 # clean up any outstanding regex-y characters.
386 pattern
= pattern
.replace('^', '').replace('$', '').replace('?', '').replace('//', '/').replace('\\', '')
387 if not pattern
.startswith('/'):
388 pattern
= '/' + pattern