* adding xml and json to the editable files
* try to save attachment, on failure encode and try to save again
This commit is contained in:
Piotr Zalewa 2012-09-13 23:25:05 +02:00
Родитель 68987e7baa
Коммит ad33de2c65
4 изменённых файлов: 50 добавлений и 18 удалений

Просмотреть файл

@ -59,7 +59,7 @@ from elasticutils.utils import retry_on_timeout
log = commonware.log.getLogger('f.jetpack')
EDITABLE_EXTENSIONS = ("html", "css", "js", "txt")
EDITABLE_EXTENSIONS = ("html", "css", "js", "txt", "xml", "json")
def make_name(value=None):
" wrap for slugify "
@ -2376,6 +2376,11 @@ class Attachment(BaseModel):
kwargs['encoding'] = 'utf-8'
kwargs['mode'] = 'w'
try:
with codecs.open(self.get_file_path(), **kwargs) as f:
f.write(data)
except UnicodeDecodeError, UnicodeEncodeError:
data = unicode(data, 'utf-8')
try:
with codecs.open(self.get_file_path(), **kwargs) as f:
f.write(data)

Просмотреть файл

@ -125,6 +125,27 @@ class AttachmentTest(TestCase):
assert os.path.isfile(attachment.get_file_path())
assert attachment.read()
def test_attachment_with_kanji_from_web(self):
url = ("file://%s/apps/jetpack/tests/"
"sample_attachment_kanji_UTF8_without_BOM.json") % settings.ROOT
URLField.clean = Mock(return_value=url)
addon = Package.objects.create(
type='a',
author=self.author)
self.author.set_password('secure')
self.author.save()
self.client.login(username=self.author.username, password='secure')
response = self.client.post(addon.latest.get_add_attachment_url(), {
'url': url,
'filename': 'sample_attachment_kanji_UTF8_without_BOM.json',
'force_contenttype': 'utf-8'})
eq_(response.status_code, 200)
addon = Package.objects.get(author=self.author)
eq_(addon.latest.revision_number, 1)
attachment = addon.latest.attachments.get()
eq_('sample_attachment_kanji_UTF8_without_BOM', attachment.filename)
assert os.path.isfile(attachment.get_file_path())
assert attachment.read()
def test_create_image_attachment(self):
" test simply shouldn't raise any errors "

Просмотреть файл

@ -0,0 +1,3 @@
{ "test": [
{"country":"日本"}
]}

Просмотреть файл

@ -0,0 +1,3 @@
{ "test": [
{"country":"日本"}
]}