示例#1
0
文件: webdav.py 项目: Sisouvan/ogh
 def get_childs(self, uri, filter=None):
     res = []
     dbname, dburi = self._get_dburi(uri)
     if not dbname:
         database = Database().connect()
         cursor = database.cursor()
         try:
             lists = database.list(cursor)
         except Exception:
             lists = []
         finally:
             cursor.close()
         for dbname in lists:
             res.append(urlparse.urljoin(uri, dbname))
         return res
     pool = Pool(Transaction().cursor.database_name)
     try:
         Collection = pool.get('webdav.collection')
         scheme, netloc, path, params, query, fragment = \
             urlparse.urlparse(uri)
         if path[-1:] != '/':
             path += '/'
         for child in Collection.get_childs(dburi, filter=filter,
                 cache=CACHE):
             res.append(urlparse.urlunparse((scheme, netloc,
                         path + child.encode('utf-8'), params, query,
                         fragment)))
     except KeyError:
         return res
     except (DAV_Error, DAV_NotFound, DAV_Secret, DAV_Forbidden), exception:
         self._log_exception(exception)
         raise
示例#2
0
    def init(self):
        from trytond.config import CONFIG
        CONFIG['database_type'] = 'sqlite'
        self.db_name = ':memory:'

        from trytond.backend import Database
        database = Database()
        cursor = database.cursor()
        databases = database.list(cursor)
        cursor.close()

        if self.db_name not in databases:
            from trytond.protocols.dispatcher import create
            create(self.db_name, 'admin', 'en_US', 'admin')


        self.user = 1
        self.context = None

        from trytond.pool import Pool
        from trytond.transaction import Transaction
        with Transaction().start(self.db_name, self.user, self.context) as txn:
            self.pool = Pool(self.db_name)
            self.pool.init()


        self.initialised = True
示例#3
0
文件: webdav.py 项目: Sisouvan/ogh
    def get_userinfo(self, user, password, command=''):
        path = urlparse.urlparse(self.path).path
        dbname = urllib.unquote_plus(path.split('/', 2)[1])
        database = Database().connect()
        cursor = database.cursor()
        databases = database.list(cursor)
        cursor.close()
        if not dbname or dbname not in databases:
            return True
        if user:
            user = int(login(dbname, user, password, cache=False))
            if not user:
                return None
        else:
            url = urlparse.urlparse(self.path)
            query = urlparse.parse_qs(url.query)
            path = url.path[len(dbname) + 2:]
            if 'key' in query:
                key, = query['key']
                with Transaction().start(dbname, 0) as transaction:
                    database_list = Pool.database_list()
                    pool = Pool(dbname)
                    if not dbname in database_list:
                        pool.init()
                    Share = pool.get('webdav.share')
                    user = Share.get_login(key, command, path)
                    transaction.cursor.commit()
            if not user:
                return None

        Transaction().start(dbname, user)
        Cache.clean(dbname)
        return user
def install_module(name):
    '''
    Install module for the tested database
    '''
    database = Database().connect()
    cursor = database.cursor()
    databases = database.list(cursor)
    cursor.close()
    if DB_NAME not in databases:
        create(DB_NAME, CONFIG['admin_passwd'], 'en_US', USER_PASSWORD)
    with Transaction().start(DB_NAME, USER,
            context=CONTEXT) as transaction:
        module_obj = POOL.get('ir.module.module')

        module_ids = module_obj.search([
            ('name', '=', name),
            ])
        assert module_ids

        module_ids = module_obj.search([
            ('name', '=', name),
            ('state', '!=', 'installed'),
            ])

        if not module_ids:
            return

        module_obj.install(module_ids)
        transaction.cursor.commit()

        install_upgrade_obj = POOL.get('ir.module.module.install_upgrade',
                type='wizard')
        install_upgrade_obj.transition_upgrade(None)
        transaction.cursor.commit()
示例#5
0
def database_list():
    """Fetch all databases in current instance"""
    from trytond.backend import Database
    database = Database().connect()
    cursor = database.cursor()
    databases = database.list(cursor)
    cursor.close()
    return databases
 def start(self, database_name, user, readonly=False, context=None):
     '''
     Start transaction
     '''
     assert self.user is None
     assert self.cursor is None
     assert self.context is None
     database = Database(database_name).connect()
     self.cursor = database.cursor(readonly=readonly)
     self.user = user
     self.context = context or {}
     self.create_records = {}
     self.delete_records = {}
     self.delete = {}
     self.timestamp = {}
     return _TransactionManager()
示例#7
0
 def clean(dbname):
     if not CONFIG["multi_server"]:
         return
     database = Database(dbname).connect()
     cursor = database.cursor()
     try:
         cursor.execute('SELECT "timestamp", "name" FROM ir_cache')
         timestamps = {}
         for timestamp, name in cursor.fetchall():
             timestamps[name] = timestamp
     finally:
         cursor.commit()
         cursor.close()
     for inst in Cache._cache_instance:
         if inst._name in timestamps:
             with inst._lock:
                 if not inst._timestamp or timestamps[inst._name] > inst._timestamp:
                     inst._timestamp = timestamps[inst._name]
                     inst._cache[dbname] = LRUDict(inst.size_limit)
def dump(database_name, password):
    security.check_super(password)
    Database(database_name).close()
    # Sleep to let connections close
    time.sleep(1)
    logger = logging.getLogger('database')

    data = Database.dump(database_name)
    logger.info('DUMP DB: %s' % (database_name))
    return buffer(data)
示例#9
0
 def clean(dbname):
     if not CONFIG['multi_server']:
         return
     database = Database(dbname).connect()
     cursor = database.cursor()
     try:
         cursor.execute('SELECT "timestamp", "name" FROM ir_cache')
         timestamps = {}
         for timestamp, name in cursor.fetchall():
             timestamps[name] = timestamp
     finally:
         cursor.commit()
         cursor.close()
     for obj in Cache._cache_instance:
         if obj._name in timestamps:
             if (not obj._timestamp
                     or timestamps[obj._name] > obj._timestamp):
                 obj._timestamp = timestamps[obj._name]
                 obj._lock.acquire()
                 try:
                     obj._cache[dbname] = {}
                 finally:
                     obj._lock.release()
示例#10
0
 def resets(dbname):
     if not CONFIG['multi_server']:
         return
     database = Database(dbname).connect()
     cursor = database.cursor()
     Cache._resets_lock.acquire()
     Cache._resets.setdefault(dbname, set())
     try:
         for name in Cache._resets[dbname]:
             cursor.execute('SELECT name FROM ir_cache WHERE name = %s',
                         (name,))
             if cursor.fetchone():
                 cursor.execute('UPDATE ir_cache SET "timestamp" = %s '\
                         'WHERE name = %s', (datetime.datetime.now(), name))
             else:
                 cursor.execute('INSERT INTO ir_cache ' \
                         '("timestamp", "name") ' \
                         'VALUES (%s, %s)', (datetime.datetime.now(), name))
         Cache._resets[dbname].clear()
     finally:
         cursor.commit()
         cursor.close()
         Cache._resets_lock.release()
示例#11
0
 def resets(dbname):
     if not CONFIG["multi_server"]:
         return
     database = Database(dbname).connect()
     cursor = database.cursor()
     try:
         with Cache._resets_lock:
             Cache._resets.setdefault(dbname, set())
             for name in Cache._resets[dbname]:
                 cursor.execute("SELECT name FROM ir_cache WHERE name = %s", (name,))
                 if cursor.fetchone():
                     # It would be better to insert only
                     cursor.execute(
                         'UPDATE ir_cache SET "timestamp" = %s ' "WHERE name = %s", (datetime.datetime.now(), name)
                     )
                 else:
                     cursor.execute(
                         "INSERT INTO ir_cache " '("timestamp", "name") ' "VALUES (%s, %s)",
                         (datetime.datetime.now(), name),
                     )
             Cache._resets[dbname].clear()
     finally:
         cursor.commit()
         cursor.close()
def drop(database_name, password):
    security.check_super(password)
    Database(database_name).close()
    # Sleep to let connections close
    time.sleep(1)
    logger = logging.getLogger('database')

    database = Database().connect()
    cursor = database.cursor(autocommit=True)
    try:
        try:
            database.drop(cursor, database_name)
            cursor.commit()
        except Exception:
            logger.error('DROP DB: %s failed' % (database_name,))
            tb_s = ''.join(traceback.format_exception(*sys.exc_info()))
            logger.error('Exception in call: \n' + tb_s)
            raise
        else:
            logger.info('DROP DB: %s' % (database_name))
            Pool.stop(database_name)
    finally:
        cursor.close(close=True)
    return True
示例#13
0
def drop_all():
    from trytond.backend import Database
    database = Database().connect()
    cursor = database.cursor()
    erase_cursor = database.cursor(autocommit=True)
    for db in database.list(cursor):
        print "Dropping database %s" % db
        database.drop(erase_cursor, db)
    erase_cursor.close()
    cursor.close()
 def new_cursor(self):
     manager = _CursorManager(self.cursor)
     database = Database(self.cursor.database_name).connect()
     self.cursor = database.cursor()
     return manager
示例#15
0
if os.path.isdir(DIR):
    sys.path.insert(0,os.path.dirname(DIR))


from trytond.modules import register_classes
from tryton.pool import Pool
from trytond.backend import Database
from trytond.tools import Cache

#register class populates the pool of moduls:

register_classes()


#Instantiate the dataase and the pool
DB = Database(settings,TRYTON_db).connect()
POOL = Pool(setting.TRYTON_DB)
POOL.init()
user_obj  = POOL.get('res_user')
cursur = DB.cursor()
Cache.clean(settings.TRYTON_DB)
try:
    #user 0 is  aroot user .we ue it to get user id
    USER = user.obj.search(cursor,0, [
            ('login','=',settings.TRYTON_UN),
            ], limit=1)[0]
finally:
    cursor.close()
    Cache.reset(settings.TRYTON_DB) 

示例#16
0
warnings.filterwarnings("ignore", message="Old style callback, usecb_func(ok, store) instead")

TRYTOND_PATH = settings.TRYTOND_PATH

DIR = os.path.abspath(os.path.normpath(os.path.join(TRYTOND_PATH,'trytond')))
if os.path.isdir(DIR):
    sys.path.insert(0, os.path.dirname(DIR))

from trytond.modules import register_classes
from trytond.pool import Pool
from trytond.backend import Database
from trytond.tools import Cache

# Register classes populates the pool of models:
register_classes()

# Instantiate the database and the pool
DB = Database(settings.TRYTON_DB).connect()
POOL = Pool(settings.TRYTON_DB)
POOL.init()
user_obj = POOL.get('res.user')
cursor = DB.cursor()
Cache.clean(settings.TRYTON_DB)
try:
    # User 0 is root user. We use it to get the user id:
    USER = user_obj.search(cursor, 0, [
            ('login', '=', settings.TRYTON_UN),
            ], limit=1)[0]
finally:
    cursor.close()
    Cache.resets(settings.TRYTON_DB)
def restore(database_name, password, data, update=False):
    logger = logging.getLogger('database')
    security.check_super(password)
    try:
        database = Database().connect()
        cursor = database.cursor()
        cursor.close(close=True)
        raise Exception("Database already exists!")
    except Exception:
        pass
    Database.restore(database_name, data)
    logger.info('RESTORE DB: %s' % (database_name))
    if update:
        cursor = Database(database_name).connect().cursor()
        cursor.execute('SELECT code FROM ir_lang ' \
                'WHERE translatable')
        lang = [x[0] for x in cursor.fetchall()]
        cursor.execute('UPDATE ir_module_module SET ' \
                "state = 'to upgrade' " \
                "WHERE state = 'installed'")
        cursor.commit()
        cursor.close()
        Pool(database_name).init(update=update, lang=lang)
        logger.info('Update/Init succeed!')
    return True
def dispatch(host, port, protocol, database_name, user, session, object_type,
        object_name, method, *args, **kargs):

    if object_type == 'common':
        if method == 'login':
            try:
                database = Database(database_name).connect()
                cursor = database.cursor()
                cursor.close()
            except Exception:
                return False
            res = security.login(database_name, user, session)
            Cache.clean(database_name)
            logger = logging.getLogger('dispatcher')
            msg = res and 'successful login' or 'bad login or password'
            logger.info('%s \'%s\' from %s:%d using %s on database \'%s\'' % \
                    (msg, user, host, port, protocol, database_name))
            Cache.resets(database_name)
            return res or False
        elif method == 'logout':
            name = security.logout(database_name, user, session)
            logger = logging.getLogger('dispatcher')
            logger.info('logout \'%s\' from %s:%d ' \
                    'using %s on database \'%s\'' %
                    (name, host, port, protocol, database_name))
            return True
        elif method == 'version':
            return VERSION
        elif method == 'timezone_get':
            return CONFIG['timezone']
        elif method == 'list_lang':
            return [
                ('bg_BG', 'Български'),
                ('ca_ES', 'Català'),
                ('cs_CZ', 'Čeština'),
                ('de_DE', 'Deutsch'),
                ('en_US', 'English'),
                ('es_AR', 'Español (Argentina)'),
                ('es_ES', 'Español (España)'),
                ('es_CO', 'Español (Colombia)'),
                ('fr_FR', 'Français'),
                ('nl_NL', 'Nederlands'),
                ('ru_RU', 'Russian'),
            ]
        elif method == 'db_exist':
            try:
                database = Database(*args, **kargs).connect()
                cursor = database.cursor()
                cursor.close(close=True)
                return True
            except Exception:
                return False
        elif method == 'list':
            if CONFIG['prevent_dblist']:
                raise Exception('AccessDenied')
            database = Database().connect()
            try:
                cursor = database.cursor()
                try:
                    res = database.list(cursor)
                finally:
                    cursor.close(close=True)
            except Exception:
                res = []
            return res
        elif method == 'create':
            return create(*args, **kargs)
        elif method == 'restore':
            return restore(*args, **kargs)
        elif method == 'drop':
            return drop(*args, **kargs)
        elif method == 'dump':
            return dump(*args, **kargs)
        return
    elif object_type == 'system':
        database = Database(database_name).connect()
        database_list = Pool.database_list()
        pool = Pool(database_name)
        if not database_name in database_list:
            pool.init()
        if method == 'listMethods':
            res = []
            for type in ('model', 'wizard', 'report'):
                for object_name, obj in pool.iterobject(type=type):
                    for method in obj._rpc:
                        res.append(type + '.' + object_name + '.' + method)
            return res
        elif method == 'methodSignature':
            return 'signatures not supported'
        elif method == 'methodHelp':
            res = []
            args_list = args[0].split('.')
            object_type = args_list[0]
            object_name = '.'.join(args_list[1:-1])
            method = args_list[-1]
            obj = pool.get(object_name, type=object_type)
            return pydoc.getdoc(getattr(obj, method))

    user = security.check(database_name, user, session)

    Cache.clean(database_name)
    database_list = Pool.database_list()
    pool = Pool(database_name)
    if not database_name in database_list:
        with Transaction().start(database_name, user,
                readonly=True) as transaction:
            pool.init()
    obj = pool.get(object_name, type=object_type)

    if method in obj._rpc:
        readonly = not obj._rpc[method]
    elif method in getattr(obj, '_buttons', {}):
        readonly = False
    else:
        raise UserError('Calling method %s on %s %s is not allowed!'
            % (method, object_type, object_name))

    for count in range(int(CONFIG['retry']), -1, -1):
        with Transaction().start(database_name, user,
                readonly=readonly) as transaction:
            try:

                args_without_context = list(args)
                if 'context' in kargs:
                    context = kargs.pop('context')
                else:
                    context = args_without_context.pop()
                if '_timestamp' in context:
                    transaction.timestamp = context['_timestamp']
                    del context['_timestamp']
                transaction.context = context
                res = getattr(obj, method)(*args_without_context, **kargs)
                if not readonly:
                    transaction.cursor.commit()
            except DatabaseOperationalError, exception:
                transaction.cursor.rollback()
                if count and not readonly:
                    continue
                raise
            except Exception, exception:
                if CONFIG['verbose'] and not isinstance(exception, (
                            NotLogged, ConcurrencyException, UserError,
                            UserWarning)):
                    tb_s = ''.join(traceback.format_exception(*sys.exc_info()))
                    logger = logging.getLogger('dispatcher')
                    logger.error('Exception calling method %s on '
                        '%s %s from %s@%s:%d/%s:\n'
                        % (method, object_type, object_name, user, host, port,
                            database_name) + tb_s)
                transaction.cursor.rollback()
                raise
def create(database_name, password, lang, admin_password):
    '''
    Create a database

    :param database_name: the database name
    :param password: the server password
    :param lang: the default language for the database
    :param admin_password: the admin password
    :return: True if succeed
    '''
    security.check_super(password)
    res = False
    logger = logging.getLogger('database')

    try:
        database = Database().connect()
        cursor = database.cursor(autocommit=True)
        try:
            database.create(cursor, database_name)
            cursor.commit()
            cursor.close(close=True)
        except Exception:
            cursor.close()
            raise

        with Transaction().start(database_name, 0) as transaction:
            database.init(transaction.cursor)
            transaction.cursor.commit()

        pool = Pool(database_name)
        pool.init(update=True, lang=[lang])
        with Transaction().start(database_name, 0) as transaction:
            cursor = transaction.cursor
            #XXX replace with model write
            cursor.execute('UPDATE ir_lang ' \
                    'SET translatable = %s ' \
                    'WHERE code = %s', (True, lang))
            cursor.execute('UPDATE res_user ' \
                    'SET language = (' + \
                        cursor.limit_clause('SELECT id FROM ir_lang ' \
                        'WHERE code = %s', 1) + ')' \
                    'WHERE login <> \'root\'', (lang,))
            if hashlib:
                admin_password = hashlib.sha1(admin_password).hexdigest()
            else:
                admin_password = sha.new(admin_password).hexdigest()
            cursor.execute('UPDATE res_user ' \
                    'SET password = %s ' \
                    'WHERE login = \'admin\'', (admin_password,))
            module_obj = pool.get('ir.module.module')
            if module_obj:
                module_obj.update_list()
            cursor.commit()
            res = True
    except Exception:
        logger.error('CREATE DB: %s failed' % (database_name,))
        tb_s = ''.join(traceback.format_exception(*sys.exc_info()))
        logger.error('Exception in call: \n' + tb_s)
        raise
    else:
        logger.info('CREATE DB: %s' % (database_name,))
    return res