Exemplo n.º 1
0
    def session(self):
        """ Context Manager: start a new session and ensure that the
        session's cursor is:

        * rollbacked on errors
        * commited at the end of the ``with`` context when no error occured
        * always closed at the end of the ``with`` context
        * it handles the registry signaling
        """
        with openerp.api.Environment.manage():
            db = openerp.sql_db.db_connect(self.db_name)
            session = ConnectorSession(db.cursor(),
                                       self.uid,
                                       context=self.context)

            try:
                with session.env.clear_upon_failure():
                    RegistryManager.check_registry_signaling(self.db_name)
                    yield session
                    RegistryManager.signal_caches_change(self.db_name)
            except:
                session.rollback()
                raise
            else:
                session.commit()
            finally:
                session.close()
    def _scan_disk(self, cr, uid, context=None):
        """Scan the file system and register the result in database"""
        found_fonts = []
        for font_path in customfonts.list_all_sysfonts():
            try:
                font = ttfonts.TTFontFile(font_path)
                _logger.debug("Found font %s at %s", font.name, font_path)
                found_fonts.append((font.familyName, font.name, font_path, font.styleName))
            except ttfonts.TTFError:
                _logger.warning("Could not register Font %s", font_path)

        for family, name, path, mode in found_fonts:
            if not self.search(cr, uid, [('family', '=', family), ('name', '=', name)], context=context):
                self.create(cr, uid, {
                    'family': family, 'name': name,
                    'path': path, 'mode': mode,
                }, context=context)

        # remove fonts not present on the disk anymore
        existing_font_names = [name for (family, name, path, mode) in found_fonts]
        inexistant_fonts = self.search(cr, uid, [('name', 'not in', existing_font_names), ('path', '!=', '/dev/null')], context=context)
        if inexistant_fonts:
            self.unlink(cr, uid, inexistant_fonts, context=context)

        RegistryManager.signal_caches_change(cr.dbname)
        self._sync(cr, uid, context=context)
        return True
Exemplo n.º 3
0
 def _work_database(self, cr):
     db_name = cr.dbname
     try:
         cr.execute(
             "SELECT 1 FROM ir_module_module "
             "WHERE name = %s "
             "AND state = %s", ('connector', 'installed'),
             log_exceptions=False)
     except ProgrammingError as err:
         if unicode(err).startswith(
                 'relation "ir_module_module" does not exist'):
             _logger.debug(
                 'Database %s is not an Odoo database,'
                 ' connector worker not started', db_name)
         else:
             raise
     else:
         if cr.fetchone():
             RegistryManager.check_registry_signaling(db_name)
             registry = openerp.pooler.get_pool(db_name)
             if registry:
                 queue_worker = registry['queue.worker']
                 queue_worker.assign_then_enqueue(cr,
                                                  openerp.SUPERUSER_ID,
                                                  max_jobs=MAX_JOBS)
             RegistryManager.signal_caches_change(db_name)
Exemplo n.º 4
0
    def session(self):
        """ Context Manager: start a new session and ensure that the
        session's cursor is:

        * rollbacked on errors
        * commited at the end of the ``with`` context when no error occured
        * always closed at the end of the ``with`` context
        * it handles the registry signaling
        """
        with openerp.api.Environment.manage():
            db = openerp.sql_db.db_connect(self.db_name)
            session = ConnectorSession(db.cursor(),
                                       self.uid,
                                       context=self.context)

            try:
                with session.env.clear_upon_failure():
                    RegistryManager.check_registry_signaling(self.db_name)
                    yield session
                    RegistryManager.signal_caches_change(self.db_name)
            except:
                session.rollback()
                raise
            else:
                session.commit()
            finally:
                session.close()
Exemplo n.º 5
0
 def clear_cache(self, cr):
     """clear cache and update models. Notify other workers to restart their registry."""
     self.precision_get.clear_cache(self)
     for obj in self.pool.obj_list():
         for colname, col in self.pool.get(obj)._columns.items():
             if hasattr(col, 'digits_change'):
                 col.digits_change(cr)
     RegistryManager.signal_caches_change(cr.dbname)
Exemplo n.º 6
0
 def clear_cache(self, cr):
     """clear cache and update models. Notify other workers to restart their registry."""
     self.precision_get.clear_cache(self)
     env = openerp.api.Environment(cr, SUPERUSER_ID, {})
     for model in self.pool.values():
         for field in model._fields.values():
             if field.type == 'float':
                 field._setup_digits(env)
     RegistryManager.signal_caches_change(cr.dbname)
Exemplo n.º 7
0
 def clear_cache(self, cr):
     """clear cache and update models. Notify other workers to restart their registry."""
     self.precision_get.clear_cache(self)
     env = openerp.api.Environment(cr, SUPERUSER_ID, {})
     for model in self.pool.values():
         for field in model._fields.values():
             if field.type == 'float':
                 field._setup_digits(env)
     RegistryManager.signal_caches_change(cr.dbname)
Exemplo n.º 8
0
    def _scan_disk(self, cr, uid, context=None):
        """Scan the file system and register the result in database"""
        found_fonts = []
        for font_path in customfonts.list_all_sysfonts():
            try:
                font = ttfonts.TTFontFile(font_path)
                _logger.debug("Found font %s at %s", font.name, font_path)
                found_fonts.append(
                    (font.familyName, font.name, font_path, font.styleName))
            except Exception as ex:
                _logger.warning("Could not register Font %s: %s", font_path,
                                ex)

        for family, name, path, mode in found_fonts:
            if not self.search(cr,
                               uid, [('family', '=', family),
                                     ('name', '=', name)],
                               context=context):
                self.create(cr,
                            uid, {
                                'family': family,
                                'name': name,
                                'path': path,
                                'mode': mode,
                            },
                            context=context)

        # remove fonts not present on the disk anymore
        existing_font_names = [
            name for (family, name, path, mode) in found_fonts
        ]
        inexistant_fonts = self.search(
            cr,
            uid, [('name', 'not in', existing_font_names),
                  ('path', '!=', '/dev/null')],
            context=context)
        if inexistant_fonts:
            self.unlink(cr, uid, inexistant_fonts, context=context)

        RegistryManager.signal_caches_change(cr.dbname)
        self._sync(cr, uid, context=context)
        return True
    def session(self, db_name):
        """ Context Manager: start a new session and ensure that the
        session's cursor is:

        * rollbacked on errors
        * commited at the end of the ``with`` context when no error occured
        * always closed at the end of the ``with`` context
        * it handles the registry signaling
        """
        db = openerp.sql_db.db_connect(db_name)
        cr = db.cursor()

        try:
            RegistryManager.check_registry_signaling(db_name)
            yield cr
            RegistryManager.signal_caches_change(db_name)
        except:
            cr.rollback()
            raise
        else:
            cr.commit()
        finally:
            cr.close()
Exemplo n.º 10
0
 def _work_database(self, cr):
     db_name = cr.dbname
     try:
         cr.execute("SELECT 1 FROM ir_module_module "
                    "WHERE name = %s "
                    "AND state = %s", ('connector', 'installed'),
                    log_exceptions=False)
     except ProgrammingError as err:
         if unicode(err).startswith(
                 'relation "ir_module_module" does not exist'):
             _logger.debug('Database %s is not an Odoo database,'
                           ' connector worker not started', db_name)
         else:
             raise
     else:
         if cr.fetchone():
             RegistryManager.check_registry_signaling(db_name)
             registry = openerp.pooler.get_pool(db_name)
             if registry:
                 queue_worker = registry['queue.worker']
                 queue_worker.assign_then_enqueue(cr,
                                                  openerp.SUPERUSER_ID,
                                                  max_jobs=MAX_JOBS)
             RegistryManager.signal_caches_change(db_name)
Exemplo n.º 11
0
                _logger.warning("Could not register Font %s", font_path)

        for family, name, path, mode in found_fonts:
            if not self.search(cr, uid, [('family', '=', family), ('name', '=', name)], context=context):
                self.create(cr, uid, {
                    'family': family, 'name': name,
                    'path': path, 'mode': mode,
                }, context=context)

        # remove fonts not present on the disk anymore
        existing_font_names = [name for (family, name, path, mode) in found_fonts]
        inexistant_fonts = self.search(cr, uid, [('name', 'not in', existing_font_names), ('path', '!=', '/dev/null')], context=context)
        if inexistant_fonts:
            self.unlink(cr, uid, inexistant_fonts, context=context)

        RegistryManager.signal_caches_change(cr.dbname)
        self._sync(cr, uid, context=context)
        return True

    def _sync(self, cr, uid, context=None):
        """Set the customfonts.CustomTTFonts list to the content of the database"""
        customfonts.CustomTTFonts = []
        local_family_modes = set()
        local_font_paths = {}
        found_fonts_ids = self.search(cr, uid, [('path', '!=', '/dev/null')], context=context)
        for font in self.browse(cr, uid, found_fonts_ids, context=None):
            local_family_modes.add((font.family, font.mode))
            local_font_paths[font.name] = font.path
            customfonts.CustomTTFonts.append((font.family, font.name, font.path, font.mode))

        # Attempt to remap the builtin fonts (Helvetica, Times, Courier) to better alternatives
Exemplo n.º 12
0
                            },
                            context=context)

        # remove fonts not present on the disk anymore
        existing_font_names = [
            name for (family, name, path, mode) in found_fonts
        ]
        inexistant_fonts = self.search(
            cr,
            uid, [('name', 'not in', existing_font_names),
                  ('path', '!=', '/dev/null')],
            context=context)
        if inexistant_fonts:
            self.unlink(cr, uid, inexistant_fonts, context=context)

        RegistryManager.signal_caches_change(cr.dbname)
        self._sync(cr, uid, context=context)
        return True

    def _sync(self, cr, uid, context=None):
        """Set the customfonts.CustomTTFonts list to the content of the database"""
        customfonts.CustomTTFonts = []
        local_family_modes = set()
        local_font_paths = {}
        found_fonts_ids = self.search(cr,
                                      uid, [('path', '!=', '/dev/null')],
                                      context=context)
        for font in self.browse(cr, uid, found_fonts_ids, context=None):
            local_family_modes.add((font.family, font.mode))
            local_font_paths[font.name] = font.path
            customfonts.CustomTTFonts.append(
Exemplo n.º 13
0
class res_font(osv.Model):
    _name = "res.font"
    _description = 'Fonts available'
    _order = 'family,name,id'
    _rec_name = 'family'
    _log_unlink = False

    _columns = {
        'family': fields.char("Font family", required=True),
        'name': fields.char("Font Name", required=True),
        'path': fields.char("Path", required=True),
        'mode': fields.char("Mode", required=True),
    }

    _sql_constraints = [
        ('name_font_uniq', 'unique(family, name)',
         'You can not register two fonts with the same name'),
    ]

    def font_scan(self, cr, uid, lazy=False, context=None):
        """Action of loading fonts
        In lazy mode will scan the filesystem only if there is no founts in the database and sync if no font in CustomTTFonts
        In not lazy mode will force scan filesystem and sync
        """
        if lazy:
            # lazy loading, scan only if no fonts in db
            found_fonts_ids = self.search(cr,
                                          uid, [('path', '!=', '/dev/null')],
                                          context=context)
            if not found_fonts_ids:
                # no scan yet or no font found on the system, scan the filesystem
                self._scan_disk(cr, uid, context=context)
            elif len(customfonts.CustomTTFonts) == 0:
                # CustomTTFonts list is empty
                self._sync(cr, uid, context=context)
        else:
            self._scan_disk(cr, uid, context=context)
        return True

    def _scan_disk(self, cr, uid, context=None):
        """Scan the file system and register the result in database"""
        found_fonts = []
        for font_path in customfonts.list_all_sysfonts():
            try:
                font = ttfonts.TTFontFile(font_path)
                _logger.debug("Found font %s at %s", font.name, font_path)
                found_fonts.append(
                    (font.familyName, font.name, font_path, font.styleName))
            except Exception, ex:
                _logger.warning("Could not register Font %s: %s", font_path,
                                ex)

        for family, name, path, mode in found_fonts:
            if not self.search(cr,
                               uid, [('family', '=', family),
                                     ('name', '=', name)],
                               context=context):
                self.create(cr,
                            uid, {
                                'family': family,
                                'name': name,
                                'path': path,
                                'mode': mode,
                            },
                            context=context)

        # remove fonts not present on the disk anymore
        existing_font_names = [
            name for (family, name, path, mode) in found_fonts
        ]
        inexistant_fonts = self.search(
            cr,
            uid, [('name', 'not in', existing_font_names),
                  ('path', '!=', '/dev/null')],
            context=context)
        if inexistant_fonts:
            self.unlink(cr, uid, inexistant_fonts, context=context)

        RegistryManager.signal_caches_change(cr.dbname)
        self._sync(cr, uid, context=context)
        return True
            db = request.session.db
            if db:
                RegistryManager.check_registry_signaling(db)
                try:
                    with openerp.tools.mute_logger('openerp.sql_db'):
                        ir_http = request.registry['ir.http']
                except (AttributeError, psycopg2.OperationalError):
                    # psycopg2 error or attribute error while constructing
                    # the registry. That means the database probably does
                    # not exists anymore or the code doesnt match the db.
                    # Log the user out and fall back to nodb
                    request.session.logout()
                    result = _dispatch_nodb()
                else:
                    result = ir_http._dispatch()
                    RegistryManager.signal_caches_change(db)
            else:
                result = _dispatch_nodb()

            # Set the 'template' property to make the Response object
            # pass the 'is_qweb' check.
            result.template = 'fake_template'

            # Mock the Response.flatten() method
            # (because it's not what is being tested here)
            # and check that the mock has actually been called.
            def log_mock_called():
                _logger.debug(
                    'Mock of Response.flatten() method called during the test')
                raise Exception('Expected exception raised during the test.')
Exemplo n.º 15
0
            db = request.session.db
            if db:
                RegistryManager.check_registry_signaling(db)
                try:
                    with openerp.tools.mute_logger('openerp.sql_db'):
                        ir_http = request.registry['ir.http']
                except (AttributeError, psycopg2.OperationalError):
                    # psycopg2 error or attribute error while constructing
                    # the registry. That means the database probably does
                    # not exists anymore or the code doesnt match the db.
                    # Log the user out and fall back to nodb
                    request.session.logout()
                    result = _dispatch_nodb()
                else:
                    result = ir_http._dispatch()
                    RegistryManager.signal_caches_change(db)
            else:
                result = _dispatch_nodb()

            # Set the 'template' property to make the Response object
            # pass the 'is_qweb' check.
            result.template = 'fake_template'

            # Mock the Response.flatten() method
            # (because it's not what is being tested here)
            # and check that the mock has actually been called.
            def log_mock_called():
                _logger.debug(
                    'Mock of Response.flatten() method called during the test')
                raise Exception('Expected exception raised during the test.')
Exemplo n.º 16
0
 def clear_cache(self, cr):
     """clear cache and update models. Notify other workers to restart their registry."""
     self.precision_get.clear_cache(self)
     RegistryManager.signal_caches_change(cr.dbname)