Commits

Yohan Boniface committed 8093a00

HandlerField : some improvements in doc method

  • Participants
  • Parent commits f2a80da

Comments (0)

Files changed (2)

 from django.core.urlresolvers import get_resolver, get_callable, get_script_prefix
 from django.shortcuts import render_to_response
 from django.template import RequestContext
-from django.db.models.fields.related import RelatedField
+from django.db.models.fields.related import RelatedField, FieldDoesNotExist
 
 def generate_doc(handler_cls):
     """
 class HandlerField(object):
     """
     Fields introspection.
+    The goal is to display a documentation for a field when possible.
     """
     def __init__(self, handler, field):
         self.handler = handler
     @property
     def doc(self):
         """
-        Try to find the must convenient doc.
+        Try to find the most convenient doc.
         """
-        #We first check if there is a local method
+        # We first check if there is a local method
         if hasattr(self.handler, self.name):
             attr = getattr(self.handler, self.name)
             if callable(attr):
                 return attr.__doc__ or ""
         if hasattr(self.handler, "model"):
+            # Is the field a field of the handler's model ?
             try:
                 attr, model, _, _ = self.handler.model._meta.get_field_by_name(self.name)
-                print attr
-                #We now check if it will be processed by another handler
-                #If it's many to many, or related, or FK
+                # We now check if it will be processed by another handler
+                # If it's many to many, or related, or FK
                 if isinstance(attr, RelatedField):
-                    print "isinstance"
-                    return "See related fields for resource %s" % self.name
-                #We now try to get the help_text if its a model field
-                final = unicode(attr.help_text)
-            except:
-                final = ""
-            return final
-            #Is the field a method of the handler's model?
+                    return "See resource %s 'related fields' for details" % \
+                                              attr.related.parent_model.__name__
+                # We now try to get the help_text if its a model field
+                return unicode(attr.help_text)
+            except FieldDoesNotExist:
+                pass
+            # Is the field a method of the handler's model?
             if hasattr(self.handler.model, self.name):
                 attr = getattr(self.handler.model, self.name)
                 if callable(attr):

piston/emitters.py

                         inst = getattr(data, model, None)
 
                         if inst:
-                            if hasattr(inst, 'all'):#seems ugly :/
+                            if hasattr(inst, 'all'):
                                 ret[model] = _related(inst, fields)
                             elif callable(inst):
                                 if len(inspect.getargspec(inst)[0]) == 1: