diff --git a/README.rst b/README.rst index f091557..5638d9d 100644 --- a/README.rst +++ b/README.rst @@ -40,7 +40,7 @@ https://www.transifex.com/projects/p/django-filebrowser/ Releases -------- -* FileBrowser 3.5.5 (Development Version, not yet released, see Branch Stable/3.5.x) -* FileBrowser 3.5.4 (February 21st, 2014): Compatible with Django 1.4/1.5/1.6 +* FileBrowser 3.5.6 (Development Version, not yet released, see Branch Stable/3.5.x) +* FileBrowser 3.5.5 (April 13th, 2014): Compatible with Django 1.4/1.5/1.6 Older versions are availabe at GitHub, but are not supported anymore. diff --git a/docs/admin.rst b/docs/admin.rst index 4e4dcde..cb02258 100644 --- a/docs/admin.rst +++ b/docs/admin.rst @@ -22,7 +22,7 @@ FileBrowser Site .. py:class:: FileBrowserSite(name=None, app_name='filebrowser', storage=default_storage) - Respresens the FileBrowser admin application (similar to Django's admin site). + Respresents the FileBrowser admin application (similar to Django's admin site). :param name: A name for the site, defaults to None. :param app_name: Defaults to 'filebrowser'. @@ -41,7 +41,7 @@ Now you are able to browse the location defined with the storage engine associat .. code-block:: python from django.core.files.storage import DefaultStorage - from filebrowser.sites import import FileBrowserSite + from filebrowser.sites import FileBrowserSite # Default FileBrowser site site = FileBrowserSite(name='filebrowser', storage=DefaultStorage()) @@ -182,7 +182,7 @@ All views use the ``staff_member_requird`` and ``path_exists`` decorator in orde * Upload, ``fb_upload`` Multiple upload. - * Optional query string args: ``dir`` + * Optional query string args: ``dir``, ``type`` * Signals: `filebrowser_pre_upload`, `filebrowser_post_upload` * Edit, ``fb_edit`` @@ -285,4 +285,4 @@ Here's a small example for using the above Signals:: print "Filesize:", kwargs['file'].filesize print "Orientation:", kwargs['file'].orientation print "Extension:", kwargs['file'].extension - signals.filebrowser_post_upload.connect(post_upload_callback) \ No newline at end of file + signals.filebrowser_post_upload.connect(post_upload_callback) diff --git a/docs/changelog.rst b/docs/changelog.rst index 740d4f5..a5d6a46 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -8,12 +8,17 @@ Changelog ========= -3.5.5 (not yet released) +3.5.6 (not yet released) ------------------------ -* Improved tests with convert/normalize (removed special chars from test filename). -* Fixed file selection after using search box (CKEditor). -* Removed encoding of file URIs with CKEditor. +3.5.5 (April 13th, 2014) +------------------------ + +* New: Added client-side (JavaScript) file extension validation to the AJAX uploader. +* New: Added experimental Python 3.3 support. +* Improved: Tests with convert/normalize (removed special chars from test filename). +* Fixed: File selection after using search box (CKEditor). +* Fixed: Removed encoding of file URIs with CKEditor. 3.5.4 (February 21st, 2014) --------------------------- @@ -87,7 +92,7 @@ Changelog ----------------- * Fixed security bug: added staff_member_required decorator to the upload-function. -* Fixed a XSS vulnerability with fb_tags. +* Fixed a XSS vulnerability with fb_tags. 3.4.1 (7.3.2012) ---------------- @@ -108,4 +113,4 @@ Changelog 3.4.0 (15/11/2011) ------------------ -* Final release of 3.4, see :ref:`releasenotes` \ No newline at end of file +* Final release of 3.4, see :ref:`releasenotes` diff --git a/docs/index.rst b/docs/index.rst index 57c9d14..81e1b91 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -90,7 +90,7 @@ Use the `FileBrowser Google Group 1: return HttpResponseBadRequest('Invalid request! Multiple files included.') - filedata = request.FILES.values()[0] + filedata = list(request.FILES.values())[0] fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("filebrowser:fb_upload", current_app=self.name)) folder = fb_uploadurl_re.sub('', folder) diff --git a/filebrowser/static/filebrowser/img/TEST IMAGE ***.jpg b/filebrowser/static/filebrowser/img/TEST IMAGE 000.jpg similarity index 100% rename from filebrowser/static/filebrowser/img/TEST IMAGE ***.jpg rename to filebrowser/static/filebrowser/img/TEST IMAGE 000.jpg diff --git a/filebrowser/static/filebrowser/js/fileuploader.js b/filebrowser/static/filebrowser/js/fileuploader.js index d813cfb..7afbb68 100644 --- a/filebrowser/static/filebrowser/js/fileuploader.js +++ b/filebrowser/static/filebrowser/js/fileuploader.js @@ -447,7 +447,7 @@ qq.FileUploaderBasic.prototype = { return name; }, _isAllowedExtension: function(fileName){ - var ext = (-1 !== fileName.indexOf('.')) ? fileName.replace(/.*[.]/, '').toLowerCase() : ''; + var ext = (-1 !== fileName.indexOf('.')) ? fileName.substr(fileName.lastIndexOf('.')).toLowerCase() : ''; var allowed = this._options.allowedExtensions; if (!allowed.length){return true;} @@ -1247,4 +1247,4 @@ qq.extend(qq.UploadHandlerXhr.prototype, { this._xhrs[id] = null; } } -}); \ No newline at end of file +}); diff --git a/filebrowser/templates/filebrowser/upload.html b/filebrowser/templates/filebrowser/upload.html index 3774748..cce7b0b 100644 --- a/filebrowser/templates/filebrowser/upload.html +++ b/filebrowser/templates/filebrowser/upload.html @@ -46,6 +46,7 @@ 'csrf_xname': 'X-CSRFToken', 'folder': '{{ query.dir|escapejs }}', }, + allowedExtensions: {% get_file_extensions request.GET %}, sizeLimit: {{ settings_var.MAX_UPLOAD_SIZE|unlocalize }}, minSizeLimit: 0, debug: false, diff --git a/filebrowser/templatetags/fb_tags.py b/filebrowser/templatetags/fb_tags.py index 524b812..47a8757 100644 --- a/filebrowser/templatetags/fb_tags.py +++ b/filebrowser/templatetags/fb_tags.py @@ -5,7 +5,7 @@ from django.utils.http import urlquote # FILEBROWSER IMPORTS -from filebrowser.settings import SELECT_FORMATS +from filebrowser.settings import EXTENSIONS, SELECT_FORMATS register = template.Library() @@ -52,7 +52,7 @@ def get_query_string(p, new_params=None, remove=None): if remove is None: remove = [] for r in remove: - for k in p.keys(): + for k in list(p): #if k.startswith(r): if k == r: del p[k] @@ -136,8 +136,22 @@ def selectable(parser, token): try: tag, filetype, format = token.split_contents() except: - raise TemplateSyntaxError, "%s tag requires 2 arguments" % token.contents.split()[0] + raise TemplateSyntaxError("%s tag requires 2 arguments" % token.contents.split()[0]) return SelectableNode(filetype, format) register.tag(selectable) + + +def get_file_extensions(qs): + extensions = [] + if "type" in qs and qs.get("type") in SELECT_FORMATS: + for format in SELECT_FORMATS.get(qs.get("type"), []): + extensions.extend(EXTENSIONS[format]) + else: + for k, v in EXTENSIONS.items(): + for item in v: + if item: extensions.append(item) + return extensions + +register.simple_tag(get_file_extensions) diff --git a/filebrowser/templatetags/fb_versions.py b/filebrowser/templatetags/fb_versions.py index 78c41a0..624d617 100644 --- a/filebrowser/templatetags/fb_versions.py +++ b/filebrowser/templatetags/fb_versions.py @@ -44,7 +44,7 @@ def render(self, context): try: version = fileobject.version_generate(version_suffix) return version.url - except Exception, e: + except Exception as e: if settings.TEMPLATE_DEBUG: raise e return "" @@ -93,7 +93,7 @@ def render(self, context): try: version = fileobject.version_generate(version_suffix) context[self.var_name] = version - except Exception, e: + except Exception as e: if settings.TEMPLATE_DEBUG: raise e context[self.var_name] = "" @@ -146,9 +146,9 @@ def version_setting(parser, token): try: tag, version_suffix = token.split_contents() except: - raise TemplateSyntaxError, "%s tag requires 1 argument" % token.contents.split()[0] + raise TemplateSyntaxError("%s tag requires 1 argument" % token.contents.split()[0]) if (version_suffix[0] == version_suffix[-1] and version_suffix[0] in ('"', "'")) and version_suffix.lower()[1:-1] not in VERSIONS: - raise TemplateSyntaxError, "%s tag received bad version_suffix %s" % (tag, version_suffix) + raise TemplateSyntaxError("%s tag received bad version_suffix %s" % (tag, version_suffix)) return VersionSettingNode(version_suffix) diff --git a/filebrowser/tests/test_base.py b/filebrowser/tests/test_base.py index 97af575..5c739cc 100644 --- a/filebrowser/tests/test_base.py +++ b/filebrowser/tests/test_base.py @@ -301,7 +301,7 @@ def test_version_attributes_1(self): } filebrowser.base.ADMIN_VERSIONS = ['large'] # expected test results - version_list = ['fb_test_directory/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg', 'fb_test_directory/fb_tmp_dir/fb_tmp_dir_sub/testimage_admin_thumbnail.jpg'] + version_list = ['fb_test_directory/fb_tmp_dir/fb_tmp_dir_sub/testimage_admin_thumbnail.jpg', 'fb_test_directory/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg',] admin_version_list = ['fb_test_directory/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg'] self.assertEqual(self.f_image.is_version, False) @@ -346,7 +346,7 @@ def test_version_attributes_2(self): } filebrowser.base.ADMIN_VERSIONS = ['large'] # expected test results - version_list = ['fb_test_directory/_versions/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg', 'fb_test_directory/_versions/fb_tmp_dir/fb_tmp_dir_sub/testimage_admin_thumbnail.jpg'] + version_list = ['fb_test_directory/_versions/fb_tmp_dir/fb_tmp_dir_sub/testimage_admin_thumbnail.jpg', 'fb_test_directory/_versions/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg'] admin_version_list = ['fb_test_directory/_versions/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg'] self.assertEqual(self.f_image.is_version, False) @@ -392,7 +392,7 @@ def test_version_attributes_3(self): } filebrowser.base.ADMIN_VERSIONS = ['large'] # expected test results - version_list = ['_versionstestdirectory/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg', '_versionstestdirectory/fb_tmp_dir/fb_tmp_dir_sub/testimage_admin_thumbnail.jpg'] + version_list = ['_versionstestdirectory/fb_tmp_dir/fb_tmp_dir_sub/testimage_admin_thumbnail.jpg', '_versionstestdirectory/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg'] admin_version_list = ['_versionstestdirectory/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg'] self.assertEqual(self.f_image.is_version, False) diff --git a/filebrowser/tests/test_commands.py b/filebrowser/tests/test_commands.py index 51d7778..5ecaf12 100644 --- a/filebrowser/tests/test_commands.py +++ b/filebrowser/tests/test_commands.py @@ -6,7 +6,6 @@ import posixpath import shutil import sys -import StringIO # DJANGO IMPORTS from django.conf import settings @@ -15,6 +14,7 @@ from django.utils.encoding import filepath_to_uri from django.template import Context, Template, TemplateSyntaxError from django.core.management import call_command +from django.utils.six import StringIO # FILEBROWSER IMPORTS import filebrowser @@ -100,7 +100,7 @@ def test_fb_version_generate(self): # no versions self.assertEqual(os.path.exists(os.path.join(settings.MEDIA_ROOT, "fb_test_directory/_versions/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg")), False) - sys.stdin = StringIO.StringIO("large") + sys.stdin = StringIO("large") call_command('fb_version_generate', 'fb_test_directory') # versions diff --git a/filebrowser/tests/test_sites.py b/filebrowser/tests/test_sites.py index a2172de..9611c1b 100644 --- a/filebrowser/tests/test_sites.py +++ b/filebrowser/tests/test_sites.py @@ -13,7 +13,6 @@ import os import sys import shutil -from urllib import urlencode from types import MethodType # DJANGO IMPORTS @@ -22,6 +21,10 @@ from django.core.urlresolvers import get_resolver, get_urlconf, resolve, reverse from django.contrib.admin.templatetags.admin_static import static from django.test.utils import override_settings +try: + from django.utils.six.moves.urllib.parse import urlencode +except: + from django.utils.http import urlencode # FILEBROWSER IMPORTS import filebrowser.settings @@ -185,8 +188,8 @@ def test_convert_normalize(test): """ url = reverse('%s:fb_do_upload' % test.site_name) - url = '?'.join([url, urlencode({'folder': test.tmpdir.path_relative_directory, 'qqfile': 'TEST IMAGE ***.jpg'})]) - f = open(os.path.join(FILEBROWSER_PATH, u'static/filebrowser/img/TEST IMAGE ***.jpg'), "rb") + url = '?'.join([url, urlencode({'folder': test.tmpdir.path_relative_directory, 'qqfile': 'TEST IMAGE 000.jpg'})]) + f = open(os.path.join(FILEBROWSER_PATH, u'static/filebrowser/img/TEST IMAGE 000.jpg'), "rb") # Save settings oe = filebrowser.sites.OVERWRITE_EXISTING @@ -198,84 +201,92 @@ def test_convert_normalize(test): filebrowser.sites.NORMALIZE_FILENAME = False filebrowser.utils.CONVERT_FILENAME = False filebrowser.utils.NORMALIZE_FILENAME = False - response = test.c.post(url, data={'qqfile': 'TEST IMAGE ***.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - path = os.path.join(test.tmpdir.path, 'TEST IMAGE ***.jpg') + response = test.c.post(url, data={'qqfile': 'TEST IMAGE 000.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') + path = os.path.join(test.tmpdir.path, 'TEST IMAGE 000.jpg') test.assertTrue(test.site.storage.exists(path)) - test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE ***.jpg', u'testimage.jpg', u'testimage_1.jpg']) + test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE 000.jpg', u'testimage.jpg', u'testimage_1.jpg']) # OVERWRITE true filebrowser.sites.OVERWRITE_EXISTING = True - response = test.c.post(url, data={'qqfile': 'TEST IMAGE ***.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - path = os.path.join(test.tmpdir.path, 'TEST IMAGE ***.jpg') + response = test.c.post(url, data={'qqfile': 'TEST IMAGE 000.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') + path = os.path.join(test.tmpdir.path, 'TEST IMAGE 000.jpg') test.assertTrue(test.site.storage.exists(path)) - path = os.path.join(test.tmpdir.path, 'TEST IMAGE ***_1.jpg') + path = os.path.join(test.tmpdir.path, 'TEST IMAGE 000_1.jpg') test.assertFalse(test.site.storage.exists(path)) - test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE ***.jpg', u'testimage.jpg', u'testimage_1.jpg']) + test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE 000.jpg', u'testimage.jpg', u'testimage_1.jpg']) # OVERWRITE false filebrowser.sites.OVERWRITE_EXISTING = False - response = test.c.post(url, data={'qqfile': 'TEST IMAGE ***.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - path = os.path.join(test.tmpdir.path, 'TEST IMAGE ***.jpg') + response = test.c.post(url, data={'qqfile': 'TEST IMAGE 000.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') + path = os.path.join(test.tmpdir.path, 'TEST IMAGE 000.jpg') test.assertTrue(test.site.storage.exists(path)) - path = os.path.join(test.tmpdir.path, 'TEST IMAGE ***_1.jpg') + path = os.path.join(test.tmpdir.path, 'TEST IMAGE 000_1.jpg') test.assertTrue(test.site.storage.exists(path)) - test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE ***.jpg', u'TEST IMAGE ***_1.jpg', u'testimage.jpg', u'testimage_1.jpg']) + test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE 000.jpg', u'TEST IMAGE 000_1.jpg', u'testimage.jpg', u'testimage_1.jpg']) # Set CONVERT_FILENAME, NORMALIZE_FILENAME filebrowser.sites.CONVERT_FILENAME = True filebrowser.sites.NORMALIZE_FILENAME = False filebrowser.utils.CONVERT_FILENAME = True filebrowser.utils.NORMALIZE_FILENAME = False - response = test.c.post(url, data={'qqfile': 'TEST IMAGE ***.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - path = os.path.join(test.tmpdir.path, 'test_image_***.jpg') + response = test.c.post(url, data={'qqfile': 'TEST IMAGE 000.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') + path = os.path.join(test.tmpdir.path, 'test_image_000.jpg') test.assertTrue(test.site.storage.exists(path)) - test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE ***.jpg', u'TEST IMAGE ***_1.jpg', u'test_image_***.jpg', u'testimage.jpg', u'testimage_1.jpg']) + test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE 000.jpg', u'TEST IMAGE 000_1.jpg', u'test_image_000.jpg', u'testimage.jpg', u'testimage_1.jpg']) # OVERWRITE true filebrowser.sites.OVERWRITE_EXISTING = True - response = test.c.post(url, data={'qqfile': 'TEST IMAGE ***.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - path = os.path.join(test.tmpdir.path, 'test_image_***.jpg') + response = test.c.post(url, data={'qqfile': 'TEST IMAGE 000.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') + path = os.path.join(test.tmpdir.path, 'test_image_000.jpg') test.assertTrue(test.site.storage.exists(path)) - path = os.path.join(test.tmpdir.path, 'test_image_***_1.jpg') + path = os.path.join(test.tmpdir.path, 'test_image_000_1.jpg') test.assertFalse(test.site.storage.exists(path)) - test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE ***.jpg', u'TEST IMAGE ***_1.jpg', u'test_image_***.jpg', u'testimage.jpg', u'testimage_1.jpg']) + test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE 000.jpg', u'TEST IMAGE 000_1.jpg', u'test_image_000.jpg', u'testimage.jpg', u'testimage_1.jpg']) # OVERWRITE false filebrowser.sites.OVERWRITE_EXISTING = False - response = test.c.post(url, data={'qqfile': 'TTEST IMAGE ***.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - path = os.path.join(test.tmpdir.path, 'test_image_***.jpg') + response = test.c.post(url, data={'qqfile': 'TTEST IMAGE 000.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') + path = os.path.join(test.tmpdir.path, 'test_image_000.jpg') test.assertTrue(test.site.storage.exists(path)) - path = os.path.join(test.tmpdir.path, 'test_image_***_1.jpg') + path = os.path.join(test.tmpdir.path, 'test_image_000_1.jpg') test.assertTrue(test.site.storage.exists(path)) - test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE ***.jpg', u'TEST IMAGE ***_1.jpg', u'test_image_***.jpg', u'test_image_***_1.jpg', u'testimage.jpg', u'testimage_1.jpg']) + test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE 000.jpg', u'TEST IMAGE 000_1.jpg', u'test_image_000.jpg', u'test_image_000_1.jpg', u'testimage.jpg', u'testimage_1.jpg']) # Set CONVERT_FILENAME, NORMALIZE_FILENAME filebrowser.sites.CONVERT_FILENAME = True filebrowser.sites.NORMALIZE_FILENAME = True filebrowser.utils.CONVERT_FILENAME = True filebrowser.utils.NORMALIZE_FILENAME = True - response = test.c.post(url, data={'qqfile': 'TEST IMAGE ***.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - path = os.path.join(test.tmpdir.path, 'test_image.jpg') + response = test.c.post(url, data={'qqfile': 'TEST IMAGE 000.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') + path = os.path.join(test.tmpdir.path, 'test_image_000.jpg') test.assertTrue(test.site.storage.exists(path)) - test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE ***.jpg', u'TEST IMAGE ***_1.jpg', u'test_image.jpg', u'test_image_***.jpg', u'test_image_***_1.jpg', u'testimage.jpg', u'testimage_1.jpg']) + test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE 000.jpg', u'TEST IMAGE 000_1.jpg', u'test_image_000.jpg', u'test_image_000_1.jpg', u'test_image_000_2.jpg', u'testimage.jpg', u'testimage_1.jpg']) # OVERWRITE true filebrowser.sites.OVERWRITE_EXISTING = True - response = test.c.post(url, data={'qqfile': 'TEST IMAGE ***.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - path = os.path.join(test.tmpdir.path, 'test_image.jpg') + response = test.c.post(url, data={'qqfile': 'TEST IMAGE 000.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') + path = os.path.join(test.tmpdir.path, 'test_image_000.jpg') test.assertTrue(test.site.storage.exists(path)) - path = os.path.join(test.tmpdir.path, 'test_image_1.jpg') + path = os.path.join(test.tmpdir.path, 'test_image_000_1.jpg') + test.assertTrue(test.site.storage.exists(path)) + path = os.path.join(test.tmpdir.path, 'test_image_000_2.jpg') + test.assertTrue(test.site.storage.exists(path)) + path = os.path.join(test.tmpdir.path, 'test_image_000_3.jpg') test.assertFalse(test.site.storage.exists(path)) - test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE ***.jpg', u'TEST IMAGE ***_1.jpg', u'test_image.jpg', u'test_image_***.jpg', u'test_image_***_1.jpg', u'testimage.jpg', u'testimage_1.jpg']) + test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE 000.jpg', u'TEST IMAGE 000_1.jpg', u'test_image_000.jpg', u'test_image_000_1.jpg', u'test_image_000_2.jpg', u'testimage.jpg', u'testimage_1.jpg']) # OVERWRITE false filebrowser.sites.OVERWRITE_EXISTING = False - response = test.c.post(url, data={'qqfile': 'TEST IMAGE ***.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - path = os.path.join(test.tmpdir.path, 'test_image.jpg') + response = test.c.post(url, data={'qqfile': 'TEST IMAGE 000.jpg', 'file': f}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') + path = os.path.join(test.tmpdir.path, 'test_image_000.jpg') + test.assertTrue(test.site.storage.exists(path)) + path = os.path.join(test.tmpdir.path, 'test_image_000_1.jpg') + test.assertTrue(test.site.storage.exists(path)) + path = os.path.join(test.tmpdir.path, 'test_image_000_2.jpg') test.assertTrue(test.site.storage.exists(path)) - path = os.path.join(test.tmpdir.path, 'test_image_1.jpg') + path = os.path.join(test.tmpdir.path, 'test_image_000_3.jpg') test.assertTrue(test.site.storage.exists(path)) - test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE ***.jpg', u'TEST IMAGE ***_1.jpg', u'test_image.jpg', u'test_image_***.jpg', u'test_image_***_1.jpg', u'test_image_1.jpg', u'testimage.jpg', u'testimage_1.jpg']) + test.assertEqual(sorted(test.site.storage.listdir(test.tmpdir)[1]), [u'TEST IMAGE 000.jpg', u'TEST IMAGE 000_1.jpg', u'test_image_000.jpg', u'test_image_000_1.jpg', u'test_image_000_2.jpg', u'test_image_000_3.jpg', u'testimage.jpg', u'testimage_1.jpg']) # Reset settings filebrowser.sites.CONVERT_FILENAME = cf @@ -388,7 +399,7 @@ def setUp(self): def tearDown(self): # Delete a left-over tmp directories, if there's any if hasattr(self, 'tmpdir') and self.tmpdir: - print "Removing left-over tmp dir:", self.tmpdir.path + print("Removing left-over tmp dir:", self.tmpdir.path) self.site.storage.rmtree(self.tmpdir.path) @@ -417,13 +428,13 @@ def runTest(self): ## Create a test class for each deployed filebrowser site for site in all_sites: - print 'Creating Test for the FileBrowser site:', site + print('Creating Test for the FileBrowser site:', site) # Create a subclass of TestCase testcase_class = type('TestSite_' + site, (TestCase,), {'site_name': site, 'c': Client(), 'tmpdirs': None}) # Add setUp, tearDown, and runTest methods - setattr(testcase_class, 'setUp', MethodType(setUp, None, testcase_class)) - setattr(testcase_class, 'tearDown', MethodType(tearDown, None, testcase_class)) - setattr(testcase_class, 'runTest', MethodType(runTest, None, testcase_class)) + setattr(testcase_class, 'setUp', setUp) + setattr(testcase_class, 'tearDown', tearDown) + setattr(testcase_class, 'runTest', runTest) # Add the test case class to this module setattr(this_module, 'TestSite_' + site, testcase_class) diff --git a/filebrowser/tests/test_templatetags.py b/filebrowser/tests/test_templatetags.py new file mode 100644 index 0000000..2dfb70c --- /dev/null +++ b/filebrowser/tests/test_templatetags.py @@ -0,0 +1,20 @@ +# coding: utf-8 + +# DJANGO IMPORTS +from django.test import TestCase +from django.http import QueryDict + +# FILEBROWSER IMPORTS +from filebrowser.templatetags.fb_tags import get_file_extensions + + +class TemplateTagsTests(TestCase): + def test_get_file_extensions(self): + self.assertEqual(sorted(get_file_extensions('')), + sorted(['.pdf', '.doc', '.rtf', '.txt', '.xls', '.csv', '.docx', '.mov', + '.wmv', '.mpeg', '.mpg', '.avi', '.rm', '.jpg', '.jpeg', '.gif', '.png', + '.tif', '.tiff', '.mp3', '.mp4', '.wav', '.aiff', '.midi', '.m4p'])) + self.assertEqual( + get_file_extensions(QueryDict('type=image')), + ['.jpg', '.jpeg', '.gif', '.png', '.tif', '.tiff'] + ) diff --git a/filebrowser/utils.py b/filebrowser/utils.py index f1f9008..dd805e9 100644 --- a/filebrowser/utils.py +++ b/filebrowser/utils.py @@ -5,6 +5,9 @@ import os import unicodedata +# DJANGO IMPORTS +from django.utils import six + # FILEBROWSER IMPORTS from filebrowser.settings import STRICT_PIL, NORMALIZE_FILENAME, CONVERT_FILENAME @@ -27,7 +30,7 @@ def convert_filename(value): chunks = value.split(os.extsep) normalized = [] for v in chunks: - v = unicodedata.normalize('NFKD', unicode(v)).encode('ascii', 'ignore') + v = unicodedata.normalize('NFKD', six.text_type(v)).encode('ascii', 'ignore').decode('ascii') v = re.sub(r'[^\w\s-]', '', v).strip() normalized.append(v) diff --git a/setup.py b/setup.py index d2f41f4..41f914e 100644 --- a/setup.py +++ b/setup.py @@ -25,5 +25,4 @@ def read(fname): 'Programming Language :: Python', ], zip_safe = False, - use_2to3=True, )