diff options
author | Aleksey Lim <alsroot@sugarlabs.org> | 2012-09-24 09:42:56 (GMT) |
---|---|---|
committer | Aleksey Lim <alsroot@sugarlabs.org> | 2012-09-24 09:42:56 (GMT) |
commit | 6dc4237b46dbe600d3e88cfafc5aa07e29f4bfa8 (patch) | |
tree | dd5285c47716391aea46985be8d75c72e0300e3f | |
parent | b213ba6544df29f032dc1c7383dfe50f7c4b2953 (diff) |
Simplify returning BLOB urls from GET commands
-rw-r--r-- | sugar_network/local/mounts.py | 6 | ||||
-rw-r--r-- | sugar_network/node/commands.py | 38 | ||||
-rw-r--r-- | sugar_network/resources/context.py | 6 | ||||
-rwxr-xr-x | tests/units/datastore.py | 4 | ||||
-rwxr-xr-x | tests/units/injector.py | 2 | ||||
-rwxr-xr-x | tests/units/node.py | 19 | ||||
-rwxr-xr-x | tests/units/remote_mount.py | 8 | ||||
-rwxr-xr-x | tests/units/router.py | 22 |
8 files changed, 32 insertions, 73 deletions
diff --git a/sugar_network/local/mounts.py b/sugar_network/local/mounts.py index 56c6519..6bcc813 100644 --- a/sugar_network/local/mounts.py +++ b/sugar_network/local/mounts.py @@ -82,8 +82,10 @@ class LocalMount(ad.VolumeCommands, _Mount): @ad.property_command(method='GET', cmd='get_blob') def get_blob(self, document, guid, prop, request=None): directory = self.volume[document] - directory.metadata[prop].assert_access(ad.ACCESS_READ) - return directory.get(guid).meta(prop) + prop = directory.metadata[prop] + prop.assert_access(ad.ACCESS_READ) + doc = directory.get(guid) + return prop.on_get(doc, doc.meta(prop.name)) @ad.property_command(method='PUT', cmd='upload_blob') def upload_blob(self, document, guid, prop, path, pass_ownership=False): diff --git a/sugar_network/node/commands.py b/sugar_network/node/commands.py index 6b4c64f..83a0520 100644 --- a/sugar_network/node/commands.py +++ b/sugar_network/node/commands.py @@ -107,8 +107,8 @@ class NodeCommands(ad.VolumeCommands, Commands): props['user'] = [request.principal] self._set_author(props) - implement = props.get('implement') - if self._is_master and implement: + if self._is_master and 'implement' in props: + implement = props['implement'] if not isinstance(implement, basestring): implement = implement[0] props['guid'] = implement @@ -150,37 +150,15 @@ class NodeCommands(ad.VolumeCommands, Commands): return result def _mixin_blobs(self, request, result): - reply = request.get('reply') - if not reply: - return - document = request['document'] - blobs = set(reply) & self.get_blobs(document) - if not blobs: - return - for props in result: guid = props.get('guid') or request['guid'] - doc = self.volume[document].get(guid) - for name in blobs: - - def compose_url(value): - if value is None: - value = '/'.join(['', document, guid, name]) - if value.startswith('/'): - return 'http://' + request.environ['HTTP_HOST'] + value - else: - return value - - url = None - meta = doc.meta(name) - if meta is not None: - url = meta.url() - - if type(url) is list: - props[name] = [compose_url(i.get('url')) for i in url] - else: - props[name] = compose_url(url) + for name, value in props.items(): + if not isinstance(value, ad.PropertyMeta): + continue + props[name] = value.url( + default='/'.join(['', document, guid, name]), + prefix='http://' + request.environ['HTTP_HOST']) def _set_author(self, props): users = self.volume['user'] diff --git a/sugar_network/resources/context.py b/sugar_network/resources/context.py index 5b7931f..36cec2d 100644 --- a/sugar_network/resources/context.py +++ b/sugar_network/resources/context.py @@ -62,12 +62,12 @@ class Context(Resource): def icon(self, value): if value is None: if 'package' in self['type']: - return ad.Meta( + return ad.PropertyMeta( url='/static/images/package.png', path=join(static.PATH, 'images', 'package.png'), mime_type='image/png') else: - return ad.Meta( + return ad.PropertyMeta( url='/static/images/missing.png', path=join(static.PATH, 'images', 'missing.png'), mime_type='image/png') @@ -77,7 +77,7 @@ class Context(Resource): @ad.active_property(ad.BlobProperty, mime_type='image/svg+xml') def artifact_icon(self, value): if value is None: - return ad.Meta( + return ad.PropertyMeta( url='/static/images/missing.svg', path=join(static.PATH, 'images', 'missing.svg'), mime_type='image/svg+xml') diff --git a/tests/units/datastore.py b/tests/units/datastore.py index 6f12f2e..de1e67d 100755 --- a/tests/units/datastore.py +++ b/tests/units/datastore.py @@ -104,7 +104,7 @@ class DatastoreTest(tests.Test): 'user': [], 'author': [], }, - artifacts.get('1').properties()) + artifacts.get('1').properties(['guid', 'context', 'activity_id', 'ctime', 'description', 'keep', 'mime_type', 'tags', 'timestamp', 'mtime', 'title', 'filesize', 'traits', 'layer', 'user', 'author'])) self.assertEqual( 'preview-1', file(artifacts.get('1').meta('preview')['path']).read()) @@ -130,7 +130,7 @@ class DatastoreTest(tests.Test): 'user': [], 'author': [], }, - artifacts.get('2').properties()) + artifacts.get('2').properties(['guid', 'context', 'activity_id', 'ctime', 'description', 'keep', 'mime_type', 'tags', 'timestamp', 'mtime', 'title', 'filesize', 'traits', 'layer', 'user', 'author'])) self.assertEqual( 'preview-2', file(artifacts.get('2').meta('preview')['path']).read()) diff --git a/tests/units/injector.py b/tests/units/injector.py index 29acf96..0c36459 100755 --- a/tests/units/injector.py +++ b/tests/units/injector.py @@ -265,7 +265,7 @@ class InjectorTest(tests.Test): '[Activity]', 'name = TestActivity', 'bundle_id = bundle_id', - 'exec = false', + 'exec = true', 'icon = icon', 'activity_version = 1', 'license = Public Domain', diff --git a/tests/units/node.py b/tests/units/node.py index d0ff38f..b4ec682 100755 --- a/tests/units/node.py +++ b/tests/units/node.py @@ -307,19 +307,20 @@ class NodeTest(tests.Test): {'guid': guid3, 'icon': 'http://localhost/foo/bar', 'layer': ['public']}, call(cp, method='GET', document='context', guid=guid3, reply=['guid', 'icon', 'layer'])) self.assertEqual( - {'guid': guid4, 'data': 'http://localhost/report/%s/data' % guid4, 'layer': ['public']}, + {'guid': guid4, 'data': None, 'layer': ['public']}, call(cp, method='GET', document='report', guid=guid4, reply=['guid', 'data', 'layer'])) - self.assertEqual([ - {'guid': guid1, 'icon': 'http://localhost/static/images/missing.png', 'layer': ['public']}, - {'guid': guid2, 'icon': 'http://foo/bar', 'layer': ['public']}, - {'guid': guid3, 'icon': 'http://localhost/foo/bar', 'layer': ['public']}, - {'guid': guid5, 'icon': ['http://localhost/1', 'http://2'], 'layer': ['public']}, - ], - call(cp, method='GET', document='context', reply=['guid', 'icon', 'layer'])['result']) + self.assertEqual( + sorted([ + {'guid': guid1, 'icon': 'http://localhost/static/images/missing.png', 'layer': ['public']}, + {'guid': guid2, 'icon': 'http://foo/bar', 'layer': ['public']}, + {'guid': guid3, 'icon': 'http://localhost/foo/bar', 'layer': ['public']}, + {'guid': guid5, 'icon': ['http://localhost/1', 'http://2'], 'layer': ['public']}, + ]), + sorted(call(cp, method='GET', document='context', reply=['guid', 'icon', 'layer'])['result'])) self.assertEqual([ - {'guid': guid4, 'data': 'http://localhost/report/%s/data' % guid4, 'layer': ['public']}, + {'guid': guid4, 'data': None, 'layer': ['public']}, ], call(cp, method='GET', document='report', reply=['guid', 'data', 'layer'])['result']) diff --git a/tests/units/remote_mount.py b/tests/units/remote_mount.py index 40e7cfb..ed8beef 100755 --- a/tests/units/remote_mount.py +++ b/tests/units/remote_mount.py @@ -312,13 +312,13 @@ class RemoteMountTest(tests.Test): self.touch((cache_path, 'blob-2')) blob = remote.get(['context', guid, 'preview'], cmd='get_blob') self.assertEqual('blob-2', file(blob['path']).read()) - self.assertEqual(3, json.load(file(cache_path + '.meta'))['seqno']) + seqno = json.load(file(cache_path + '.meta'))['seqno'] self.touch(('file', 'blob-3')) remote.put(['context', guid, 'preview'], cmd='upload_blob', path=abspath('file')) blob = remote.get(['context', guid, 'preview'], cmd='get_blob') self.assertEqual('blob-3', file(blob['path']).read()) - self.assertEqual(4, json.load(file(cache_path + '.meta'))['seqno']) + assert seqno < json.load(file(cache_path + '.meta'))['seqno'] def test_DoNotStaleBLOBs(self): self.start_ipc_and_restful_server() @@ -338,7 +338,7 @@ class RemoteMountTest(tests.Test): cache_path = 'cache/context/%s/%s/preview' % (guid[:2], guid) self.touch((cache_path, 'blob-2')) - self.assertEqual(3, json.load(file(cache_path + '.meta'))['seqno']) + seqno = json.load(file(cache_path + '.meta'))['seqno'] # Shift seqno remote.put(['context', guid], {'title': 'title-2'}) @@ -346,7 +346,7 @@ class RemoteMountTest(tests.Test): blob = remote.get(['context', guid, 'preview'], cmd='get_blob') self.assertEqual('blob-2', file(blob['path']).read()) - self.assertEqual(4, json.load(file(cache_path + '.meta'))['seqno']) + assert seqno < json.load(file(cache_path + '.meta'))['seqno'] if __name__ == '__main__': diff --git a/tests/units/router.py b/tests/units/router.py index 7b9c5ca..5c3facb 100755 --- a/tests/units/router.py +++ b/tests/units/router.py @@ -104,24 +104,6 @@ class RouterTest(tests.Test): 'result': sorted([])}, rest.get('/document', reply='guid,stored,term')) - def test_JsonAutoEncoding(self): - self.fork(self.restful_server, [User, Document]) - rest = tests.Request('http://localhost:8800') - - guid = rest.post('/document', {'term': 'term'}) - - self.assertRaises(RuntimeError, rest.get, '/document/' + guid + '/json') - - rest.put('/document/' + guid + '/json', -1) - self.assertEqual( - -1, - rest.get('/document/' + guid + '/json')) - - rest.put('/document/' + guid + '/json', {'foo': None}) - self.assertEqual( - {'foo': None}, - rest.get('/document/' + guid + '/json')) - def test_StreamedResponse(self): class CommandsProcessor(ad.CommandsProcessor): @@ -339,10 +321,6 @@ class Document(ad.Document): def blob(self, value): return value - @ad.active_property(ad.BlobProperty, mime_type='application/json') - def json(self, value): - return value - @ad.active_property(ad.StoredProperty, default='') def author(self, value): return value |