Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

better handling of filenames

  • Loading branch information...
commit fce9dc8fa4884f750a34bea5a0050406789bff27 1 parent 4bb79a4
@fcurella authored
View
2  django_zipfile/__init__.py
@@ -1,3 +1,3 @@
-VERSION = (0, 1, 4)
+VERSION = (0, 1, 5)
from django_zipfile.templatezipfile import TemplateZipFile
View
17 django_zipfile/templatezipfile.py
@@ -55,8 +55,17 @@ def _filename(self, templates):
if self.template_root is not None:
template_root = self.template_root[0]
- return template_name.split(template_root)[1]
- return template_name.split('/')[-1]
+ filename = template_name.split(template_root)[1]
+ else:
+ filename = template_name.split('/')[-1]
+
+ # Zipfile.writestr`` on Python2.5 can't handle unicode.
+ # Note that trying to re-encode a utf-8 encodef str fails if it contains
+ # characters outside of the ASCII range. Hence te type-check.
+ if isinstance(filename, unicode):
+ filename = filename.encode('utf-8')
+
+ return filename
def _to_list(self, var):
if isinstance(var, basestring):
@@ -85,9 +94,9 @@ def write_template(self, template_list, filename=None, context=None, compress_ty
filename = self._filename(templates_hierarchy)
if compress_type is not None:
- self.writestr(filename.encode('utf-8'), render.encode('utf-8'), compress_type)
+ self.writestr(filename, render.encode('utf-8'), compress_type)
else:
- self.writestr(filename.encode('utf-8'), render.encode('utf-8'))
+ self.writestr(filename, render.encode('utf-8'))
def write_template_dir(self, directory, context=None, compress_type=None):
self._check_individual_compression_supported(compress_type)
View
8 tests/__init__.py
@@ -51,6 +51,14 @@ def test_names(self):
templates = self.zipfile2._templates(['folderone/onetwo.txt', 'folderone/oneone.txt'])
self.assertEqual(['override/folderone/onetwo.txt', 'override/folderone/oneone.txt'], templates)
+ templates = self.zipfile2._templates([u'snowman_\u2603.txt'])
+ filename = self.zipfile2._filename(templates)
+ self.assertEqual('snowman_\xe2\x98\x83.txt', filename)
+
+ templates = self.zipfile2._templates(['snowman_\xe2\x98\x83.txt'])
+ filename = self.zipfile2._filename(templates)
+ self.assertEqual('snowman_\xe2\x98\x83.txt', filename)
+
def test_adding_files(self):
self.assertRaises(TemplateDoesNotExist, self.zipfile1.write_template, 'abc.html')
Please sign in to comment.
Something went wrong with that request. Please try again.