diff --git a/docs/fileobject.rst b/docs/fileobject.rst index 51ce29e99..bb881f68b 100644 --- a/docs/fileobject.rst +++ b/docs/fileobject.rst @@ -293,9 +293,13 @@ Version methods .. method:: version_name(version_suffix, extra_options=None) - :param version_suffix: A version name as defined in :ref:`settingsversions_versions`. - :param extra_options: Optional ``dict`` that will be merged with the - predefined ``VERSION`` definition if the given ``version_suffix`` exists. + :param version_suffix: A suffix to compose the version name acordinly to + the :ref:`settingsversions_version_namer` in use. If the suffix + is a key in :ref:`settingsversions_versions` the options will be used. + :param extra_options: An optional ``dict`` to be used in the version + generation. If ``version_suffix`` matches a key in + :ref:`settingsversions_versions` they will be merged and the + ``extra_options`` will take precedence. Get the filename for a version:: @@ -310,9 +314,13 @@ Version methods .. method:: version_path(version_suffix, extra_options=None) - :param version_suffix: A version name as defined in :ref:`settingsversions_versions`. - :param extra_options: Optional ``dict`` that will be merged with the - predefined ``VERSION`` definition if the given ``version_suffix`` exists. + :param version_suffix: A suffix to compose the version name acordinly to + the :ref:`settingsversions_version_namer` in use. If the suffix + is a key in :ref:`settingsversions_versions` the options will be used. + :param extra_options: An optional ``dict`` to be used in the version + generation. If ``version_suffix`` matches a key in + :ref:`settingsversions_versions` they will be merged and the + ``extra_options`` will take precedence. Get the path for a version:: @@ -326,9 +334,14 @@ Version methods .. method:: version_generate(version_suffix, extra_options=None) - :param version_suffix: A version name as defined in :ref:`settingsversions_versions`. - :param extra_options: Optional ``dict`` that will be merged with the - predefined ``VERSION`` definition if the given ``version_suffix`` exists. + :param version_suffix: A suffix to compose the version name acordinly to + the :ref:`settingsversions_version_namer` in use. If the suffix + is a key in :ref:`settingsversions_versions` the options will be used. + :param extra_options: An optional ``dict`` to be used in the version + generation. If ``version_suffix`` matches a key in + :ref:`settingsversions_versions` they will be merged and the + ``extra_options`` will take precedence. + Generate a version:: @@ -337,11 +350,6 @@ Version methods Please note that a version is only generated, if it does not already exist or if the original image is newer than the existing version. - .. note:: - The param ``version_suffix`` is not required to be in ``VERSIONS`` - anymore, any suffix should work, if the new ``extra_options`` param - contains enough data to generate a version. - Delete methods ^^^^^^^^^^^^^^ diff --git a/filebrowser/base.py b/filebrowser/base.py index 5af21cf26..eb14f8b9a 100644 --- a/filebrowser/base.py +++ b/filebrowser/base.py @@ -8,7 +8,7 @@ import time from django.core.files import File -from django.utils.encoding import python_2_unicode_compatible, smart_str +from django.utils.encoding import python_2_unicode_compatible, force_text from django.utils.six import string_types from django.utils.functional import cached_property @@ -219,13 +219,13 @@ def __init__(self, path, site=None): else: self.path = path self.head = os.path.dirname(path) - self.filename = os.path.basename(path) + self.filename = force_text(os.path.basename(path)) self.filename_lower = self.filename.lower() self.filename_root, self.extension = os.path.splitext(self.filename) self.mimetype = mimetypes.guess_type(self.filename) def __str__(self): - return smart_str(self.path) + return force_text(self.path) @property def name(self): diff --git a/filebrowser/namers.py b/filebrowser/namers.py index 672504751..441ac2f35 100644 --- a/filebrowser/namers.py +++ b/filebrowser/namers.py @@ -1,3 +1,4 @@ +from __future__ import unicode_literals import re from django.utils import six from django.utils.module_loading import import_string @@ -24,7 +25,7 @@ def get_version_name(self): def get_original_name(self): tmp = self.file_object.filename_root.split("_") if tmp[len(tmp) - 1] in VERSIONS: - return u"%s%s" % ( + return "%s%s" % ( self.file_object.filename_root.replace("_%s" % tmp[len(tmp) - 1], ""), self.file_object.extension) @@ -47,7 +48,8 @@ def get_original_name(self): root = self.file_object.filename_root tmp = root.split("_") options_part = tmp[len(tmp) - 1] - return u"%s%s" % (root.replace("_%s" % options_part, ""), self.file_object.extension) + name = re.sub('_%s$' % options_part, '', root) + return "%s%s" % (name, self.file_object.extension) @property def options_as_string(self): diff --git a/tests/test_base.py b/tests/test_base.py index 417d61080..e8eaf02ef 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -66,6 +66,17 @@ def test_posix_paths(self): self.assertEqual(f.path_relative_directory, '$%^&*/測試文件.jpg') self.assertEqual(f.dirname, r'$%^&*') + @patch('filebrowser.base.os.path', posixpath) + @patch('filebrowser.namers.VERSION_NAMER', 'filebrowser.namers.OptionsNamer') + def test_unicode_options_namer_version(self): + path_unicode = os.path.join(self.FOLDER_PATH, '測試文件.jpg') + expected = u'測試文件_large--680x0.jpg' + + shutil.copy(self.STATIC_IMG_PATH, path_unicode) + f = FileObject(path_unicode, site=site) + version = f.version_generate('large') + self.assertEqual(version.filename, expected) + class FileObjectAttributeTests(TestCase):