Exemple #1
0
    def setUpClass(cls):
        super(ClientApiTest, cls).setUpClass()

        # Fill the cache with getprojectsettings response so we don't need a QGIS instance running
        # TODO: eventually move to QgsServer
        cls.prj_test = Project.objects.get(title='Un progetto')

        # new properties has to save before caching, signal on svaing project invalidate cache['django']

        cls.prj_test.thumbnail = '/fake/project.png'
        cls.prj_test.save()

        # create a group print for follow project
        cls.print_group = Group(name='Print Group',
                                title='Print Group',
                                header_logo_img='',
                                srid=cls.prj_test.group.srid)
        cls.print_group.save()

        qgis_project_file_print = File(
            open('{}/{}'.format(DATASOURCE_PATH, QGS310_FILE), 'r'))
        cls.project_print310 = QgisProject(qgis_project_file_print)
        cls.project_print310.group = cls.print_group
        cls.project_print310.save()

        cache_key = settings.QDJANGO_PRJ_CACHE_KEY.format(cls.prj_test.pk)
        cache = caches['qdjango']
        cache.set(
            cache_key,
            open(
                os.path.join(
                    DATASOURCE_PATH,
                    'getProjectSettings_gruppo-1_un-progetto_qgis310.xml'),
                'rb').read())

        cache_key = settings.QDJANGO_PRJ_CACHE_KEY.format(
            cls.project_print310.instance.pk)
        cache.set(
            cache_key,
            open(
                os.path.join(
                    DATASOURCE_PATH,
                    'getProjectSettings_g3wsuite_project_test_qgis310.xml'),
                'rb').read())

        qgis_project_file_1 = File(
            open('{}/{}'.format(DATASOURCE_PATH, QGS310_FILE_1), 'r'))
        cls.project_print310_1 = QgisProject(qgis_project_file_1)
        cls.project_print310_1.group = cls.print_group
        cls.project_print310_1.save()
    def setUpTestData(cls):

        call_command('loaddata', 'BaseLayer.json',
                     '--database=default', verbosity=0)
        call_command('loaddata', 'G3WMapControls.json',
                     '--database=default', verbosity=0)
        call_command('loaddata', 'G3WSpatialRefSys.json',
                     '--database=default', verbosity=0)
        call_command('loaddata', 'G3WGeneralDataSuite.json',
                     '--database=default', verbosity=0)

        setup_testing_user(cls)

        cls.project_group = CoreGroup(
            name='Group1', title='Group3857', header_logo_img='', srid=G3WSpatialRefSys.objects.get(auth_srid=3857))
        cls.project_group.save()
        qgis_project_file = File(open(QGS_FILE, 'r', encoding='UTF8'))
        cls.project = QgisProject(qgis_project_file)
        cls.project.title = 'Transaction group test project'
        cls.project.group = cls.project_group
        cls.project.save()

        qgis_project_file.close()

        cls.admin01 = User.objects.get(username='******')
Exemple #3
0
    def setUpClass(cls):
        super().setUpClass()

        if cls.fixtures:
            for db_name in cls._databases_names(include_mirrors=False):
                try:
                    call_command('loaddata', *cls.fixtures, **{
                        'verbosity': 0,
                        'database': db_name
                    })
                except Exception:
                    cls._rollback_atomics(cls.cls_atomics)
                    cls._remove_databases_failures()
                    raise

        setup_testing_user(cls)

        # main project group
        cls.project_group = CoreGroup(
            name='Group1',
            title='Group1',
            header_logo_img='',
            srid=G3WSpatialRefSys.objects.get(auth_srid=4326))
        cls.project_group.save()

        qgis_project_file = File(
            open(
                '{}{}{}'.format(CURRENT_PATH, '/caching/tests/data/',
                                QGS_FILE), 'r'))
        cls.project = QgisProject(qgis_project_file)
        cls.project.title = 'A project'
        cls.project.group = cls.project_group
        cls.project.save()
Exemple #4
0
    def setUp(self):

        qgis_project_file = File(open('{}{}{}'.format(CURRENT_PATH, TEST_BASE_PATH, QGS_FILE), 'r', encoding='utf-8'))

        # Replace name property with only file name without path to simulate UploadedFileWithId instance.
        qgis_project_file.name = qgis_project_file.name.split('/')[-1]
        self.project = QgisProject(qgis_project_file)
        qgis_project_file.close()
Exemple #5
0
    def test_datasource_validator(self):
        """ Test layer exception for validator DataSourceNotExists"""

        qgis_filename = 'test_wrong_geodata_gdal_type_path.qgs'
        qgis_file = File(
            open('{}{}{}'.format(CURRENT_PATH, TEST_BASE_PATH, qgis_filename),
                 'r',
                 encoding='utf-8'))

        # DatasourceExists
        project = QgisProject(qgis_file)
        project.group = self.project_group
        with self.assertRaises(QgisProjectLayerException):
            project.clean()

        qgis_file.close()

        # indirect datasource validator into getDataFields for ogr layer
        qgis_filename = 'test_wrong_geodata_org_type_path.qgs'
        qgis_file = File(
            open('{}{}{}'.format(CURRENT_PATH, TEST_BASE_PATH, qgis_filename),
                 'r',
                 encoding='utf-8'))

        # Project is not valid
        with self.assertRaises(Exception) as exc:
            project = QgisProject(qgis_file)
        qgis_file.close()
Exemple #6
0
    def setUpClass(cls):

        super().setUpClass()
        cls.client = APIClient()

        qgis_project_file_widget = File(
            open(
                '{}{}{}'.format(CURRENT_PATH, TEST_BASE_PATH,
                                QGS310_WIDGET_FILE), 'r'))
        cls.project_widget310 = QgisProject(qgis_project_file_widget)
        cls.project_widget310.title = 'A project with widget QGIS 3.10'
        cls.project_widget310.group = cls.project_group
        cls.project_widget310.save()
    def setUpTestData(cls):
        super().setUpTestData()

        cls.project_path = os.path.join(
            CURRENT_PATH + TEST_BASE_PATH, 'multiple_styles_manager_test.qgs')

        Project.objects.filter(
            title='Test qdjango postgres multiple styles manager project').delete()

        project_file = File(open(cls.project_path, 'r'))
        project = QgisProject(project_file)
        project.title = 'Test qdjango postgres multiple styles manager project'
        project.group = cls.project_group
        project.save()

        cls.qdjango_project = project.instance
        cls.qdjango_layer = cls.qdjango_project.layer_set.all()[0]
    def setUp(self):
        """Setup test data"""

        super().setUp()
        self.project_path = os.path.join(
            CURRENT_PATH + TEST_BASE_PATH, 'multiple_styles_manager_test.qgs')

        Project.objects.filter(
            title='Test qdjango postgres multiple styles manager project').delete()

        project_file = File(open(self.project_path, 'r'))
        project = QgisProject(project_file)
        project.title = 'Test qdjango postgres multiple styles manager project'
        project.group = self.project_group
        project.save()

        self.qdjango_project = project.instance
        self.qdjango_layer = self.qdjango_project.layer_set.all()[0]

        self.client = APIClient()
Exemple #9
0
    def setUpTestData(cls):

        call_command('loaddata',
                     'BaseLayer.json',
                     '--database=default',
                     verbosity=0)
        call_command('loaddata',
                     'G3WMapControls.json',
                     '--database=default',
                     verbosity=0)
        call_command('loaddata',
                     'G3WSpatialRefSys.json',
                     '--database=default',
                     verbosity=0)
        call_command('loaddata',
                     'G3WGeneralDataSuite.json',
                     '--database=default',
                     verbosity=0)

        # Make a copy of the test project's databases
        cls.reset_db_data()

        # Admin level 1
        cls.test_user_admin1 = User.objects.create_user(username='******',
                                                        password='******')
        cls.test_user_admin1.is_superuser = True
        cls.test_user_admin1.save()

        # Editor level 1
        cls.test_user1 = User.objects.create_user(username='******',
                                                  password='******')
        cls.group = UserGroup.objects.get(name='Editor Level 1')
        cls.test_user1.groups.add(cls.group)
        cls.test_user1.save()

        # Editor level 2
        cls.test_user2 = User.objects.create_user(username='******',
                                                  password='******')
        cls.group = UserGroup.objects.get(name='Editor Level 2')
        cls.test_user2.groups.add(cls.group)
        cls.test_user2.save()

        cls.test_user3 = User.objects.create_user(username='******',
                                                  password='******')
        cls.group = UserGroup.objects.get(name='Viewer Level 1')
        cls.test_user3.groups.add(cls.group)
        cls.test_user3.save()

        cls.test_user4 = User.objects.create_user(username='******',
                                                  password='******')
        cls.test_user4.groups.add(cls.group)
        cls.test_user3.save()

        cls.project_group = CoreGroup(
            name='Group1',
            title='Group1',
            header_logo_img='',
            srid=G3WSpatialRefSys.objects.get(auth_srid=4326))
        cls.project_group.save()
        cls.project_group.addPermissionsToEditor(cls.test_user2)

        qgis_project_file = File(
            open('{}{}{}'.format(CURRENT_PATH, TEST_BASE_PATH, QGS_FILE),
                 'r',
                 encoding='UTF8'))
        cls.project = QgisProject(qgis_project_file)
        cls.project.title = 'A project'
        cls.project.group = cls.project_group
        cls.project.save()

        # give permission on project and layer
        cls.project.instance.addPermissionsToEditor(cls.test_user2)
        cls.project.instance.addPermissionsToViewers([cls.test_user3.pk])
        cls.editing_layer = cls.project.instance.layer_set.get(
            name='editing_layer')

        setPermissionUserObject(cls.test_user3, cls.editing_layer,
                                ['change_layer'])
        setPermissionUserObject(cls.group, cls.editing_layer, ['change_layer'])

        qgis_project_file.close()

        # load QGIS editing project
        qgis_project_file = File(
            open('{}{}{}'.format(CURRENT_PATH, TEST_BASE_PATH,
                                 QGS_EDITING_FILE),
                 'r',
                 encoding='UTF8'))
        cls.editing_project = QgisProject(qgis_project_file)
        cls.editing_project.group = cls.project_group
        cls.editing_project.save()
        qgis_project_file.close()
Exemple #10
0
    def setUp(self):

        qgis_project_file = File(
            open('{}{}{}'.format(CURRENT_PATH, TEST_BASE_PATH, QGS_FILE), 'r'))
        self.project = QgisProject(qgis_project_file)
        qgis_project_file.close()
Exemple #11
0
    def test_model_methods(self):
        """Test model style manager CRUD methods"""

        self.assertEqual(self.qdjango_layer.styles, [
            {
                'name': 'style1',
                'current': False
            },
            {
                'name': 'style2',
                'current': True
            },
        ])

        self.assertFalse(self.qdjango_project.is_dirty)
        self.assertFalse(
            self.qdjango_layer.set_current_style('style1234567890'))
        self.assertTrue(self.qdjango_layer.set_current_style('style1'))
        self.assertTrue(self.qdjango_project.is_dirty)
        self.assertEqual(self.qdjango_layer.styles, [
            {
                'name': 'style1',
                'current': True
            },
            {
                'name': 'style2',
                'current': False
            },
        ])

        # Verify the project was written
        p = QgsProject()
        p.read(self.qdjango_project.qgis_project.fileName())
        l = p.mapLayer(self.qdjango_layer.qgis_layer.id())
        self.assertTrue(l.isValid())
        sm = l.styleManager()
        self.assertEqual(sm.currentStyle(), 'style1')
        del(sm)
        del(p)

        # Test rename
        self.assertFalse(self.qdjango_layer.rename_style(
            'style1234567890', 'new_name'))
        self.assertFalse(self.qdjango_layer.rename_style('style2', 'style1'))
        self.assertTrue(self.qdjango_layer.rename_style('style2', 'new_name'))
        self.assertTrue(self.qdjango_layer.rename_style('style1', 'new_name1'))
        self.assertTrue(self.qdjango_layer.rename_style('new_name1', 'style1'))

        # Verify the project was written
        p = QgsProject()
        p.read(self.qdjango_project.qgis_project.fileName())
        l = p.mapLayer(self.qdjango_layer.qgis_layer.id())
        self.assertTrue(l.isValid())
        sm = l.styleManager()
        self.assertEqual(sm.styles(), ['new_name', 'style1'])
        del(sm)
        del(p)

        # Test remove/delete
        self.assertFalse(self.qdjango_layer.delete_style('style1234567890'))
        self.assertTrue(self.qdjango_layer.delete_style('style1'))
        self.assertFalse(self.qdjango_layer.delete_style('new_name'))
        assert self.qdjango_layer.rename_style('new_name', 'style1')

        # Verify the project was written
        p = QgsProject()
        p.read(self.qdjango_project.qgis_project.fileName())
        l = p.mapLayer(self.qdjango_layer.qgis_layer.id())
        self.assertTrue(l.isValid())
        sm = l.styleManager()
        self.assertEqual(sm.styles(), ['style1'])
        del(sm)
        del(p)

        # Test add new style
        with open(os.path.join(
                CURRENT_PATH + TEST_BASE_PATH, 'multiple_styles_manager_test.qml'), 'r') as f:
            qml = f.read()

        self.assertFalse(self.qdjango_layer.add_style('style1', qml))
        self.assertTrue(self.qdjango_layer.add_style(
            'My new fancy èé style', qml))

        # Verify the project was written
        p = QgsProject()
        p.read(self.qdjango_project.qgis_project.fileName())
        l = p.mapLayer(self.qdjango_layer.qgis_layer.id())
        self.assertTrue(l.isValid())
        sm = l.styleManager()
        self.assertEqual(sm.styles(), ['My new fancy èé style', 'style1'])
        del(sm)
        del(p)

        # Test invalid QML
        self.assertFalse(self.qdjango_layer.add_style(
            'My invalid style', '<xxxx>this is not a valid style</xxxx>'))

        # Restore the project and check the dirt flag
        project_file = File(open(self.project_path, 'r'))
        project = QgisProject(project_file)
        project.instance = self.qdjango_project
        project.title = 'Test qdjango postgres multiple styles manager project'
        project.group = self.project_group
        project.save()
        self.assertFalse(self.qdjango_project.is_dirty)
Exemple #12
0
    def setUpTestData(cls):

        # main project group
        cls.project_group = CoreGroup(name='Group1', title='Group1', header_logo_img='',
                                      srid=G3WSpatialRefSys.objects.get(auth_srid=4326))

        cls.project_group.save()

        qgis_project_file = File(open('{}{}{}'.format(CURRENT_PATH, TEST_BASE_PATH, QGS_FILE), 'r'))
        cls.project = QgisProject(qgis_project_file)
        cls.project.title = 'A project'
        cls.project.group = cls.project_group
        cls.project.save()

        # make a fake vector postgis layer
        # ===================================
        fake_layer1, created = Layer.objects.get_or_create(
            name='fakelayer',
            title='fakelayer',
            origname='fakelayer',
            qgs_layer_id='fakelayer_23456',
            project=cls.project.instance,
            layer_type='postgres',
            datasource="dbname='geo_demo' host=localhost port=5432 user='******' password='******' sslmode=disable "
                       "key='id' srid=3003 type=Polygon checkPrimaryKeyUnicity='1' "
                       "table=\"casentino\".\"piano_parco\" (geom) sql="

        )

        fake_layer2, created = Layer.objects.get_or_create(
            name='fakelayer2',
            title='fakelayer2',
            origname='fakelayer2',
            qgs_layer_id='fakelayer2_23456',
            project=cls.project.instance,
            layer_type='postgres',
            datasource="dbname='geo_demo' host=localhost port=5432 user='******' password='******' sslmode=disable "
                       "key='id' srid=3003 type=Polygon checkPrimaryKeyUnicity='1' "
                       "table=\"casentino\".\"piano_parco\" (geom) sql="

        )


        # change datasource
        fake_layer3, created = Layer.objects.get_or_create(
            name='fakelayer3',
            title='fakelayer3',
            origname='fakelayer3',
            qgs_layer_id='fakelayer3_23456',
            project=cls.project.instance,
            layer_type='postgres',
            datasource="dbname='geo_demo' host=localhost port=5432 user='******' password='******' sslmode=disable "
                       "key='id' srid=3003 type=Polygon checkPrimaryKeyUnicity='1' "
                       "table=\"casentino\".\"piano_parco\" (geom) sql="

        )

        layers = cls.project.instance.layer_set.all()
        for l in layers:
            if l.qgs_layer_id == 'fakelayer_23456':
                cls.fake_layer1 = l
            if l.qgs_layer_id == 'fakelayer2_23456':
                cls.fake_layer2 = l
            if l.qgs_layer_id == 'fakelayer3_23456':
                cls.fake_layer3 = l


        qgis_project_file = File(open('{}{}{}'.format(CURRENT_PATH, TEST_BASE_PATH, QGS310_FILE), 'r'))
        cls.project310 = QgisProject(qgis_project_file)
        cls.project310.title = 'A project QGIS 3.10'
        cls.project310.group = cls.project_group
        cls.project310.save()
Exemple #13
0
    def setUpClass(cls):
        super(ClientApiTest, cls).setUpClass()

        # Fill the cache with getprojectsettings response so we don't need a QGIS instance running
        # TODO: eventually move to QgsServer
        cls.prj_test = Project.objects.get(title='Un progetto')

        # new properties has to save before caching, signal on svaing project invalidate cache['django']

        cls.prj_test.thumbnail = '/fake/project.png'
        cls.prj_test.save()

        # create a group print for follow project
        cls.print_group = Group(name='Print Group',
                                title='Print Group',
                                header_logo_img='',
                                srid=cls.prj_test.group.srid)
        cls.print_group.save()

        qgis_project_file_print = File(
            open('{}/{}'.format(PROJECTS_PATH, QGS310_FILE), 'r'))
        cls.project_print310 = QgisProject(qgis_project_file_print)
        cls.project_print310.group = cls.print_group
        cls.project_print310.save()

        cache_key = settings.QDJANGO_PRJ_CACHE_KEY.format(cls.prj_test.pk)
        cache = caches['qdjango']
        cache.set(
            cache_key,
            open(
                os.path.join(
                    PROJECTS_PATH,
                    'getProjectSettings_gruppo-1_un-progetto_qgis310.xml'),
                'rb').read())

        cache_key = settings.QDJANGO_PRJ_CACHE_KEY.format(
            cls.project_print310.instance.pk)
        cache.set(
            cache_key,
            open(
                os.path.join(
                    PROJECTS_PATH,
                    'getProjectSettings_g3wsuite_project_test_qgis310.xml'),
                'rb').read())

        qgis_project_file_1 = File(
            open('{}/{}'.format(PROJECTS_PATH, QGS310_FILE_1), 'r'))
        cls.project_print310_1 = QgisProject(qgis_project_file_1)
        cls.project_print310_1.group = cls.print_group
        cls.project_print310_1.save()
        qgis_project_file_1.close()

        cls.extent_group = Group(name='Extent Group',
                                 title='Extent Group',
                                 header_logo_img='',
                                 srid=G3WSpatialRefSys.objects.get(srid=32633))
        cls.extent_group.save()
        qgis_project_file_2 = File(
            open('{}/{}'.format(PROJECTS_PATH, QGS310_FILE_2), 'r'))
        cls.project_extent310_2 = QgisProject(qgis_project_file_2)
        cls.project_extent310_2.group = cls.extent_group
        cls.project_extent310_2.save()
        qgis_project_file_2.close()

        cls.custom_order_layer_group = Group(
            name='Custom Order Layer Group',
            title='Custom Order Layer Group',
            header_logo_img='',
            srid=G3WSpatialRefSys.objects.get(srid=4326))
        cls.custom_order_layer_group.save()
        qgis_project_file_3 = File(
            open('{}/{}'.format(PROJECTS_PATH, QGS310_FILE_3), 'r'))
        cls.project_extent316_1 = QgisProject(qgis_project_file_3)
        cls.project_extent316_1.group = cls.custom_order_layer_group
        cls.project_extent316_1.save()
        qgis_project_file_3.close()

        cls.empty_vector_layer_layer_group = Group(
            name='Empty vector layer Layer Group',
            title='Empty vector layer Layer Group',
            header_logo_img='',
            srid=G3WSpatialRefSys.objects.get(srid=4326))
        cls.empty_vector_layer_layer_group.save()
        qgis_project_file_4 = File(
            open('{}/{}'.format(PROJECTS_PATH, QGS310_FILE_4), 'r'))
        cls.project_extent316_2 = QgisProject(qgis_project_file_4)
        cls.project_extent316_2.group = cls.empty_vector_layer_layer_group
        cls.project_extent316_2.save()
        qgis_project_file_4.close()