Commit 0986a4d2 authored by Karen Tracey's avatar Karen Tracey
Browse files

Fixed #7977: Fixed admindocs to use docstrings instead of a static array to...

Fixed #7977: Fixed admindocs to use docstrings instead of a static array to locate type information.  
Thanks J. Clifford Dyer.



git-svn-id: http://code.djangoproject.com/svn/django/trunk@11833 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent 783884af
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
# Empty models.py to allow for specifying admindocs as a test label.
+36 −0
Original line number Diff line number Diff line
import unittest
from django.contrib.admindocs import views
import fields

from django.db.models import fields as builtin_fields

class TestFieldType(unittest.TestCase):
    def setUp(self):
        pass
        
    def test_field_name(self):
        self.assertRaises(AttributeError,
            views.get_readable_field_data_type, "NotAField"
        )
        
    def test_builtin_fields(self):
        self.assertEqual(
            views.get_readable_field_data_type(builtin_fields.BooleanField()),
            u'Boolean (Either True or False)'
        )
    
    def test_custom_fields(self):
        self.assertEqual(
            views.get_readable_field_data_type(fields.CustomField()),
            u'A custom field type'
        )
        self.assertEqual(
            views.get_readable_field_data_type(fields.DocstringLackingField()),
            u'Field of type: DocstringLackingField'
        )
    
    def test_multiline_custom_field_truncation(self):
        self.assertEqual(
            views.get_readable_field_data_type(fields.ManyLineDocstringField()),
            u'Many-line custom field'
        )
+13 −0
Original line number Diff line number Diff line
from django.db import models

class CustomField(models.Field):
    """A custom field type"""
    
class ManyLineDocstringField(models.Field):
    """Many-line custom field
    
    This docstring has many lines.  Lorum ipsem etc. etc.  Four score 
    and seven years ago, and so on and so forth."""

class DocstringLackingField(models.Field):
    pass
+13 −36
Original line number Diff line number Diff line
@@ -326,43 +326,20 @@ def get_return_data_type(func_name):
            return 'Integer'
    return ''

# Maps Field objects to their human-readable data types, as strings.
# Column-type strings can contain format strings; they'll be interpolated
# against the values of Field.__dict__ before being output.
# If a column type is set to None, it won't be included in the output.
DATA_TYPE_MAPPING = {
    'AutoField'                 : _('Integer'),
    'BooleanField'              : _('Boolean (Either True or False)'),
    'CharField'                 : _('String (up to %(max_length)s)'),
    'CommaSeparatedIntegerField': _('Comma-separated integers'),
    'DateField'                 : _('Date (without time)'),
    'DateTimeField'             : _('Date (with time)'),
    'DecimalField'              : _('Decimal number'),
    'EmailField'                : _('E-mail address'),
    'FileField'                 : _('File path'),
    'FilePathField'             : _('File path'),
    'FloatField'                : _('Floating point number'),
    'ForeignKey'                : _('Integer'),
    'ImageField'                : _('File path'),
    'IntegerField'              : _('Integer'),
    'IPAddressField'            : _('IP address'),
    'ManyToManyField'           : '',
    'NullBooleanField'          : _('Boolean (Either True, False or None)'),
    'OneToOneField'             : _('Relation to parent model'),
    'PhoneNumberField'          : _('Phone number'),
    'PositiveIntegerField'      : _('Integer'),
    'PositiveSmallIntegerField' : _('Integer'),
    'SlugField'                 : _('String (up to %(max_length)s)'),
    'SmallIntegerField'         : _('Integer'),
    'TextField'                 : _('Text'),
    'TimeField'                 : _('Time'),
    'URLField'                  : _('URL'),
    'USStateField'              : _('U.S. state (two uppercase letters)'),
    'XMLField'                  : _('XML text'),
}

def get_readable_field_data_type(field):
    return DATA_TYPE_MAPPING[field.get_internal_type()] % field.__dict__
    """Returns the first line of a doc string for a given field type, if it 
    exists.  Fields' docstrings can contain format strings, which will be 
    interpolated against the values of Field.__dict__ before being output.  
    If no docstring is given, a sensible value will be auto-generated from 
    the field's class name."""

    if field.__doc__:
        doc = field.__doc__.split('\n')[0]
        return _(doc) % field.__dict__
    else:
        return _(u'Field of type: %(field_type)s') % {
            'field_type': field.__class__.__name__
        } 

def extract_views_from_urlpatterns(urlpatterns, base=''):
    """
+8 −1
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ def get_srid_info(srid):
    return _srid_cache[srid]

class GeometryField(SpatialBackend.Field):
    "The base GIS field -- maps to the OpenGIS Specification Geometry type."
    """The base GIS field -- maps to the OpenGIS Specification Geometry type."""

    # The OpenGIS Geometry name.
    geom_type = 'GEOMETRY'
@@ -257,22 +257,29 @@ class GeometryField(SpatialBackend.Field):

# The OpenGIS Geometry Type Fields
class PointField(GeometryField):
    """Point"""
    geom_type = 'POINT'

class LineStringField(GeometryField):
    """Line string"""
    geom_type = 'LINESTRING'

class PolygonField(GeometryField):
    """Polygon"""
    geom_type = 'POLYGON'

class MultiPointField(GeometryField):
    """Multi-point"""
    geom_type = 'MULTIPOINT'

class MultiLineStringField(GeometryField):
    """Multi-line string"""
    geom_type = 'MULTILINESTRING'

class MultiPolygonField(GeometryField):
    """Multi polygon"""
    geom_type = 'MULTIPOLYGON'

class GeometryCollectionField(GeometryField):
    """Geometry collection"""
    geom_type = 'GEOMETRYCOLLECTION'
Loading