Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds JOSN attributes to UI #375

Merged
merged 1 commit into from
Jul 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions cadasta/party/forms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django import forms
from jsonattrs.forms import AttributeModelForm
from .models import Party, TenureRelationshipType, TenureRelationship


class PartyForm(forms.ModelForm):
class PartyForm(AttributeModelForm):
attributes_field = 'attributes'

class Meta:
model = Party
fields = ['name', 'type']
Expand All @@ -18,7 +20,9 @@ def save(self):
return instance


class TenureRelationshipEditForm(forms.ModelForm):
class TenureRelationshipEditForm(AttributeModelForm):
attributes_field = 'attributes'

class Meta:
model = TenureRelationship
fields = ['tenure_type']
Expand Down
49 changes: 46 additions & 3 deletions cadasta/party/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,70 @@
from django.test import TestCase
from django.contrib.contenttypes.models import ContentType
from jsonattrs.models import Attribute, AttributeType, Schema

from organization.tests.factories import ProjectFactory
from ..models import Party, TenureRelationshipType
from .. import forms


class PartyFormTest(TestCase):
def test_create_location(self):
def test_create_party(self):
data = {
'name': 'Cadasta',
'type': 'IN'
}
project = ProjectFactory.create()
form = forms.PartyForm(project_id=project.id, data=data)
form = forms.PartyForm(project_id=project.id,
data=data,
schema_selectors=())
form.is_valid()
form.save()

assert Party.objects.filter(project=project).count() == 1

def test_create_party_with_attributes(self):
data = {
'name': 'Cadasta',
'type': 'IN',
'attributes::fname': 'test'
}
project = ProjectFactory.create()

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

Attribute.objects.create(
schema=schema,
name='fname',
long_name='Test field',
attr_type=AttributeType.objects.get(name='text'),
index=0
)

form = forms.PartyForm(project_id=project.id,
data=data,
schema_selectors=(
{'name': 'organization',
'value': project.organization,
'selector': project.organization.id},
{'name': 'project',
'value': project,
'selector': project.id}
))
form.is_valid()
form.save()

assert Party.objects.filter(project=project).count() == 1
party = Party.objects.filter(project=project).first()
assert party.attributes.get('fname') == 'test'


class TenureRelationshipEditFormTest(TestCase):
def test_init(self):
form = forms.TenureRelationshipEditForm()
form = forms.TenureRelationshipEditForm(schema_selectors=())
tenure_types = TenureRelationshipType.objects.values_list('id',
'label')
assert list(form.fields['tenure_type'].choices) == list(tenure_types)
74 changes: 67 additions & 7 deletions cadasta/party/tests/test_views_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
from django.http import Http404
from django.conf import settings
from django.contrib.messages.api import get_messages
from django.contrib.contenttypes.models import ContentType

from buckets.test.utils import ensure_dirs
from buckets.test.storage import FakeS3Storage
from tutelary.models import Policy, assign_user_policies
from jsonattrs.models import Attribute, AttributeType, Schema

from organization.tests.factories import ProjectFactory
from resources.tests.factories import ResourceFactory
Expand Down Expand Up @@ -108,7 +110,7 @@ def assign_policies(self):
def get_template_context(self):
return {
'object': self.project,
'form': forms.PartyForm()
'form': forms.PartyForm(schema_selectors=())
}

def get_url_kwargs(self):
Expand Down Expand Up @@ -173,12 +175,28 @@ class PartyDetailTest(TestCase):
def set_up_models(self):
self.project = ProjectFactory.create()
self.party = PartyFactory.create(project=self.project)
content_type = ContentType.objects.get(
app_label='party', model='party')
schema = Schema.objects.create(
content_type=content_type,
selectors=(self.project.organization.id, self.project.id, ))
attr_type = AttributeType.objects.get(name='text')
Attribute.objects.create(
schema=schema,
name='fname', long_name='Test field',
attr_type=attr_type, index=0,
required=False, omit=False
)
self.party = PartyFactory.create(project=self.project,
attributes={'fname': 'test'})

def assign_policies(self):
assign_policies(self.authorized_user)

def get_template_context(self):
return {'object': self.project, 'party': self.party}
return {'object': self.project,
'party': self.party,
'attributes': (('Test field', 'test', ), )}

def get_url_kwargs(self):
return {
Expand Down Expand Up @@ -220,12 +238,27 @@ class PartiesEditTest(TestCase):
success_url_name = 'parties:detail'
post_data = {
'name': 'Party',
'type': 'GR'
'type': 'GR',
'attributes::fname': 'New text'
}

def set_up_models(self):
self.project = ProjectFactory.create()
self.party = PartyFactory.create(project=self.project)
content_type = ContentType.objects.get(
app_label='party', model='party')
schema = Schema.objects.create(
content_type=content_type,
selectors=(self.project.organization.id, self.project.id, ))
attr_type = AttributeType.objects.get(name='text')
Attribute.objects.create(
schema=schema,
name='fname', long_name='Test field',
attr_type=attr_type, index=0,
required=False, omit=False
)
self.party = PartyFactory.create(project=self.project,
attributes={'fname': 'test'})

def assign_policies(self):
assign_policies(self.authorized_user)
Expand Down Expand Up @@ -283,6 +316,7 @@ def test_post_with_authorized_user(self):
self.party.refresh_from_db()
assert self.party.name == self.post_data['name']
assert self.party.type == self.post_data['type']
assert self.party.attributes.get('fname') == 'New text'

def test_post_with_unauthorized_user(self):
response = self.request(method='POST', user=self.unauthorized_user)
Expand Down Expand Up @@ -576,8 +610,20 @@ class PartyRelationshipDetailTest(TestCase):

def set_up_models(self):
self.project = ProjectFactory.create()
content_type = ContentType.objects.get(
app_label='party', model='tenurerelationship')
schema = Schema.objects.create(
content_type=content_type,
selectors=(self.project.organization.id, self.project.id, ))
attr_type = AttributeType.objects.get(name='text')
Attribute.objects.create(
schema=schema,
name='fname', long_name='Test field',
attr_type=attr_type, index=0,
required=False, omit=False
)
self.relationship = TenureRelationshipFactory.create(
project=self.project)
project=self.project, attributes={'fname': 'test'})

def assign_policies(self):
assign_policies(self.authorized_user)
Expand All @@ -586,7 +632,8 @@ def get_template_context(self):
return {'object': self.project,
'relationship': self.relationship,
'location': self.relationship.spatial_unit,
'geojson': '{"type": "FeatureCollection", "features": []}'}
'geojson': '{"type": "FeatureCollection", "features": []}',
'attributes': (('Test field', 'test', ), )}

def get_url_kwargs(self):
return {
Expand Down Expand Up @@ -626,12 +673,24 @@ class PartyRelationshipEditTest(TestCase):
view = default.PartyRelationshipEdit
template = 'party/relationship_edit.html'
success_url_name = 'parties:relationship_detail'
post_data = {'tenure_type': 'LH'}
post_data = {'tenure_type': 'LH', 'attributes::fname': 'New text'}

def set_up_models(self):
self.project = ProjectFactory.create()
content_type = ContentType.objects.get(
app_label='party', model='tenurerelationship')
schema = Schema.objects.create(
content_type=content_type,
selectors=(self.project.organization.id, self.project.id, ))
attr_type = AttributeType.objects.get(name='text')
Attribute.objects.create(
schema=schema,
name='fname', long_name='Test field',
attr_type=attr_type, index=0,
required=False, omit=False
)
self.relationship = TenureRelationshipFactory.create(
project=self.project)
project=self.project, attributes={'fname': 'test'})

def assign_policies(self):
assign_policies(self.authorized_user)
Expand Down Expand Up @@ -687,6 +746,7 @@ def test_post_with_authorized_user(self):

self.relationship.refresh_from_db()
assert self.relationship.tenure_type_id == 'LH'
assert self.relationship.attributes.get('fname') == 'New text'

def test_post_with_unauthorized_user(self):
response = self.request(method='POST', user=self.unauthorized_user)
Expand Down
10 changes: 10 additions & 0 deletions cadasta/party/views/default.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.views import generic
from django.core.urlresolvers import reverse
from jsonattrs.mixins import JsonAttrsMixin
from core.mixins import LoginPermissionRequiredMixin

from resources.forms import AddResourceFromLibraryForm
Expand Down Expand Up @@ -28,13 +29,20 @@ class PartiesAdd(LoginPermissionRequiredMixin,
def get_perms_objects(self):
return [self.get_project()]

def get_form_kwargs(self, *args, **kwargs):
kwargs = super().get_form_kwargs(*args, **kwargs)
kwargs['schema_selectors'] = ()
return kwargs


class PartiesDetail(LoginPermissionRequiredMixin,
JsonAttrsMixin,
mixins.PartyObjectMixin,
generic.DetailView):
template_name = 'party/party_detail.html'
permission_required = 'party.view'
permission_denied_message = error_messages.PARTY_VIEW
attributes_field = 'attributes'


class PartiesEdit(LoginPermissionRequiredMixin,
Expand Down Expand Up @@ -85,11 +93,13 @@ class PartyResourcesNew(LoginPermissionRequiredMixin,


class PartyRelationshipDetail(LoginPermissionRequiredMixin,
JsonAttrsMixin,
mixins.PartyRelationshipObjectMixin,
generic.DetailView):
template_name = 'party/relationship_detail.html'
permission_required = 'tenure_rel.view'
permission_denied_message = error_messages.TENURE_REL_VIEW
attributes_field = 'attributes'


class PartyRelationshipEdit(LoginPermissionRequiredMixin,
Expand Down
Loading