From 290f052abe221fe82a4c7a45bd0db5bde7df41e4 Mon Sep 17 00:00:00 2001 From: Alexander Griffen Date: Thu, 3 Aug 2023 15:38:08 +0100 Subject: [PATCH] Make changes to pass tests This commit is a combination of altering tests and fixtures to accomodate schema changes, as well as altering some of the views themselves to accomodate these changes. --- hub/fixtures/areas.json | 16 +++++++++++++--- hub/fixtures/duplicate_mps.json | 17 +++++++++++++---- hub/management/commands/import_areas.py | 11 +++++++++-- hub/models.py | 2 +- hub/tests/test_import_area_data.py | 5 +++-- hub/views/area.py | 14 ++++++++++++-- hub/views/explore.py | 2 +- 7 files changed, 52 insertions(+), 15 deletions(-) diff --git a/hub/fixtures/areas.json b/hub/fixtures/areas.json index 0571480a5..0ecb0603f 100644 --- a/hub/fixtures/areas.json +++ b/hub/fixtures/areas.json @@ -1,4 +1,14 @@ [ + { + "model": "hub.areatype", + "pk": 1, + "fields": { + "name": "Constituency", + "code": "WMC", + "area_type": "Constituency", + "description": "Constituency" + } + }, { "model": "hub.area", "pk": 1, @@ -6,7 +16,7 @@ "gss": "E10000001", "mapit_id": "1", "name": "South Borsetshire", - "area_type": "WMC", + "area_type_id": 1, "geometry": "{\"name\": \"South Borsetshire\", \"properties\": {}}" } }, @@ -17,7 +27,7 @@ "gss": "E10000002", "mapit_id": "2", "name": "Borsetshire West", - "area_type": "WMC", + "area_type_id": 1, "geometry": "{\"name\": \"Borsetshire West\", \"properties\": {}}" } }, @@ -28,7 +38,7 @@ "gss": "E10000003", "mapit_id": "3", "name": "Borsetshire East", - "area_type": "WMC", + "area_type_id": 1, "geometry": "{\"name\": \"Borsetshire East\", \"properties\": {}}" } } diff --git a/hub/fixtures/duplicate_mps.json b/hub/fixtures/duplicate_mps.json index 87101c4d7..7c8303ba2 100644 --- a/hub/fixtures/duplicate_mps.json +++ b/hub/fixtures/duplicate_mps.json @@ -32,6 +32,16 @@ "area": 2 } }, + { + "model": "hub.areatype", + "pk": 1, + "fields": { + "name": "Constituency", + "code": "WMC", + "area_type": "Constituency", + "description": "Constituency" + } + }, { "model": "hub.area", "pk": 1, @@ -39,7 +49,7 @@ "gss": "E10000001", "mapit_id": "1", "name": "South Borsetshire", - "area_type": "WMC", + "area_type_id": 1, "geometry": "{\"name\": \"South Borsetshire\", \"properties\": {}}" } }, @@ -50,7 +60,7 @@ "gss": "E10000002", "mapit_id": "2", "name": "North Borsetshire", - "area_type": "WMC", + "area_type_id": 1, "geometry": "{\"name\": \"North Borsetshire\", \"properties\": {}}" } }, @@ -106,5 +116,4 @@ } } ] - - \ No newline at end of file + diff --git a/hub/management/commands/import_areas.py b/hub/management/commands/import_areas.py index 19347db6c..982f55742 100644 --- a/hub/management/commands/import_areas.py +++ b/hub/management/commands/import_areas.py @@ -4,7 +4,7 @@ from tqdm import tqdm -from hub.models import Area +from hub.models import Area, AreaType from utils import mapit @@ -19,6 +19,13 @@ def add_arguments(self, parser): def handle(self, quiet: bool = False, *args, **options): mapit_client = mapit.MapIt() areas = mapit_client.areas_of_type(["WMC"]) + area_type, created = AreaType.objects.get_or_create( + name="2010 Parliamentary Constituency", + code="WMC", + area_type="Westminster Constituency", + description="Westminster Parliamentary Constituency boundaries, as created in 2010", + ) + if not quiet: print("Importing Areas") for area in tqdm(areas, disable=quiet): @@ -42,7 +49,7 @@ def handle(self, quiet: bool = False, *args, **options): mapit_id=area["id"], gss=area["codes"]["gss"], name=area["name"], - area_type="WMC", + area_type=area_type, ) a.geometry = geom diff --git a/hub/models.py b/hub/models.py index 5cd767cc4..72fec8e57 100644 --- a/hub/models.py +++ b/hub/models.py @@ -486,7 +486,7 @@ def __str__(self): return self.name def get_absolute_url(self): - return f"/area/{self.area_type}/{self.name}" + return f"/area/{self.area_type.code}/{self.name}" def get_value(self, dataset): area = self diff --git a/hub/tests/test_import_area_data.py b/hub/tests/test_import_area_data.py index ef6e16b69..558355836 100644 --- a/hub/tests/test_import_area_data.py +++ b/hub/tests/test_import_area_data.py @@ -7,7 +7,7 @@ import pandas as pd from hub.management.commands.base_importers import BaseAreaImportCommand -from hub.models import Area, AreaData, DataSet, DataType +from hub.models import Area, AreaData, AreaType, DataSet, DataType class ImportTestCase(TestCase): @@ -46,12 +46,13 @@ def setUp(self): }, } } + area_type = AreaType.objects.get(code="WMC") for i, area_name in enumerate("ABCD"): Area.objects.create( mapit_id=i, gss=i, name=area_name, - area_type="WMC", + area_type=area_type, ) self.command.add_data_sets() diff --git a/hub/views/area.py b/hub/views/area.py index 6f8d15c71..aa30034b2 100644 --- a/hub/views/area.py +++ b/hub/views/area.py @@ -6,7 +6,15 @@ from django.views.generic import DetailView, TemplateView, View from hub.mixins import TitleMixin -from hub.models import Area, AreaData, DataSet, Person, PersonData, UserDataSets +from hub.models import ( + Area, + AreaData, + AreaType, + DataSet, + Person, + PersonData, + UserDataSets, +) from utils import is_valid_postcode from utils.mapit import ( BadRequestException, @@ -30,7 +38,9 @@ class BaseAreaView(TitleMixin, DetailView): def get_object(self): return get_object_or_404( - Area, area_type=self.kwargs.get("area_type"), name=self.kwargs.get("name") + Area, + area_type=AreaType.objects.get(code=self.kwargs.get("area_type")), + name=self.kwargs.get("name"), ) def get_page_title(self): diff --git a/hub/views/explore.py b/hub/views/explore.py index 691db283c..c6e1168f3 100644 --- a/hub/views/explore.py +++ b/hub/views/explore.py @@ -160,7 +160,7 @@ def render_to_response(self, context, **response_kwargs): "properties": { "PCON13CD": area_obj.gss, "name": area_obj.name, - "type": area_obj.area_type, + "type": area_obj.area_type.name, } } for k in area.keys():