Skip to content

Commit

Permalink
docs updated to explain version_suffix and extra_options; fix unicode…
Browse files Browse the repository at this point in the history
… paths with special chars when generating versions using OptionsNamer
  • Loading branch information
fgmacedo committed Jun 15, 2016
1 parent 3e60c86 commit 6b3233d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
36 changes: 22 additions & 14 deletions docs/fileobject.rst
Expand Up @@ -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::

Expand All @@ -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::

Expand All @@ -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::

Expand All @@ -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
^^^^^^^^^^^^^^
Expand Down
6 changes: 3 additions & 3 deletions filebrowser/base.py
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions 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
Expand All @@ -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)

Expand All @@ -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):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_base.py
Expand Up @@ -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):

Expand Down

0 comments on commit 6b3233d

Please sign in to comment.