diff --git a/cadasta/core/fixtures.py b/cadasta/core/fixtures.py index ddeb50e7d..8eeaa62ce 100644 --- a/cadasta/core/fixtures.py +++ b/cadasta/core/fixtures.py @@ -11,7 +11,7 @@ from spatial.tests.factories import ( SpatialUnitFactory, SpatialRelationshipFactory ) -from spatial.models import SpatialUnit + from tutelary.models import Policy, Role @@ -293,8 +293,19 @@ def add_test_spatial_units(self): project = Project.objects.get( name__contains='Pekapuran Laut Test Project') + # add attribute schema + content_type = ContentType.objects.get( + app_label='spatial', model='spatialunit') + sch = Schema.objects.create( + content_type=content_type, + selectors=(project.organization.pk, project.pk)) + attr_type = AttributeType.objects.get(name="text") + Attribute.objects.create( + schema=sch, name='name', long_name='Name', + required=False, index=1, attr_type=attr_type + ) + su1 = SpatialUnitFactory( - name='Building Unit (Test)', geometry=GEOSGeometry('{"type": "Polygon",' '"coordinates": [[' '[-245.3920519351959, -3.3337982265513184],' @@ -305,20 +316,10 @@ def add_test_spatial_units(self): '}' ), project=project, - type='BU') - - # add attribute schema - content_type = ContentType.objects.get( - app_label='spatial', model='spatialunit') - sch = Schema.objects.create(content_type=content_type, selectors=()) - attr_type = AttributeType.objects.get(name="text") - Attribute.objects.create( - schema=sch, name='testing', long_name='Testing', - required=False, index=1, attr_type=attr_type - ) + type='BU', + attributes={'name': 'Building Unit (Test)'}) su2 = SpatialUnitFactory( - name='Apartment Unit (Test)', geometry=GEOSGeometry('{"type": "Polygon",' '"coordinates": [[' '[-245.39200901985168, -3.333808937230755],' @@ -330,13 +331,12 @@ def add_test_spatial_units(self): ), project=project, type='AP', - attributes={"testing": "attributes"}) + attributes={"name": "Apartment Unit (Test)"}) SpatialRelationshipFactory( su1=su1, su2=su2, type='C', project=project) su3 = SpatialUnitFactory( - name='Parcel (Test)', geometry=GEOSGeometry('{"type": "Polygon",' '"coordinates": [[' '[-245.39088249206543, -3.333262692430284],' @@ -347,23 +347,23 @@ def add_test_spatial_units(self): ']}' ), project=project, - type='PA') + type='PA', + attributes={ + 'name': 'Parcel (Test)', + }) su4 = SpatialUnitFactory( - name='Point Inside Parcel (Test)', geometry=GEOSGeometry('{"type": "Point",' '"coordinates": [' '-245.39034605026242, -3.333294824485769]}' ), project=project, - type='PA', - attributes={"testing": "attributes"}) + type='PA') SpatialRelationshipFactory( su1=su3, su2=su4, type='C', project=project) SpatialUnitFactory( - name='Line (Test)', geometry=GEOSGeometry('{"type": "LineString",' '"coordinates": [' '[-245.3934037685394, -3.334258785662196],' @@ -374,32 +374,23 @@ def add_test_spatial_units(self): type='RW') SpatialUnitFactory( - name='Uncontained Point (Test)', geometry=GEOSGeometry('{"type": "Point",' '"coordinates": [' '-245.39366126060483, -3.334130257559935]}' ), project=project, - type='MI') + type='MI', + attributes={"name": 'Uncontained Point (Test)'}) SpatialUnitFactory( - name='Kibera Test Spatial Unit (Test)', geometry=GEOSGeometry('{"type": "Point",' '"coordinates": [' '-4.9383544921875,' '7.833452408875349' ']}'), project=Project.objects.get(name='Kibera Test Project'), - type='MI') - - def delete_test_spatial_units(self): - sus = SpatialUnit.objects.filter(name__contains='Test') - for su in sus: - su.delete() - content_type = ContentType.objects.get( - app_label='spatial', model='spatialunit' - ) - Schema.objects.filter(content_type=content_type, selectors=()).delete() + type='MI', + attributes={}) def delete_test_organizations(self): orgs = Organization.objects.filter(name__contains='Test') diff --git a/cadasta/core/management/commands/loadfixtures.py b/cadasta/core/management/commands/loadfixtures.py index 14c658aac..c9671c6c2 100644 --- a/cadasta/core/management/commands/loadfixtures.py +++ b/cadasta/core/management/commands/loadfixtures.py @@ -21,7 +21,6 @@ def handle(self, *args, **options): data = FixturesData() if options['delete']: - data.delete_test_spatial_units() data.delete_test_projects() data.delete_test_users() data.delete_test_organizations() diff --git a/cadasta/core/tests/test_management_commands.py b/cadasta/core/tests/test_management_commands.py index 635fc4e1d..2c181d39a 100644 --- a/cadasta/core/tests/test_management_commands.py +++ b/cadasta/core/tests/test_management_commands.py @@ -15,7 +15,6 @@ class FixturesTest(TestCase): def test_fixture_setup(self): data = FixturesData() - data.delete_test_spatial_units() data.delete_test_users() data.delete_test_organizations() data.delete_test_projects() @@ -34,7 +33,6 @@ def test_fixture_setup(self): assert SpatialUnit.objects.count() == 7 assert SpatialRelationship.objects.count() == 2 - data.delete_test_spatial_units() data.delete_test_users() data.delete_test_organizations() data.delete_test_projects() diff --git a/cadasta/party/models.py b/cadasta/party/models.py index a6e9468a1..9b2b27407 100644 --- a/cadasta/party/models.py +++ b/cadasta/party/models.py @@ -269,7 +269,7 @@ class TutelaryMeta: def __str__(self): return " {type} <{su}>>".format( - party=self.party.name, su=self.spatial_unit.name, + party=self.party.name, su=self.spatial_unit.get_type_display(), type=self.tenure_type.label) def __repr__(self): diff --git a/cadasta/party/tests/test_models.py b/cadasta/party/tests/test_models.py index f49e28357..d21638a7f 100644 --- a/cadasta/party/tests/test_models.py +++ b/cadasta/party/tests/test_models.py @@ -132,7 +132,7 @@ def test_str(self): party__project=project, party__name='Family', spatial_unit__project=project, - spatial_unit__name='Parcel', + spatial_unit__type='PA', tenure_type=tenure_type) assert str(relationship) == ( " Leasehold >") diff --git a/cadasta/party/tests/test_views_api_relationship_list.py b/cadasta/party/tests/test_views_api_relationship_list.py index 6cd96d936..db166b9ec 100644 --- a/cadasta/party/tests/test_views_api_relationship_list.py +++ b/cadasta/party/tests/test_views_api_relationship_list.py @@ -25,9 +25,9 @@ def _test_objs(self, access='public'): org = OrganizationFactory.create(slug='namati') prj = ProjectFactory.create( slug='test-project', organization=org, access=access) - su1 = SpatialUnitFactory.create(project=prj, name='Parcel') - su2 = SpatialUnitFactory.create(project=prj, name='House') - su3 = SpatialUnitFactory.create(project=prj, name='Village') + su1 = SpatialUnitFactory.create(project=prj) + su2 = SpatialUnitFactory.create(project=prj) + su3 = SpatialUnitFactory.create(project=prj) self.su1 = su1 self.su2 = su2 self.su3 = su3 diff --git a/cadasta/party/tests/test_views_api_tenure_relationships.py b/cadasta/party/tests/test_views_api_tenure_relationships.py index af8dbc4bb..7bc44013e 100644 --- a/cadasta/party/tests/test_views_api_tenure_relationships.py +++ b/cadasta/party/tests/test_views_api_tenure_relationships.py @@ -34,8 +34,8 @@ def _test_objs(self, access='public'): slug='test-project', organization=org, access=access) party1 = PartyFactory.create(project=prj, name='Landowner') party2 = PartyFactory.create(project=prj, name='Family') - su1 = SpatialUnitFactory.create(project=prj, name='Parcel') - su2 = SpatialUnitFactory.create(project=prj, name='House') + su1 = SpatialUnitFactory.create(project=prj, type='PA') + su2 = SpatialUnitFactory.create(project=prj, type='PA') TR = TenureRelationshipFactory rel1 = TR.create(project=prj, party=party1, spatial_unit=su1) TR.create(project=prj, party=party1, spatial_unit=su2) @@ -100,8 +100,8 @@ def _test_objs(self, access='public'): slug='test-project', organization=org, access=access) party = PartyFactory.create(project=prj, name='Landowner') party2 = PartyFactory.create(project=prj, name='Family') - spatial_unit = SpatialUnitFactory.create(project=prj, name='Parcel') - spatial_unit2 = SpatialUnitFactory.create(project=prj, name='House') + spatial_unit = SpatialUnitFactory.create(project=prj, type='PA') + spatial_unit2 = SpatialUnitFactory.create(project=prj, type='PA') rel = TenureRelationshipFactory.create( project=prj, party=party, spatial_unit=spatial_unit) self.party2 = party2 @@ -132,7 +132,7 @@ def check_for_updated(self, content): def check_for_unchanged(self, content): assert content['party']['name'] == "Landowner" - assert content['spatial_unit']['properties']['name'] == "Parcel" + assert content['spatial_unit']['properties']['type'] == "PA" # Additional tests diff --git a/cadasta/questionnaires/tests/files/standard_cadasta_questionnaire_0.5.xlsx b/cadasta/questionnaires/tests/files/standard_cadasta_questionnaire_0.5.xlsx new file mode 100644 index 000000000..2e85bb2c7 Binary files /dev/null and b/cadasta/questionnaires/tests/files/standard_cadasta_questionnaire_0.5.xlsx differ diff --git a/cadasta/questionnaires/tests/files/test_standard_questionnaire.xlsx b/cadasta/questionnaires/tests/files/test_standard_questionnaire.xlsx index 79486b1bb..abe282aeb 100644 Binary files a/cadasta/questionnaires/tests/files/test_standard_questionnaire.xlsx and b/cadasta/questionnaires/tests/files/test_standard_questionnaire.xlsx differ diff --git a/cadasta/questionnaires/tests/test_attr_schemas.py b/cadasta/questionnaires/tests/test_attr_schemas.py index 20bef7b92..d9748b8b1 100644 --- a/cadasta/questionnaires/tests/test_attr_schemas.py +++ b/cadasta/questionnaires/tests/test_attr_schemas.py @@ -98,7 +98,7 @@ def test_spatial_unit_attribute_schema(self): project=project, dict=location_xform_group, content_type=content_type, errors=[]) spatial_unit = SpatialUnitFactory.create( - name='Test', project=project, + project=project, attributes={ 'quality': 'polygon_high' } @@ -124,7 +124,7 @@ def test_spatial_unit_invalid_attribute(self): assert 1 == Schema.objects.all().count() with pytest.raises(KeyError): SpatialUnitFactory.create( - name='TestLocation', project=project, + project=project, attributes={ 'invalid_attribute': 'yes', } diff --git a/cadasta/spatial/migrations/0001_initial.py b/cadasta/spatial/migrations/0001_initial.py index 05493830c..9c19266f4 100644 --- a/cadasta/spatial/migrations/0001_initial.py +++ b/cadasta/spatial/migrations/0001_initial.py @@ -23,7 +23,6 @@ class Migration(migrations.Migration): name='HistoricalSpatialUnit', fields=[ ('id', models.CharField(db_index=True, max_length=24)), - ('name', models.CharField(max_length=200)), ('geometry', django.contrib.gis.db.models.fields.GeometryField(null=True, srid=4326)), ('type', models.CharField(choices=[('PA', 'Parcel'), ('CB', 'Community boundary'), ('BU', 'Building'), ('AP', 'Apartment'), ('PX', 'Project extent'), ('RW', 'Right-of-way'), ('UC', 'Utility corridor'), ('NP', 'National park boundary'), ('MI', 'Miscellaneous')], default='PA', max_length=2)), ('attributes', jsonattrs.fields.JSONAttributeField(default=jsonattrs.fields.JSONAttributes)), @@ -61,14 +60,13 @@ class Migration(migrations.Migration): name='SpatialUnit', fields=[ ('id', models.CharField(max_length=24, primary_key=True, serialize=False)), - ('name', models.CharField(max_length=200)), ('geometry', django.contrib.gis.db.models.fields.GeometryField(null=True, srid=4326)), ('type', models.CharField(choices=[('PA', 'Parcel'), ('CB', 'Community boundary'), ('BU', 'Building'), ('AP', 'Apartment'), ('PX', 'Project extent'), ('RW', 'Right-of-way'), ('UC', 'Utility corridor'), ('NP', 'National park boundary'), ('MI', 'Miscellaneous')], default='PA', max_length=2)), ('attributes', jsonattrs.fields.JSONAttributeField(default=jsonattrs.fields.JSONAttributes)), ('project', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='spatial_units', to='organization.Project')), ], options={ - 'ordering': ('name',), + 'ordering': ('type',), }, ), migrations.CreateModel( diff --git a/cadasta/spatial/migrations/0002_auto_20160708_2001.py b/cadasta/spatial/migrations/0002_auto_20160712_1513.py similarity index 90% rename from cadasta/spatial/migrations/0002_auto_20160708_2001.py rename to cadasta/spatial/migrations/0002_auto_20160712_1513.py index 08fb0af33..50e7eeace 100644 --- a/cadasta/spatial/migrations/0002_auto_20160708_2001.py +++ b/cadasta/spatial/migrations/0002_auto_20160712_1513.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.9.6 on 2016-07-08 20:01 +# Generated by Django 1.9.6 on 2016-07-12 15:13 from __future__ import unicode_literals from django.db import migrations diff --git a/cadasta/spatial/models.py b/cadasta/spatial/models.py index 532d41235..fd56b654b 100644 --- a/cadasta/spatial/models.py +++ b/cadasta/spatial/models.py @@ -31,10 +31,6 @@ class SpatialUnit(ResourceModelMixin, RandomIDModel): project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='spatial_units') - # All spatial units have a name used to provide - # a human-readable label for it. - name = models.CharField(max_length=200) - # Spatial unit type: used to manage range of allowed attributes. type = models.CharField(max_length=2, choices=TYPE_CHOICES, default='PA') @@ -59,7 +55,7 @@ class SpatialUnit(ResourceModelMixin, RandomIDModel): history = HistoricalRecords() class Meta: - ordering = ('name',) + ordering = ('type',) class TutelaryMeta: perm_type = 'spatial' @@ -88,7 +84,7 @@ class TutelaryMeta: ) def __str__(self): - return "".format(self.name) + return "".format(self.get_type_display()) def __repr__(self): return str(self) @@ -223,7 +219,7 @@ class TutelaryMeta: def __str__(self): return " {type} <{su2}>>".format( - su1=self.su1.name, su2=self.su2.name, + su1=self.su1.get_type_display(), su2=self.su2.get_type_display(), type=dict(self.TYPE_CHOICES).get(self.type)) def __repr__(self): diff --git a/cadasta/spatial/serializers.py b/cadasta/spatial/serializers.py index cb83f5544..efc85941d 100644 --- a/cadasta/spatial/serializers.py +++ b/cadasta/spatial/serializers.py @@ -31,7 +31,7 @@ class Meta: context_key = 'project' geo_field = 'geometry' id_field = False - fields = ('id', 'name', + fields = ('id', 'geometry', 'type', 'attributes', 'relationships', 'project',) read_only_fields = ('id', 'project',) @@ -66,8 +66,8 @@ def get_type(self, location): class SpatialRelationshipReadSerializer(serializers.ModelSerializer): - su1 = SpatialUnitSerializer(fields=('id', 'name', 'geometry', 'type')) - su2 = SpatialUnitSerializer(fields=('id', 'name', 'geometry', 'type')) + su1 = SpatialUnitSerializer(fields=('id', 'geometry', 'type')) + su2 = SpatialUnitSerializer(fields=('id', 'geometry', 'type')) rel_class = serializers.SerializerMethodField() class Meta: diff --git a/cadasta/spatial/tests/factories.py b/cadasta/spatial/tests/factories.py index 1dba64dac..db5b5bf09 100644 --- a/cadasta/spatial/tests/factories.py +++ b/cadasta/spatial/tests/factories.py @@ -9,7 +9,6 @@ class SpatialUnitFactory(ExtendedFactory): class Meta: model = SpatialUnit - name = factory.Sequence(lambda n: "Location #{}".format(n)) project = factory.SubFactory(ProjectFactory) diff --git a/cadasta/spatial/tests/test_models.py b/cadasta/spatial/tests/test_models.py index 7b8ccfabd..1a31b04e7 100644 --- a/cadasta/spatial/tests/test_models.py +++ b/cadasta/spatial/tests/test_models.py @@ -12,9 +12,9 @@ class SpatialUnitTest(UserTestCase): def test_str(self): - spatial_unit = SpatialUnitFactory.create(name='Disneyland') - assert str(spatial_unit) == '' - assert repr(spatial_unit) == '' + spatial_unit = SpatialUnitFactory.create(type='PA') + assert str(spatial_unit) == '' + assert repr(spatial_unit) == '' def test_has_random_id(self): spatial_unit = SpatialUnitFactory.create() @@ -89,28 +89,28 @@ def test_str(self): relationship = SpatialRelationshipFactory( project=self.project, su1__project=self.project, - su1__name='Los Angeles', + su1__type='PA', su2__project=self.project, - su2__name='California', + su2__type='CB', type='C') assert str(relationship) == ( " is-contained-in >" + " is-contained-in >" ) assert repr(relationship) == ( " is-contained-in >" + " is-contained-in >" ) def test_relationships_creation(self): relationship = SpatialRelationshipFactory( project=self.project, su1__project=self.project, - su1__name='Disneyworld', + su1__type='PA', su2__project=self.project, - su2__name='Disneyland') - su2_name = str(relationship.su1.relationships.all()[0]) - assert su2_name == '' + su2__type='CB') + su2_type = str(relationship.su1.relationships.all()[0]) + assert su2_type == '' def test_relationship_type(self): relationship = SpatialRelationshipFactory(type='S') @@ -129,16 +129,16 @@ def test_adding_attributes(self): required=False, index=1, attr_type=attr_type ) relationship = SpatialRelationshipFactory( - su1__name='Disneyworld', - su2__name='Disneyland', + su1__type='BU', + su2__type='AP', attributes={'test': 'Partner amusement parks.'}) assert relationship.attributes['test'] == 'Partner amusement parks.' def test_traversing_contained_spatial_unit(self): relationship = SpatialRelationshipFactory( - su1__name='Building', - su2__name='Apartment', + su1__type='BU', + su2__type='AP', type='C') su1_contains = str(relationship.su1.relationships.all()[0]) su2_is_contained_in = str(relationship.su2.relationships_set.all()[0]) @@ -149,31 +149,31 @@ def test_traversing_contained_spatial_unit(self): def test_traversing_split_spatial_unit(self): relationship = SpatialRelationshipFactory( - su1__name='Parent Property', - su2__name='Inheritance', + su1__type='BU', + su2__type='AP', type='S') su1_split_into = str(relationship.su1.relationships.all()[0]) su2_split_from = str(relationship.su2.relationships_set.all()[0]) assert relationship.get_type_display() == 'is-split-of' - assert su1_split_into == '' - assert su2_split_from == '' + assert su1_split_into == '' + assert su2_split_from == '' def test_traversing_merged_spatial_unit(self): relationship = SpatialRelationshipFactory( - su1__name='Married Property', - su2__name='Individual Property', + su1__type='BU', + su2__type='AP', type='M') su1_merged_from = str(relationship.su1.relationships.all()[0]) su2_merged_into = str(relationship.su2.relationships_set.all()[0]) assert relationship.get_type_display() == 'is-merge-of' - assert su1_merged_from == '' - assert su2_merged_into == '' + assert su1_merged_from == '' + assert su2_merged_into == '' def test_spatial_unit_contains_anothers_geometry(self): relationship = SpatialRelationshipFactory( - su1__name='Building', + su1__type='BU', su1__geometry='SRID=4326;POLYGON((' '-91.9947 34.7994, ' '-91.9950 34.7846, ' @@ -182,7 +182,7 @@ def test_spatial_unit_contains_anothers_geometry(self): '-91.9174 34.7627, ' '-91.9153 34.8032, ' '-91.9947 34.7994))', - su2__name='Apartment', + su2__type='AP', su2__geometry='SRID=4326;POLYGON((' '-91.9320 34.7918, ' '-91.9335 34.7846, ' @@ -195,7 +195,7 @@ def test_spatial_unit_contains_anothers_geometry(self): def test_relationship_fails_if_contained_unit_expands_outside_parent(self): with pytest.raises(Exception): SpatialRelationshipFactory( - su1__name='Building', + su1__type='BU', su2__geometry='SRID=4326;POLYGON((' '-91.9947 34.7994, ' '-91.9950 34.7846, ' @@ -204,7 +204,7 @@ def test_relationship_fails_if_contained_unit_expands_outside_parent(self): '-91.9174 34.7627, ' '-91.9153 34.8032, ' '-91.9947 34.7994))', - su2__name='Apartment', + su2__type='AP', su1__geometry='SRID=4326;POLYGON((' '-91.9320 34.7918, ' '-91.9335 34.7846, ' @@ -216,14 +216,14 @@ def test_relationship_fails_if_contained_unit_expands_outside_parent(self): def test_spatial_unit_does_not_contain_anothers_geometry(self): with pytest.raises(Exception): SpatialRelationshipFactory( - su1__name='Building', + su1__type='BU', su1__geometry='SRID=4326;POLYGON((' '-91.9960 34.7850, ' '-91.9960 34.8016, ' '-91.9785 34.8016, ' '-91.9785 34.7850, ' '-91.9960 34.7850))', - su2__name='Apartment', + su2__type='AP', su2__geometry='SRID=4326;POLYGON((' '11.36667 47.25000, ' '11.41667 47.25000, ' @@ -234,7 +234,7 @@ def test_spatial_unit_does_not_contain_anothers_geometry(self): def test_spatial_unit_contains_a_point(self): relationship = SpatialRelationshipFactory( - su1__name='Building', + su1__type='BU', su1__geometry='SRID=4326;POLYGON((' '-109.0461 40.2617, ' '-108.6039 40.2459,' @@ -243,7 +243,7 @@ def test_spatial_unit_contains_a_point(self): '-108.8841 40.7836, ' '-109.0434 40.8657, ' '-109.0461 40.2617))', - su2__name='Apartment', + su2__type='AP', su2__geometry='SRID=4326;POINT(' '-108.7536 40.5054)', type='C') @@ -252,7 +252,7 @@ def test_spatial_unit_contains_a_point(self): def test_spatial_unit_does_not_contain_point(self): with pytest.raises(Exception): SpatialRelationshipFactory( - su1__name='Building', + su1__type='BU', su1__geometry='SRID=4326;POLYGON((' '-109.0461 40.2617, ' '-108.6039 40.2459,' @@ -261,17 +261,17 @@ def test_spatial_unit_does_not_contain_point(self): '-108.8841 40.7836, ' '-109.0434 40.8657, ' '-109.0461 40.2617))', - su2__name='Apartment', + su2__type='AP', su2__geometry='SRID=4326;POINT(' '-108.0972 40.9508)', type='C') def test_spatial_unit_point_contains_relationship_still_created(self): relationship = SpatialRelationshipFactory( - su1__name='Building', + su1__type='BU', su1__geometry='SRID=4326;POINT(' '-108.7536 40.5054)', - su2__name='Apartment', + su2__type='AP', su2__geometry='SRID=4326;POLYGON((' '-109.0461 40.2617, ' '-108.6039 40.2459,' diff --git a/cadasta/spatial/tests/test_serializers.py b/cadasta/spatial/tests/test_serializers.py index 7d8ca40d5..1627a4801 100644 --- a/cadasta/spatial/tests/test_serializers.py +++ b/cadasta/spatial/tests/test_serializers.py @@ -40,29 +40,13 @@ def test_project_detail_not_serialized(self): serializer = serializers.SpatialUnitSerializer(spatial_data) assert 'description' not in serializer.data['properties']['project'] - def test_create_without_name(self): - project = ProjectFactory.create() - spatial_data = { - 'properties': { - 'name': '', - 'project': project - } - } - - serializer = serializers.SpatialUnitSerializer( - data=spatial_data, - context={'project': project} - ) - with pytest.raises(ValidationError): - serializer.is_valid(raise_exception=True) - def test_update_spatial_unit(self): project = ProjectFactory.create() - su = SpatialUnitFactory.create(name='Test Spatial Unit', + su = SpatialUnitFactory.create(type='BU', project=project) spatial_data = { 'properties': { - 'name': 'Updated Spatial Unit', + 'type': 'AP', 'project': project } } @@ -77,15 +61,15 @@ def test_update_spatial_unit(self): serializer.save() su_update = SpatialUnit.objects.get(id=su.id) - assert su_update.name == 'Updated Spatial Unit' + assert su_update.type == 'AP' def test_update_spatial_unit_fails(self): project = ProjectFactory.create() - su = SpatialUnitFactory.create(name='Test Spatial Unit', + su = SpatialUnitFactory.create(type='BU', project=project) spatial_data = { 'properties': { - 'name': '' + 'type': '' } } serializer = serializers.SpatialUnitSerializer( @@ -100,8 +84,7 @@ def test_update_spatial_unit_fails(self): def test_update_spatial_unit_project_fails(self): project = ProjectFactory.create(name='Original Project') - su = SpatialUnitFactory.create(name='Test Spatial Unit', - project=project) + su = SpatialUnitFactory.create(project=project) ProjectFactory.create(name='New Project') spatial_data = { 'properties': { diff --git a/cadasta/spatial/tests/test_views_api_spatial_relationships.py b/cadasta/spatial/tests/test_views_api_spatial_relationships.py index eb1e8494d..f6fccc981 100644 --- a/cadasta/spatial/tests/test_views_api_spatial_relationships.py +++ b/cadasta/spatial/tests/test_views_api_spatial_relationships.py @@ -31,9 +31,9 @@ def _test_objs(self, access='public'): org = OrganizationFactory.create(slug='namati') prj = ProjectFactory.create( slug='test-project', organization=org, access=access) - su1 = SpatialUnitFactory.create(project=prj, name="House") - su2 = SpatialUnitFactory.create(project=prj, name="Parcel") - su3 = SpatialUnitFactory.create(project=prj, name="Bungalow") + su1 = SpatialUnitFactory.create(project=prj, type='AP') + su2 = SpatialUnitFactory.create(project=prj, type='BU') + su3 = SpatialUnitFactory.create(project=prj, type='CB') SpatialRelationshipFactory.create(project=prj, su1=su1, su2=su2) SpatialRelationshipFactory.create(project=prj, su1=su2, su2=su3) SpatialRelationshipFactory.create(project=prj, su1=su1, su2=su3) @@ -55,6 +55,10 @@ def default_create_data(self): 'type': 'C' } + def check_for_unchanged(self, content): + assert content['su1']['properties']['type'] == "AP" + assert content['su2']['properties']['type'] == "BU" + # Additional tests def test_create_invalid_record_with_dupe_su(self): @@ -73,7 +77,7 @@ def test_create_invalid_record_with_dupe_su(self): def test_create_invalid_record_with_different_project(self): org, prj = self._test_objs() other_prj = ProjectFactory.create(slug='other', organization=org) - other_su = SpatialUnitFactory.create(project=other_prj, name="Other") + other_su = SpatialUnitFactory.create(project=other_prj) invalid_data = { 'su1': self.su1.id, 'su2': other_su.id, @@ -105,9 +109,9 @@ def _test_objs(self, access='public'): org = OrganizationFactory.create(slug='namati') prj = ProjectFactory.create( slug='test-project', organization=org, access=access) - su1 = SpatialUnitFactory.create(project=prj, name="House") - su2 = SpatialUnitFactory.create(project=prj, name="Parcel") - su3 = SpatialUnitFactory.create(project=prj, name="Bungalow") + su1 = SpatialUnitFactory.create(project=prj, type='AP') + su2 = SpatialUnitFactory.create(project=prj, type='BU') + su3 = SpatialUnitFactory.create(project=prj) rel = SpatialRelationshipFactory.create( project=prj, su1=su1, su2=su2) self.su1 = su1 @@ -137,8 +141,8 @@ def check_for_updated(self, content): assert content['su2'] == self.su3.id def check_for_unchanged(self, content): - assert content['su1']['properties']['name'] == "House" - assert content['su2']['properties']['name'] == "Parcel" + assert content['su1']['properties']['type'] == "AP" + assert content['su2']['properties']['type'] == "BU" # Additional tests @@ -156,7 +160,7 @@ def test_update_invalid_record_with_different_project(self): other_org = OrganizationFactory.create(slug='other') other_prj = ProjectFactory.create(slug='other', organization=other_org) - other_su = SpatialUnitFactory.create(project=other_prj, name="Other") + other_su = SpatialUnitFactory.create(project=other_prj) def get_invalid_data(): return {'su2': other_su.id} diff --git a/cadasta/spatial/tests/test_views_api_spatial_units.py b/cadasta/spatial/tests/test_views_api_spatial_units.py index a3c26d415..ad7f1a283 100644 --- a/cadasta/spatial/tests/test_views_api_spatial_units.py +++ b/cadasta/spatial/tests/test_views_api_spatial_units.py @@ -30,10 +30,10 @@ def _test_objs(self, access='public'): org = OrganizationFactory.create(slug='namati') prj = ProjectFactory.create( slug='test-project', organization=org, access=access) - SpatialUnitFactory.create(project=prj, name='Top of the world') - SpatialUnitFactory.create(project=prj, name='South Pole') + SpatialUnitFactory.create(project=prj, type='AP') + SpatialUnitFactory.create(project=prj, type='BU') SpatialUnitFactory.create( - project=prj, name='Center of the earth', type='RW') + project=prj, type='RW') self.num_records = 3 return (org, prj) @@ -47,26 +47,26 @@ def test_full_list(self): content = self._get( org_slug=org.slug, prj_slug=prj.slug, status=status_code.HTTP_200_OK, length=self.num_records) - assert extra_record.name not in ( - [u['properties']['name'] for u in content['features']]) - - def test_search_filter(self): - org, prj = self._test_objs() - content = self._get( - org_slug=org.slug, prj_slug=prj.slug, - status=status_code.HTTP_200_OK, length=1, query='search=earth') - assert all( - record['properties']['name'] == 'Center of the earth' for - record in content['features']) + assert extra_record.id not in ( + [u['properties']['id'] for u in content['features']]) + + # def test_search_filter(self): + # org, prj = self._test_objs() + # content = self._get( + # org_slug=org.slug, prj_slug=prj.slug, + # status=status_code.HTTP_200_OK, length=1, query='search=AP') + # assert all( + # record['properties']['type'] == 'AP' for + # record in content['features']) def test_ordering(self): org, prj = self._test_objs() content = self._get( org_slug=org.slug, prj_slug=prj.slug, status=status_code.HTTP_200_OK, length=self.num_records, - query='ordering=name') + query='ordering=type') names = [ - record['properties']['name'] for record in content['features']] + record['properties']['type'] for record in content['features']] assert names == sorted(names) def test_reverse_ordering(self): @@ -74,9 +74,9 @@ def test_reverse_ordering(self): content = self._get( org_slug=org.slug, prj_slug=prj.slug, status=status_code.HTTP_200_OK, length=self.num_records, - query='ordering=-name') + query='ordering=-type') names = [ - record['properties']['name'] for record in content['features']] + record['properties']['type'] for record in content['features']] assert names == sorted(names, reverse=True) def test_type_filter(self): @@ -94,7 +94,7 @@ class SpatialUnitCreateAPITest(SpatialUnitListTestCase, default_create_data = { 'properties': { - 'name': "Small world" + 'type': "AP" }, 'geometry': { 'type': 'Point', @@ -107,6 +107,7 @@ class SpatialUnitCreateAPITest(SpatialUnitListTestCase, def test_create_invalid_spatial_unit(self): org, prj = self._test_objs() invalid_data = { + 'type': '', 'geometry': { 'type': 'Point', 'coordinates': [100, 0] @@ -115,12 +116,12 @@ def test_create_invalid_spatial_unit(self): content = self._post( org_slug=org.slug, prj_slug=prj.slug, data=invalid_data, status=status_code.HTTP_400_BAD_REQUEST) - assert content['name'][0] == _("This field is required.") + assert content['type'][0] == _('"" is not a valid choice.') def test_create_spatial_unit_with_invalid_geometry(self): org, prj = self._test_objs() invalid_data = { - 'name': "Cat points!", + 'type': "BU", 'geometry': { 'type': 'Cats', 'coordinates': [100, 0] @@ -149,7 +150,7 @@ def _test_objs(self, access='public'): org = OrganizationFactory.create(slug='namati') prj = ProjectFactory.create( slug='test-project', organization=org, access=access) - su = SpatialUnitFactory.create(project=prj, name="House") + su = SpatialUnitFactory.create(project=prj, type='AP') self.su = su return (su, org) @@ -165,24 +166,24 @@ class SpatialUnitUpdateAPITest(SpatialUnitDetailTestCase, RecordUpdateAPITest): def get_valid_updated_data(self): - return {'name': "Way Cooler Name"} + return {'type': "BU"} def check_for_updated(self, content): data = self.get_valid_updated_data() - assert content['properties']['name'] == data['name'] + assert content['properties']['type'] == data['type'] def check_for_unchanged(self, content): - assert content['properties']['name'] == self.su.name + assert content['properties']['type'] == self.su.type # Additional tests def test_update_with_invalid_data(self): - def get_invalid_data(): return {'name': ''} + def get_invalid_data(): return {'type': ''} content = self._test_patch_public_record( get_invalid_data, status_code.HTTP_400_BAD_REQUEST) - assert content['name'][0] == _('This field may not be blank.') + assert content['type'][0] == _('"" is not a valid choice.') def test_update_with_invalid_geometry(self): diff --git a/cadasta/spatial/views/api.py b/cadasta/spatial/views/api.py index 7157ae63b..a47ab76cf 100644 --- a/cadasta/spatial/views/api.py +++ b/cadasta/spatial/views/api.py @@ -15,8 +15,6 @@ class SpatialUnitList(APIPermissionRequiredMixin, filters.SearchFilter, filters.OrderingFilter,) filter_fields = ('type',) - search_fields = ('name',) - ordering_fields = ('name',) permission_required = { 'GET': 'spatial.list', diff --git a/cadasta/xforms/mixins/model_helper.py b/cadasta/xforms/mixins/model_helper.py index 62c6b3fec..c2d1d188d 100644 --- a/cadasta/xforms/mixins/model_helper.py +++ b/cadasta/xforms/mixins/model_helper.py @@ -36,7 +36,6 @@ def add_data_to_party(self, data, project): def add_data_to_spatial_unit(self, data, project): location = SpatialUnit.objects.create( project=project, - name=data['location_name'], type=data['location_type'], geometry=self._format_geometry(data['location_geometry']), attributes=data['location_attributes'] @@ -57,7 +56,7 @@ def add_data_to_resource(self, data, user, project, content_object=None): Storage = get_storage_class() storage = Storage() url = storage.save('resources/' + data.name, data.file.read()) - # if not Resource.objects.filter(name=data.name).exists(): + Resource.objects.create( name=data.name, file=url, @@ -119,10 +118,10 @@ def upload_files(self, data, survey): if i != 'xml_submission_file': if i == survey['location_photo']: content_object = SpatialUnit.objects.get( - id=survey['location_name']) + id=survey['location']) elif i == survey['party_photo']: content_object = Party.objects.get( - id=survey['party_name']) + id=survey['party']) self.add_data_to_resource(data=data[i], user=user, diff --git a/cadasta/xforms/serializers.py b/cadasta/xforms/serializers.py index c87f9aba9..25c947c29 100644 --- a/cadasta/xforms/serializers.py +++ b/cadasta/xforms/serializers.py @@ -52,7 +52,7 @@ def create(self, request, *args, **kwargs): 'instanceID': survey['meta']['instanceID'], 'project': create_models['project'], 'location_photo': survey['location_photo'], - 'location_name': create_models['location'].id, + 'location': create_models['location'].id, 'party_photo': survey['party_photo'], - 'party_name': create_models['party'].id + 'party': create_models['party'].id } diff --git a/cadasta/xforms/tests/attr_schemas.py b/cadasta/xforms/tests/attr_schemas.py index f39bb3dd2..8d9a85652 100644 --- a/cadasta/xforms/tests/attr_schemas.py +++ b/cadasta/xforms/tests/attr_schemas.py @@ -66,91 +66,10 @@ "label": "Location Attributes", "children": [ { - "default": "none", - "choices": [ - { - "name": "none", - "label": "No data" - }, - { - "name": "text", - "label": "Textual" - }, - { - "name": "point", - "label": "Point data" - }, - { - "name": "polygon_low", - "label": "Low quality polygon" - }, - { - "name": "polygon_high", - "label": "High quality polygon" - } - ], - "hint": "Quality of parcel geometry", - "name": "quality", - "type": "select one", - "label": "Spatial Unit Quality" - }, - { - "label": "Acquired when", - "name": "acquired_when", - "type": "date" - }, - { - "label": "Acquired how", - "name": "acquired_how", - "type": "select one", - "choices": [ - { - "name": "CS", - "label": "Contractual Share Crop" - }, - { - "name": "CA", - "label": "Customary Arrangement" - }, - { - "name": "GF", - "label": "Gift" - }, - { - "name": "HS", - "label": "Homestead" - }, - { - "name": "IO", - "label": "Informal Occupant" - }, - { - "name": "IN", - "label": "Inheritance" - }, - { - "name": "LH", - "label": "Leasehold" - }, - { - "name": "PF", - "label": "Purchased Freehold" - }, - { - "name": "RN", - "label": "Rental" - }, - { - "name": "OT", - "label": "Other" - } - ], - }, - { - "label": "Notes", - "name": "notes", + "label": "Name", + "name": "name", "type": "text", - "hint": "Additional Notes" + "hint": "Name of the location" } ] } diff --git a/cadasta/xforms/tests/files/test_resources.py b/cadasta/xforms/tests/files/test_resources.py index ebfbea609..b50c56ca9 100644 --- a/cadasta/xforms/tests/files/test_resources.py +++ b/cadasta/xforms/tests/files/test_resources.py @@ -54,17 +54,13 @@ <party_type>IN</party_type> <party_name>Bilbo Baggins</party_name> - <location_name>Middle Earth</location_name> <location_geometry>40.6890612 -73.9925067 0.0 0.0;</location_geometry> <location_type>MI</location_type> <location_photo>test_image.png</location_photo> <party_photo /> <tenure_type>LH</tenure_type> <location_attributes> - <quality>none</quality> - <acquired_how>LH</acquired_how> - <acquired_when>2016-07-07</acquired_when> - <notes>Locaiton attribute notes.</notes> + <name>Middle Earth</name> </location_attributes> <party_attributes_default> <notes>Party attribute default notes.</notes> @@ -94,17 +90,13 @@ <title /> <party_type>IN</party_type> <party_name>Bilbo Baggins</party_name> - <location_name>Middle Earth</location_name> <location_geometry /> <location_type>MI</location_type> <location_photo>test_image.png</location_photo> <party_photo>this-is-fine.png</party_photo> <tenure_type>LH</tenure_type> <location_attributes> - <quality>none</quality> - <acquired_how>LH</acquired_how> - <acquired_when>2016-07-07</acquired_when> - <notes>Locaiton attribute notes.</notes> + <name>Middle Earth</name> </location_attributes> <party_attributes_default> <notes>Party attribute default notes.</notes> @@ -134,17 +126,13 @@ <title /> <party_type>IN</party_type> <party_name></party_name> - <location_name>Null Island</location_name> <location_geometry>40.6890612 -73.9925067 0.0 0.0;</location_geometry> <location_type>MI</location_type> <location_photo>test_image.png</location_photo> <party_photo /> <tenure_type>LH</tenure_type> <location_attributes> - <quality>none</quality> - <acquired_how>LH</acquired_how> - <acquired_when>2016-07-07</acquired_when> - <notes>Locaiton attribute notes.</notes> + <name>Null Island</name> </location_attributes> <party_attributes_default> <notes>Party attribute default notes.</notes> @@ -174,7 +162,6 @@ <title /> <party_type>IN</party_type> <party_name>Peggy Carter</party_name> - <location_name>Polygon</location_name> <location_geometry>40.6890612 -73.9925067 0.0 0.0; 41.6890612 -73.9925067 0.0 0.0;41.6890612 -72.9925067 0.0 0.0; 40.6890612 -72.9925067 0.0 0.0;40.6890612 -73.9925067 0.0 0.0; @@ -184,10 +171,7 @@ <party_photo /> <tenure_type>LH</tenure_type> <location_attributes> - <quality>none</quality> - <acquired_how>LH</acquired_how> - <acquired_when>2016-07-07</acquired_when> - <notes>Locaiton attribute notes.</notes> + <name>Polygon</name> </location_attributes> <party_attributes_default> <notes>Party attribute default notes.</notes> @@ -217,7 +201,6 @@ <title /> <party_type>IN</party_type> <party_name>Buckey Barnes</party_name> - <location_name>Line</location_name> <location_geometry>45.56342779158167 -122.67650283873081 0.0 0.0; 45.56176327330353 -122.67669159919024 0.0 0.0; 45.56151562182025 -122.67490658909082 0.0 0.0; @@ -229,10 +212,7 @@ <party_photo /> <tenure_type>LH</tenure_type> <location_attributes> - <quality>none</quality> - <acquired_how>LH</acquired_how> - <acquired_when>2016-07-07</acquired_when> - <notes>Locaiton attribute notes.</notes> + <name>Line</name> </location_attributes> <party_attributes_default> <notes>Party attribute default notes.</notes> @@ -262,7 +242,6 @@ <title /> <party_type>IN</party_type> <party_name>Stever Rogers</party_name> - <location_name>Missing Semi</location_name> <location_geometry>40.6890612 -73.9925067 0.0 0.0 </location_geometry> <location_type>MI</location_type> @@ -270,10 +249,7 @@ <party_photo /> <tenure_type>LH</tenure_type> <location_attributes> - <quality>none</quality> - <acquired_how>LH</acquired_how> - <acquired_when>2016-07-07</acquired_when> - <notes>Locaiton attribute notes.</notes> + <name>Missing Semi</name> </location_attributes> <party_attributes_default> <notes>Party attribute default notes.</notes> @@ -303,7 +279,6 @@ <title /> <party_type>IN</party_type> <party_name>Scrooge McDuck</party_name> - <location_name>Bank</location_name> <location_geometry>40.6890612 -73.9925067 0.0 0.0; 41.6890612 -73.9925067 0.0 0.0;41.6890612 -72.9925067 0.0 0.0; 40.6890612 -72.9925067 0.0 0.0;40.6890612 -73.9925067 0.0 0.0; @@ -313,10 +288,7 @@ <party_photo /> <tenure_type>LH</tenure_type> <location_attributes> - <quality>none</quality> - <acquired_how>LH</acquired_how> - <acquired_when>2016-07-07</acquired_when> - <notes>Locaiton attribute notes.</notes> + <name>Bank</name> </location_attributes> <party_attributes_default> <notes>Party attribute default notes.</notes> diff --git a/cadasta/xforms/tests/test_renderers.py b/cadasta/xforms/tests/test_renderers.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/cadasta/xforms/tests/test_views_api.py b/cadasta/xforms/tests/test_views_api.py index e8ea0af34..97076e954 100644 --- a/cadasta/xforms/tests/test_views_api.py +++ b/cadasta/xforms/tests/test_views_api.py @@ -227,10 +227,11 @@ def test_survey_upload(self): image=images['test_image'], status=201) assert Party.objects.filter(name='Bilbo Baggins').exists() - assert SpatialUnit.objects.filter(name='Middle Earth').exists() + assert SpatialUnit.objects.filter( + attributes={'name': 'Middle Earth'}).exists() party = Party.objects.get(name='Bilbo Baggins') - location = SpatialUnit.objects.get(name='Middle Earth') + location = SpatialUnit.objects.get(attributes={'name': 'Middle Earth'}) assert location in party.tenure_relationships.all() assert Resource.objects.filter(name__contains='test_image').exists() @@ -243,10 +244,11 @@ def test_multiple_resource_upload(self): image=images, status=201) assert Party.objects.filter(name='Bilbo Baggins').exists() - assert SpatialUnit.objects.filter(name='Middle Earth').exists() + assert SpatialUnit.objects.filter( + attributes={'name': 'Middle Earth'}).exists() party = Party.objects.get(name='Bilbo Baggins') - location = SpatialUnit.objects.get(name='Middle Earth') + location = SpatialUnit.objects.get(attributes={'name': 'Middle Earth'}) assert location in party.tenure_relationships.all() assert Resource.objects.filter(name__contains='test_image').exists() @@ -259,15 +261,16 @@ def test_multiple_resource_upload(self): def test_invalid_resource_upload(self): # testing submitting with a missing xml_submission_file self._post(form=images['test_image'], status=400, valid=False) - assert not SpatialUnit.objects.filter(name='Null Island').exists() + assert not SpatialUnit.objects.filter( + attributes={'name': 'Null Island'}).exists() def test_geometry_upload(self): self._post(form='line_form', status=201) self._post(form='poly_form', status=201) self._post(form='missing_semi_form', status=201) - polygon = SpatialUnit.objects.get(name='Polygon') - line = SpatialUnit.objects.get(name='Line') - point = SpatialUnit.objects.get(name='Missing Semi') + polygon = SpatialUnit.objects.get(attributes={'name': 'Polygon'}) + line = SpatialUnit.objects.get(attributes={'name': 'Line'}) + point = SpatialUnit.objects.get(attributes={'name': 'Missing Semi'}) assert polygon.geometry.geom_type == 'Polygon' assert line.geometry.geom_type == 'LineString'