From d3157c888cb61d77242b6052be53836a4fc98ef3 Mon Sep 17 00:00:00 2001 From: Oliver Roick Date: Tue, 14 Feb 2017 16:08:20 +0100 Subject: [PATCH] Fix #1001 -- Add relationships to party detail page --- cadasta/party/tests/test_views_default.py | 70 +++++++++++++- cadasta/party/views/default.py | 24 +++++ cadasta/party/views/mixins.py | 2 +- cadasta/templates/party/party_detail.html | 108 ++++++++++++++++------ 4 files changed, 173 insertions(+), 31 deletions(-) diff --git a/cadasta/party/tests/test_views_default.py b/cadasta/party/tests/test_views_default.py index 194d0e7a3..8ac343819 100644 --- a/cadasta/party/tests/test_views_default.py +++ b/cadasta/party/tests/test_views_default.py @@ -15,6 +15,7 @@ from resources.tests.utils import clear_temp # noqa from skivvy import ViewTestCase from spatial.models import SpatialUnit +from spatial.tests.factories import SpatialUnitFactory from tutelary.models import Policy, assign_user_policies from questionnaires.tests import factories as q_factories @@ -330,6 +331,54 @@ def test_get_with_with_questionnaire(self): name='IN', label={'en': 'Individual', 'de': 'Einzelperson'}) + location_type_question = q_factories.QuestionFactory.create( + questionnaire=questionnaire, + name='location_type', + label={'en': 'Location type', 'de': 'Parzelle Typ'}, + type='S1') + + tenure_type_question = q_factories.QuestionFactory.create( + questionnaire=questionnaire, + name='tenure_type', + label={'en': 'Location type', 'de': 'Parzelle Typ'}, + type='S1') + q_factories.QuestionOptionFactory.create( + question=tenure_type_question, + name='LH', + label={'en': 'Leasehold', 'de': 'Miete'}) + q_factories.QuestionOptionFactory.create( + question=tenure_type_question, + name='WR', + label={'en': 'Water rights', 'de': 'Wasserecht'}) + + lh_ten = TenureRelationshipFactory.create( + tenure_type=TenureRelationshipType.objects.get(id='LH'), + party=self.party, + spatial_unit=SpatialUnitFactory(project=self.project, type='PA'), + project=self.project) + q_factories.QuestionOptionFactory.create( + question=location_type_question, + name='PA', + label={'en': 'Parcel', 'de': 'Parzelle'}) + lh_ten.type_labels = ('data-label-de="Miete" ' + 'data-label-en="Leasehold"') + lh_ten.location_labels = ('data-label-de="Parzelle" ' + 'data-label-en="Parcel"') + + wr_ten = TenureRelationshipFactory.create( + tenure_type=TenureRelationshipType.objects.get(id='WR'), + party=self.party, + spatial_unit=SpatialUnitFactory(project=self.project, type='BU'), + project=self.project) + q_factories.QuestionOptionFactory.create( + question=location_type_question, + name='BU', + label={'en': 'Building', 'de': 'Haus'}) + wr_ten.type_labels = ('data-label-de="Wasserecht" ' + 'data-label-en="Water rights"') + wr_ten.location_labels = ('data-label-de="Haus" ' + 'data-label-en="Building"') + user = UserFactory.create() assign_policies(user) response = self.request(user=user) @@ -342,7 +391,19 @@ def test_get_with_with_questionnaire(self): type_choice_labels=('data-label-de="Einzelperson" ' 'data-label-en="Individual"'), form_lang_default='en', - form_langs=[('en', 'English'), ('de', 'German')]) + form_langs=[('en', 'English'), ('de', 'German')], + relationships=[wr_ten, lh_ten]) + + def test_get_with_authorized_user_including_relationships(self): + TenureRelationshipFactory.create_batch(2, + party=self.party, + project=self.project) + user = UserFactory.create() + assign_policies(user) + response = self.request(user=user) + assert response.status_code == 200 + assert response.content == self.render_content( + relationships=self.party.tenurerelationship_set.all()) def test_get_from_non_existend_project(self): user = UserFactory.create() @@ -401,7 +462,8 @@ def setup_template_context(self): return {'object': self.project, 'party': self.party, 'form': forms.PartyForm(instance=self.party, - project=self.project)} + project=self.project), + 'attributes': (('Test field', 'test'), )} def setup_url_kwargs(self): return { @@ -726,7 +788,7 @@ def test_post_with_authorized_user(self): assign_policies(user) response = self.request(method='POST', user=user) assert response.status_code == 302 - assert response.location == self.expected_success_url + assert response.location == self.expected_success_url + '#resources' party_resources = self.party.resources.all() assert len(party_resources) == 2 @@ -851,7 +913,7 @@ def test_post_with_authorized_user(self): assign_policies(user) response = self.request(method='POST', user=user) assert response.status_code == 302 - assert response.location == self.expected_success_url + assert response.location == self.expected_success_url + '#resources' assert self.party.resources.count() == 1 diff --git a/cadasta/party/views/default.py b/cadasta/party/views/default.py index 5bab14e5e..88c73c5f5 100644 --- a/cadasta/party/views/default.py +++ b/cadasta/party/views/default.py @@ -71,6 +71,8 @@ class PartiesDetail(LoginPermissionRequiredMixin, def get_context_data(self, *args, **kwargs): context = super().get_context_data(*args, **kwargs) + context['relationships'] = self.object.tenurerelationship_set.all( + ).select_related('spatial_unit').defer('spatial_unit__attributes') project = context['object'] if project.current_questionnaire: @@ -90,10 +92,31 @@ def get_context_data(self, *args, **kwargs): context['type_choice_labels'] = template_xlang_labels( option.label_xlat) + tenure_type = Question.objects.get( + name='tenure_type', + questionnaire_id=project.current_questionnaire) + tenure_opts = QuestionOption.objects.filter(question=tenure_type) + tenure_opts = dict(tenure_opts.values_list('name', 'label_xlat')) + + location_type = Question.objects.get( + name='location_type', + questionnaire_id=project.current_questionnaire) + location_opts = QuestionOption.objects.filter( + question=location_type) + location_opts = dict( + location_opts.values_list('name', 'label_xlat')) + + for rel in context['relationships']: + rel.type_labels = template_xlang_labels( + tenure_opts.get(rel.tenure_type_id)) + rel.location_labels = template_xlang_labels( + location_opts.get(rel.spatial_unit.type)) + return context class PartiesEdit(LoginPermissionRequiredMixin, + JsonAttrsMixin, mixins.PartyObjectMixin, organization_mixins.ProjectAdminCheckMixin, generic.UpdateView): @@ -101,6 +124,7 @@ class PartiesEdit(LoginPermissionRequiredMixin, form_class = forms.PartyForm permission_required = update_permissions('party.update') permission_denied_message = error_messages.PARTY_UPDATE + attributes_field = 'attributes' def get_form_kwargs(self): kwargs = super().get_form_kwargs() diff --git a/cadasta/party/views/mixins.py b/cadasta/party/views/mixins.py index d01738634..56da0dad3 100644 --- a/cadasta/party/views/mixins.py +++ b/cadasta/party/views/mixins.py @@ -73,7 +73,7 @@ def get_content_object(self): def get_success_url(self): kwargs = self.kwargs kwargs['party'] = self.get_object().id - return reverse('parties:detail', kwargs=kwargs) + return reverse('parties:detail', kwargs=kwargs) + '#resources' def get_party(self): if not hasattr(self, 'party_object'): diff --git a/cadasta/templates/party/party_detail.html b/cadasta/templates/party/party_detail.html index df067631a..58a8628b5 100644 --- a/cadasta/templates/party/party_detail.html +++ b/cadasta/templates/party/party_detail.html @@ -18,11 +18,19 @@

{% trans "Party detail" %}

-
- -

{% trans "Details" %}

-
-
+
+ +

{% trans "Party" %} {{ party.name }}

+ + + +
+ +
@@ -42,34 +50,64 @@

{% trans "Details" %}

-
- - -

{% trans "Resources" %}

- {% if resource_list %} -
- {% if has_unattached_resources %} - - {% else %} - - {% endif %} - {% trans "Attach" %} + + + +
+ {% if relationships %} + + + + + + + + + {% for rel in relationships %} + {% url 'parties:relationship_detail' object.organization.slug object.slug rel.id as relationship_url %} + + + + + {% endfor %} + +
{% trans "Relationship" %}{% trans "Location Type" %}
{{ rel.tenure_type_label }}{{ rel.spatial_unit.get_type_display }}
+ {% else %} +
+

{% trans "This party does not have any relationships and is not connected to any locations." %}

+
+ {% endif %}
- {% include 'resources/table.html' %} - {% else %} -
-

{% trans "This party does not have any attached resources. To attach a resource, select the button below." %}

-
+ + + +
+ {% if resource_list %} + + {% include 'resources/table.html' %} + {% else %} +
+

{% trans "This party does not have any attached resources. To attach a resource, select the button below." %}

+
+ {% if has_unattached_resources %} + + {% else %} + + {% endif %} + {% trans "Attach" %} +
+
+ {% endif %}
- {% endif %} - + +
@@ -77,3 +115,21 @@

{% trans "Resources" %}

{% endblock %} + +{% block extra_script %} +{{ block.super }} + +{% endblock %}