Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix asset serialization on empty files #49

Merged
merged 1 commit into from Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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=''>")