Permalink
Browse files

documenting model fields

  • Loading branch information...
1 parent b2cd3e1 commit f72f7d5d9eaff03c9f6d90444f12561f95afbf71 @Fantomas42 committed Apr 30, 2012
Showing with 30 additions and 2 deletions.
  1. +1 −1 docs/conf.py
  2. +0 −1 docs/extensions/settings.py
  3. +29 −0 docs/extensions/zinnia_docs.py
View
@@ -23,7 +23,7 @@
os.environ['DJANGO_SETTINGS_MODULE'] = 'extensions.settings'
from django.core.management import call_command
-call_command('syncdb', verbosity=1, interactive=False)
+call_command('syncdb', verbosity=0, interactive=False)
import zinnia
@@ -1,5 +1,4 @@
"""Settings for Zinnia documentation"""
-import os
from zinnia.xmlrpc import ZINNIA_XMLRPC_METHODS
DATABASES = {'default': {'NAME': ':memory:',
@@ -4,6 +4,33 @@
Inspired, stealed and needed for
cross linking the django documentation.
"""
+import inspect
+
+from django.db import models
+from django.utils.html import strip_tags
+from django.utils.encoding import force_unicode
+
+
+def process_model_docstring(app, what, name, obj, options, lines):
+ if inspect.isclass(obj) and issubclass(obj, models.Model):
+ fields = obj._meta._fields()
+ for field in fields:
+ # Decode and strip any html out of the field's help text
+ help_text = strip_tags(force_unicode(field.help_text))
+ # Decode and capitalize the verbose name, for use if there isn't
+ # any help text
+ verbose_name = force_unicode(field.verbose_name).capitalize()
+
+ if help_text:
+ lines.append(u':param %s: %s' % (field.attname, help_text))
+ else:
+ lines.append(u':param %s: %s' % (field.attname, verbose_name))
+ # Add the field's type to the docstring
+ lines.append(u':type %s: %s' % (field.attname,
+ type(field).__name__))
+ # Return the extended docstring
+ return lines
+
def setup(app):
app.add_crossref_type(
@@ -16,3 +43,5 @@ def setup(app):
rolename = "ttag",
indextemplate = "pair: %s; template tag"
)
+ app.connect('autodoc-process-docstring',
+ process_model_docstring)

0 comments on commit f72f7d5

Please sign in to comment.