Exemplo n.º 1
0
 def _get_temp_file(self):
     """
     Generate and return a temporary file instance with the same extension
     as self.file_field.
     """
     filename, ext = split_extension(self.file_field.name)
     return tempfile.NamedTemporaryFile(suffix='.%s' % ext)
Exemplo n.º 2
0
 def test_split_extension(self):
     """
     Test that split_extension method works properly.
     """
     scenarios = {
         'awesome_filename.jpg': ['awesome_filename', 'jpg'],
         'path/awesome_filename.GIF': ['path/awesome_filename', 'gif'],
         'path/path/awesome_filename': ['path/path/awesome_filename', ''],
     }
     for input, output in scenarios.items():
         name, ext = split_extension(input)
         self.assertEqual(name, output[0])
         self.assertEqual(ext, output[1])
Exemplo n.º 3
0
def friendly_name(filename):
    """
    Creates a 'friendly' name based on the given filename:
    - Get everything after the last slash
    - Remove the extension
    - Convert slashes, underscores, bracket, pound to space
    - Convert consecutive spaces to just one space
    """
    filename = filename.split('\\')[-1].split('/')[-1]
    filename, ext = split_extension(filename)
    filename = re.sub('[/\\_\[\]#]', ' ', filename)
    filename = re.sub('\s{2,}', ' ', filename)
    return filename.strip()[:200]