コード例 #1
0
    def get_cursor(self, create_session_store_db=True):
        db_name = config.get('session_store_db', 'session_store')
        try:
            con = db_connect(db_name)
            cr = con.cursor()
        except:
            if not create_session_store_db:
                raise
            db_connect('postgres')
            with closing(db_connect('postgres').cursor()) as cr:
                cr.autocommit(True)  # avoid transaction block
                cr.execute(
                    """CREATE DATABASE "%s" ENCODING 'unicode' TEMPLATE "%s" """
                    % (db_name, config['db_template']))
            return self.get_cursor(create_session_store_db=False)

        cr.execute("""
            CREATE TABLE IF NOT EXISTS sessionstore (
              id varchar(40),
              data bytea
            );
            """)
        cr.commit()

        return cr
コード例 #2
0
def list_db_incompatible(databases):
    """"Check a list of databases if they are compatible with this version of Flectra

        :param databases: A list of existing Postgresql databases
        :return: A list of databases that are incompatible
    """
    incompatible_databases = []
    server_version = '.'.join(str(v) for v in version_info[:2])
    for database_name in databases:
        with closing(db_connect(database_name).cursor()) as cr:
            if flectra.tools.table_exists(cr, 'ir_module_module'):
                cr.execute(
                    "SELECT latest_version FROM ir_module_module WHERE name=%s",
                    ('base', ))
                base_version = cr.fetchone()
                if not base_version or not base_version[0]:
                    incompatible_databases.append(database_name)
                else:
                    # e.g. 10.saas~15
                    local_version = '.'.join(base_version[0].split('.')[:2])
                    if local_version != server_version:
                        incompatible_databases.append(database_name)
            else:
                incompatible_databases.append(database_name)
    for database_name in incompatible_databases:
        # release connection
        flectra.sql_db.close_db(database_name)
    return incompatible_databases
コード例 #3
0
    def addons_data_dir(self):
        add_dir = os.path.join(self['data_dir'], 'addons')
        d = os.path.join(add_dir, release.series)
        if not os.path.exists(d):
            try:
                # bootstrap parent dir +rwx
                if not os.path.exists(add_dir):
                    os.makedirs(add_dir, 0o700)
                # try to make +rx placeholder dir, will need manual +w to activate it
                os.makedirs(d, 0o700)
            except OSError:
                logging.getLogger(__name__).debug(
                    'Failed to create addons data dir %s', d)
        try:
            try:
                from flectra.http import request, root
                if request.session.db and not os.listdir(os.path.join(d)):
                    from flectra.sql_db import db_connect
                    with closing(db_connect(
                            request.session.db).cursor()) as cr:
                        if flectra.tools.table_exists(cr, 'ir_module_module'):
                            cr.execute(
                                "SELECT latest_version FROM ir_module_module WHERE name=%s",
                                ('base', ))
                            base_version = cr.fetchone()
                            if base_version and base_version[0]:
                                tmp = base_version[0].split('.')[:2]
                                last_version = '.'.join(str(v) for v in tmp)
                                s = os.path.join(add_dir, last_version)
                                if float(last_version) < float(
                                        release.series) and os.listdir(
                                            os.path.join(s)):
                                    self.copytree(s, d)
                                    root.load_addons()
            except:
                pass

            if self.get('app_store') == 'install':
                if not os.access(d, os.W_OK):
                    os.chmod(d, 0o700)
            else:
                if os.access(d, os.W_OK):
                    os.chmod(d, 0o500)
        except OSError:
            logging.getLogger(__name__).debug("No such file or directory: %s",
                                              d)
        return d
コード例 #4
0
    def import_lang(self):
        this = self[0]
        with TemporaryFile('wb+') as buf:
            try:
                buf.write(base64.decodebytes(this.data))

                # now we determine the file format
                buf.seek(0)
                fileformat = os.path.splitext(this.filename)[-1][1:].lower()

                Lang = self.env["res.lang"]
                lang = Lang._activate_lang(self.code) or Lang._create_lang(
                    self.code, lang_name=self.name)

                tools.trans_load_data(this._cr,
                                      buf,
                                      fileformat,
                                      this.code,
                                      overwrite=self.overwrite)
            except ProgrammingError as e:
                _logger.exception(
                    'File unsuccessfully imported, due to a malformed file.')

                with closing(sql_db.db_connect(
                        self._cr.dbname).cursor()) as cr:
                    raise UserError(
                        _('File %r not imported due to a malformed file.\n\n'
                          'This issue can be caused by duplicates entries who are referring to the same field. '
                          'Please check the content of the file you are trying to import.\n\n'
                          'Technical Details:\n%s') %
                        (self.filename, tools.ustr(e)))
            except Exception as e:
                _logger.exception(
                    'File unsuccessfully imported, due to format mismatch.')
                raise UserError(
                    _('File %r not imported due to format mismatch or a malformed file.'
                      ' (Valid formats are .csv, .po, .pot)\n\nTechnical Details:\n%s') % \
                    (this.filename, tools.ustr(e))
                )
        return True