Exemple #1
0
def test_data(dbsession):
    from c2cgeoportal_commons.models.main import (
        LayerWMS,
        OGCServer,
        OGCSERVER_TYPE_QGISSERVER,
        OGCSERVER_AUTH_STANDARD,
        RestrictionArea,
        Role,
    )
    from c2cgeoportal_commons.models.static import User

    ogc_server1 = OGCServer(
        name="qgisserver1",
        type_=OGCSERVER_TYPE_QGISSERVER,
        image_type="image/png",
        auth=OGCSERVER_AUTH_STANDARD,
    )
    ogc_server2 = OGCServer(
        name="qgisserver2",
        type_=OGCSERVER_TYPE_QGISSERVER,
        image_type="image/png",
        auth=OGCSERVER_AUTH_STANDARD,
    )
    ogc_servers = {
        ogc_server.name: ogc_server
        for ogc_server in (ogc_server1, ogc_server2)
    }
    dbsession.add_all(ogc_servers.values())

    role1 = Role("role1")
    role2 = Role("role2")
    roles = {role.name: role for role in (role1, role2)}
    dbsession.add_all(roles.values())

    root = User("root")
    root.id = 0
    user1 = User("user1", roles=[role1])
    user2 = User("user12", roles=[role1, role2])
    users = {user.username: user for user in (root, user1, user2)}
    dbsession.add_all(users.values())

    project = QgsProject.instance()

    def add_node(parent_node, node_def):

        if node_def["type"] == "layer":
            vlayer = QgsVectorLayer("Point", node_def["name"], "memory")
            if "shortName" in node_def:
                vlayer.setShortName(node_def["shortName"])
            project.addMapLayer(vlayer)
            node = project.layerTreeRoot().findLayer(vlayer)
            clone = node.clone()
            parent_node.addChildNode(clone)
            node.parent().takeChild(node)

        if node_def["type"] == "group":
            node = parent_node.addGroup(node_def["name"])
            if "shortName" in node_def:
                node.setCustomProperty("wmsShortName", node_def["shortName"])
            for child_def in node_def["children"]:
                add_node(node, child_def)

    for node in [{
            "name":
            "root",
            "type":
            "group",
            "children": [
                {
                    "name": "public_group",
                    "type": "group",
                    "children": [{
                        "name": "public_layer",
                        "type": "layer"
                    }],
                },
                {
                    "name": "private_group1",
                    "type": "group",
                    "children": [{
                        "name": "private_layer1",
                        "type": "layer"
                    }],
                },
                {
                    "name": "private_group2",
                    "type": "group",
                    "children": [{
                        "name": "private_layer2",
                        "type": "layer"
                    }],
                },
                # For group and layer short names
                {
                    "name":
                    "private_group3",
                    "type":
                    "group",
                    "shortName":
                    "pg3",
                    "children": [{
                        "name": "private_layer3",
                        "type": "layer",
                        "shortName": "pl3"
                    }],
                },
            ],
    }]:
        add_node(project.layerTreeRoot(), node)

    public_group = LayerWMS(name="public_group",
                            layer="public_group",
                            public=True)
    public_group.ogc_server = ogc_server1

    public_layer = LayerWMS(name="public_layer",
                            layer="public_layer",
                            public=True)
    public_layer.ogc_server = ogc_server1

    private_layer1 = LayerWMS(name="private_layer1",
                              layer="private_layer1",
                              public=False)
    private_layer1.ogc_server = ogc_server1

    private_layer2 = LayerWMS(name="private_layer2",
                              layer="private_layer2",
                              public=False)
    private_layer2.ogc_server = ogc_server1

    private_group3 = LayerWMS(name="private_group3", layer="pg3", public=False)
    private_group3.ogc_server = ogc_server1

    private_layer3 = LayerWMS(name="private_layer3", layer="pl3", public=False)
    private_layer3.ogc_server = ogc_server1

    layers = {
        layer.name: layer
        for layer in (
            public_group,
            public_layer,
            private_layer1,
            private_layer2,
            private_group3,
            private_layer3,
        )
    }
    dbsession.add_all(layers.values())

    ra1 = RestrictionArea(
        "restriction_area1",
        layers=[private_layer1, private_layer3],
        roles=[role1],
        area=from_shape(area1, srid=21781),
    )
    ra2 = RestrictionArea("restriction_area2",
                          layers=[private_layer2],
                          roles=[role2],
                          readwrite=True)
    restriction_areas = {ra.name: ra for ra in (ra1, ra2)}
    dbsession.add_all(restriction_areas.values())

    t = dbsession.begin_nested()

    dbsession.flush()

    yield {
        "ogc_servers": ogc_servers,
        "roles": roles,
        "users": users,
        "layers": layers,
        "restriction_areas": restriction_areas,
    }

    t.rollback()
def test_data(dbsession):
    from c2cgeoportal_commons.models.main import (
        LayerWMS,
        OGCServer,
        OGCSERVER_TYPE_QGISSERVER,
        OGCSERVER_AUTH_STANDARD,
        RestrictionArea,
        Role,
    )
    from c2cgeoportal_commons.models.static import User

    ogc_server1 = OGCServer(
        name='qgisserver1',
        type_=OGCSERVER_TYPE_QGISSERVER,
        image_type='image/png',
        auth=OGCSERVER_AUTH_STANDARD
    )
    ogc_server2 = OGCServer(
        name='qgisserver2',
        type_=OGCSERVER_TYPE_QGISSERVER,
        image_type='image/png',
        auth=OGCSERVER_AUTH_STANDARD
    )
    ogc_servers = {ogc_server.name: ogc_server for ogc_server in (ogc_server1, ogc_server2)}
    dbsession.add_all(ogc_servers.values())

    role1 = Role('role1')
    role2 = Role('role2')
    roles = {role.name: role for role in (role1, role2)}
    dbsession.add_all(roles.values())

    root = User('root')
    root.id = 0
    user1 = User('user1', roles=[role1])
    user2 = User('user12', roles=[role1, role2])
    users = {user.username: user for user in (root, user1, user2)}
    dbsession.add_all(users.values())

    project = QgsProject.instance()

    def add_node(parent_node, node_def):

        if node_def['type'] == 'layer':
            vlayer = QgsVectorLayer("Point", node_def['name'], 'memory')
            if 'shortName' in node_def:
                vlayer.setShortName(node_def['shortName'])
            project.addMapLayer(vlayer)
            node = project.layerTreeRoot().findLayer(vlayer)
            clone = node.clone()
            parent_node.addChildNode(clone)
            node.parent().takeChild(node)

        if node_def['type'] == 'group':
            node = parent_node.addGroup(node_def['name'])
            if 'shortName' in node_def:
                node.setCustomProperty("wmsShortName", node_def['shortName'])
            for child_def in node_def['children']:
                add_node(node, child_def)

    for node in [
        {'name': 'root', 'type': 'group', 'children': [
            {'name': 'public_group', 'type': 'group', 'children': [
                {'name': 'public_layer', 'type': 'layer'},
            ]},
            {'name': 'private_group1', 'type': 'group', 'children': [
                {'name': 'private_layer1', 'type': 'layer'},
            ]},
            {'name': 'private_group2', 'type': 'group', 'children': [
                {'name': 'private_layer2', 'type': 'layer'},
            ]},
            # For group and layer short names
            {'name': 'private_group3', 'type': 'group', 'shortName': 'pg3', 'children': [
                {'name': 'private_layer3', 'type': 'layer', 'shortName': 'pl3'},
            ]},
        ]},
    ]:
        add_node(project.layerTreeRoot(), node)

    public_group = LayerWMS(name='public_group', layer='public_group', public=True)
    public_group.ogc_server = ogc_server1

    public_layer = LayerWMS(name='public_layer', layer='public_layer', public=True)
    public_layer.ogc_server = ogc_server1

    private_layer1 = LayerWMS(name='private_layer1', layer='private_layer1', public=False)
    private_layer1.ogc_server = ogc_server1

    private_layer2 = LayerWMS(name='private_layer2', layer='private_layer2', public=False)
    private_layer2.ogc_server = ogc_server1

    private_group3 = LayerWMS(name='private_group3', layer='pg3', public=False)
    private_group3.ogc_server = ogc_server1

    private_layer3 = LayerWMS(name='private_layer3', layer='pl3', public=False)
    private_layer3.ogc_server = ogc_server1

    layers = {layer.name: layer for layer in (
        public_group, public_layer, private_layer1, private_layer2, private_group3, private_layer3
    )}
    dbsession.add_all(layers.values())

    ra1 = RestrictionArea('restriction_area1',
                          layers=[private_layer1, private_layer3],
                          roles=[role1],
                          area=from_shape(area1, srid=21781))
    ra2 = RestrictionArea('restriction_area2',
                          layers=[private_layer2],
                          roles=[role2],
                          readwrite=True)
    restriction_areas = {ra.name: ra for ra in (ra1, ra2)}
    dbsession.add_all(restriction_areas.values())

    t = dbsession.begin_nested()

    dbsession.flush()

    yield {
        'ogc_servers': ogc_servers,
        'roles': roles,
        'users': users,
        'layers': layers,
        'restriction_areas': restriction_areas,
    }

    t.rollback()
def test_data(clean_dbsession):
    from c2cgeoportal_commons.models.main import (
        OGCSERVER_AUTH_STANDARD,
        OGCSERVER_TYPE_QGISSERVER,
        LayerWMS,
        OGCServer,
        RestrictionArea,
        Role,
    )
    from c2cgeoportal_commons.models.static import User

    DBSession = clean_dbsession  # noqa: ignore=N806

    dbsession = DBSession()

    ogc_server1 = OGCServer(
        name="qgisserver1",
        type_=OGCSERVER_TYPE_QGISSERVER,
        image_type="image/png",
        auth=OGCSERVER_AUTH_STANDARD,
    )
    ogc_server2 = OGCServer(
        name="qgisserver2",
        type_=OGCSERVER_TYPE_QGISSERVER,
        image_type="image/png",
        auth=OGCSERVER_AUTH_STANDARD,
    )
    dbsession.add_all((ogc_server1, ogc_server2))

    role1 = Role("role1")
    role2 = Role("role2")
    dbsession.add_all((role1, role2))

    root = User("root")
    root.id = 0
    user1 = User("user1", roles=[role1])
    user2 = User("user12", roles=[role1, role2])
    dbsession.add_all((root, user1, user2))

    project = QgsProject.instance()

    for node in [{
            "name":
            "root",
            "type":
            "group",
            "children": [
                {
                    "name": "public_group",
                    "type": "group",
                    "children": [{
                        "name": "public_layer",
                        "type": "layer"
                    }],
                },
                {
                    "name": "private_group1",
                    "type": "group",
                    "children": [{
                        "name": "private_layer1",
                        "type": "layer"
                    }],
                },
                {
                    "name": "private_group2",
                    "type": "group",
                    "children": [{
                        "name": "private_layer2",
                        "type": "layer"
                    }],
                },
                # For group and layer short names
                {
                    "name":
                    "private_group3",
                    "type":
                    "group",
                    "shortName":
                    "pg3",
                    "children": [{
                        "name": "private_layer3",
                        "type": "layer",
                        "shortName": "pl3"
                    }],
                },
            ],
    }]:
        add_node_in_qgis_project(project, project.layerTreeRoot(), node)

    public_group = LayerWMS(name="public_group",
                            layer="public_group",
                            public=True)
    public_group.ogc_server = ogc_server1

    public_layer = LayerWMS(name="public_layer",
                            layer="public_layer",
                            public=True)
    public_layer.ogc_server = ogc_server1

    private_layer1 = LayerWMS(name="private_layer1",
                              layer="private_layer1",
                              public=False)
    private_layer1.ogc_server = ogc_server1

    private_layer2 = LayerWMS(name="private_layer2",
                              layer="private_layer2",
                              public=False)
    private_layer2.ogc_server = ogc_server1

    private_group3 = LayerWMS(name="private_group3", layer="pg3", public=False)
    private_group3.ogc_server = ogc_server1

    private_layer3 = LayerWMS(name="private_layer3", layer="pl3", public=False)
    private_layer3.ogc_server = ogc_server1

    dbsession.add_all((
        public_group,
        public_layer,
        private_layer1,
        private_layer2,
        private_group3,
        private_layer3,
    ))

    ra1 = RestrictionArea(
        "restriction_area1",
        layers=[private_layer1, private_layer3],
        roles=[role1],
        area=from_shape(area1, srid=21781),
    )
    ra2 = RestrictionArea("restriction_area2",
                          layers=[private_layer2],
                          roles=[role2],
                          readwrite=True)
    dbsession.add_all((ra1, ra2))

    dbsession.flush()

    users = {
        user.username: {
            "id": user.id,
            "role_ids": [r.id for r in user.roles]
        }
        for user in (root, user1, user2)
    }
    roles = {role.name: {"id": role.id} for role in (role1, role2)}

    dbsession.commit()
    dbsession.close()

    yield {
        # "ogc_servers": ogc_servers,
        "roles": roles,
        "users": users,
        # "layers": layers,
        # "restriction_areas": restriction_areas,
        "project": project,
    }