diff --git a/hub/mixins.py b/hub/mixins.py index 6d550acee..e9d1415d1 100644 --- a/hub/mixins.py +++ b/hub/mixins.py @@ -42,7 +42,9 @@ def filters(self): value = value.split(",") try: - dataset = DataSet.objects.get(name=name, areas_available=area_type) + dataset = DataSet.objects.get( + name=name, areas_available=area_type, visible=True + ) if is_non_member and not dataset.is_public: continue filters.append( @@ -57,7 +59,9 @@ def filters(self): ) except DataSet.DoesNotExist: # pragma: nocover try: - datatype = DataType.objects.get(name=name, area_type=area_type) + datatype = DataType.objects.get( + name=name, area_type=area_type, data_set__visible=True + ) filters.append( { "dataset": datatype.data_set, @@ -92,7 +96,9 @@ def columns(self, mp_name=False): continue try: - dataset = DataSet.objects.get(name=col, areas_available=area_type) + dataset = DataSet.objects.get( + name=col, areas_available=area_type, visible=True + ) if is_non_member and not dataset.is_public: continue columns.append( @@ -106,7 +112,9 @@ def columns(self, mp_name=False): ) except DataSet.DoesNotExist: try: - datatype = DataType.objects.get(name=col, area_type=area_type) + datatype = DataType.objects.get( + name=col, area_type=area_type, data_set__visible=True + ) columns.append( { "dataset": datatype.data_set, @@ -289,9 +297,13 @@ def shader(self): name = self.request.GET.get("shader") area_type = self.area_type() try: - return DataSet.objects.get(name=name, areas_available=area_type) + return DataSet.objects.get( + name=name, areas_available=area_type, visible=True + ) except DataSet.DoesNotExist: try: - return DataType.objects.get(name=name, area_type=area_type) + return DataType.objects.get( + name=name, area_type=area_type, data_set__visible=True + ) except DataType.DoesNotExist: return None diff --git a/hub/tests/test_admin.py b/hub/tests/test_admin.py index b9a53db4e..51436d5c6 100644 --- a/hub/tests/test_admin.py +++ b/hub/tests/test_admin.py @@ -53,4 +53,4 @@ def test_dataset_field_availability(self): context = response.context form = context["adminform"] - self.assertEqual(len(form.fields), 17) + self.assertEqual(len(form.fields), 18) diff --git a/hub/tests/test_views.py b/hub/tests/test_views.py index 5538d3f00..11d4aeb71 100644 --- a/hub/tests/test_views.py +++ b/hub/tests/test_views.py @@ -158,6 +158,23 @@ def test_explore_view_extra_columns_new_cons_diff_numbers(self): output_csv, ) + def test_explore_view_extra_column_not_visible(self): + ds = DataSet.objects.get(name="constituency_polling_data") + ds.visible = False + ds.save() + + output_csv = str.encode( + "Constituency Name,APPG membership\r\nSouth Borsetshire,MadeUpAPPG; MadeUpAPPG2\r\n" + ) + + url = f"{reverse('explore_csv')}?mp_appg_membership__exact=MadeUpAPPG&columns=wind_support" + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + self.assertEqual( + response.content, + output_csv, + ) + class TestExploreFilteringPage(TestCase): fixtures = [ @@ -421,6 +438,25 @@ def test_area_page_multiple_mps(self): mp = context["mp"] self.assertEqual(mp["person"].name, "James Madeupname") + def test_area_page_hidden_datasets(self): + DataSet.objects.update(featured=True) + for name in ["constituency_fuel_poverty", "mp_election_majority"]: + ds = DataSet.objects.get(name=name) + ds.visible = False + ds.save() + + url = reverse("area", args=("WMC", "South Borsetshire")) + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + + context = response.context + mp = context["mp"] + self.assertEqual(len(mp.keys()), 8) + self.assertIsNone(mp.get("mp_election_majority")) + + places = context["categories"]["place"] + self.assertEqual(len(places), 1) + class TestAreaSearchPage(TestCase): fixtures = ["areas.json", "areas_23.json", "mps_23.json"] diff --git a/hub/views/area.py b/hub/views/area.py index 9635ac2cd..be25bd50f 100644 --- a/hub/views/area.py +++ b/hub/views/area.py @@ -231,7 +231,7 @@ def get_context_data(self, **kwargs): context["no_mp"] = False data = PersonData.objects.filter( - person=context["mp"]["person"] + data_type__data_set__visible=True, person=context["mp"]["person"] ).select_related("data_type") area_type_q = Q(data_type__area_type=area_type) | Q( @@ -326,7 +326,7 @@ def get_context_data(self, **kwargs): favs = self.get_user_favourite_datasets() auto_converted = self.get_auto_convered_datasets() data_sets = DataSet.objects.order_by("order", "label").filter( - areas_available=self.object.area_type + areas_available=self.object.area_type, visible=True ) if is_non_member: diff --git a/hub/views/explore.py b/hub/views/explore.py index 733414057..61eec7f8c 100644 --- a/hub/views/explore.py +++ b/hub/views/explore.py @@ -60,7 +60,7 @@ def render_to_response(self, context, **response_kwargs): is_non_member = self.request.user.is_anonymous datasets = [] - for d in DataSet.objects.all(): + for d in DataSet.objects.filter(visible=True): try: options = list(map(itemgetter("title"), d.options)) # catch bad options and ignore them for now