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.app.pagetemplate import ViewPageTemplateFile | ||||||
| from zope.cachedescriptors.property import Lazy | from zope.cachedescriptors.property import Lazy | ||||||
| 
 | 
 | ||||||
| import config |  | ||||||
| from cco.storage.common import Storage, getEngine |  | ||||||
| from cybertools.browser.configurator import ViewConfigurator, MacroViewProperty | from cybertools.browser.configurator import ViewConfigurator, MacroViewProperty | ||||||
| from loops.browser.node import NodeView | from loops.browser.node import NodeView | ||||||
| from loops.common import adapted | from loops.common import adapted | ||||||
|  | @ -41,24 +39,6 @@ personal_macros = ViewPageTemplateFile('personal_macros.pt') | ||||||
| 
 | 
 | ||||||
| class FavoriteView(NodeView): | 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 |     @Lazy | ||||||
|     def item(self): |     def item(self): | ||||||
|         return self |         return self | ||||||
|  | @ -69,14 +49,7 @@ class FavoriteView(NodeView): | ||||||
| 
 | 
 | ||||||
|     @Lazy |     @Lazy | ||||||
|     def favorites(self): |     def favorites(self): | ||||||
|         if self.useRecordsStorage: |         return FavAdapter(util.records(self.context, 'favorites', Favorites)) | ||||||
|             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 |  | ||||||
| 
 | 
 | ||||||
|     def listFavorites(self): |     def listFavorites(self): | ||||||
|         if self.favorites is None: |         if self.favorites is None: | ||||||
|  |  | ||||||
							
								
								
									
										24
									
								
								util.py
									
										
									
									
									
								
							
							
						
						
									
										24
									
								
								util.py
									
										
									
									
									
								
							|  | @ -20,6 +20,7 @@ | ||||||
| Utility functions. | Utility functions. | ||||||
| """ | """ | ||||||
| 
 | 
 | ||||||
|  | from __future__ import absolute_import | ||||||
| import os | import os | ||||||
| from zope.publisher.browser import BrowserView | from zope.publisher.browser import BrowserView | ||||||
| from zope import component | from zope import component | ||||||
|  | @ -38,7 +39,10 @@ try: | ||||||
| except ImportError: | except ImportError: | ||||||
|     markdown = None |     markdown = None | ||||||
| 
 | 
 | ||||||
|  | import config | ||||||
|  | from cco.storage.common import Storage, getEngine | ||||||
| import cybertools | import cybertools | ||||||
|  | from cybertools.meta.interfaces import IOptions | ||||||
| from loops.browser.util import html_quote | from loops.browser.util import html_quote | ||||||
| 
 | 
 | ||||||
| _ = MessageFactory('loops') | _ = MessageFactory('loops') | ||||||
|  | @ -115,6 +119,8 @@ def toUnicode(value, encoding='UTF-8'): | ||||||
|         return value |         return value | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | # catalog and index stuff | ||||||
|  | 
 | ||||||
| def getCatalog(context): | def getCatalog(context): | ||||||
|     from loops.common import baseObject |     from loops.common import baseObject | ||||||
|     context = baseObject(context) |     context = baseObject(context) | ||||||
|  | @ -130,6 +136,23 @@ def reindex(obj, catalog=None): | ||||||
|         catalog.index_doc(int(getUidForObject(obj)), obj) |         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 | # UID stuff | ||||||
| 
 | 
 | ||||||
| class IUid(Interface): | class IUid(Interface): | ||||||
|  | @ -137,7 +160,6 @@ class IUid(Interface): | ||||||
| 
 | 
 | ||||||
|     uid = Attribute("Unique Identifier") |     uid = Attribute("Unique Identifier") | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| def getItem(uid, intIds=None, storage=None): | def getItem(uid, intIds=None, storage=None): | ||||||
|     if storage is not None and '-' in uid: |     if storage is not None and '-' in uid: | ||||||
|         return storage.getItem(uid) |         return storage.getItem(uid) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue