Skip to content

Commit

Permalink
do not display non visible data sets
Browse files Browse the repository at this point in the history
Fixes #237
  • Loading branch information
struan committed Jul 11, 2024
1 parent d3117a9 commit dbcc735
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
24 changes: 18 additions & 6 deletions hub/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion hub/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
36 changes: 36 additions & 0 deletions hub/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions hub/views/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion hub/views/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dbcc735

Please sign in to comment.