def login(self, user, pwd): if self.verbose: print "MongoConnectionProvider.login: start." if pwd is None or len(pwd) == 0 or user is None or len(user) == 0: raise DBLayerException("Missing username or password") cerca = self.dbmgr.getClazzByTypeName('DBEUser')(attrs={'login': user, 'pwd': pwd}) ret = [] try: collectionName = self.dbmgr.buildTableName(cerca) if collectionName not in self._server.collection_names(): raise DBLayerException("Collection '%s' does not exists." % (collectionName)) ret = self.search(cerca, uselike=False) except Exception, e: myerr = "%s" % e print myerr print "".join(traceback.format_tb(sys.exc_info()[2])) #raise e try: self.user = cerca # Sporco trucco: altrimenti i controlli isLoggedIn falliscono!!! self.initDB() newuser = self.dbmgr.getClazzByTypeName('DBEUser')(attrs={'login': user, 'pwd': pwd}) newuser = self.insert(newuser) self.user = newuser searchGroup = self.dbmgr.getClazzByTypeName('DBEGroup')(attrs={'name': user}) newgroup = self.search(searchGroup, uselike=False)[0] if self.verbose: print "MongoConnectionProvider.login: newuser=%s" % (newuser) newfolder = self.dbmgr.getClazzByTypeName('DBEFolder')(attrs={ 'owner': newuser.getValue('id'), 'group_id': newgroup.getValue('id'), 'creator': newuser.getValue('id'), 'last_modify': newuser.getValue('id'), 'name': user}) newfolder = self.insert(newfolder) if self.verbose: print "MongoConnectionProvider.login: newfolder=%s" % (newfolder) ret = self.search(cerca, uselike=False) except Exception, e1: if self.verbose: print "MongoConnectionProvider.login: ECCEZIONE=%s" % (e) if self.verbose: print "".join(traceback.format_tb(sys.exc_info()[2]))
def search(self, dbe, uselike=1, orderby=None, ignore_deleted=True, full_object=True): casesensitive = 0 self._log("JsonConnectionProvider.search: start time = %s" % (datetime.datetime.now())) # tablename = dbe.getTableName() # Search dbexml = self._dbeToJson(dbe) self._log("JsonConnectionProvider.search: dbexml = %s" % (dbexml)) self._log("JsonConnectionProvider.search: uselike = %s" % (uselike)) # if self.verbose: print "JsonConnectionProvider.search: casesensitive = %s" % (casesensitive) self._log("JsonConnectionProvider.search: orderby = %s" % (orderby)) try: if orderby is None: orderby = '' # self.semaforo.acquire(True) msg, lista = self._server.search(dbexml, uselike, casesensitive, orderby, ignore_deleted, full_object) msg = b64decode(msg) # self.semaforo.release() self._log("JsonConnectionProvider.search: msg=\n %s" % "\n ".join(("%s" % msg).split("\n"))) self.lastMessages = msg except Exception, e: print "JsonConnectionProvider.search: ECCEZIONE ############# Inizio." print "ECCEZIONE: %s" % (e) print "".join(traceback.format_tb(sys.exc_info()[2])) print "JsonConnectionProvider.search: ECCEZIONE ############# Fine." raise DBLayerException("Problemi di comunicazione col server: %s" % e)
def search(self, dbe, uselike=1, orderby=None, ignore_deleted=True, full_object=True): casesensitive = 0 if self.verbose: print "MongoConnectionProvider.search: start time = %s" % (datetime.datetime.now()) tablename = dbe.getTableName() # Search dbedict = dbe.getValuesDictionary() if self.verbose: print "MongoConnectionProvider.search: dbedict = %s" % (dbedict) if self.verbose: print "MongoConnectionProvider.search: uselike = %s" % (uselike) #if self.verbose: print "MongoConnectionProvider.search: casesensitive = %s" % (casesensitive) if self.verbose: print "MongoConnectionProvider.search: orderby = %s" % (orderby) lista = [] try: if orderby is None: orderby = '' #self.semaforo.acquire(True) collectionName = self.dbmgr.buildTableName(dbe) #if not collectionName in self._server.collection_names(): # raise DBLayerException("Collection '%s' does not exists." % (collectionName)) coll = self._server[collectionName] if self.verbose: print "MongoConnectionProvider.search: coll=%s (%s)\n %s" % (coll, coll.count(), dir(coll)) lista = coll.find(dbedict) #self.lastMessages = msg except Exception, e: print "MongoConnectionProvider.search: ECCEZIONE ############# Inizio." print "ECCEZIONE: %s" % (e) print "".join(traceback.format_tb(sys.exc_info()[2])) print "MongoConnectionProvider.search: ECCEZIONE ############# Fine." raise DBLayerException("Problemi di comunicazione col server: %s" % e)
def __init__(self, host, db, user, pwd, verbose=0): """host: here is the url of the server db: TODO is meaningless here?""" DBConnectionProvider.__init__(self, host, db, user, pwd, verbose) self.verbose = verbose if self._host.startswith('jsons'): raise DBLayerException("TODO: Json via https") self._server = None self._cache = {} self.dbmgr = None # Needed by _jsonToDbe
def select(self, tablename, searchString): if self.verbose: print "MongoConnectionProvider.select: tablename=%s" % (tablename) if self.verbose: print "MongoConnectionProvider.select: searchString=%s" % (searchString) if self.verbose: print "MongoConnectionProvider.select: start search=%s" % (datetime.datetime.now()) try: #self.semaforo.acquire(True) tmp = self._server.select(tablename, searchString) #self.semaforo.release() res = tmp[1] self.lastMessages = b64decode(tmp[0]) except DBLayerException, e: raise DBLayerException("Problemi di comunicazione col server: %s" % e)
def fullObjectByName(self, myid, ignore_deleted=True): self.semaforo.acquire(True) ret = [] try: self.msg, ret = self._server.fullObjectByName( Binary(myid), ignore_deleted) except Exception, e: print "XmlrpcConnectionProvider.fullObjectByName: ECCEZIONE ############# Inizio." print "ECCEZIONE: %s" % (e) print "".join(traceback.format_tb(sys.exc_info()[2])) print "XmlrpcConnectionProvider.fullObjectByName: ECCEZIONE ############# Fine." raise DBLayerException( "Communication troubles with the remote server: %s" % e)
def select(self, tablename, searchString): self._log("XmlrpcConnectionProvider.select: tablename=%s" % (tablename)) self._log("XmlrpcConnectionProvider.select: searchString=%s" % (searchString)) self._log("XmlrpcConnectionProvider.select: start search=%s" % (datetime.datetime.now())) try: self.semaforo.acquire(True) tmp = self._server.select(tablename, searchString) self.semaforo.release() res = tmp[1] self.lastMessages = tmp[0].data except DBLayerException, e: raise DBLayerException("Problemi di comunicazione col server: %s" % e)
def fullObjectByName(self, myid, ignore_deleted=True): #self.semaforo.acquire(True) ret = [] try: tmp = self._server.objectByName(myid, ignore_deleted) if len(tmp) > 0: self.msg = b64decode(tmp[0]) if len(tmp) > 1: ret = tmp[1:][0] #self.msg, ret = self._server.fullObjectByName(myid,ignore_deleted) #self.msg = b64decode(self.msg) except Exception, e: print "MongoConnectionProvider.fullObjectByName: ECCEZIONE ############# Inizio." print "ECCEZIONE: %s" % (e) print "".join(traceback.format_tb(sys.exc_info()[2])) print "MongoConnectionProvider.fullObjectByName: ECCEZIONE ############# Fine." raise DBLayerException("Communication troubles with the remote server: %s" % e)
def getRegisteredTypes(self): """Returns the registered types""" ret = {} try: # self.semaforo.acquire(True) msg, lista = self._server.getTypeList() # self.semaforo.release() self.lastMessages = msg.data for k, v in lista[0].iteritems(): ret[k] = RemoteDBE(k, v.data, self) self._cache = ret except Exception, e: print "JsonConnectionProvider.getRegisteredTypes: ECCEZIONE ############# Inizio." print "ECCEZIONE: %s" % (e) print "".join(traceback.format_tb(sys.exc_info()[2])) print "JsonConnectionProvider.getRegisteredTypes: ECCEZIONE ############# Fine." raise DBLayerException("Problemi di comunicazione col server: %s" % e)
def objectById(self, myid, ignore_deleted=True): tipi = self.dbmgr.getRegisteredTypes() DBEObject = self.dbmgr.getClazzByTypeName("DBEObject") #DBAssociation = self.dbmgr.getClazzByTypeName("DBAssociation") q = [] for tablename, clazz in tipi.items(): mydbe = clazz() if not isinstance(mydbe, DBEObject):# or isinstance(mydbe, DBAssociation): # or mydbe.getTypeName=="DBEObject": continue tmp_q = "select '%s' as classname,id,owner,group_id,permissions,creator,creation_date,last_modify,last_modify_date,father_id,name,description from %s where id='%s'" % (mydbe.getTypeName(), self.dbmgr.buildTableName(mydbe), DBEObject.hex2uuid(myid)) if ignore_deleted: tmp_q += " and deleted_date='0000-00-00 00:00:00'" q.append(tmp_q) searchString = " union ".join(q) if self.verbose: print "ObjectMgr.objectById: searchString=%s" % (searchString) raise DBLayerException("DA FINIRE") lista = self.select("objects", searchString) if len(lista) == 1: return lista[0] if self.verbose: print "ObjectMgr.objectById: lista=%s" % (len(lista)) return None
def initDB(self): """Initialize DB""" if self.verbose: print "MongoConnectionProvider.initDB: start." mytypes = self.dbmgr.dbeFactory.getRegisteredTypes() for tablename in mytypes.keys(): if tablename == 'default': continue mydbe = mytypes[tablename]() collectionName = self.dbmgr.buildTableName(mydbe) if self.verbose: print "MongoConnectionProvider.initDB: collectionName = %s" % (collectionName) # 1. Table definition try: self._server.create_collection(collectionName) dbErrors = self._server.error() if dbErrors is not None: if self.verbose: print "MongoConnectionProvider.initDB: dbErrors = %s" % dbErrors raise DBLayerException(dbErrors) except Exception, e: if self.verbose: print "MongoConnectionProvider.initDB: Exception = %s" % (e) print "".join(traceback.format_tb(sys.exc_info()[2]))
def copy(self, dbe): raise DBLayerException("copy: not yet implemented")