def test_mixing_get_class_and_queries(self): """ This test shows that we can mix the use of DBSession and the db reflection API. """ from c2cgeoportal.lib.dbreflection import get_class from c2cgeoportal.models import DBSession from sqlalchemy import text import transaction self._create_table("table_c") DBSession.execute(text("SELECT id FROM table_c")) modelclass = get_class("table_c") self.assertEquals(modelclass.__name__, "Table_c") # This commits the transaction created by DBSession.execute. This # is required here in the test because tearDown does table.drop, # which will block forever if the transaction is not committed. transaction.commit()
def test_mixing_get_class_and_queries(self): """ This test shows that we can mix the use of DBSession and the db reflection API. """ from c2cgeoportal.lib.dbreflection import get_class from c2cgeoportal.models import DBSession from sqlalchemy import text import transaction self._create_table('table_c') DBSession.execute(text('SELECT id FROM table_c')) modelclass = get_class('table_c') self.assertEquals(modelclass.__name__, 'Table_c') # This commits the transaction created by DBSession.execute. This # is required here in the test because tearDown does table.drop, # which will block forever if the transaction is not committed. transaction.commit()
def main(): # pragma: no cover parser = ArgumentParser( prog=sys.argv[0], add_help=True, description="Tool used to migrate your old layers from the old structure to the new one.", ) parser.add_argument( "-i", "--app-config", default="production.ini", dest="app_config", help="the application .ini config file (optional, default is 'production.ini')" ) parser.add_argument( "-n", "--app-name", default="app", dest="app_name", help="the application name (optional, default is 'app')" ) options = parser.parse_args() app_config = options.app_config app_name = options.app_name if app_name is None and "#" in app_config: app_config, app_name = app_config.split("#", 1) get_app(app_config, name=app_name) # must be done only once we have loaded the project config from c2cgeoportal.models import DBSession, \ LayerInternalWMS, LayerExternalWMS, LayerWMTS, LayerV1 session = DBSession() session.execute(LayerInternalWMS.__table__.delete()) session.execute(LayerExternalWMS.__table__.delete()) session.execute(LayerWMTS.__table__.delete()) for layer in DBSession.query(LayerV1).all(): layer_v1tov2(session, layer) transaction.commit()
class Import: def __init__(self, options): self.options = options self.imported = set() settings = get_config(".build/config.yaml") package = settings["package"] self.fts_languages = settings["fulltextsearch"]["languages"] self.languages = settings["available_locale_names"] # must be done only once we have loaded the project config from c2cgeoportal.models import DBSession, FullTextSearch, Interface, Theme, Role self.session = DBSession() self.session.execute(FullTextSearch.__table__.delete().where(FullTextSearch.from_theme == True)) # noqa self._ = {} for lang in self.languages: self._[lang] = translation( "{}-client".format(package), os.path.join(package, "locale/"), [lang]) query = self.session.query(Interface) if options.interfaces is not None: query = query.filter( Interface.name.in_(options.interfaces) ) self.interfaces = query.all() self.public_theme = {} self.public_group = {} for interface in self.interfaces: self.public_theme[interface.id] = [] self.public_group[interface.id] = [] for theme in self.session.query(Theme).filter_by(public=True).all(): self._add_theme(theme) for role in self.session.query(Role).all(): for theme in self.session.query(Theme).all(): self._add_theme(theme, role) transaction.commit() def _add_fts(self, item, interface, action, role): from c2cgeoportal.models import FullTextSearch key = ( item.name if self.options.name else item.id, interface.id, role.id if role is not None else None ) if key not in self.imported: self.imported.add(key) for lang in self.languages: fts = FullTextSearch() fts.label = self._[lang].gettext(item.name) fts.role = role fts.interface = interface fts.lang = lang fts.public = role is None fts.ts = func.to_tsvector(self.fts_languages[lang], fts.label) fts.actions = [{ "action": action, "data": item.name, }] fts.from_theme = True self.session.add(fts) def _add_theme(self, theme, role=None): fill = False for interface in self.interfaces: if interface in theme.interfaces: for child in theme.children: fill = self._add_block(child, interface, role) or fill if fill and self.options.themes: if role is None: self.public_theme[interface.id].append(theme.id) if role is None or theme.id not in self.public_theme[interface.id]: self._add_fts(theme, interface, "add_theme", role) def _add_block(self, group, interface, role): return self._add_group(group, interface, self.options.blocks, role) def _add_folder(self, group, interface, role): return self._add_group(group, interface, self.options.folders, role) def _add_group(self, group, interface, export, role): from c2cgeoportal.models import LayerGroup fill = False for child in group.children: if isinstance(child, LayerGroup): fill = self._add_folder(child, interface, role) or fill else: fill = self._add_layer(child, interface, role) or fill if fill and export: if role is None: self.public_group[interface.id].append(group.id) if role is None or group.id not in self.public_group[interface.id]: self._add_fts(group, interface, "add_group", role) return fill def _layer_visible(self, layer, role): for restrictionarea in layer.restrictionareas: if role in restrictionarea.roles: return True return False def _add_layer(self, layer, interface, role): from c2cgeoportal.models import LayerV1 if isinstance(layer, LayerV1): return False if role is None: fill = layer.public and interface in layer.interfaces else: fill = interface in layer.interfaces and not layer.public and \ self._layer_visible(layer, role) if fill and self.options.layers: self._add_fts(layer, interface, "add_layer", role) return fill
class Import: def __init__(self, options): self.options = options self.imported = set() settings = get_config(".build/config.yaml") package = settings["package"] self.fts_languages = settings["fulltextsearch"]["languages"] self.languages = settings["available_locale_names"] # must be done only once we have loaded the project config from c2cgeoportal.models import DBSession, FullTextSearch, Interface, Theme, Role self.session = DBSession() self.session.execute(FullTextSearch.__table__.delete().where( FullTextSearch.from_theme == True)) # noqa self._ = {} for lang in self.languages: self._[lang] = translation("{}-client".format(package), os.path.join(package, "locale/"), [lang]) query = self.session.query(Interface) if options.interfaces is not None: query = query.filter(Interface.name.in_(options.interfaces)) self.interfaces = query.all() self.public_theme = {} self.public_group = {} for interface in self.interfaces: self.public_theme[interface.id] = [] self.public_group[interface.id] = [] for theme in self.session.query(Theme).filter_by(public=True).all(): self._add_theme(theme) for role in self.session.query(Role).all(): for theme in self.session.query(Theme).all(): self._add_theme(theme, role) transaction.commit() def _add_fts(self, item, interface, action, role): from c2cgeoportal.models import FullTextSearch key = (item.name if self.options.name else item.id, interface.id, role.id if role is not None else None) if key not in self.imported: self.imported.add(key) for lang in self.languages: fts = FullTextSearch() fts.label = self._[lang].gettext(item.name) fts.role = role fts.interface = interface fts.lang = lang fts.public = role is None fts.ts = func.to_tsvector(self.fts_languages[lang], fts.label) fts.actions = [{ "action": action, "data": item.name, }] fts.from_theme = True self.session.add(fts) def _add_theme(self, theme, role=None): fill = False for interface in self.interfaces: if interface in theme.interfaces: for child in theme.children: fill = self._add_block(child, interface, role) or fill if fill and self.options.themes: if role is None: self.public_theme[interface.id].append(theme.id) if role is None or theme.id not in self.public_theme[ interface.id]: self._add_fts(theme, interface, "add_theme", role) def _add_block(self, group, interface, role): return self._add_group(group, interface, self.options.blocks, role) def _add_folder(self, group, interface, role): return self._add_group(group, interface, self.options.folders, role) def _add_group(self, group, interface, export, role): from c2cgeoportal.models import LayerGroup fill = False for child in group.children: if isinstance(child, LayerGroup): fill = self._add_folder(child, interface, role) or fill else: fill = self._add_layer(child, interface, role) or fill if fill and export: if role is None: self.public_group[interface.id].append(group.id) if role is None or group.id not in self.public_group[interface.id]: self._add_fts(group, interface, "add_group", role) return fill def _layer_visible(self, layer, role): for restrictionarea in layer.restrictionareas: if role in restrictionarea.roles: return True return False def _add_layer(self, layer, interface, role): from c2cgeoportal.models import LayerV1 if isinstance(layer, LayerV1): return False if role is None: fill = layer.public and interface in layer.interfaces else: fill = interface in layer.interfaces and not layer.public and \ self._layer_visible(layer, role) if fill and self.options.layers: self._add_fts(layer, interface, "add_layer", role) return fill