Skip to content

Commit

Permalink
Make changes to pass tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alexander-griffen committed Aug 3, 2023
1 parent 7dd9686 commit 290f052
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 15 deletions.
16 changes: 13 additions & 3 deletions hub/fixtures/areas.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
[
{
"model": "hub.areatype",
"pk": 1,
"fields": {
"name": "Constituency",
"code": "WMC",
"area_type": "Constituency",
"description": "Constituency"
}
},
{
"model": "hub.area",
"pk": 1,
"fields": {
"gss": "E10000001",
"mapit_id": "1",
"name": "South Borsetshire",
"area_type": "WMC",
"area_type_id": 1,
"geometry": "{\"name\": \"South Borsetshire\", \"properties\": {}}"
}
},
Expand All @@ -17,7 +27,7 @@
"gss": "E10000002",
"mapit_id": "2",
"name": "Borsetshire West",
"area_type": "WMC",
"area_type_id": 1,
"geometry": "{\"name\": \"Borsetshire West\", \"properties\": {}}"
}
},
Expand All @@ -28,7 +38,7 @@
"gss": "E10000003",
"mapit_id": "3",
"name": "Borsetshire East",
"area_type": "WMC",
"area_type_id": 1,
"geometry": "{\"name\": \"Borsetshire East\", \"properties\": {}}"
}
}
Expand Down
17 changes: 13 additions & 4 deletions hub/fixtures/duplicate_mps.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,24 @@
"area": 2
}
},
{
"model": "hub.areatype",
"pk": 1,
"fields": {
"name": "Constituency",
"code": "WMC",
"area_type": "Constituency",
"description": "Constituency"
}
},
{
"model": "hub.area",
"pk": 1,
"fields": {
"gss": "E10000001",
"mapit_id": "1",
"name": "South Borsetshire",
"area_type": "WMC",
"area_type_id": 1,
"geometry": "{\"name\": \"South Borsetshire\", \"properties\": {}}"
}
},
Expand All @@ -50,7 +60,7 @@
"gss": "E10000002",
"mapit_id": "2",
"name": "North Borsetshire",
"area_type": "WMC",
"area_type_id": 1,
"geometry": "{\"name\": \"North Borsetshire\", \"properties\": {}}"
}
},
Expand Down Expand Up @@ -106,5 +116,4 @@
}
}
]



11 changes: 9 additions & 2 deletions hub/management/commands/import_areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from tqdm import tqdm

from hub.models import Area
from hub.models import Area, AreaType
from utils import mapit


Expand All @@ -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):
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hub/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions hub/tests/test_import_area_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 12 additions & 2 deletions hub/views/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion hub/views/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 290f052

Please sign in to comment.