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 odoo.api.Environment.manage(): db = odoo.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()
class ResFont(models.Model): _name = "res.font" _description = 'Fonts available' _order = 'family,name,id' _rec_name = 'family' family = fields.Char(string="Font family", required=True) name = fields.Char(string="Font Name", required=True) path = fields.Char(required=True) mode = fields.Char(required=True) _sql_constraints = [ ('name_font_uniq', 'unique(family, name)', 'You can not register two fonts with the same name'), ] @api.model def font_scan(self, lazy=False): """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 fonts = self.search([('path', '!=', '/dev/null')]) if not fonts: # no scan yet or no font found on the system, scan the filesystem self._scan_disk() elif len(customfonts.CustomTTFonts) == 0: # CustomTTFonts list is empty self._sync() else: self._scan_disk() return True def _scan_disk(self): """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([('family', '=', family), ('name', '=', name)]): self.create({'family': family, 'name': name, 'path': path, 'mode': mode}) # remove fonts not present on the disk anymore existing_font_names = [name for (family, name, path, mode) in found_fonts] # Remove inexistent fonts self.search([('name', 'not in', existing_font_names), ('path', '!=', '/dev/null')]).unlink() RegistryManager.signal_caches_change(self._cr.dbname) return self._sync()