move access to records container (in ZODB or SQL DB) to util.records() function
This commit is contained in:
		
							parent
							
								
									5c4b7fd730
								
							
						
					
					
						commit
						b4b93b122e
					
				
					 2 changed files with 24 additions and 29 deletions
				
			
		|  | @ -24,8 +24,6 @@ from zope import component | |||
| from zope.app.pagetemplate import ViewPageTemplateFile | ||||
| from zope.cachedescriptors.property import Lazy | ||||
| 
 | ||||
| import config | ||||
| from cco.storage.common import Storage, getEngine | ||||
| from cybertools.browser.configurator import ViewConfigurator, MacroViewProperty | ||||
| from loops.browser.node import NodeView | ||||
| from loops.common import adapted | ||||
|  | @ -41,24 +39,6 @@ personal_macros = ViewPageTemplateFile('personal_macros.pt') | |||
| 
 | ||||
| class FavoriteView(NodeView): | ||||
| 
 | ||||
|     containerName = 'favorites' | ||||
|     containerFactory = Favorites | ||||
| 
 | ||||
|     @Lazy | ||||
|     def useRecordsStorage(self): | ||||
|         return self.containerName in (self.globalOptions('cco.storage.records') or []) | ||||
| 
 | ||||
|     @Lazy | ||||
|     def recordsContainer(self): | ||||
|         schema = self.globalOptions('cco.storage.schema') or None | ||||
|         if schema is not None: | ||||
|             schema = schema[0] | ||||
|         storage = Storage(getEngine(config.dbengine, config.dbname,  | ||||
|                                 config.dbuser, config.dbpassword,  | ||||
|                                 host=config.dbhost, port=config.dbport),  | ||||
|                       schema=schema) | ||||
|         return storage.create(self.containerFactory) | ||||
| 
 | ||||
|     @Lazy | ||||
|     def item(self): | ||||
|         return self | ||||
|  | @ -69,14 +49,7 @@ class FavoriteView(NodeView): | |||
| 
 | ||||
|     @Lazy | ||||
|     def favorites(self): | ||||
|         if self.useRecordsStorage: | ||||
|             return FavAdapter(self.recordsContainer) | ||||
|         records = self.loopsRoot.getRecordManager() | ||||
|         if records is not None: | ||||
|             storage = records.get('favorites') | ||||
|             if storage is not None: | ||||
|                 return IFavorites(storage) | ||||
|         return None | ||||
|         return FavAdapter(util.records(self.context, 'favorites', Favorites)) | ||||
| 
 | ||||
|     def listFavorites(self): | ||||
|         if self.favorites is None: | ||||
|  |  | |||
							
								
								
									
										24
									
								
								util.py
									
										
									
									
									
								
							
							
						
						
									
										24
									
								
								util.py
									
										
									
									
									
								
							|  | @ -20,6 +20,7 @@ | |||
| Utility functions. | ||||
| """ | ||||
| 
 | ||||
| from __future__ import absolute_import | ||||
| import os | ||||
| from zope.publisher.browser import BrowserView | ||||
| from zope import component | ||||
|  | @ -38,7 +39,10 @@ try: | |||
| except ImportError: | ||||
|     markdown = None | ||||
| 
 | ||||
| import config | ||||
| from cco.storage.common import Storage, getEngine | ||||
| import cybertools | ||||
| from cybertools.meta.interfaces import IOptions | ||||
| from loops.browser.util import html_quote | ||||
| 
 | ||||
| _ = MessageFactory('loops') | ||||
|  | @ -115,6 +119,8 @@ def toUnicode(value, encoding='UTF-8'): | |||
|         return value | ||||
| 
 | ||||
| 
 | ||||
| # catalog and index stuff | ||||
| 
 | ||||
| def getCatalog(context): | ||||
|     from loops.common import baseObject | ||||
|     context = baseObject(context) | ||||
|  | @ -130,6 +136,23 @@ def reindex(obj, catalog=None): | |||
|         catalog.index_doc(int(getUidForObject(obj)), obj) | ||||
| 
 | ||||
| 
 | ||||
| # options => storage | ||||
| 
 | ||||
| def records(context, name, factory): | ||||
|     root = context.getLoopsRoot() | ||||
|     opts = IOptions(root) | ||||
|     if name in (opts('cco.storage.records') or []): | ||||
|         schema = (opts('cco.storage.schema') or [None])[0] | ||||
|         storage = Storage(getEngine(config.dbengine, config.dbname,  | ||||
|                                     config.dbuser, config.dbpassword,  | ||||
|                                     host=config.dbhost, port=config.dbport),  | ||||
|                           schema=schema) | ||||
|         cont = storage.create(factory) | ||||
|     else: | ||||
|         cont = root.getRecordManager().get(name) | ||||
|     return cont | ||||
| 
 | ||||
| 
 | ||||
| # UID stuff | ||||
| 
 | ||||
| class IUid(Interface): | ||||
|  | @ -137,7 +160,6 @@ class IUid(Interface): | |||
| 
 | ||||
|     uid = Attribute("Unique Identifier") | ||||
| 
 | ||||
| 
 | ||||
| def getItem(uid, intIds=None, storage=None): | ||||
|     if storage is not None and '-' in uid: | ||||
|         return storage.getItem(uid) | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue