def start_import(self): time.sleep(3) self.db, self.pool = pooler.get_db_and_pool(self.db_name) self.cr = self.db.cursor() self.errors = False self.log = [] self.progress_db, self.progress_pool = pooler.get_db_and_pool(self.db_name) self.progress_cr = self.progress_db.cursor() user_obj = self.pool.get('res.users') ir_config_parameter_obj = self.pool.get('ir.config_parameter') import_path = ir_config_parameter_obj.get_param(self.cr, self.uid, 'tk_import_path_parameter', False) if not import_path: logger.error("No import_path defined (config_parameter tk_import_path_parameter)") return False file_obj = self.pool.get('tk_import.file') logical_file = file_obj.browse(self.cr, self.uid, self.logical_file_id) number_of_record = logical_file.lines self.model_obj = self.pool.get(logical_file.model_id.model) self.model_id = logical_file.model_id.id #self.product_tmpl_columns = self.pool.get('product.template')._columns.keys() file_path = import_path + '/' + logical_file.file file = open(file_path, 'rb') dict_reader = csv.DictReader(file, delimiter=self.separator, quotechar=self.quote_delimiter) self.cr.autocommit(False) if self.ignore_errors : self.cr.autocommit(True) file_obj.write(self.cr, self.uid, self.logical_file_id, {'state': 'processing'}) self.cr.commit() self.i = 0 self.progress = 0 #Compute the number of steps possible #Round to the superior multiple of 20 steps = number_of_record / 20 if steps * 20 < number_of_record: steps += 1 for row in dict_reader: try: self.i += 1 if self.i % steps == 0: self.progress += 5 self.log_progress(self.progress) self.import_row(row, self.i) except Exception, e: self.errors = True self.log_error(self.i, e)
def zerp_del(self,session): """ Removes user session * Deletes session from cache * Removes related session tokens from each database known """ # Check if the user disconnecting has a session and # basic sanity work if not session: return if not session in CLIENT_CACHE: return sess = CLIENT_CACHE[session] del CLIENT_CACHE[session] if not sess: return for database, data in sess.items(): (database, user_uid, sess_key) = data db, pool = pooler.get_db_and_pool(database) cr = db.cursor() sess_obj = pool.get('zerp.users.sessions') sess_ids = sess_obj.search(cr,1, [ ('name','=',sess_key) ]) sess_obj.unlink(cr,1,sess_ids) cr.commit() cr.close() return
def _get_cursor(self, dbname): cr = self._dbname_to_cr.get(dbname) if not cr or (cr and cr.closed): db, pool = pooler.get_db_and_pool(dbname) cr = db.cursor() self._dbname_to_cr[dbname] = cr return cr
def update_printers_status(self, db_name, uid, context=None): """ Add creation of tray if no tray are defined """ db, pool = pooler.get_db_and_pool(db_name) cr = db.cursor() res = super(Printer, self).update_printers_status(db_name, uid, context=context) try: connection = cups.Connection() printers = connection.getPrinters() server_error = False except: server_error = True printer_ids = self.search(cr, uid, [('system_name', 'in', printers.keys())], context=context) if server_error: vals = {'status': 'server_error'} self.write(cr, uid, printer_ids, vals, context=context) return res printer_list = self.browse(cr, uid, printer_ids, context=context) for printer in printer_list: # XXX we consider config of printer won't change if not printer.tray_ids: self._update_tray_option(db_name, uid, printer, context=context) return res
def zerp_get(self,details,uri=None): """ Returns a user session from database or creates a new one """ global CLIENT_CACHE # authid is the login of the authenticated user login = details["caller_authid"] if not login: raise ApplicationError(u"com.izaber.zerp.error.invalid_login", u"could not authenticate session") # Zerp needs lowercase usernames login = login.lower() # Parse out what database the user is trying to attach to if uri is None: uri = ZERPWampUri(details) # Ensure that the database they're trying to access actually exists databases = openerp.service.web_services.db().exp_list() if uri.database not in databases: raise ApplicationError(u"com.izaber.zerp.error.invalid_login", u"could not authenticate session") # Try and find the record based upon the session session = details["caller"] if not session: return # If we've got a cached session, let's use that user_zerp = CLIENT_CACHE.get(session,{}).get(uri.database) if user_zerp: return user_zerp # Verify that the user actually exists in the database db, pool = pooler.get_db_and_pool(uri.database) cr = db.cursor() user_uids = pool.get('res.users').search(cr,1,[('login','=',login)]) if not user_uids: raise ApplicationError(u"com.izaber.zerp.error.invalid_login", u"could not authenticate session") # If they do, create the session user_uid = user_uids[0] sess_obj = pool.get('zerp.users.sessions') sess_key = sess_obj.create_session(cr,1,user_uid) # Store the session information # Cache the session for faster lookup and continue on user_zerp = [uri.database, user_uid, sess_key] cr.commit() cr.close() CLIENT_CACHE.setdefault(session,{})[uri.database] = user_zerp return user_zerp
def __init__(self, dbname, model_name, res_id, uid=0): assert isinstance(uid, (int, long)), 'uid should be an integer' self._logger = logging.getLogger('smile_log') db, pool = pooler.get_db_and_pool(dbname) pid = 0 try: cr = db.cursor() cr.execute("select nextval('smile_log_seq')") res = cr.fetchone() pid = res and res[0] or 0 finally: cr.close() self._logger_start = datetime.datetime.now() self._logger_args = {'dbname': dbname, 'model_name': model_name, 'res_id': res_id, 'uid': uid, 'pid': pid}
def update_printers_status(self, db_name, uid, context=None): db, pool = pooler.get_db_and_pool(db_name) cr = db.cursor() try: connection = cups.Connection() printers = connection.getPrinters() server_error = False except: server_error = True mapping = {3: "available", 4: "printing", 5: "error"} if context is None: context = {} try: # Skip update to avoid the thread being created again ctx = context.copy() ctx["skip_update"] = True ids = self.search(cr, uid, [], context=ctx) for printer in self.browse(cr, uid, ids, context=ctx): vals = {} if server_error: status = "server-error" elif printer.system_name in printers: info = printers[printer.system_name] status = mapping.get(info["printer-state"], "unknown") vals = { "model": info.get("printer-make-and-model", False), "location": info.get("printer-location", False), "uri": info.get("device-uri", False), } else: status = "unavailable" vals["status"] = status self.write(cr, uid, [printer.id], vals, context=context) cr.commit() except: cr.rollback() raise finally: cr.close() with self.lock: self.updating = False self.last_update = time.time()
def update_printers_status(self, db_name, uid, context=None): db, pool = pooler.get_db_and_pool(db_name) cr = db.cursor() try: connection = cups.Connection() printers = connection.getPrinters() server_error = False except: server_error = True mapping = {3: 'available', 4: 'printing', 5: 'error'} if context is None: context = {} try: # Skip update to avoid the thread being created again ctx = context.copy() ctx['skip_update'] = True ids = self.search(cr, uid, [], context=ctx) for printer in self.browse(cr, uid, ids, context=ctx): vals = {} if server_error: status = 'server-error' elif printer.system_name in printers: info = printers[printer.system_name] status = mapping.get(info['printer-state'], 'unknown') vals = { 'model': info.get('printer-make-and-model', False), 'location': info.get('printer-location', False), 'uri': info.get('device-uri', False), } else: status = 'unavailable' vals['status'] = status self.write(cr, uid, [printer.id], vals, context=context) cr.commit() except: cr.rollback() raise finally: cr.close() with self.lock: self.updating = False self.last_update = time.time()
def get_cr(self, uri, allow_last=False): """ Split the uri, grab a cursor for that db """ pdb = self.parent.auth_provider.last_auth dbname, uri2 = self.get_db(uri, rest_ret=True, allow_last=allow_last) uri2 = (uri2 and uri2.split('/')) or [] if not dbname: return None, None, None, False, uri2 # if dbname was in our uri, we should have authenticated # against that. assert pdb == dbname, " %s != %s" % (pdb, dbname) res = self.parent.auth_provider.auth_creds.get(dbname, False) if not res: self.parent.auth_provider.checkRequest(self.parent, uri, dbname) res = self.parent.auth_provider.auth_creds[dbname] user, passwd, dbn2, uid = res db, pool = pooler.get_db_and_pool(dbname) cr = db.cursor() return cr, uid, pool, dbname, uri2
def get_cr(self, uri, allow_last=False): """ Split the uri, grab a cursor for that db """ pdb = self.parent.auth_provider.last_auth dbname, uri2 = self.get_db(uri, rest_ret=True, allow_last=allow_last) uri2 = (uri2 and uri2.split('/')) or [] if not dbname: return None, None, None, False, uri2 # if dbname was in our uri, we should have authenticated # against that. assert pdb == dbname, " %s != %s" %(pdb, dbname) res = self.parent.auth_provider.auth_creds.get(dbname, False) if not res: self.parent.auth_provider.checkRequest(self.parent, uri, dbname) res = self.parent.auth_provider.auth_creds[dbname] user, passwd, dbn2, uid = res db,pool = pooler.get_db_and_pool(dbname) cr = db.cursor() return cr, uid, pool, dbname, uri2
def _update_tray_option(self, db_name, uid, printer, context=None): """ Create missing tray for a printer """ db, pool = pooler.get_db_and_pool(db_name) cr = db.cursor() tray_obj = pool.get('printing.tray') # get printers options from a PPD file try: connection = cups.Connection() ppd_file_path = connection.getPPD3(printer.system_name) except: return if not ppd_file_path[2]: return ppd = PPD(ppd_file_path[2]) option = ppd.findOption('InputSlot') if not option: return try: for tray_opt in option.choices: if tray_opt['choice'] not in [ t.system_name for t in printer.tray_ids ]: tray_vals = { 'name': tray_opt['text'], 'system_name': tray_opt['choice'], 'printer_id': printer.id, } tray_obj.create(cr, uid, tray_vals, context=context) cr.commit() except: cr.rollback() raise finally: cr.close() return True
def _update_tray_option(self, db_name, uid, printer, context=None): """ Create missing tray for a printer """ db, pool = pooler.get_db_and_pool(db_name) cr = db.cursor() tray_obj = pool.get('printing.tray') # get printers options from a PPD file try: connection = cups.Connection() ppd_file_path = connection.getPPD3(printer.system_name) except: return if not ppd_file_path[2]: return ppd = PPD(ppd_file_path[2]) option = ppd.findOption('InputSlot') if not option: return try: for tray_opt in option.choices: if tray_opt['choice'] not in [t.system_name for t in printer.tray_ids]: tray_vals = { 'name': tray_opt['text'], 'system_name': tray_opt['choice'], 'printer_id': printer.id, } tray_obj.create(cr, uid, tray_vals, context=context) cr.commit() except: cr.rollback() raise finally: cr.close() return True
def start_import(self): time.sleep(3) self.db, self.pool = pooler.get_db_and_pool(self.db_name) self.cr = self.db.cursor() self.errors = False self.log = [] self.progress_db, self.progress_pool = pooler.get_db_and_pool( self.db_name) self.progress_cr = self.progress_db.cursor() user_obj = self.pool.get('res.users') ir_config_parameter_obj = self.pool.get('ir.config_parameter') import_path = ir_config_parameter_obj.get_param( self.cr, self.uid, 'tk_import_path_parameter', False) if not import_path: logger.error( "No import_path defined (config_parameter tk_import_path_parameter)" ) return False file_obj = self.pool.get('tk_import.file') logical_file = file_obj.browse(self.cr, self.uid, self.logical_file_id) number_of_record = logical_file.lines self.model_obj = self.pool.get(logical_file.model_id.model) self.model_id = logical_file.model_id.id # self.product_tmpl_columns = self.pool.get('product.template')._columns.keys() file_path = import_path + '/' + logical_file.file file = open(file_path, 'rb') dict_reader = csv.DictReader(file, delimiter=self.separator, quotechar=self.quote_delimiter) self.cr.autocommit(False) if self.ignore_errors: self.cr.autocommit(True) file_obj.write(self.cr, self.uid, self.logical_file_id, {'state': 'processing'}) self.cr.commit() self.i = 0 self.progress = 0 # Compute the number of steps possible # Round to the superior multiple of 20 steps = number_of_record / 20 if steps * 20 < number_of_record: steps += 1 for row in dict_reader: try: self.i += 1 if self.i % steps == 0: self.progress += 5 self.log_progress(self.progress) self.import_row(row, self.i) except Exception, e: self.errors = True self.log_error(self.i, e)
def start(self): self.db, self.pool = pooler.get_db_and_pool(self.db_name) self.cr = self.db.cursor() self.thread.start()
ERP_PATH = settings.ERP_PATH if not ERP_PATH in sys.path: sys.path.insert(0, os.path.dirname(ERP_PATH)) from openerp.tools import config from openerp.modules import get_modules from openerp.pooler import get_db_and_pool # Configurar el server con los parametros adecuados. config.parse_config(['-c', settings.ERP_CONF]) # Modulos en nuestra conf de OpenERP get_modules() DB, POOL = get_db_and_pool(settings.ERP_DB) cursor = DB.cursor() user_obj = POOL.get('res.users') try: USER = user_obj.search(cursor, 1, [ ('login', '=', settings.ERP_UN), ], limit=1)[0] except: cursor.rollback() finally: cursor.close()
reader = csv.reader(open(cFileName, 'rb')) if reader: # If we get a good reader, we initialize the connection using xmlrpclib # User access credentials username = "******" pwd = "solutions" dbname = "boatcompany" # Call xmlrpclib.ServerProxy("IP Address") sock_common = xmlrpclib.ServerProxy("http://localhost:8069/xmlrpc/common") # To Logon with given credentials uid = sock_common.login(dbname, username, pwd) # Open actual location of object sock = xmlrpclib.ServerProxy("http://localhost:8069/xmlrpc/object") db, pool = pooler.get_db_and_pool(dbname) cr = db.cursor() # Lookup Stock Location # Use short name of warehouse here cStockLocation = 'BStoc' args = [('name', '=', cStockLocation)] stock_id = sock.execute(dbname, uid, pwd, 'stock.location', 'search', args) print stock_id cStagingLocation = "Staging" args = [('name', '=', cStagingLocation)] staging_id = sock.execute(dbname, uid, pwd, 'stock.location', 'search', args) print staging_id