Ticket #6896: 6896.diff

File 6896.diff, 6.6 KB (added by Kyle Dodson, 14 years ago)

Adds a "RelativeFilePathField"

  • django/db/models/fields/__init__.py

     
    835835    def get_internal_type(self):
    836836        return "FilePathField"
    837837
     838class 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
    838846class FloatField(Field):
    839847    empty_strings_allowed = False
    840848    default_error_messages = {
  • django/forms/fields.py

     
    4040    'BooleanField', 'NullBooleanField', 'ChoiceField', 'MultipleChoiceField',
    4141    'ComboField', 'MultiValueField', 'FloatField', 'DecimalField',
    4242    'SplitDateTimeField', 'IPAddressField', 'FilePathField', 'SlugField',
    43     'TypedChoiceField'
     43    'TypedChoiceField', 'RelativeFilePathField'
    4444)
    4545
    4646def en_format(name):
     
    874874
    875875        self.widget.choices = self.choices
    876876
     877class 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
    877885class SplitDateTimeField(MultiValueField):
    878886    widget = SplitDateTimeWidget
    879887    hidden_widget = SplitHiddenDateTimeWidget
  • docs/ref/forms/fields.txt

     
    522522When you use a ``FileField`` in a form, you must also remember to
    523523:ref:`bind the file data to the form <binding-uploaded-files>`.
    524524
     525``RelativeFilePathField``
     526~~~~~~~~~~~~~~~~~~~~~~~~~
     527
     528.. versionadded:: 1.3
     529
     530.. class:: RelativeFilePathField(**kwargs)
     531
     532This field is an extension of the :class:`~django.forms.fields.FilePathField`
     533whose choices are relative file paths (as opposed to the absolute file paths
     534used by ``FilePathField``).
     535
    525536``FilePathField``
    526537~~~~~~~~~~~~~~~~~
    527538
  • docs/ref/models/fields.txt

     
    654654created as ``varchar(100)`` columns in your database. As with other fields, you
    655655can change the maximum length using the :attr:`~CharField.max_length` argument.
    656656
     657The value stored in the database will be the absolute filesystem path to the
     658selected file including ``path``. See :class:`RelativeFilePathField` to store
     659the relative filesystem path.
     660
     661``RelativeFilePathField``
     662-------------------------
     663
     664.. class:: RelativeFilePathField(path=None, [match=None, recursive=False, max_length=100, **options])
     665
     666A :class:`FilePathField` whose values represent relative paths from the
     667absolute 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
     675See :class:`RelativeFilePathField` to store the absolute filesystem path.
     676
    657677``FloatField``
    658678--------------
    659679
  • docs/topics/forms/modelforms.txt

     
    6565
    6666    ``FileField``                    ``FileField``
    6767
    68     ``FilePathField``                ``CharField``
     68    ``FilePathField``                ``SelectField``
    6969
    7070    ``FloatField``                   ``FloatField``
    7171
     
    8989
    9090    ``PositiveSmallIntegerField``    ``IntegerField``
    9191
     92    ``RelativeFilePathField``        ``SelectField``
     93
    9294    ``SlugField``                    ``SlugField``
    9395
    9496    ``SmallIntegerField``            ``IntegerField``
  • tests/regressiontests/forms/fields.py

     
    862862        self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', ''])
    863863        self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10'])
    864864        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]))
Back to Top