Ticket #6896: 6896.diff
File 6896.diff, 6.6 KB (added by , 14 years ago) |
---|
-
django/db/models/fields/__init__.py
835 835 def get_internal_type(self): 836 836 return "FilePathField" 837 837 838 class RelativeFilePathField(FilePathField): 839 def formfield(self, **kwargs): 840 defaults = { 841 'form_class': forms.RelativeFilePathField, 842 } 843 defaults.update(kwargs) 844 return super(RelativeFilePathField, self).formfield(**defaults) 845 838 846 class FloatField(Field): 839 847 empty_strings_allowed = False 840 848 default_error_messages = { -
django/forms/fields.py
40 40 'BooleanField', 'NullBooleanField', 'ChoiceField', 'MultipleChoiceField', 41 41 'ComboField', 'MultiValueField', 'FloatField', 'DecimalField', 42 42 'SplitDateTimeField', 'IPAddressField', 'FilePathField', 'SlugField', 43 'TypedChoiceField' 43 'TypedChoiceField', 'RelativeFilePathField' 44 44 ) 45 45 46 46 def en_format(name): … … 874 874 875 875 self.widget.choices = self.choices 876 876 877 class RelativeFilePathField(FilePathField): 878 def __init__(self, path, *args, **kwargs): 879 super(RelativeFilePathField, self).__init__(path, *args, **kwargs) 880 choices = [] 881 for choice in self.choices: 882 choices.append((os.path.relpath(choice[0], path), choice[1])) 883 self.choices = choices 884 877 885 class SplitDateTimeField(MultiValueField): 878 886 widget = SplitDateTimeWidget 879 887 hidden_widget = SplitHiddenDateTimeWidget -
docs/ref/forms/fields.txt
522 522 When you use a ``FileField`` in a form, you must also remember to 523 523 :ref:`bind the file data to the form <binding-uploaded-files>`. 524 524 525 ``RelativeFilePathField`` 526 ~~~~~~~~~~~~~~~~~~~~~~~~~ 527 528 .. versionadded:: 1.3 529 530 .. class:: RelativeFilePathField(**kwargs) 531 532 This field is an extension of the :class:`~django.forms.fields.FilePathField` 533 whose choices are relative file paths (as opposed to the absolute file paths 534 used by ``FilePathField``). 535 525 536 ``FilePathField`` 526 537 ~~~~~~~~~~~~~~~~~ 527 538 -
docs/ref/models/fields.txt
654 654 created as ``varchar(100)`` columns in your database. As with other fields, you 655 655 can change the maximum length using the :attr:`~CharField.max_length` argument. 656 656 657 The value stored in the database will be the absolute filesystem path to the 658 selected file including ``path``. See :class:`RelativeFilePathField` to store 659 the relative filesystem path. 660 661 ``RelativeFilePathField`` 662 ------------------------- 663 664 .. class:: RelativeFilePathField(path=None, [match=None, recursive=False, max_length=100, **options]) 665 666 A :class:`FilePathField` whose values represent relative paths from the 667 absolute filesystem path. For example: 668 669 RelativeFilePathField(path="/home/images") 670 671 ...will store the value "foo.gif" in the database for an image located at 672 "/home/images/foo.gif" and "foo/bar.gif" for an image located at 673 "/home/images/foo/bar.gif". 674 675 See :class:`RelativeFilePathField` to store the absolute filesystem path. 676 657 677 ``FloatField`` 658 678 -------------- 659 679 -
docs/topics/forms/modelforms.txt
65 65 66 66 ``FileField`` ``FileField`` 67 67 68 ``FilePathField`` `` CharField``68 ``FilePathField`` ``SelectField`` 69 69 70 70 ``FloatField`` ``FloatField`` 71 71 … … 89 89 90 90 ``PositiveSmallIntegerField`` ``IntegerField`` 91 91 92 ``RelativeFilePathField`` ``SelectField`` 93 92 94 ``SlugField`` ``SlugField`` 93 95 94 96 ``SmallIntegerField`` ``IntegerField`` -
tests/regressiontests/forms/fields.py
862 862 self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', '']) 863 863 self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10']) 864 864 self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['', '07:30']) 865 866 # RelativeFilePathField ####################################################### 867 868 def test_relativefilepathfield_71(self): 869 path = forms.__file__ 870 path = os.path.dirname(os.path.abspath(path)) + '/' 871 f = RelativeFilePathField(path=path) 872 f.choices = [p for p in f.choices if p[0].endswith('.py')] 873 f.choices.sort() 874 expected = [ 875 ('__init__.py', '__init__.py'), 876 ('fields.py', 'fields.py'), 877 ('forms.py', 'forms.py'), 878 ('formsets.py', 'formsets.py'), 879 ('models.py', 'models.py'), 880 ('util.py', 'util.py'), 881 ('widgets.py', 'widgets.py') 882 ] 883 for exp, got in zip(expected, fix_os_paths(f.choices)): 884 self.assertEqual(exp[1], got[1]) 885 self.assertTrue(got[0].endswith(exp[0])) 886 887 def test_relativefilepathfield_72(self): 888 path = os.path.abspath(forms.__file__) 889 path = os.path.dirname(path) + '/' 890 f = FilePathField(path=path, recursive=True, match='^.*?\.py$') 891 f.choices.sort() 892 expected = [ 893 ('__init__.py', '__init__.py'), 894 ('extras/__init__.py', 'extras/__init__.py'), 895 ('extras/widgets.py', 'extras/widgets.py'), 896 ('fields.py', 'fields.py'), 897 ('forms.py', 'forms.py'), 898 ('formsets.py', 'formsets.py'), 899 ('models.py', 'models.py'), 900 ('util.py', 'util.py'), 901 ('widgets.py', 'widgets.py') 902 ] 903 for exp, got in zip(expected, fix_os_paths(f.choices)): 904 self.assertEqual(exp[1], got[1]) 905 self.assertTrue(got[0].endswith(exp[0]))