diff --git a/trytond/web_service/db.py b/trytond/web_service/db.py index 5bb9c0d9c7..1528c8a864 100644 --- a/trytond/web_service/db.py +++ b/trytond/web_service/db.py @@ -14,93 +14,54 @@ class DB(Service): super(DB, self).__init__(name) self.join_group("web-services") self.export_method(self.create) - self.export_method(self.get_progress) self.export_method(self.drop) self.export_method(self.dump) self.export_method(self.restore) self.export_method(self.list) self.export_method(self.list_lang) self.export_method(self.change_admin_password) - self.actions = {} - self._id = 0 - self.id_protect = threading.Semaphore() def create(self, password, db_name, demo, lang): security.check_super(password) - self.id_protect.acquire() - self._id += 1 - db_id = self._id - self.id_protect.release() - - self.actions[db_id] = {'clean': False} - - database = sql_db.db_connect('template1', serialize=1) - database.truedb.autocommit() - cursor = database.cursor() - cursor.execute('CREATE DATABASE ' + db_name + ' ENCODING \'unicode\'') - cursor.close() - - - class DBInitialize(object): - def __call__(self, service, db_id, db_name, demo, lang): - try: - service.actions[db_id]['progress'] = 0 - cursor = sql_db.db_connect(db_name).cursor() - sql_db.init_db(cursor) - cursor.commit() - cursor.close() - cursor = None - pooler.get_pool(db_name, demo, service.actions[db_id], - update_module=True) - if lang and lang != 'en_US': - filename = tools.CONFIG["root_path"] + "/i18n/" + \ - lang + ".csv" - tools.trans_load(db_name, filename, lang) - service.actions[db_id]['clean'] = True - cursor = sql_db.db_connect(db_name).cursor() - cursor.execute('select login, password, name ' \ - 'from res_users ' \ - 'where login <> \'root\' order by login') - service.actions[db_id]['users'] = cursor.dictfetchall() - cursor.close() - except Exception, exp: - service.actions[db_id]['clean'] = False - service.actions[db_id]['exception'] = exp - from StringIO import StringIO - import traceback - e_str = StringIO() - traceback.print_exc(file=e_str) - traceback_str = e_str.getvalue() - e_str.close() - service.actions[db_id]['traceback'] = traceback_str - if cursor: - cursor.close() + res = False logger = Logger() - logger.notify_channel("web-services", LOG_INFO, - 'CREATE DB: %s' % (db_name)) - dbi = DBInitialize() - create_thread = threading.Thread(target=dbi, - args=(self, db_id, db_name, demo, lang)) - create_thread.start() - self.actions[db_id]['thread'] = create_thread - return db_id - def get_progress(self, password, db_id): - security.check_super(password) - if self.actions[db_id]['thread'].isAlive(): -# return addons.init_progress[db_name] - return (min(self.actions[db_id].get('progress', 0), 0.95), []) - else: - clean = self.actions[db_id]['clean'] - if clean: - users = self.actions[db_id]['users'] - del self.actions[db_id] - return (1.0, users) + try: + try: + database = sql_db.db_connect('template1', serialize=1) + database.truedb.autocommit() + cursor = database.cursor() + cursor.execute('CREATE DATABASE ' + db_name + ' ENCODING \'unicode\'') + cursor.close() + + cursor = sql_db.db_connect(db_name).cursor() + sql_db.init_db(cursor) + cursor.commit() + cursor.close() + cursor = None + pooler.get_pool(db_name, demo, update_module=True) + filename = os.path.join( + os.path.dirname(os.path.dirname(__file__)), + 'i18n', lang + '.csv') + if os.path.isfile(filename): + tools.trans_load(db_name, filename, lang) + cursor = sql_db.db_connect(db_name).cursor() + cursor.execute('SELECT login, password, name ' \ + 'FROM res_users ' \ + 'WHERE login <> \'root\' ORDER BY login') + res = cursor.dictfetchall() + except Exception, e: + print e + logger.notify_channel("web-service", LOG_ERROR, + 'CREATE DB: %s failed' % (db_name,)) + raise else: - exp = self.actions[db_id]['exception'] - del self.actions[db_id] - raise Exception, exp + logger.notify_channel("web-service", LOG_INFO, + 'CREATE DB: %s' % (db_name,)) + finally: + cursor.close() + return res def drop(self, password, db_name): security.check_super(password)