diff options
author | Aleksey Lim <alsroot@sugarlabs.org> | 2012-11-02 15:58:21 (GMT) |
---|---|---|
committer | Aleksey Lim <alsroot@sugarlabs.org> | 2012-11-02 20:50:22 (GMT) |
commit | f3e8a492277ca660331d5183a3faccb4a7c9143d (patch) | |
tree | d28fabeb59a303674a2357fbb18c53e19f69ec83 | |
parent | 96520a65957acce39f144c26d15c02158dea1db1 (diff) |
Polish design, since commands processor the only access point, typecast input data there
-rw-r--r-- | active_document/commands.py | 1 | ||||
-rw-r--r-- | active_document/directory.py | 13 | ||||
-rw-r--r-- | active_document/metadata.py | 4 | ||||
-rw-r--r-- | active_document/volume.py | 8 | ||||
-rwxr-xr-x | tests/units/document.py | 3 | ||||
-rwxr-xr-x | tests/units/volume.py | 17 |
6 files changed, 26 insertions, 20 deletions
diff --git a/active_document/commands.py b/active_document/commands.py index c904032..185eb6f 100644 --- a/active_document/commands.py +++ b/active_document/commands.py @@ -282,6 +282,7 @@ class _ObjectCommand(_Command): def get_args(self, request): document = self._directory.get(request['guid']) + document.request = request return (document,) diff --git a/active_document/directory.py b/active_document/directory.py index c19bbac..a298278 100644 --- a/active_document/directory.py +++ b/active_document/directory.py @@ -391,19 +391,6 @@ class Directory(object): self._notify({'event': 'commit'}) def _post(self, guid, props, new): - for prop_name, value in props.items(): - prop = self.metadata[prop_name] - enforce(isinstance(prop, StoredProperty), - 'Property %r in %r cannot be set', - prop_name, self.metadata.name) - try: - props[prop_name] = prop.decode(value) - except Exception: - error = 'Value %r for %r property for %r is invalid' % \ - (value, prop_name, self.metadata.name) - util.exception(error) - raise RuntimeError(error) - event = {'event': 'create' if new else 'update', 'props': props.copy(), 'guid': guid, diff --git a/active_document/metadata.py b/active_document/metadata.py index f0e54e0..c79f810 100644 --- a/active_document/metadata.py +++ b/active_document/metadata.py @@ -182,6 +182,8 @@ class Property(object): def decode(self, value): """Convert property value according to its `typecast`.""" + if self.typecast is None: + return value return _decode(self.typecast, value) def to_string(self, value): @@ -351,7 +353,7 @@ def _decode(typecast, value): value, ', '.join([str(i) for i in typecast])) elif isinstance(typecast, types.FunctionType): value = typecast(value) - elif typecast in [None, str]: + elif typecast is str: if isinstance(value, unicode): value = value.encode('utf-8') else: diff --git a/active_document/volume.py b/active_document/volume.py index d5b7f92..5206c66 100644 --- a/active_document/volume.py +++ b/active_document/volume.py @@ -250,7 +250,13 @@ class VolumeCommands(CommandsProcessor): else: if prop.localized and isinstance(value, basestring): value = {(request.accept_language or self._lang)[0]: value} - doc.props[name] = value + try: + doc.props[name] = prop.decode(value) + except Exception, error: + error = 'Value %r for %r property is invalid: %s' % \ + (value, prop.name, error) + util.exception(error) + raise RuntimeError(error) yield directory, doc diff --git a/tests/units/document.py b/tests/units/document.py index 34f76bd..6ccd0f0 100755 --- a/tests/units/document.py +++ b/tests/units/document.py @@ -169,13 +169,10 @@ class DocumentTest(tests.Test): directory = Directory(tests.tmpdir, Document, IndexWriter) - self.assertRaises(RuntimeError, directory.create, {'blob': 'probe'}) - guid = directory.create({}) blob_path = join(tests.tmpdir, guid[:2], guid, 'blob') self.assertEqual(ad.PropertyMeta(), directory.get(guid).blob) - self.assertRaises(RuntimeError, directory.update, guid, {'blob': 'foo'}) data = 'payload' directory.set_blob(guid, 'blob', StringIO(data)) diff --git a/tests/units/volume.py b/tests/units/volume.py index 3977b2b..42f7603 100755 --- a/tests/units/volume.py +++ b/tests/units/volume.py @@ -236,6 +236,19 @@ class VolumeTest(tests.Test): sorted(['prop']), sorted(self.call('GET', document='testdocument', reply=['prop'])['result'][0].keys())) + def test_DecodeBeforeSetting(self): + + class TestDocument(Document): + + @active_property(slot=1, typecast=int) + def prop(self, value): + return value + + self.volume = SingleVolume(tests.tmpdir, [TestDocument]) + + guid = self.call(method='POST', document='testdocument', content={'prop': '-1'}) + self.assertEqual(-1, self.call(method='GET', document='testdocument', guid=guid, prop='prop')) + def test_LocalizedSet(self): env.DEFAULT_LANG = 'en' @@ -850,7 +863,7 @@ class VolumeTest(tests.Test): self.call('POST', document='testdocument', content={'prop': 1}) self.assertEqual( - sorted([{'prop': '1'}, {'prop': '2'}]), + sorted([{'prop': 1}, {'prop': 2}]), sorted(self.call('GET', document='testdocument', reply='prop', group_by='prop')['result'])) def test_CallSetterEvenIfThereIsNoCreatePermissions(self): @@ -870,7 +883,7 @@ class VolumeTest(tests.Test): self.assertRaises(ad.Forbidden, self.call, 'POST', document='testdocument', content={'prop': 1}) guid = self.call('POST', document='testdocument', content={}) - self.assertEqual('1', self.call('GET', document='testdocument', guid=guid, prop='prop')) + self.assertEqual(1, self.call('GET', document='testdocument', guid=guid, prop='prop')) def call(self, method, document=None, guid=None, prop=None, accept_language=None, content=None, content_stream=None, |