Sunday, November 18, 2012

Is there a way to generate documentation for the URLs supported by the app


Hello,

    We are developing our Django application such that the UI is completely client side MVC, and Django will serve either CSV data, or json responses for the UI to consume.  In this regard, each URL is essentially an API for others to consume.  (I am sure this is the new norm).  Is there a way to generate the list of URLs and their documentation automatically.  As of now, I wrote a small admin command as follows.  But I am looking for better ways than this, if there are any.


from django.core.management.base import BaseCommand
from django.conf import settings


class Command(BaseCommand):
    ''' Generates documentation for the URLs.  For each URL includes the documentation from
    the callback implementing the URL and prints the default arguments along with the URL pattern and name'''
    def handle(self, *args, **options):
        url_module = None
        exec 'import '+settings.ROOT_URLCONF+'; url_module='+settings.ROOT_URLCONF
        doc = file('doc.html','w')
        doc.write("<table border=1 cellspacing=0 cellpadding=5>")
        doc.write("<tr><th>URL Pattern<th>Name<th>Documentation<th>Parameters")
        for x in url_module.urlpatterns:
            doc.write("<tr>")
            doc.write("<td>")
            doc.write(x._regex)
            doc.write("<td>")
            doc.write(str(x.name))
            try:
                documentation = str(x.callback.__doc__)
                doc.write("<td><pre>")
                doc.write(documentation)
                doc.write("</pre>")
            except:
                doc.write("<td> Unable to find module")
            doc.write("<td>")
            doc.write(str(x.default_args))
        doc.write("</table>")
        doc.close()

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/Z-NmulQhm54J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate