コード例 #1
0
    def test_do_not_send_questionnaire_when_project_has_data(self):
        project = ProjectFactory.create()
        questionnaire = QuestionnaireFactory.create(
            project=project,
            xls_form=self._get_form('xls-form'))

        SpatialUnitFactory.create(project=project)

        data = {
            'name': 'New name',
            'access': project.access,
            'contacts-TOTAL_FORMS': 1,
            'contacts-INITIAL_FORMS': 0,
            'contacts-0-name': '',
            'contacts-0-email': '',
            'contacts-0-tel': ''
        }

        form = forms.ProjectEditDetails(
            instance=project,
            data=data,
            initial={'questionnaire': questionnaire.xls_form.url})

        assert form.is_valid() is True
        form.save()
        project.refresh_from_db()
        assert project.name == data['name']
        assert project.current_questionnaire == questionnaire.id
コード例 #2
0
    def test_replace_questionnaire_when_project_has_data(self):
        project = ProjectFactory.create()
        questionnaire = QuestionnaireFactory.create(
            project=project,
            xls_form=self._get_form('xls-form'))

        SpatialUnitFactory.create(project=project)

        data = {
            'name': 'New name',
            'questionnaire': self._get_form('xls-form-copy'),
            'access': project.access,
            'contacts-TOTAL_FORMS': 1,
            'contacts-INITIAL_FORMS': 0,
            'contacts-0-name': '',
            'contacts-0-email': '',
            'contacts-0-tel': ''
        }

        form = forms.ProjectEditDetails(
            instance=project,
            data=data,
            initial={'questionnaire': questionnaire.xls_form.url})

        assert form.is_valid() is False
        assert ("Data has already been contributed to this project. To "
                "ensure data integrity, uploading a new questionnaire is "
                "disabled for this project." in
                form.errors.get('questionnaire'))
コード例 #3
0
    def test_has_records(self):
        project = ProjectFactory.create()
        assert project.has_records is False

        project = ProjectFactory.create()
        SpatialUnitFactory.create(project=project)
        assert project.has_records is True
コード例 #4
0
    def test_has_records(self):
        project = ProjectFactory.create()
        assert project.has_records is False

        project = ProjectFactory.create()
        SpatialUnitFactory.create(project=project)
        assert project.has_records is True
コード例 #5
0
    def test_write_features(self):
        ensure_dirs()
        project = ProjectFactory.create(current_questionnaire='123abc')
        exporter = ShapeExporter(project)

        content_type = ContentType.objects.get(app_label='spatial',
                                               model='spatialunit')
        schema = Schema.objects.create(content_type=content_type,
                                       selectors=(
                                           project.organization.id,
                                           project.id,
                                           '123abc',
                                       ))
        attr_type = AttributeType.objects.get(name='text')
        Attribute.objects.create(schema=schema,
                                 name='key',
                                 long_name='Test field',
                                 attr_type=attr_type,
                                 index=0,
                                 required=False,
                                 omit=False)

        su1 = SpatialUnitFactory.create(project=project,
                                        geometry='POINT (1 1)',
                                        attributes={'key': 'value 1'})
        su2 = SpatialUnitFactory.create(project=project,
                                        geometry='POINT (2 2)',
                                        attributes={'key': 'value 2'})

        dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/file4')
        ds = exporter.create_datasource(dst_dir, 'file4')
        layers = exporter.create_shp_layers(ds, 'file4')

        csvfile = os.path.join(dst_dir, 'locations.csv')
        exporter.write_features(layers, csvfile)

        assert len(layers[0]) == 2
        f = layers[0].GetNextFeature()
        while f:
            geom = f.geometry()
            assert geom.ExportToWkt() in ['POINT (1 1)', 'POINT (2 2)']
            assert f.GetFieldAsString('id') in [su1.id, su2.id]

            f = layers[0].GetNextFeature()

        ds.Destroy()

        with open(csvfile) as csvfile:
            csvreader = csv.reader(csvfile)
            for i, row in enumerate(csvreader):
                if i == 0:
                    assert row == ['id', 'type', 'key']
                elif row[0] == su1.id:
                    assert row == [su1.id, su1.type, 'value 1']
                elif row[1] == su2.id:
                    assert row == [su2.id, su2.type, 'value 2']
コード例 #6
0
    def test_create_with_blocked_questionnaire_upload(self):
        data = {'xls_form': self.get_form('xls-form')}
        SpatialUnitFactory.create(project=self.prj)
        response = self.request(method='PUT', user=self.user, post_data=data)

        assert response.status_code == 400
        assert ("Data has already been contributed to this "
                "project. To ensure data integrity, uploading a "
                "new questionnaire is disabled for this project." in
                response.content.get('xls_form'))
コード例 #7
0
    def test_create_with_blocked_questionnaire_upload(self):
        data = {'xls_form': self.get_form('xls-form')}
        SpatialUnitFactory.create(project=self.prj)
        response = self.request(method='PUT', user=self.user, post_data=data)

        assert response.status_code == 400
        assert ("Data has already been contributed to this "
                "project. To ensure data integrity, uploading a "
                "new questionnaire is disabled for this project." in
                response.content.get('xls_form'))
コード例 #8
0
    def setup_models(self):
        self.user = UserFactory.create()
        assign_policies(self.user)

        self.prj = ProjectFactory.create(slug='test-project', access='public')
        self.party = PartyFactory.create(project=self.prj)
        self.spatial_unit = SpatialUnitFactory.create(project=self.prj)
        self.rel = TenureRelationshipFactory.create(
            project=self.prj, party=self.party, spatial_unit=self.spatial_unit)
        self.party2 = PartyFactory.create(project=self.prj)
        self.spatial_unit2 = SpatialUnitFactory.create(project=self.prj)
コード例 #9
0
    def setup_models(self):
        self.user = UserFactory.create()
        assign_policies(self.user)

        self.prj = ProjectFactory.create(slug='test-project', access='public')
        self.party = PartyFactory.create(project=self.prj)
        self.spatial_unit = SpatialUnitFactory.create(project=self.prj)
        self.rel = TenureRelationshipFactory.create(
            project=self.prj, party=self.party, spatial_unit=self.spatial_unit)
        self.party2 = PartyFactory.create(project=self.prj)
        self.spatial_unit2 = SpatialUnitFactory.create(project=self.prj)
コード例 #10
0
 def test_get_xls_download(self):
     ensure_dirs()
     data = {"type": "xls"}
     user = UserFactory.create()
     project = ProjectFactory.create()
     geometry = "SRID=4326;POINT (30 10)"
     SpatialUnitFactory.create(project=project, geometry=geometry)
     form = forms.DownloadForm(project, user, data=data)
     assert form.is_valid() is True
     path, mime = form.get_file()
     assert "{}-{}".format(project.id, user.id) in path
     assert mime == "application/vnd.openxmlformats-officedocument." "spreadsheetml.sheet"
コード例 #11
0
 def test_repr(self):
     project = ProjectFactory.build(slug='prj')
     su1 = SpatialUnitFactory.build(type='PA', id='abc123', project=project)
     su2 = SpatialUnitFactory.build(type='PA', id='def456', project=project)
     relationship = SpatialRelationshipFactory.build(id='abc123',
                                                     project=project,
                                                     su1=su1,
                                                     su2=su2,
                                                     type='C')
     assert repr(relationship) == ('<SpatialRelationship id=abc123'
                                   ' project=prj su1=abc123 su2=def456'
                                   ' type=C>')
コード例 #12
0
    def test_get_shape_download(self):
        ensure_dirs()
        data = {'type': 'shp'}
        user = UserFactory.create()
        project = ProjectFactory.create()
        content_type = ContentType.objects.get(app_label='spatial',
                                               model='spatialunit')
        schema = Schema.objects.create(
            content_type=content_type,
            selectors=(project.organization.id, project.id))
        attr_type = AttributeType.objects.get(name='text')
        Attribute.objects.create(
            schema=schema,
            name='key', long_name='Test field',
            attr_type=attr_type, index=0,
            required=False, omit=False
        )

        su1 = SpatialUnitFactory.create(
            project=project,
            geometry='POINT (1 1)',
            attributes={'key': 'value 1'})
        SpatialUnitFactory.create(
            project=project,
            geometry='SRID=4326;'
                     'MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),'
                     '((15 5, 40 10, 10 20, 5 10, 15 5)))',
            attributes={'key': 'value 2'})
        party = PartyFactory.create(project=project)
        TenureRelationshipFactory.create(
            spatial_unit=su1, party=party, project=project)

        form = forms.DownloadForm(project, user, data=data)
        assert form.is_valid() is True
        path, mime = form.get_file()
        assert '{}-{}'.format(project.id, user.id) in path
        assert (mime == 'application/zip')

        with ZipFile(path, 'r') as testzip:
            assert len(testzip.namelist()) == 12
            assert 'point.dbf' in testzip.namelist()
            assert 'point.prj' in testzip.namelist()
            assert 'point.shp' in testzip.namelist()
            assert 'point.shx' in testzip.namelist()
            assert 'multipolygon.dbf' in testzip.namelist()
            assert 'multipolygon.prj' in testzip.namelist()
            assert 'multipolygon.shp' in testzip.namelist()
            assert 'multipolygon.shx' in testzip.namelist()
            assert 'relationships.csv' in testzip.namelist()
            assert 'parties.csv' in testzip.namelist()
            assert 'locations.csv' in testzip.namelist()
            assert 'README.txt' in testzip.namelist()
コード例 #13
0
 def test_get_xls_download(self):
     ensure_dirs()
     data = {'type': 'xls'}
     user = UserFactory.create()
     project = ProjectFactory.create()
     geometry = 'SRID=4326;POINT (30 10)'
     SpatialUnitFactory.create(project=project, geometry=geometry)
     form = forms.DownloadForm(project, user, data=data)
     assert form.is_valid() is True
     path, mime = form.get_file()
     assert '{}-{}'.format(project.id, user.id) in path
     assert (mime == 'application/vnd.openxmlformats-officedocument.'
                     'spreadsheetml.sheet')
コード例 #14
0
 def test_repr(self):
     project = ProjectFactory.build(slug='prj')
     su1 = SpatialUnitFactory.build(type='PA', id='abc123', project=project)
     su2 = SpatialUnitFactory.build(type='PA', id='def456', project=project)
     relationship = SpatialRelationshipFactory.build(
         id='abc123',
         project=project,
         su1=su1,
         su2=su2,
         type='C')
     assert repr(relationship) == ('<SpatialRelationship id=abc123'
                                   ' project=prj su1=abc123 su2=def456'
                                   ' type=C>')
コード例 #15
0
 def test_omit_attribute(self):
     project = ProjectFactory.create(name='TestProject')
     QuestionnaireFactory.create(project=project)
     content_type = ContentType.objects.get(app_label='spatial',
                                            model='spatialunit')
     create_attrs_schema(project=project,
                         dict=location_xform_group,
                         content_type=content_type,
                         errors=[])
     with pytest.raises(KeyError):
         SpatialUnitFactory.create(project=project,
                                   attributes={
                                       'notes': 'Some textual content',
                                   })
コード例 #16
0
    def setUp(self):
        self.project = ProjectFactory.create()

        self.su1 = SpatialUnitFactory.build(
            project=self.project,
            geometry='POLYGON((12.323006 51.327645,12.322913 '
            '51.327355,12.323114 51.327330,12.323189 '
            '51.327624,12.323006 51.327645))')

        self.su2 = SpatialUnitFactory.build(
            project=self.project,
            geometry='POLYGON((12.323041 51.32775,12.323012 '
            '51.327661,12.323197 51.327638,12.323224 '
            '51.327727,12.323041 51.32775))')
コード例 #17
0
    def test_make_download(self):
        ensure_dirs()
        project = ProjectFactory.create(current_questionnaire='123abc')
        exporter = ShapeExporter(project)

        content_type = ContentType.objects.get(app_label='spatial',
                                               model='spatialunit')
        schema = Schema.objects.create(content_type=content_type,
                                       selectors=(
                                           project.organization.id,
                                           project.id,
                                           '123abc',
                                       ))
        attr_type = AttributeType.objects.get(name='text')
        Attribute.objects.create(schema=schema,
                                 name='key',
                                 long_name='Test field',
                                 attr_type=attr_type,
                                 index=0,
                                 required=False,
                                 omit=False)

        SpatialUnitFactory.create(project=project,
                                  geometry='POINT (1 1)',
                                  attributes={'key': 'value 1'})
        SpatialUnitFactory.create(
            project=project,
            geometry='SRID=4326;'
            'MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),'
            '((15 5, 40 10, 10 20, 5 10, 15 5)))',
            attributes={'key': 'value 2'})

        path, mime = exporter.make_download('file5')

        assert path == os.path.join(settings.MEDIA_ROOT, 'temp/file5.zip')
        assert mime == 'application/zip'

        with ZipFile(path, 'r') as testzip:
            assert len(testzip.namelist()) == 10
            assert 'point.dbf' in testzip.namelist()
            assert 'point.prj' in testzip.namelist()
            assert 'point.shp' in testzip.namelist()
            assert 'point.shx' in testzip.namelist()
            assert 'multipolygon.dbf' in testzip.namelist()
            assert 'multipolygon.prj' in testzip.namelist()
            assert 'multipolygon.shp' in testzip.namelist()
            assert 'multipolygon.shx' in testzip.namelist()
            assert 'locations.csv' in testzip.namelist()
            assert 'README.txt' in testzip.namelist()
コード例 #18
0
    def test_write_items(self):
        project = ProjectFactory.create(current_questionnaire='123abc')

        content_type = ContentType.objects.get(app_label='spatial',
                                               model='spatialunit')
        schema = Schema.objects.create(content_type=content_type,
                                       selectors=(
                                           project.organization.id,
                                           project.id,
                                           '123abc',
                                       ))
        attr_type = AttributeType.objects.get(name='text')
        Attribute.objects.create(schema=schema,
                                 name='key',
                                 long_name='Test field',
                                 attr_type=attr_type,
                                 index=0,
                                 required=False,
                                 omit=False)

        spatialunit_1 = SpatialUnitFactory.create(
            project=project,
            geometry='POINT (1 1)',
            attributes={'key': 'value 1'})
        spatialunit_2 = SpatialUnitFactory.create(
            project=project,
            geometry='POINT (2 2)',
            attributes={'key': 'value 2'})
        spatialunits = [spatialunit_1, spatialunit_2]
        attrs = ['id', 'geometry.ewkt']

        workbook = Workbook()
        worksheet = workbook.create_sheet()
        worksheet.title = 'locations'

        exporter = XLSExporter(project)
        exporter.write_items(worksheet, spatialunits, content_type, attrs)

        assert worksheet['A1'].value == 'id'
        assert worksheet['B1'].value == 'geometry.ewkt'
        assert worksheet['C1'].value == 'key'

        assert worksheet['A2'].value == spatialunit_1.id
        assert worksheet['B2'].value == spatialunit_1.geometry.ewkt
        assert worksheet['C2'].value == 'value 1'

        assert worksheet['A3'].value == spatialunit_2.id
        assert worksheet['B3'].value == spatialunit_2.geometry.ewkt
        assert worksheet['C3'].value == 'value 2'
コード例 #19
0
 def test_omit_attribute(self):
     project = ProjectFactory.create(name='TestProject')
     QuestionnaireFactory.create(project=project)
     content_type = ContentType.objects.get(
         app_label='spatial', model='spatialunit')
     create_attrs_schema(
         project=project, dict=location_xform_group,
         content_type=content_type, errors=[])
     with pytest.raises(KeyError):
         SpatialUnitFactory.create(
             project=project,
             attributes={
                 'notes': 'Some textual content',
             }
         )
コード例 #20
0
 def test_spatial_unit_invalid_attribute(self):
     project = ProjectFactory.create(name='TestProject')
     QuestionnaireFactory.create(project=project)
     content_type = ContentType.objects.get(app_label='spatial',
                                            model='spatialunit')
     create_attrs_schema(project=project,
                         dict=location_xform_group,
                         content_type=content_type,
                         errors=[])
     assert 1 == Schema.objects.all().count()
     with pytest.raises(KeyError):
         SpatialUnitFactory.create(project=project,
                                   attributes={
                                       'invalid_attribute': 'yes',
                                   })
コード例 #21
0
 def test_spatial_unit_attribute_schema(self):
     project = ProjectFactory.create(name='TestProject')
     QuestionnaireFactory.create(project=project)
     content_type = ContentType.objects.get(
         app_label='spatial', model='spatialunit')
     create_attrs_schema(
         project=project, dict=location_xform_group,
         content_type=content_type, errors=[])
     spatial_unit = SpatialUnitFactory.create(
         project=project,
         attributes={
             'quality': 'polygon_high',
             'infrastructure': ['water', 'food']
         }
     )
     assert 1 == Schema.objects.all().count()
     schema = Schema.objects.get(content_type=content_type)
     assert schema is not None
     assert schema.selectors == [
         project.organization.pk, project.pk, project.current_questionnaire]
     assert 'quality' in spatial_unit.attributes.attributes
     assert 'polygon_high' == spatial_unit.attributes['quality']
     assert 'infrastructure' in spatial_unit.attributes.attributes
     assert 'water' in spatial_unit.attributes['infrastructure']
     # notes field is omitted in xform
     assert 'notes' not in spatial_unit.attributes.attributes
コード例 #22
0
    def setup_models(self):
        self.project = ProjectFactory.create()
        self.org_slug = self.project.organization.slug
        self.resource = ResourceFactory.create(project=self.project)
        self.location = SpatialUnitFactory.create(project=self.project)
        self.party = PartyFactory.create(project=self.project)
        self.tenurerel = TenureRelationshipFactory.create(project=self.project)
        self.project_attachment = ContentObject.objects.create(
            resource_id=self.resource.id,
            content_object=self.project,
        )
        self.location_attachment = ContentObject.objects.create(
            resource_id=self.resource.id,
            content_object=self.location,
        )
        self.party_attachment = ContentObject.objects.create(
            resource_id=self.resource.id,
            content_object=self.party,
        )
        self.tenurerel_attachment = ContentObject.objects.create(
            resource_id=self.resource.id,
            content_object=self.tenurerel,
        )

        self.user = UserFactory.create()
        assign_permissions(self.user)
コード例 #23
0
    def setup_models(self):
        self.user = UserFactory.create()
        assign_policies(self.user)

        self.prj = ProjectFactory.create(slug='test-project', access='public')
        self.party1 = PartyFactory.create(project=self.prj, name='Landowner')
        self.su2 = SpatialUnitFactory.create(project=self.prj, type='PA')
コード例 #24
0
    def test_invalid_custom_tenure_type(self):
        project = ProjectFactory.create()

        questionnaire = q_factories.QuestionnaireFactory.create(
            project=project)
        question = q_factories.QuestionFactory.create(
            type='S1',
            name='tenure_type',
            questionnaire=questionnaire)
        q_factories.QuestionOptionFactory.create(
            question=question,
            name='FU',
            label='FU Label')

        su = SpatialUnitFactory.create(project=project)
        party = PartyFactory.create(project=project)
        data = {
            'tenure_type': 'FH',
            'spatial_unit': su.id,
            'party': party.id
        }

        serializer = serializers.TenureRelationshipSerializer(
            data=data,
            context={'project': project})
        assert serializer.is_valid() is False
        assert ("'FH' is not a valid choice for field 'tenure_type'." in
                serializer.errors['tenure_type'])
コード例 #25
0
    def setup_models(self):
        self.user = UserFactory.create()
        assign_policies(self.user)
        self.project = ProjectFactory.create(slug='test-project')

        QuestionnaireFactory.create(project=self.project)
        content_type = ContentType.objects.get(app_label='party',
                                               model='party')
        create_attrs_schema(project=self.project,
                            dict=individual_party_xform_group,
                            content_type=content_type,
                            errors=[])
        create_attrs_schema(project=self.project,
                            dict=default_party_xform_group,
                            content_type=content_type,
                            errors=[])

        content_type = ContentType.objects.get(app_label='party',
                                               model='tenurerelationship')
        create_attrs_schema(project=self.project,
                            dict=tenure_relationship_xform_group,
                            content_type=content_type,
                            errors=[])

        self.su = SpatialUnitFactory.create(project=self.project, type='CB')
        self.party = PartyFactory.create(project=self.project,
                                         type='IN',
                                         attributes={
                                             'gender': 'm',
                                             'homeowner': 'yes',
                                             'dob': '1951-05-05'
                                         })
        self.tenure_rel = TenureRelationshipFactory.create(
            spatial_unit=self.su,
            party=self.party,
            project=self.project,
            attributes={'notes': 'PBS is the best.'})
        self.resource = ResourceFactory.create(project=self.project)

        self.results = get_fake_es_api_results(self.project, self.su,
                                               self.party, self.tenure_rel,
                                               self.resource)
        self.proj_result = self.results['hits']['hits'][0]
        self.su_result = self.results['hits']['hits'][1]
        self.party_result = self.results['hits']['hits'][2]
        self.tenure_rel_result = self.results['hits']['hits'][3]
        self.resource_result = self.results['hits']['hits'][4]

        self.query = 'searching'
        self.query_body = {
            'query': {
                'simple_query_string': {
                    'default_operator': 'and',
                    'query': self.query,
                }
            }
        }
        self.es_endpoint = '{}/project-{}/_search'.format(
            api_url, self.project.id)
        self.es_body = json.dumps(self.query_body, sort_keys=True)
コード例 #26
0
    def test_unknown_attributes(self):
        project = ProjectFactory.create(name='Test Project')
        su = SpatialUnitFactory.create(project=project)
        party = PartyFactory.create(project=project)

        content_type = ContentType.objects.get(
            app_label='party', model='tenurerelationship')
        schema = Schema.objects.create(
            content_type=content_type,
            selectors=(project.organization.id, project.id, ))

        Attribute.objects.create(
            schema=schema,
            name='notes',
            long_name='Notes',
            attr_type=AttributeType.objects.get(name='text'),
            index=0
        )

        data = {
            'spatial_unit': su.id,
            'party': party.id,
            'tenure_type': 'WR',
            'attributes': {
                'notes': 'Blah',
                'age': 'Blah'
            }
        }
        serializer = serializers.TenureRelationshipSerializer(
            data=data,
            context={'project': project}
        )
        assert serializer.is_valid() is False
        assert 'Unknown attribute "age"' in serializer.errors['attributes']
コード例 #27
0
 def test_spatial_unit_invalid_attribute(self):
     project = ProjectFactory.create(name='TestProject')
     QuestionnaireFactory.create(project=project)
     content_type = ContentType.objects.get(
         app_label='spatial', model='spatialunit')
     create_attrs_schema(
         project=project, dict=location_xform_group,
         content_type=content_type, errors=[])
     assert 1 == Schema.objects.all().count()
     with pytest.raises(KeyError):
         SpatialUnitFactory.create(
             project=project,
             attributes={
                 'invalid_attribute': 'yes',
             }
         )
コード例 #28
0
 def test_repr(self):
     project = ProjectFactory.build(slug='prj')
     spatial_unit = SpatialUnitFactory.build(type='PA',
                                             id='abc123',
                                             project=project)
     assert repr(spatial_unit) == ('<SpatialUnit id=abc123 project=prj'
                                   ' type=PA>')
コード例 #29
0
    def test_serialize(self):
        location = SpatialUnitFactory.build(id='abc123')
        serializer = serializers.SpatialUnitGeoJsonSerializer(location)

        assert 'type' in serializer.data['properties']
        assert 'url' in serializer.data['properties']
        assert 'geometry' in serializer.data
コード例 #30
0
 def setup_models(self):
     self.project = ProjectFactory.create(slug='test-project')
     self.su = SpatialUnitFactory.create(project=self.project)
     self.party = PartyFactory.create(project=self.project)
     self.tenure_rel = TenureRelationshipFactory.create(
         spatial_unit=self.su, party=self.party, project=self.project)
     self.resource = ResourceFactory.create(project=self.project)
コード例 #31
0
    def setup_models(self):
        self.user = UserFactory.create()
        assign_policies(self.user)

        self.prj = ProjectFactory.create(slug='test-project', access='public')
        self.party1 = PartyFactory.create(project=self.prj, name='Landowner')
        self.su2 = SpatialUnitFactory.create(project=self.prj, type='PA')
コード例 #32
0
 def test_spatial_unit_attribute_schema(self):
     project = ProjectFactory.create(name='TestProject')
     QuestionnaireFactory.create(project=project)
     content_type = ContentType.objects.get(app_label='spatial',
                                            model='spatialunit')
     create_attrs_schema(project=project,
                         question_group_dict=location_xform_group,
                         content_type=content_type,
                         errors=[],
                         attr_type_ids=get_attr_type_ids())
     spatial_unit = SpatialUnitFactory.create(project=project,
                                              attributes={
                                                  'quality':
                                                  'polygon_high',
                                                  'infrastructure':
                                                  ['water', 'food']
                                              })
     assert 1 == Schema.objects.all().count()
     schema = Schema.objects.get(content_type=content_type)
     assert schema is not None
     assert schema.selectors == [
         project.organization.pk, project.pk, project.current_questionnaire
     ]
     assert 'quality' in spatial_unit.attributes.attributes
     assert 'polygon_high' == spatial_unit.attributes['quality']
     assert 'infrastructure' in spatial_unit.attributes.attributes
     assert 'water' in spatial_unit.attributes['infrastructure']
     # notes field is omitted in xform
     assert 'notes' not in spatial_unit.attributes.attributes
コード例 #33
0
 def test_get_absolute_url(self):
     su = SpatialUnitFactory.create()
     assert su.get_absolute_url() == ('/organizations/{org}/projects/{prj}/'
                                      'records/locations/{id}/'.format(
                                          org=su.project.organization.slug,
                                          prj=su.project.slug,
                                          id=su.id))
コード例 #34
0
 def test_get_absolute_url(self):
     su = SpatialUnitFactory.create()
     assert su.get_absolute_url() == (
         '/organizations/{org}/projects/{prj}/'
         'records/locations/{id}/'.format(
             org=su.project.organization.slug,
             prj=su.project.slug,
             id=su.id))
コード例 #35
0
ファイル: fixtures.py プロジェクト: vvianle/cadasta-platform
    def add_huge_project(self):
        project = Project.objects.get(slug='london-2')
        content_type = ContentType.objects.get(app_label='spatial',
                                               model='spatialunit')
        attr_type = AttributeType.objects.get(name="text")
        sch = Schema.objects.create(content_type=content_type,
                                    selectors=(project.organization.pk,
                                               project.pk))
        Attribute.objects.create(schema=sch,
                                 name='name',
                                 long_name='Name',
                                 required=False,
                                 index=1,
                                 attr_type=attr_type)

        spatial_units = []
        for i in range(0, 4000):
            row = i // 63
            col = i - (row * 63)

            north_west = [11.67 + (col * 0.025), 50.88 + (row * 0.025)]
            south_east = [
                11.67 + ((col + 1) * 0.025), 50.88 + ((row + 1) * 0.025)
            ]

            geometry = {
                'type':
                'Polygon',
                'coordinates': [[
                    north_west, [north_west[0], south_east[1]], south_east,
                    [south_east[0], north_west[1]], north_west
                ]]
            }
            name = 'Spatial Unit #{}'.format(i)
            type = random.choice([c[0] for c in TYPE_CHOICES])

            spatial_units.append({
                'geometry': GEOSGeometry(json.dumps(geometry)),
                'type': type,
                'project': project,
                'attributes': {
                    'name': name
                }
            })

        SpatialUnitFactory.create_from_kwargs(spatial_units)
コード例 #36
0
    def test_make_download(self):
        ensure_dirs()
        project = ProjectFactory.create(current_questionnaire='123abc')
        exporter = ShapeExporter(project)

        content_type = ContentType.objects.get(app_label='spatial',
                                               model='spatialunit')
        schema = Schema.objects.create(
            content_type=content_type,
            selectors=(project.organization.id, project.id, '123abc', ))
        attr_type = AttributeType.objects.get(name='text')
        Attribute.objects.create(
            schema=schema,
            name='key', long_name='Test field',
            attr_type=attr_type, index=0,
            required=False, omit=False
        )

        SpatialUnitFactory.create(
            project=project,
            geometry='POINT (1 1)',
            attributes={'key': 'value 1'})
        SpatialUnitFactory.create(
            project=project,
            geometry='SRID=4326;'
                     'MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),'
                     '((15 5, 40 10, 10 20, 5 10, 15 5)))',
            attributes={'key': 'value 2'})

        path, mime = exporter.make_download('file5')

        assert path == os.path.join(settings.MEDIA_ROOT, 'temp/file5.zip')
        assert mime == 'application/zip'

        with ZipFile(path, 'r') as testzip:
            assert len(testzip.namelist()) == 10
            assert 'point.dbf' in testzip.namelist()
            assert 'point.prj' in testzip.namelist()
            assert 'point.shp' in testzip.namelist()
            assert 'point.shx' in testzip.namelist()
            assert 'multipolygon.dbf' in testzip.namelist()
            assert 'multipolygon.prj' in testzip.namelist()
            assert 'multipolygon.shp' in testzip.namelist()
            assert 'multipolygon.shx' in testzip.namelist()
            assert 'locations.csv' in testzip.namelist()
            assert 'README.txt' in testzip.namelist()
コード例 #37
0
 def test_geometry(self):
     spatial_unit = SpatialUnitFactory.create(geometry='SRID=4326;POLYGON(('
                                              '11.36667 47.25000, '
                                              '11.41667 47.25000, '
                                              '11.41667 47.28333, '
                                              '11.36667 47.28333, '
                                              '11.36667 47.25000))')
     assert spatial_unit.geometry is not None
コード例 #38
0
    def setup_models(self):
        self.user = UserFactory.create()
        assign_policies(self.user)
        self.project = ProjectFactory.create(slug='test-project')

        QuestionnaireFactory.create(project=self.project)
        content_type = ContentType.objects.get(
            app_label='party', model='party')
        create_attrs_schema(
            project=self.project, dict=individual_party_xform_group,
            content_type=content_type, errors=[])
        create_attrs_schema(
            project=self.project, dict=default_party_xform_group,
            content_type=content_type, errors=[])

        content_type = ContentType.objects.get(
            app_label='party', model='tenurerelationship')
        create_attrs_schema(
            project=self.project, dict=tenure_relationship_xform_group,
            content_type=content_type, errors=[])

        self.su = SpatialUnitFactory.create(project=self.project, type='CB')
        self.party = PartyFactory.create(
            project=self.project,
            type='IN',
            attributes={
                'gender': 'm',
                'homeowner': 'yes',
                'dob': '1951-05-05'
            })
        self.tenure_rel = TenureRelationshipFactory.create(
            spatial_unit=self.su, party=self.party, project=self.project,
            attributes={'notes': 'PBS is the best.'})
        self.resource = ResourceFactory.create(project=self.project)

        self.results = get_fake_es_api_results(
            self.project, self.su, self.party, self.tenure_rel, self.resource)
        self.proj_result = self.results['hits']['hits'][0]
        self.su_result = self.results['hits']['hits'][1]
        self.party_result = self.results['hits']['hits'][2]
        self.tenure_rel_result = self.results['hits']['hits'][3]
        self.resource_result = self.results['hits']['hits'][4]

        self.query = 'searching'
        self.query_body = {
            'query': {
                'simple_query_string': {
                    'default_operator': 'and',
                    'query': self.query,
                }
            },
            'from': 10,
            'size': 20,
            'sort': {'_score': {'order': 'desc'}},
        }
        self.es_endpoint = '{}/project-{}/_search/'.format(api_url,
                                                           self.project.id)
        self.es_body = json.dumps(self.query_body, sort_keys=True)
コード例 #39
0
 def test_geometry(self):
     spatial_unit = SpatialUnitFactory.create(
         geometry='SRID=4326;POLYGON(('
         '11.36667 47.25000, '
         '11.41667 47.25000, '
         '11.41667 47.28333, '
         '11.36667 47.28333, '
         '11.36667 47.25000))')
     assert spatial_unit.geometry is not None
コード例 #40
0
    def test_valid_tenure_type(self):
        project = ProjectFactory.create()
        su = SpatialUnitFactory.create(project=project)
        party = PartyFactory.create(project=project)
        data = {'tenure_type': 'FH', 'spatial_unit': su.id, 'party': party.id}

        serializer = serializers.TenureRelationshipSerializer(
            data=data, context={'project': project})
        assert serializer.is_valid() is True
コード例 #41
0
    def test_update_spatial_unit_fails(self):
        project = ProjectFactory.create()
        su = SpatialUnitFactory.create(type='BU', project=project)
        spatial_data = {'properties': {'type': ''}}
        serializer = serializers.SpatialUnitSerializer(
            su, data=spatial_data, partial=True, context={'project': project})

        with pytest.raises(ValidationError):
            serializer.is_valid(raise_exception=True)
コード例 #42
0
    def setup_models(self):
        self.user = UserFactory.create()
        assign_policies(self.user)

        self.prj = ProjectFactory.create(slug='test-project', access='public')
        party = PartyFactory.create(project=self.prj, name='Landowner')
        spatial_unit = SpatialUnitFactory.create(project=self.prj, type='PA')
        self.rel = TenureRelationshipFactory.create(
            project=self.prj, party=party, spatial_unit=spatial_unit)
コード例 #43
0
    def test_write_features(self):
        ensure_dirs()
        project = ProjectFactory.create(current_questionnaire="123abc")
        exporter = ShapeExporter(project)

        content_type = ContentType.objects.get(app_label="spatial", model="spatialunit")
        schema = Schema.objects.create(
            content_type=content_type, selectors=(project.organization.id, project.id, "123abc")
        )
        attr_type = AttributeType.objects.get(name="text")
        Attribute.objects.create(
            schema=schema, name="key", long_name="Test field", attr_type=attr_type, index=0, required=False, omit=False
        )

        su1 = SpatialUnitFactory.create(project=project, geometry="POINT (1 1)", attributes={"key": "value 1"})
        su2 = SpatialUnitFactory.create(project=project, geometry="POINT (2 2)", attributes={"key": "value 2"})

        dst_dir = os.path.join(settings.MEDIA_ROOT, "temp/file4")
        ds = exporter.create_datasource(dst_dir, "file4")
        layers = exporter.create_shp_layers(ds, "file4")

        csvfile = os.path.join(dst_dir, "locations.csv")
        exporter.write_features(layers, csvfile)

        assert len(layers[0]) == 2
        f = layers[0].GetNextFeature()
        while f:
            geom = f.geometry()
            assert geom.ExportToWkt() in ["POINT (1 1)", "POINT (2 2)"]
            assert f.GetFieldAsString("id") in [su1.id, su2.id]

            f = layers[0].GetNextFeature()

        ds.Destroy()

        with open(csvfile) as csvfile:
            csvreader = csv.reader(csvfile)
            for i, row in enumerate(csvreader):
                if i == 0:
                    assert row == ["id", "type", "key"]
                elif row[0] == su1.id:
                    assert row == [su1.id, su1.type, "value 1"]
                elif row[1] == su2.id:
                    assert row == [su2.id, su2.type, "value 2"]
コード例 #44
0
 def test_get_values_null_geom(self):
     project = ProjectFactory.create(current_questionnaire='123abc')
     exporter = Exporter(project)
     item = SpatialUnitFactory.create(project=project, geometry=None)
     content_type = ContentType.objects.get(app_label='spatial',
                                            model='spatialunit')
     model_attrs = ('id', 'geometry.wkt')
     schema_attrs = exporter.get_schema_attrs(content_type)
     values = exporter.get_values(item, model_attrs, schema_attrs)
     assert values == {'id': item.id, 'geometry.wkt': None}
コード例 #45
0
 def test_location_type_label_questionnaire_single_lang(self):
     questionnaire = q_factories.QuestionnaireFactory.create()
     question = q_factories.QuestionFactory.create(
         questionnaire=questionnaire, name='location_type', type='S1')
     q_factories.QuestionOptionFactory.create(question=question,
                                              name='HOUSE',
                                              label='Haus')
     su = SpatialUnitFactory.create(project=questionnaire.project,
                                    type='HOUSE')
     assert su.location_type_label == 'Haus'
コード例 #46
0
    def test_get_url(self):
        location = SpatialUnitFactory.build(id='abc123')
        serializer = serializers.SpatialUnitGeoJsonSerializer(location)

        expected_url = ('/organizations/{o}/projects/{p}/records/'
                        'locations/{l}/'.format(
                            o=location.project.organization.slug,
                            p=location.project.slug,
                            l=location.id
                        ))

        assert serializer.get_url(location) == expected_url
コード例 #47
0
 def test_get_values_null_geom(self):
     project = ProjectFactory.create(current_questionnaire='123abc')
     exporter = Exporter(project)
     item = SpatialUnitFactory.create(
         project=project,
         geometry=None)
     content_type = ContentType.objects.get(app_label='spatial',
                                            model='spatialunit')
     model_attrs = ('id', 'geometry.wkt')
     schema_attrs = exporter.get_schema_attrs(content_type)
     values = exporter.get_values(item, model_attrs, schema_attrs)
     assert values == {'id': item.id, 'geometry.wkt': None}
コード例 #48
0
 def test_reassign_extent(self):
     spatial_unit = SpatialUnitFactory.create(
         geometry='SRID=4326;POLYGON(('
         '211.36667 47.25000, '
         '211.41667 47.25000, '
         '211.41667 47.28333, '
         '211.36667 47.28333, '
         '211.36667 47.25000))'
     )
     assert spatial_unit.geometry.boundary.coords == (
         (-148.63333, 47.25), (-148.58333, 47.25), (-148.58333, 47.28333),
         (-148.63333, 47.28333), (-148.63333, 47.25))
コード例 #49
0
    def test_get_all_download(self):
        ensure_dirs()
        data = {'type': 'all'}
        user = UserFactory.create()
        project = ProjectFactory.create()
        geometry = 'SRID=4326;POINT (30 10)'
        SpatialUnitFactory.create(project=project, geometry=geometry)
        res = ResourceFactory.create(project=project)

        form = forms.DownloadForm(project, user, data=data)
        assert form.is_valid() is True
        path, mime = form.get_file()
        assert '{}-{}'.format(project.id, user.id) in path
        assert mime == 'application/zip'

        with ZipFile(path, 'r') as testzip:
            assert len(testzip.namelist()) == 4
            assert res.original_file in testzip.namelist()
            assert 'resources.xlsx' in testzip.namelist()
            assert 'data.xlsx' in testzip.namelist()
            assert 'data-shp.zip' in testzip.namelist()
コード例 #50
0
    def test_get_all_download(self):
        ensure_dirs()
        data = {"type": "all"}
        user = UserFactory.create()
        project = ProjectFactory.create()
        geometry = "SRID=4326;POINT (30 10)"
        SpatialUnitFactory.create(project=project, geometry=geometry)
        res = ResourceFactory.create(project=project)

        form = forms.DownloadForm(project, user, data=data)
        assert form.is_valid() is True
        path, mime = form.get_file()
        assert "{}-{}".format(project.id, user.id) in path
        assert mime == "application/zip"

        with ZipFile(path, "r") as testzip:
            assert len(testzip.namelist()) == 4
            assert res.original_file in testzip.namelist()
            assert "resources.xlsx" in testzip.namelist()
            assert "data.xlsx" in testzip.namelist()
            assert "data-shp.zip" in testzip.namelist()
コード例 #51
0
 def test_repr(self):
     project = ProjectFactory.build(slug='prj')
     party = PartyFactory.build(id='abc123', project=project)
     su = SpatialUnitFactory.build(id='def456', project=project)
     relationship = TenureRelationshipFactory.build(
         id='abc123',
         project=project,
         party=party,
         spatial_unit=su,
         tenure_type=TenureRelationshipType(id='CR'))
     assert repr(relationship) == ('<TenureRelationship id=abc123'
                                   ' party=abc123 spatial_unit=def456'
                                   ' project=prj tenure_type=CR>')
コード例 #52
0
    def test_make_download(self):
        ensure_dirs()
        project = ProjectFactory.create(current_questionnaire="123abc")
        exporter = ShapeExporter(project)

        content_type = ContentType.objects.get(app_label="spatial", model="spatialunit")
        schema = Schema.objects.create(
            content_type=content_type, selectors=(project.organization.id, project.id, "123abc")
        )
        attr_type = AttributeType.objects.get(name="text")
        Attribute.objects.create(
            schema=schema, name="key", long_name="Test field", attr_type=attr_type, index=0, required=False, omit=False
        )

        SpatialUnitFactory.create(project=project, geometry="POINT (1 1)", attributes={"key": "value 1"})

        path, mime = exporter.make_download("file5")

        assert path == os.path.join(settings.MEDIA_ROOT, "temp/file5.zip")
        assert mime == "application/zip"

        with ZipFile(path, "r") as testzip:
            assert len(testzip.namelist()) == 16
            assert project.slug + "-point.dbf" in testzip.namelist()
            assert project.slug + "-point.prj" in testzip.namelist()
            assert project.slug + "-point.shp" in testzip.namelist()
            assert project.slug + "-point.shx" in testzip.namelist()
            assert project.slug + "-line.dbf" in testzip.namelist()
            assert project.slug + "-line.prj" in testzip.namelist()
            assert project.slug + "-line.shp" in testzip.namelist()
            assert project.slug + "-line.shx" in testzip.namelist()
            assert project.slug + "-polygon.dbf" in testzip.namelist()
            assert project.slug + "-polygon.prj" in testzip.namelist()
            assert project.slug + "-polygon.shp" in testzip.namelist()
            assert project.slug + "-polygon.shx" in testzip.namelist()
            assert "relationships.csv" in testzip.namelist()
            assert "parties.csv" in testzip.namelist()
            assert "locations.csv" in testzip.namelist()
            assert "README.txt" in testzip.namelist()
コード例 #53
0
    def test_detach_spatial_unit_resources(self):
        project = ProjectFactory.create()
        su = SpatialUnitFactory.create(project=project)
        resource = ResourceFactory.create(project=project)
        resource.content_objects.create(
          content_object=su)
        assert ContentObject.objects.filter(
            object_id=su.id,
            resource=resource,).exists()
        assert resource in su.resources

        su.delete()
        assert not ContentObject.objects.filter(
            object_id=su.id, resource=resource).exists()
コード例 #54
0
 def test_empty_geometries(self):
     geoms = (
         "POINT EMPTY",
         # "POLYGON EMPTY",  # Uncomment after Django 1.11 or libgeos 3.6.1
         "LINESTRING EMPTY",
         "MULTIPOINT EMPTY",
         "MULTILINESTRING EMPTY",
         "MULTIPOLYGON EMPTY",
         "GEOMETRYCOLLECTION EMPTY",
     )
     for geom in geoms:
         spatial_unit = SpatialUnitFactory.create(
             geometry=GEOSGeometry(geom))
         assert spatial_unit.geometry.wkt == geom
コード例 #55
0
ファイル: __init__.py プロジェクト: mikael19/cadasta-platform
    def add_huge_project(self, max_num_records=4000):
        project = models.Project.objects.get(slug='london-2')
        content_type = ContentType.objects.get(
            app_label='spatial', model='spatialunit')
        attr_type = AttributeType.objects.get(name="text")
        sch = Schema.objects.create(
            content_type=content_type,
            selectors=(project.organization.pk, project.pk))
        Attribute.objects.create(
            schema=sch, name='name', long_name='Name',
            required=False, index=1, attr_type=attr_type
        )

        spatial_units = []
        choices = [c[0] for c in TYPE_CHOICES]

        with open(os.path.join(os.path.dirname(__file__),
                               "londondata.txt"), "r") as ins:
            for geometry in ins:
                if not geometry.rstrip() or geometry.startswith('#'):
                    continue

                num_records = len(spatial_units)
                if not num_records < max_num_records:
                    break

                name = 'Spatial Unit #{}'.format(num_records)
                type = random.choice(choices)

                spatial_units.append({
                    'geometry': GEOSGeometry(geometry),
                    'type': type,
                    'project': project,
                    'attributes': {'name': name}
                })

        SpatialUnitFactory.create_from_kwargs(spatial_units)