Skip to content
Browse files

Added `get_member_id_as_string` to context interface.

The main use for this is marshalling IDs for use in URLs.

Implemented for SQLAlchemy ORM context.

Added tests.
  • Loading branch information...
1 parent 42f4bab commit 50f28c1d04abdfc938cf70ba5aaf93ecbc128db5 @wylee committed Jun 22, 2011
Showing with 28 additions and 0 deletions.
  1. +3 −0 pyramid_restler/interfaces.py
  2. +17 −0 pyramid_restler/model.py
  3. +8 −0 pyramid_restler/tests.py
View
3 pyramid_restler/interfaces.py
@@ -75,3 +75,6 @@ def update_member(id, **data):
def delete_member(id):
"""Delete an existing member."""
+
+ def get_member_id_as_string(member):
+ """Get string representation of ``member`` ID."""
View
17 pyramid_restler/model.py
@@ -79,6 +79,23 @@ def delete_member(self, id):
self.session.delete(member)
self.session.commit()
+ def get_member_id(self, member):
+ pk = member._sa_instance_state.key
+ if pk is None:
+ return None
+ vals = pk[1]
+ if len(vals) == 1:
+ return vals[0]
+ else:
+ return tuple(vals)
+
+ def get_member_id_as_string(self, member):
+ id = self.get_member_id(member)
+ if isinstance(id, basestring):
+ return id
+ else:
+ return json.dumps(id, cls=self.json_encoder)
+
def to_json(self, value, fields=None, wrap=True):
"""Convert instance or sequence of instances to JSON.
View
8 pyramid_restler/tests.py
@@ -84,6 +84,11 @@ def test_member_to_json(self):
should_equal = {'id': 1, 'value': 'one'}
self.assertEqual(json.loads(json_member)['results'], should_equal)
+ def test_get_member_id_as_string(self):
+ member = self.context.get_member(1)
+ id = self.context.get_member_id_as_string(member)
+ self.assertEqual(id, '1')
+
class Test_RESTfulView(TestCase):
@@ -224,6 +229,9 @@ def delete_member(self, id):
self._collection.pop(i)
return None
+ def get_member_id_as_string(self, member):
+ return str(member['id'])
+
def to_json(self, value, fields=None, wrap=True):
if isinstance(value, dict):
value = [value]

0 comments on commit 50f28c1

Please sign in to comment.
Something went wrong with that request. Please try again.