Skip to content

Commit

Permalink
Fix asset serialization on empty files (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlitvakb committed Jul 8, 2019
1 parent 971173a commit ae3cb94
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,8 @@
# CHANGELOG

## Unreleased
### Fixed
* Assets with empty or missing files no longer error when trying to get it serialized to string. [#48](https://github.com/contentful/contentful.py/issues/48)

## v1.12.1
### Fixed
Expand Down
3 changes: 3 additions & 0 deletions contentful/asset.py
Expand Up @@ -30,6 +30,9 @@ def url(self, **kwargs):
"//images.contentful.com/spaces/foobar/...?w=120&h=160"
"""

if not hasattr(self, 'file') or not self.file:
return ""

url = self.file['url']
args = ['{0}={1}'.format(k, v) for k, v in kwargs.items()]

Expand Down
47 changes: 47 additions & 0 deletions tests/asset_test.py
Expand Up @@ -38,3 +38,50 @@ def test_asset(self):
self.assertEqual(asset.url(), '//images.contentful.com/...')
self.assertEqual(asset.url(w=123), '//images.contentful.com/...?w=123')
self.assertEqual(str(asset), "<Asset id='foo' url='//images.contentful.com/...'>")

def test_asset_with_no_file_can_be_serialized_correctly(self):
asset = Asset({
'sys': {
'type': 'Asset',
'id': 'foo',
'createdAt': '2016-12-01T11:39:20.257859',
'updatedAt': '2016-12-01T11:39:20.257859',
'space': {
'sys': {
'type': 'Link',
'linkType': 'Space',
'id': 'foo'
}
}
},
'fields': {
'title': 'Awesome Pic',
'description': 'A picture of something'
}
})

self.assertEqual(str(asset), "<Asset id='foo' url=''>")

def test_asset_with_empty_file_can_be_serialized_correctly(self):
asset = Asset({
'sys': {
'type': 'Asset',
'id': 'foo',
'createdAt': '2016-12-01T11:39:20.257859',
'updatedAt': '2016-12-01T11:39:20.257859',
'space': {
'sys': {
'type': 'Link',
'linkType': 'Space',
'id': 'foo'
}
}
},
'fields': {
'title': 'Awesome Pic',
'description': 'A picture of something',
'file': {}
}
})

self.assertEqual(str(asset), "<Asset id='foo' url=''>")

0 comments on commit ae3cb94

Please sign in to comment.