provide __repr__() and asDict() for tracks, and other improvements
This commit is contained in:
		
							parent
							
								
									7b98616008
								
							
						
					
					
						commit
						0b765ad16b
					
				
					 5 changed files with 18 additions and 10 deletions
				
			
		|  | @ -11,11 +11,8 @@ class ITraversable(Interface): | |||
| 
 | ||||
| class IContainer(ITraversable): | ||||
| 
 | ||||
|     def items(): | ||||
|         """Return a sequence of key, value pairs of child objects.""" | ||||
| 
 | ||||
|     def keys(): | ||||
|         """Return a sequence of keys of child objects.""" | ||||
|     def values(): | ||||
|         """Return a sequence of child objects.""" | ||||
| 
 | ||||
|     def __getitem__(key): | ||||
|         """Return the item addressed by `key`; rais KeyError if not found.""" | ||||
|  |  | |||
|  | @ -39,6 +39,6 @@ class DefaultView: | |||
|         ob = self.context | ||||
|         result = dict(head=ob.head, data=ob.data) | ||||
|         if IContainer.providedBy(ob): | ||||
|             result['items'] = list(ob.keys()) | ||||
|             result['items'] = [v.asDict() for v in ob.values()] | ||||
|         return json.dumps(result) | ||||
| 
 | ||||
|  |  | |||
|  | @ -14,11 +14,14 @@ class Folder(Track): | |||
|     headFields = ['parent', 'name', 'ref'] | ||||
|     prefix = 'fldr' | ||||
| 
 | ||||
|     def values(self): | ||||
|         return self.container.query(parent=self.rid) | ||||
| 
 | ||||
|     def items(self): | ||||
|         return ((f.name, f) for f in self.container.query(parent=self.rid)) | ||||
|         return ((v.name, v) for v in self.values()) | ||||
| 
 | ||||
|     def keys(self): | ||||
|         return (k for k, v in self.items()) | ||||
|         return (v.name for v in self.values()) | ||||
| 
 | ||||
|     def get(self, key, default=None): | ||||
|         value = self.container.queryLast(parent=self.rid, name=key) | ||||
|  | @ -38,7 +41,8 @@ class Folder(Track): | |||
|         self.container.save(value) | ||||
| 
 | ||||
|     def __str__(self): | ||||
|         return 'folder: %s; keys: %s' % (self.name, list(self.keys())) | ||||
|         return '%s: %s; keys: %s' % (self.__class__.__name__, | ||||
|                 self.name, list(self.keys())) | ||||
| 
 | ||||
| 
 | ||||
| class Root(Folder): | ||||
|  |  | |||
|  | @ -62,6 +62,13 @@ class Track(object): | |||
|             return '' | ||||
|         return str(self.trackId) | ||||
| 
 | ||||
|     def __repr__(self): | ||||
|         return '%s: %s' % (self.__class__.__name__, self.asDict()) | ||||
| 
 | ||||
|     def asDict(self): | ||||
|         return dict(uid=self.uid, head=self.head, data=self.data,  | ||||
|                     timeStamp = str(self.timeStamp)[:19]) | ||||
| 
 | ||||
| 
 | ||||
| @registerContainerClass | ||||
| class Container(object): | ||||
|  |  | |||
|  | @ -22,5 +22,5 @@ def test_app(self, config): | |||
|     storage = config.storageFactory(config.dbschema) | ||||
|     response = publishRequest(config, storage, '/top') | ||||
|     result = json.loads(response.consumeBody()) | ||||
|     self.assertEqual(result['items'], ['child1']) | ||||
|     self.assertEqual(result['items'][0]['head']['name'], 'child1') | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue