provide registry for views, use for default view
This commit is contained in:
		
							parent
							
								
									0bda603f1e
								
							
						
					
					
						commit
						3d10c45222
					
				
					 2 changed files with 48 additions and 21 deletions
				
			
		|  | @ -1,14 +1,14 @@ | ||||||
| # scopes.server.app | # scopes.server.app | ||||||
| 
 | 
 | ||||||
| import json | import json | ||||||
| from zope.interface import implementer |  | ||||||
| from zope.publisher.base import DefaultPublication | from zope.publisher.base import DefaultPublication | ||||||
| from zope.publisher.browser import BrowserRequest | from zope.publisher.browser import BrowserRequest | ||||||
| from zope.publisher.interfaces import NotFound | from zope.publisher.interfaces import NotFound | ||||||
| from zope.publisher.publish import publish | from zope.publisher.publish import publish | ||||||
| from zope.traversing.publicationtraverse import PublicationTraverser | from zope.traversing.publicationtraverse import PublicationTraverser | ||||||
| 
 | 
 | ||||||
| from scopes.interfaces import IContainer, ITraversable, IView | from scopes.interfaces import ITraversable, IView | ||||||
|  | from scopes.server.browser import getView | ||||||
| from scopes.storage.folder import Root | from scopes.storage.folder import Root | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -29,12 +29,11 @@ def zope_app_factory(config): | ||||||
| class Publication(DefaultPublication): | class Publication(DefaultPublication): | ||||||
| 
 | 
 | ||||||
|     def traverseName(self, request, ob, name): |     def traverseName(self, request, ob, name): | ||||||
|         next = None |         next = getView(request, ob, name) | ||||||
|  |         if next is not None: | ||||||
|  |             return next | ||||||
|         if ITraversable.providedBy(ob): |         if ITraversable.providedBy(ob): | ||||||
|             next = ob.get(name) |             next = ob.get(name) | ||||||
|         if next is None: |  | ||||||
|             if name == 'index.html': |  | ||||||
|                 next = DefaultView(ob, request) |  | ||||||
|         if next is None: |         if next is None: | ||||||
|             raise NotFound(ob, name, request) |             raise NotFound(ob, name, request) | ||||||
|         return next |         return next | ||||||
|  | @ -44,18 +43,3 @@ class Publication(DefaultPublication): | ||||||
|             return ob, () |             return ob, () | ||||||
|         return ob, ('index.html',) |         return ob, ('index.html',) | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| @implementer(IView) |  | ||||||
| class DefaultView: |  | ||||||
| 
 |  | ||||||
|     def __init__(self, context, request): |  | ||||||
|         self.context = context |  | ||||||
|         self.request = request |  | ||||||
| 
 |  | ||||||
|     def __call__(self): |  | ||||||
|         ob = self.context |  | ||||||
|         result = dict(head=ob.head, data=ob.data) |  | ||||||
|         if IContainer.providedBy(ob): |  | ||||||
|             result['items'] = list(ob.keys()) |  | ||||||
|         return json.dumps(result) |  | ||||||
| 
 |  | ||||||
|  |  | ||||||
							
								
								
									
										43
									
								
								scopes/server/browser.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								scopes/server/browser.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,43 @@ | ||||||
|  | # scopes.server.browser | ||||||
|  | 
 | ||||||
|  | import json | ||||||
|  | from zope.interface import implementer | ||||||
|  | from scopes.interfaces import IContainer, IView | ||||||
|  | 
 | ||||||
|  | views = {} | ||||||
|  | 
 | ||||||
|  | def register(contextClass, name): | ||||||
|  |     def doRegister(viewClass): | ||||||
|  |         nameEntry = views.setdefault(name, {}) | ||||||
|  |         key = contextClass and contextClass.prefix or '' | ||||||
|  |         nameEntry[key] = viewClass | ||||||
|  |         return viewClass | ||||||
|  |     return doRegister | ||||||
|  | 
 | ||||||
|  | def getView(request, ob, name): | ||||||
|  |     nameEntry = views.get(name) | ||||||
|  |     if nameEntry is None: | ||||||
|  |         return None | ||||||
|  |     viewClass = nameEntry.get(ob.__class__.prefix) | ||||||
|  |     if viewClass is None: | ||||||
|  |         viewClass = nameEntry.get('') | ||||||
|  |     if viewClass is None: | ||||||
|  |         return None | ||||||
|  |     return viewClass(ob, request) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | @register(None, 'index.html') | ||||||
|  | @implementer(IView) | ||||||
|  | class DefaultView: | ||||||
|  | 
 | ||||||
|  |     def __init__(self, context, request): | ||||||
|  |         self.context = context | ||||||
|  |         self.request = request | ||||||
|  | 
 | ||||||
|  |     def __call__(self): | ||||||
|  |         ob = self.context | ||||||
|  |         result = dict(head=ob.head, data=ob.data) | ||||||
|  |         if IContainer.providedBy(ob): | ||||||
|  |             result['items'] = list(ob.keys()) | ||||||
|  |         return json.dumps(result) | ||||||
|  | 
 | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue