Ticket #3119: filename_nomalizer_fix.diff

File filename_nomalizer_fix.diff, 2.4 KB (added by ymasuda <ymasuda@…>, 8 years ago)

adds filename_normalizer to django.db.models.fields.FileField. also includes short description on model-api.txt.

  • docs/model-api.txt

     
    196196
    197197A file-upload field.
    198198
    199 Has an extra required argument, ``upload_to``, a local filesystem path to
    200 which files should be upload. This path may contain `strftime formatting`_,
    201 which will be replaced by the date/time of the file upload (so that
    202 uploaded files don't fill up the given directory).
     199A ``FileField`` has an extra required argument ``upload_to`` and an
     200optional argument ``filename_normalizer``. The ``upload_to`` specifies a
     201local filesystem path to which files should be upload. This path may
     202contain `strftime formatting`_, which will be replaced by the date/time of
     203the file upload (so that uploaded files don't fill up the given
     204directory). The ``filename_normalizer`` is a function to "normalize"
     205filename from the value provided from file-upload widget. The default value
     206is set to ``django.utils.text.get_valid_filename`` which strips all
     207non-ascii characters.
    203208
    204209The admin represents this as an ``<input type="file">`` (a file-upload widget).
    205210
  • django/db/models/fields/__init__.py

     
    580580        return forms.EmailField(**defaults)
    581581
    582582class FileField(Field):
    583     def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs):
     583    from django.utils.text import get_valid_filename
     584    def __init__(self, verbose_name=None, name=None, upload_to='', filename_normalizer=get_valid_filename, **kwargs):
    584585        self.upload_to = upload_to
     586        self.filename_normalizer = filename_normalizer
    585587        Field.__init__(self, verbose_name, name, **kwargs)
    586588
    587589    def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False, follow=True):
     
    656658        return os.path.normpath(datetime.datetime.now().strftime(self.upload_to))
    657659
    658660    def get_filename(self, filename):
    659         from django.utils.text import get_valid_filename
    660         f = os.path.join(self.get_directory_name(), get_valid_filename(os.path.basename(filename)))
     661        f = os.path.join(self.get_directory_name(), self.filename_normalizer(os.path.basename(filename)))
    661662        return os.path.normpath(f)
    662663
    663664class FilePathField(Field):
Back to Top