Changes between Initial Version and Version 1 of Ticket #26058, comment 3


Ignore:
Timestamp:
Jan 11, 2016, 1:53:54 AM (8 years ago)
Author:
Korijn van Golen

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #26058, comment 3

    initial v1  
    1 How's this?
     1I took a little bit more time to do this neatly, here's the backwards-compatible version:
    22
    33
    44{{{
     5class StorageBackendAgnosticFileField(FileField):
     6    """
     7    Subclass of Django's `FileField` that allows for storage backends to optionally implement a function
     8    `generate_filename` through which they can generate filenames in a manner suitable for their storage mechanisms,
     9    rather than through Django's implementation which is hardwired to specific file systems.
     10    """
     11
     12    def parse_upload_to(self):
     13        """
     14        Same as `FileField.get_directory_name`, but without the file system specifics. Allows for date formatting.
     15
     16        Returns
     17        -------
     18        str
     19            The parsed `upload_to` string.
     20        """
     21        return force_text(datetime.datetime.now().strftime(force_str(self.upload_to)))
     22
    523    def generate_filename(self, instance, filename):
    6         # allow for customized filename generation
    7         if hasattr(self.storage, 'generate_filename'):
     24        """
     25        Overrides the regular implementation by first checking of the storage backend doesn't already implement this
     26        logic. Otherwise, falls back to the traditional implementation.
     27
     28        Parameters
     29        ----------
     30        instance : object
     31            The model instance the current file is attached to. It's passed to the `upload_to` call when `upload_to` is
     32            callable.
     33        filename : str
     34            The filename, if any.
     35
     36        Returns
     37        -------
     38        str
     39            The newly generated and/or validated filename.
     40        """
     41        if hasattr(self.storage, 'generate_filename') and callable(self.storage.generate_filename):
    842            if callable(self.upload_to):
    943                filename = self.upload_to(instance, filename)
    1044                return self.storage.generate_filename(filename)
    11                
    12             filename = force_text(datetime.datetime.now().strftime(force_str(self.upload_to)))
    13             return self.storage.generate_filename(filename)
    14            
    15         # If upload_to is a callable, make sure that the path it returns is
    16         # passed through get_valid_name() of the underlying storage.
    17         if callable(self.upload_to):
    18             directory_name, filename = os.path.split(self.upload_to(instance, filename))
    19             filename = self.storage.get_valid_name(filename)
    20             return os.path.normpath(os.path.join(directory_name, filename))
    2145
    22         return os.path.join(self.get_directory_name(), self.get_filename(filename))
     46            directory_name = self.parse_upload_to()
     47            return self.storage.generate_filename(directory_name + filename)
     48
     49        return super(StorageBackendAgnosticFileField, self).generate_filename(instance, filename)
    2350}}}
Back to Top