Ticket #7977: admindocs_field_types_classattr.diff
File admindocs_field_types_classattr.diff, 15.9 KB (added by , 15 years ago) |
---|
-
django/db/models/fields/__init__.py
59 59 creation_counter = 0 60 60 auto_creation_counter = -1 61 61 62 # Field type description 63 def _description(self): 64 return _(u'Field of type: %(field_type)s') % { 65 'field_type': self.__class__.__name__ 66 } 67 description = property(_description) 68 62 69 def __init__(self, verbose_name=None, name=None, primary_key=False, 63 70 max_length=None, unique=False, blank=False, null=False, 64 71 db_index=False, rel=None, default=NOT_PROVIDED, editable=True, … … 341 348 342 349 class AutoField(Field): 343 350 empty_strings_allowed = False 351 description = u"Integer" 352 344 353 def __init__(self, *args, **kwargs): 345 354 assert kwargs.get('primary_key', False) is True, "%ss must have primary_key=True." % self.__class__.__name__ 346 355 kwargs['blank'] = True … … 371 380 372 381 class BooleanField(Field): 373 382 empty_strings_allowed = False 383 description = u"Boolean (Either True or False)" 384 374 385 def __init__(self, *args, **kwargs): 375 386 kwargs['blank'] = True 376 387 if 'default' not in kwargs and not kwargs.get('null'): … … 413 424 return super(BooleanField, self).formfield(**defaults) 414 425 415 426 class CharField(Field): 427 description = u"String (up to %(max_length)s)" 428 416 429 def get_internal_type(self): 417 430 return "CharField" 418 431 … … 434 447 435 448 # TODO: Maybe move this into contrib, because it's specialized. 436 449 class CommaSeparatedIntegerField(CharField): 450 description = u"Comma-separated integers" 451 437 452 def formfield(self, **kwargs): 438 453 defaults = { 439 454 'form_class': forms.RegexField, … … 450 465 451 466 class DateField(Field): 452 467 empty_strings_allowed = False 468 description = u"Date (without time)" 469 453 470 def __init__(self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs): 454 471 self.auto_now, self.auto_now_add = auto_now, auto_now_add 455 472 #HACKs : auto_now_add/auto_now should be done as a default or a pre_save. … … 524 541 return super(DateField, self).formfield(**defaults) 525 542 526 543 class DateTimeField(DateField): 544 description = u"Date (with time)" 545 527 546 def get_internal_type(self): 528 547 return "DateTimeField" 529 548 … … 584 603 585 604 class DecimalField(Field): 586 605 empty_strings_allowed = False 606 description = u"Decimal number" 607 587 608 def __init__(self, verbose_name=None, name=None, max_digits=None, decimal_places=None, **kwargs): 588 609 self.max_digits, self.decimal_places = max_digits, decimal_places 589 610 Field.__init__(self, verbose_name, name, **kwargs) … … 637 658 return super(DecimalField, self).formfield(**defaults) 638 659 639 660 class EmailField(CharField): 661 description = u"E-mail address" 662 640 663 def __init__(self, *args, **kwargs): 641 664 kwargs['max_length'] = kwargs.get('max_length', 75) 642 665 CharField.__init__(self, *args, **kwargs) … … 647 670 return super(EmailField, self).formfield(**defaults) 648 671 649 672 class FilePathField(Field): 673 description = u"File path" 674 650 675 def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs): 651 676 self.path, self.match, self.recursive = path, match, recursive 652 677 kwargs['max_length'] = kwargs.get('max_length', 100) … … 667 692 668 693 class FloatField(Field): 669 694 empty_strings_allowed = False 695 description = u"Floating point number" 670 696 671 697 def get_db_prep_value(self, value): 672 698 if value is None: … … 692 718 693 719 class IntegerField(Field): 694 720 empty_strings_allowed = False 721 description = u"Integer" 722 695 723 def get_db_prep_value(self, value): 696 724 if value is None: 697 725 return None … … 716 744 717 745 class IPAddressField(Field): 718 746 empty_strings_allowed = False 747 description = u"IP address" 748 719 749 def __init__(self, *args, **kwargs): 720 750 kwargs['max_length'] = 15 721 751 Field.__init__(self, *args, **kwargs) … … 730 760 731 761 class NullBooleanField(Field): 732 762 empty_strings_allowed = False 763 description = u"Boolean (Either True, False or None)" 764 733 765 def __init__(self, *args, **kwargs): 734 766 kwargs['null'] = True 735 767 Field.__init__(self, *args, **kwargs) … … 769 801 return super(NullBooleanField, self).formfield(**defaults) 770 802 771 803 class PositiveIntegerField(IntegerField): 804 772 805 def get_internal_type(self): 773 806 return "PositiveIntegerField" 774 807 … … 778 811 return super(PositiveIntegerField, self).formfield(**defaults) 779 812 780 813 class PositiveSmallIntegerField(IntegerField): 814 781 815 def get_internal_type(self): 782 816 return "PositiveSmallIntegerField" 783 817 … … 787 821 return super(PositiveSmallIntegerField, self).formfield(**defaults) 788 822 789 823 class SlugField(CharField): 824 790 825 def __init__(self, *args, **kwargs): 791 826 kwargs['max_length'] = kwargs.get('max_length', 50) 792 827 # Set db_index=True unless it's been set manually. … … 803 838 return super(SlugField, self).formfield(**defaults) 804 839 805 840 class SmallIntegerField(IntegerField): 841 806 842 def get_internal_type(self): 807 843 return "SmallIntegerField" 808 844 809 845 class TextField(Field): 846 description = u"Text" 847 810 848 def get_internal_type(self): 811 849 return "TextField" 812 850 … … 816 854 return super(TextField, self).formfield(**defaults) 817 855 818 856 class TimeField(Field): 857 description = u"Time" 858 819 859 empty_strings_allowed = False 860 820 861 def __init__(self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs): 821 862 self.auto_now, self.auto_now_add = auto_now, auto_now_add 822 863 if auto_now or auto_now_add: … … 888 929 return super(TimeField, self).formfield(**defaults) 889 930 890 931 class URLField(CharField): 932 description = u"URL" 933 891 934 def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs): 892 935 kwargs['max_length'] = kwargs.get('max_length', 200) 893 936 self.verify_exists = verify_exists … … 899 942 return super(URLField, self).formfield(**defaults) 900 943 901 944 class XMLField(TextField): 945 description = u"XML text" 946 902 947 def __init__(self, verbose_name=None, name=None, schema_path=None, **kwargs): 903 948 self.schema_path = schema_path 904 949 Field.__init__(self, verbose_name, name, **kwargs) -
django/db/models/fields/related.py
683 683 684 684 class ForeignKey(RelatedField, Field): 685 685 empty_strings_allowed = False 686 description = u"Foreign Key (type determined by related field)" 687 686 688 def __init__(self, to, to_field=None, rel_class=ManyToOneRel, **kwargs): 687 689 try: 688 690 to_name = to._meta.object_name.lower() … … 777 779 always returns the object pointed to (since there will only ever be one), 778 780 rather than returning a list. 779 781 """ 782 783 description = u"One-to-one relationship" 784 780 785 def __init__(self, to, to_field=None, **kwargs): 781 786 kwargs['unique'] = True 782 787 super(OneToOneField, self).__init__(to, to_field, OneToOneRel, **kwargs) … … 791 796 return super(OneToOneField, self).formfield(**kwargs) 792 797 793 798 class ManyToManyField(RelatedField, Field): 799 description = u"Many-to-many relationship" 800 794 801 def __init__(self, to, **kwargs): 795 802 try: 796 803 assert not to._meta.abstract, "%s cannot define a relation with abstract class %s" % (self.__class__.__name__, to._meta.object_name) -
django/db/models/fields/files.py
209 209 instance.__dict__[self.field.name] = value 210 210 211 211 class FileField(Field): 212 description = u"File path" 213 212 214 # The class to wrap instance attributes in. Accessing the file object off 213 215 # the instance will always return an instance of attr_class. 214 216 attr_class = FieldFile … … 323 325 super(ImageFieldFile, self).delete(save) 324 326 325 327 class ImageField(FileField): 328 description = u"File path" 329 326 330 attr_class = ImageFieldFile 327 331 descriptor_class = ImageFileDescriptor 328 332 -
django/contrib/gis/db/models/fields/__init__.py
30 30 return _srid_cache[srid] 31 31 32 32 class GeometryField(SpatialBackend.Field): 33 "The base GIS field -- maps to the OpenGIS Specification Geometry type."34 35 33 # The OpenGIS Geometry name. 36 34 geom_type = 'GEOMETRY' 37 35 38 36 # Geodetic units. 39 37 geodetic_units = ('Decimal Degree', 'degree') 40 38 39 description = u"The base GIS field -- maps to the OpenGIS Specification Geometry type." 40 41 41 def __init__(self, verbose_name=None, srid=4326, spatial_index=True, dim=2, **kwargs): 42 42 """ 43 43 The initialization function for geometry fields. Takes the following … … 258 258 # The OpenGIS Geometry Type Fields 259 259 class PointField(GeometryField): 260 260 geom_type = 'POINT' 261 description = u"Point" 261 262 262 263 class LineStringField(GeometryField): 263 264 geom_type = 'LINESTRING' 265 description = u"Line string" 264 266 265 267 class PolygonField(GeometryField): 266 268 geom_type = 'POLYGON' 269 description = u"Polygon" 267 270 268 271 class MultiPointField(GeometryField): 269 272 geom_type = 'MULTIPOINT' 273 description = u"Multi-point" 270 274 271 275 class MultiLineStringField(GeometryField): 272 276 geom_type = 'MULTILINESTRING' 277 description = u"Multi-line string" 273 278 274 279 class MultiPolygonField(GeometryField): 275 280 geom_type = 'MULTIPOLYGON' 281 description = u"Multi-polygon" 276 282 277 283 class GeometryCollectionField(GeometryField): 278 284 geom_type = 'GEOMETRYCOLLECTION' 285 description = u"Geometry collection" 286 No newline at end of file -
django/contrib/admindocs/views.py
179 179 def model_detail(request, app_label, model_name): 180 180 if not utils.docutils_is_available: 181 181 return missing_docutils_page(request) 182 182 183 183 # Get the model class. 184 184 try: 185 185 app_mod = models.get_app(app_label) … … 307 307 return 'Integer' 308 308 return '' 309 309 310 # Maps Field objects to their human-readable data types, as strings.311 # Column-type strings can contain format strings; they'll be interpolated312 # against the values of Field.__dict__ before being output.313 # If a column type is set to None, it won't be included in the output.314 DATA_TYPE_MAPPING = {315 'AutoField' : _('Integer'),316 'BooleanField' : _('Boolean (Either True or False)'),317 'CharField' : _('String (up to %(max_length)s)'),318 'CommaSeparatedIntegerField': _('Comma-separated integers'),319 'DateField' : _('Date (without time)'),320 'DateTimeField' : _('Date (with time)'),321 'DecimalField' : _('Decimal number'),322 'EmailField' : _('E-mail address'),323 'FileField' : _('File path'),324 'FilePathField' : _('File path'),325 'FloatField' : _('Floating point number'),326 'ForeignKey' : _('Integer'),327 'ImageField' : _('File path'),328 'IntegerField' : _('Integer'),329 'IPAddressField' : _('IP address'),330 'ManyToManyField' : '',331 'NullBooleanField' : _('Boolean (Either True, False or None)'),332 'OneToOneField' : _('Relation to parent model'),333 'PhoneNumberField' : _('Phone number'),334 'PositiveIntegerField' : _('Integer'),335 'PositiveSmallIntegerField' : _('Integer'),336 'SlugField' : _('String (up to %(max_length)s)'),337 'SmallIntegerField' : _('Integer'),338 'TextField' : _('Text'),339 'TimeField' : _('Time'),340 'URLField' : _('URL'),341 'USStateField' : _('U.S. state (two uppercase letters)'),342 'XMLField' : _('XML text'),343 }344 345 310 def get_readable_field_data_type(field): 346 return DATA_TYPE_MAPPING[field.get_internal_type()] % field.__dict__ 311 """Returns the description for a given field type, if it exists, 312 Fields' descriptions can contain format strings, which will be interpolated 313 against the values of field.__dict__ before being output.""" 347 314 315 return _(field.description) % field.__dict__ 316 348 317 def extract_views_from_urlpatterns(urlpatterns, base=''): 349 318 """ 350 319 Return a list of views from a list of urlpatterns. -
django/contrib/admindocs/tests.py
1 import unittest 2 import views 3 from django.db.models import fields as builtin_fields 4 #import django.contrib.gis.db.models.fields as gis_fields 5 import models 6 7 8 class TestFieldType(unittest.TestCase): 9 def setUp(self): 10 pass 11 12 def test_field_name(self): 13 self.assertRaises(AttributeError, 14 views.get_readable_field_data_type, "NotAField" 15 ) 16 17 def test_builtin_fields(self): 18 self.assertEqual( 19 views.get_readable_field_data_type(builtin_fields.BooleanField()), 20 u'Boolean (Either True or False)' 21 ) 22 23 #def test_gis_fields(self): 24 # self.assertEqual( 25 # views.get_readable_field_data_type(gis_fields.PolygonField), 26 # u'Polygon!' 27 # ) 28 29 def test_custom_fields(self): 30 self.assertEqual( 31 views.get_readable_field_data_type(models.CustomField()), 32 u'A custom field type' 33 ) 34 self.assertEqual( 35 views.get_readable_field_data_type(models.DescriptionLackingField()), 36 u'Field of type: DescriptionLackingField' 37 ) -
django/contrib/admindocs/models.py
1 """Models for admindocs unittests.""" 2 3 from django.db import models 4 5 class CustomField(models.Field): 6 description = u"A custom field type" 7 8 class DescriptionLackingField(models.Field): 9 pass -
django/contrib/localflavor/us/models.py
1 1 from django.conf import settings 2 2 from django.db.models.fields import Field 3 3 4 class USStateField(Field): 5 def get_internal_type(self): 6 return "USStateField" 7 4 class USStateField(Field): 5 description = u"U.S. state (two uppercase letters)" 6 7 def get_internal_type(self): 8 return "USStateField" 9 8 10 def db_type(self): 9 11 if settings.DATABASE_ENGINE == 'oracle': 10 12 return 'CHAR(2)' 11 13 else: 12 14 return 'varchar(2)' 13 14 def formfield(self, **kwargs): 15 from django.contrib.localflavor.us.forms import USStateSelect 16 defaults = {'widget': USStateSelect} 17 defaults.update(kwargs) 15 16 def formfield(self, **kwargs): 17 from django.contrib.localflavor.us.forms import USStateSelect 18 defaults = {'widget': USStateSelect} 19 defaults.update(kwargs) 18 20 return super(USStateField, self).formfield(**defaults) 19 21 20 22 class PhoneNumberField(Field): 23 description = u"Phone number" 24 21 25 def get_internal_type(self): 22 26 return "PhoneNumberField" 23 27