Show
Ignore:
Timestamp:
08/05/10 18:37:06 (22 months ago)
Author:
mauro
Message:

plone4

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • plone4bio.biosql/trunk/src/plone4bio/biosql/content/root.py

    r112 r153  
     1# -*- coding: utf-8 -*- 
    12__author__ = '''Mauro Amico <mauro@biodec.com>''' 
    23__docformat__ = 'plaintext' 
    34 
     5from threading import local 
    46from zope.interface import implements 
    57from zope.component.factory import Factory 
    68from zope.schema.fieldproperty import FieldProperty 
     9 
     10from Acquisition import aq_base 
    711 
    812from plone.app.content.container import Container 
     
    1418from Products.CMFPlone.utils import base_hasattr 
    1519from Products.CMFPlone.utils import safe_callable 
     20from Products.CMFPlone.interfaces.constrains import IConstrainTypes 
     21 
     22from Products.ATContentTypes.content.base import ATCTContent 
     23from Products.ATContentTypes.content.schemata import ATContentTypeSchema 
     24 
     25from Products.Archetypes import atapi 
     26 
     27from Products.Archetypes.atapi import Schema 
     28from Products.Archetypes.atapi import BooleanWidget 
     29from Products.Archetypes.atapi import StringField 
     30from Products.Archetypes.atapi import StringWidget 
     31from Products.Archetypes.atapi import registerType 
     32 
    1633from zope.app.container.interfaces import IContainer 
    1734from plone.app.content.interfaces import INameFromTitle 
     
    4360    return self.dsn 
    4461 
    45 # WebDAV ... Collection 
    46 # class BioSQLRoot(BaseContent, BrowserDefaultMixin): 
     62""" 
     63 
    4764class BioSQLRoot(Container): 
    48     __implements__ = (BrowserDefaultMixin.__implements__) 
    4965    portal_type='BioSQLRoot' 
     66 
    5067    implements(IBioSQLRoot, IContainer, IFolderish, INameFromTitle) 
     68 
    5169    security = ClassSecurityInfo() 
    5270    # isPrincipiaFolderish = True # already on Container 
    53     dsn = FieldProperty(IBioSQLRoot['dsn']) 
     71    # dsn = FieldProperty(IBioSQLRoot['dsn']) 
     72    dsn = 'postgres://postgres@localhost/plone4bio' 
    5473    seqrecord_key = FieldProperty(IBioSQLRoot['seqrecord_key']) 
    55     _v_dbserver = None 
     74    _v_thread_local = local() 
     75 
     76    def __init__(self, *args, **kwargs): 
     77        if kwargs.has_key('parent'): 
     78            parent = kwargs['parent'] 
     79            del(kwargs['parent']) 
     80        super(BioSQLRoot, self).__init__(*args, **kwargs) 
     81""" 
     82 
     83 
     84Plone4BioSchema = ATContentTypeSchema.copy() + Schema(( 
     85    StringField("dsn", 
     86        required = True, 
     87        widget = StringWidget( 
     88            label = "dsn", 
     89            label_msgid = "dsn_label", 
     90            description = "DSN " 
     91                          "... " 
     92                          "...", 
     93            description_msgid = "dsn_help", 
     94            i18n_domain = "plone4bio") 
     95        ), 
     96    )) 
     97 
     98class BioSQLRoot(ATCTContent): 
     99    portal_type='BioSQLRoot' 
     100 
     101    implements(IBioSQLRoot, IConstrainTypes) 
     102 
     103    security = ClassSecurityInfo() 
     104    schema = Plone4BioSchema 
     105    _at_rename_after_creation = True 
     106    isPrincipiaFolderish = True  
     107    # dsn = atapi.ATFieldProperty('dsn') 
     108    dsn = 'postgres://postgres@localhost/plone4bio' 
     109    seqrecord_key = "version" 
     110    _v_thread_local = local() 
     111 
     112    # XXX 
     113    def getLocallyAllowedTypes(self): 
     114        return [] 
    56115 
    57116    # see CMFPlone/CatalogTool.py 
    58117    def refreshCatalog(self, clear=1): 
     118        return 
    59119        def indexObject(obj, path): 
    60120            if (base_hasattr(obj, 'indexObject') and 
     
    66126                    # take different args, and will fail 
    67127                    pass 
     128                except: # CatalogError: 
     129                    # import pdb; pdb.set_trace() 
     130                    # obj.indexObject() 
     131                    logger.exception("indexObject %r for %r" % (obj, self)) 
    68132        for database in self.values(): 
    69133            database.invalidateCache() 
     
    73137        portal_catalog.ZopeFindAndApply(self, search_sub=True, apply_func=indexObject) 
    74138 
     139    """ 
    75140    def __getattr__(self, name): 
    76141        if self.has_key(name): 
     
    78143        else: 
    79144            raise AttributeError, name 
     145    """ 
    80146 
    81147    security.declareProtected(View, "getBioSQLRoot") 
     
    91157        if not self.dsn: 
    92158            return None 
    93         if self._v_dbserver is None or not self._v_dbserver.adaptor.conn.is_valid: 
     159        dbserver = getattr(self._v_thread_local, 'dbserver', None) 
     160        if dbserver is None or not dbserver.adaptor.conn.is_valid: 
    94161            try: 
    95162                wrapper = getSAWrapper(self.dsn) 
     
    97164                wrapper = createSAWrapper(dsn=self.dsn, name=self.dsn) 
    98165            #TODO: manage OperationalError on connection 
    99             self._v_dbserver = DBServer(wrapper.connection, __import__(drivers[self.dsn.split(':')[0]])) 
    100         return self._v_dbserver 
     166            dbserver = DBServer(wrapper.connection, __import__(drivers[self.dsn.split(':')[0]])) 
     167            self._v_thread_local.dbserver = dbserver 
     168        return dbserver 
    101169 
    102170    security.declareProtected(View, "getValues") 
     
    123191            logger.exception("getDBServer") 
    124192            raise StopIteration 
     193        if not dbserver: 
     194            raise StopIteration 
    125195        for name in dbserver.keys(): 
    126             yield name 
     196            yield str(name) 
    127197        #try: 
    128198        #    for name in dbserver.keys(): 
     
    149219            yield (k, self[k]) 
    150220 
     221    def __bobo_traverse__(self, REQUEST, name): 
     222        try: 
     223            return self[name] 
     224        except KeyError: 
     225            pass         
     226        if hasattr(aq_base(self), name): 
     227            return getattr(self, name) 
     228        # webdav 
     229        """ 
     230        method = REQUEST.get('REQUEST_METHOD', 'GET').upper() 
     231        if (method not in ('GET', 'POST') and not 
     232              isinstance(REQUEST.RESPONSE, xmlrpc.Response) and 
     233              REQUEST.maybe_webdav_client and not REQUEST.path): 
     234            return ReflectoNullResource(self, name, REQUEST).__of__(self) 
     235        """ 
     236        return ATCTContent.__bobo_traverse__(self, REQUEST, name) 
     237 
    151238    # zope2/lib/python/OFS/ObjectManager.py 
    152239    def _getOb(self, id, default=_marker): 
     
    194281    setattr(BioSQLRoot, m, getattr(DictMixin, m).im_func) 
    195282 
     283# XXX: ??? 
    196284bioSQLRootFactory = Factory(BioSQLRoot, title=_(u"Create a new BioSQL Root")) 
     285 
     286atapi.registerType(BioSQLRoot, 'plone4bio.biosql') 
     287