From 9ee76d862fe7fa9578dd878cbabafd2d2dc73ad1 Mon Sep 17 00:00:00 2001 From: Eugene Alvin Villar Date: Fri, 23 Sep 2016 19:44:42 +0800 Subject: [PATCH 1/2] Fix #674: Fix resource count on project dashboard --- .../tests/test_views_default_projects.py | 33 +++++++++++++++++-- cadasta/organization/views/default.py | 2 +- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/cadasta/organization/tests/test_views_default_projects.py b/cadasta/organization/tests/test_views_default_projects.py index 6d889dfd9..b1fb4a1de 100644 --- a/cadasta/organization/tests/test_views_default_projects.py +++ b/cadasta/organization/tests/test_views_default_projects.py @@ -22,8 +22,12 @@ from questionnaires.tests.factories import QuestionnaireFactory from questionnaires.tests.utils import get_form from questionnaires.models import Questionnaire +from resources.tests.factories import ResourceFactory from resources.tests.utils import clear_temp # noqa from resources.utils.io import ensure_dirs +from spatial.serializers import SpatialUnitGeoJsonSerializer +from spatial.tests.factories import SpatialUnitFactory +from party.tests.factories import PartyFactory from .. import forms from ..views import default @@ -202,12 +206,22 @@ def setup_models(self): self.user.assign_policies(self.policy) def setup_template_context(self): + num_locations = self.project.spatial_units.count() + num_parties = self.project.parties.count() + num_resources = self.project.resource_set.filter( + archived=False).count() return { 'object': self.project, 'project': self.project, - 'geojson': '{"type": "FeatureCollection", "features": []}', + 'geojson': json.dumps(SpatialUnitGeoJsonSerializer( + self.project.spatial_units.all(), many=True).data), 'is_superuser': False, - 'is_administrator': False + 'is_administrator': False, + 'has_content': ( + num_locations > 0 or num_parties > 0 or num_resources > 0), + 'num_locations': num_locations, + 'num_parties': num_parties, + 'num_resources': num_resources, } def setup_url_kwargs(self): @@ -363,6 +377,21 @@ def test_get_archived_project_with_org_admin(self): assert response.status_code == 200 assert response.content == self.render_content(is_administrator=True) + def test_get_with_overview_stats(self): + SpatialUnitFactory.create(project=self.project) + SpatialUnitFactory.create(project=self.project) + PartyFactory.create(project=self.project) + PartyFactory.create(project=self.project) + PartyFactory.create(project=self.project) + ResourceFactory.create(project=self.project) + ResourceFactory.create(project=self.project, archived=True) + response = self.request(user=self.user) + assert response.status_code == 200 + assert response.content == self.expected_content + assert "
2
locations" in response.content + assert "
3
parties" in response.content + assert "
1
resource" in response.content + @pytest.mark.usefixtures('make_dirs') class ProjectAddTest(UserTestCase, TestCase): diff --git a/cadasta/organization/views/default.py b/cadasta/organization/views/default.py index 63ead2b12..e31bbd681 100644 --- a/cadasta/organization/views/default.py +++ b/cadasta/organization/views/default.py @@ -361,7 +361,7 @@ def get_context_data(self, **kwargs): context = super(ProjectDashboard, self).get_context_data(**kwargs) num_locations = self.object.spatial_units.count() num_parties = self.object.parties.count() - num_resources = self.object.resource_set.count() + num_resources = self.object.resource_set.filter(archived=False).count() context['has_content'] = ( num_locations > 0 or num_parties > 0 or num_resources > 0) context['num_locations'] = num_locations From 410665b04e426bdfa130eeee761b12857a75aafc Mon Sep 17 00:00:00 2001 From: Eugene Alvin Villar Date: Fri, 23 Sep 2016 20:57:12 +0800 Subject: [PATCH 2/2] Resolve review feedback --- .../tests/test_views_default_projects.py | 29 +++++++------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/cadasta/organization/tests/test_views_default_projects.py b/cadasta/organization/tests/test_views_default_projects.py index b1fb4a1de..925ebfffe 100644 --- a/cadasta/organization/tests/test_views_default_projects.py +++ b/cadasta/organization/tests/test_views_default_projects.py @@ -206,22 +206,12 @@ def setup_models(self): self.user.assign_policies(self.policy) def setup_template_context(self): - num_locations = self.project.spatial_units.count() - num_parties = self.project.parties.count() - num_resources = self.project.resource_set.filter( - archived=False).count() return { 'object': self.project, 'project': self.project, - 'geojson': json.dumps(SpatialUnitGeoJsonSerializer( - self.project.spatial_units.all(), many=True).data), + 'geojson': '{"type": "FeatureCollection", "features": []}', 'is_superuser': False, 'is_administrator': False, - 'has_content': ( - num_locations > 0 or num_parties > 0 or num_resources > 0), - 'num_locations': num_locations, - 'num_parties': num_parties, - 'num_resources': num_resources, } def setup_url_kwargs(self): @@ -378,18 +368,21 @@ def test_get_archived_project_with_org_admin(self): assert response.content == self.render_content(is_administrator=True) def test_get_with_overview_stats(self): - SpatialUnitFactory.create(project=self.project) - SpatialUnitFactory.create(project=self.project) - PartyFactory.create(project=self.project) - PartyFactory.create(project=self.project) + su = SpatialUnitFactory.create(project=self.project) PartyFactory.create(project=self.project) ResourceFactory.create(project=self.project) ResourceFactory.create(project=self.project, archived=True) + geojson = json.dumps( + SpatialUnitGeoJsonSerializer([su], many=True).data) response = self.request(user=self.user) assert response.status_code == 200 - assert response.content == self.expected_content - assert "
2
locations" in response.content - assert "
3
parties" in response.content + assert response.content == self.render_content(geojson=geojson, + has_content=True, + num_locations=1, + num_parties=1, + num_resources=1) + assert "
1
location" in response.content + assert "
1
party" in response.content assert "
1
resource" in response.content