Skip to content

Commit

Permalink
Merge pull request #415 from Cadasta/bugs/geoodk-spatial-name
Browse files Browse the repository at this point in the history
removed location name from model and updated geoodk api
  • Loading branch information
ian-ross authored Jul 12, 2016
2 parents 65d2820 + dea9694 commit 6a511dc
Show file tree
Hide file tree
Showing 27 changed files with 161 additions and 306 deletions.
59 changes: 25 additions & 34 deletions cadasta/core/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from spatial.tests.factories import (
SpatialUnitFactory, SpatialRelationshipFactory
)
from spatial.models import SpatialUnit

from tutelary.models import Policy, Role


Expand Down Expand Up @@ -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],'
Expand All @@ -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],'
Expand All @@ -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],'
Expand All @@ -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],'
Expand All @@ -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')
Expand Down
1 change: 0 additions & 1 deletion cadasta/core/management/commands/loadfixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 0 additions & 2 deletions cadasta/core/tests/test_management_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
19 changes: 7 additions & 12 deletions cadasta/organization/tests/test_views_default_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from buckets.test.storage import FakeS3Storage

from core.tests.base_test_case import UserTestCase
from core.tests.factories import RoleFactory
from accounts.tests.factories import UserFactory
from organization.models import OrganizationRole, Project, ProjectRole
from questionnaires.tests.factories import QuestionnaireFactory
Expand Down Expand Up @@ -231,13 +230,15 @@ def _get_private(self, status=None, user=None, make_org_member=False,
OrganizationRole.objects.create(organization=other_org, user=user)
return self._get(prj, user=user, status=status), prj

def _check_render(self, response, project, assign_context=False):
def _check_render(self, response, project,
assign_context=False, is_superuser=False):
content = response.render().content.decode('utf-8')

context = RequestContext(self.request)
context['object'] = project
context['project'] = project
context['geojson'] = '{"type": "FeatureCollection", "features": []}'
context['is_superuser'] = is_superuser

expected = render_to_string(
'organization/project_dashboard.html',
Expand All @@ -261,13 +262,10 @@ def test_get_with_unauthorized_user(self):

def test_get_with_superuser(self):
superuser = UserFactory.create()
superuser_pol = Policy.objects.get(name='superuser')
self.superuser_role = RoleFactory.create(
name='superuser', policies=[superuser_pol]
)
self.superuser_role = Role.objects.get(name='superuser')
superuser.assign_policies(self.superuser_role)
response = self._get(self.project1, user=superuser, status=200)
self._check_render(response, self.project1,)
self._check_render(response, self.project1, is_superuser=True)

def test_get_non_existent_project(self):
setattr(self.request, 'user', self.user)
Expand Down Expand Up @@ -313,15 +311,12 @@ def test_get_private_project_on_org_membership_removal(self):

def test_get_private_project_with_superuser(self):
superuser = UserFactory.create()
superuser_pol = Policy.objects.get(name='superuser')
self.superuser_role = RoleFactory.create(
name='superuser', policies=[superuser_pol]
)
self.superuser_role = Role.objects.get(name='superuser')
superuser.assign_policies(self.superuser_role)
response, prj = self._get_private(
user=superuser, status=200
)
self._check_render(response, prj)
self._check_render(response, prj, is_superuser=True)


class ProjectAddTest(UserTestCase):
Expand Down
2 changes: 1 addition & 1 deletion cadasta/party/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class TutelaryMeta:

def __str__(self):
return "<TenureRelationship: <{party}> {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):
Expand Down
2 changes: 1 addition & 1 deletion cadasta/party/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) == (
"<TenureRelationship: <Family> Leasehold <Parcel>>")
Expand Down
6 changes: 3 additions & 3 deletions cadasta/party/tests/test_views_api_relationship_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions cadasta/party/tests/test_views_api_tenure_relationships.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
Binary file not shown.
Binary file modified cadasta/questionnaires/tests/files/test_standard_questionnaire.xlsx
Binary file not shown.
4 changes: 2 additions & 2 deletions cadasta/questionnaires/tests/test_attr_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand All @@ -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',
}
Expand Down
4 changes: 1 addition & 3 deletions cadasta/spatial/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 3 additions & 7 deletions cadasta/spatial/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -59,7 +55,7 @@ class SpatialUnit(ResourceModelMixin, RandomIDModel):
history = HistoricalRecords()

class Meta:
ordering = ('name',)
ordering = ('type',)

class TutelaryMeta:
perm_type = 'spatial'
Expand Down Expand Up @@ -88,7 +84,7 @@ class TutelaryMeta:
)

def __str__(self):
return "<SpatialUnit: {}>".format(self.name)
return "<SpatialUnit: {}>".format(self.get_type_display())

def __repr__(self):
return str(self)
Expand Down Expand Up @@ -223,7 +219,7 @@ class TutelaryMeta:

def __str__(self):
return "<SpatialRelationship: <{su1}> {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):
Expand Down
6 changes: 3 additions & 3 deletions cadasta/spatial/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',)
Expand Down Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion cadasta/spatial/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class SpatialUnitFactory(ExtendedFactory):
class Meta:
model = SpatialUnit

name = factory.Sequence(lambda n: "Location #{}".format(n))
project = factory.SubFactory(ProjectFactory)


Expand Down
Loading

0 comments on commit 6a511dc

Please sign in to comment.