Skip to content

Commit

Permalink
Correctly display layer area data in tables
Browse files Browse the repository at this point in the history
  • Loading branch information
janbaykara committed Jan 8, 2025
1 parent ae23ad0 commit 93ae656
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 58 deletions.
29 changes: 20 additions & 9 deletions hub/graphql/types/model_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1509,18 +1509,29 @@ def generic_data_by_external_data_source(
)

def __get_points_for_area_and_external_data_source(
external_data_source: models.ExternalDataSource, gss: str
external_data_source: models.ExternalDataSource, gss: str, points: bool = True, rollup: bool = True
) -> List[GenericData]:
area = models.Area.objects.get(gss=gss)
data_source_records = external_data_source.get_import_data()
filters = Q(point__within=area.polygon)
postcode_io_key = lih_to_postcodes_io_key_map.get(area.area_type.code, None)
if postcode_io_key:
filters |= Q(**{f"postcode_data__codes__{postcode_io_key.value}": area.gss})

filters = Q()

if points:
filters |= Q(point__within=area.polygon)

if rollup:
# Find all data related to this area or within it — e.g. wards that share this council GSS
postcode_io_key = lih_to_postcodes_io_key_map.get(area.area_type.code, None)
if postcode_io_key:
filters |= Q(**{f"postcode_data__codes__{postcode_io_key.value}": area.gss})
else:
# Find only data specifically related to this GSS area — not about its children
filters |= Q(area__gss=area.gss)

return data_source_records.filter(filters)

@strawberry_django.field()
def generic_data_from_source_about_area(info: Info, source_id: str, gss: str) -> List[GenericData]:
def generic_data_from_source_about_area(info: Info, source_id: str, gss: str, points: bool = True, rollup: bool = True) -> List[GenericData]:
user = get_current_user(info)
# Check user can access the external data source
external_data_source = models.ExternalDataSource.objects.get(pk=source_id)
Expand All @@ -1530,12 +1541,12 @@ def generic_data_from_source_about_area(info: Info, source_id: str, gss: str) ->
):
raise ValueError(f"User {user} does not have permission to view this external data source's data")

qs = __get_points_for_area_and_external_data_source(external_data_source, gss)
qs = __get_points_for_area_and_external_data_source(external_data_source, gss, points, rollup)

return qs

@strawberry_django.field()
def generic_data_summary_from_source_about_area(info: Info, source_id: str, gss: str) -> JSON:
def generic_data_summary_from_source_about_area(info: Info, source_id: str, gss: str, points: bool = True, rollup: bool = True) -> JSON:
user = get_current_user(info)
# Check user can access the external data source
external_data_source = models.ExternalDataSource.objects.get(pk=source_id)
Expand All @@ -1545,7 +1556,7 @@ def generic_data_summary_from_source_about_area(info: Info, source_id: str, gss:
):
raise ValueError(f"User {user} does not have permission to view this external data source's data")

qs = __get_points_for_area_and_external_data_source(external_data_source, gss)
qs = __get_points_for_area_and_external_data_source(external_data_source, gss, points, rollup)

# ingest the .json data into a pandas dataframe
df = pd.DataFrame([record.json for record in qs])
Expand Down
68 changes: 68 additions & 0 deletions hub/migrations/0152_genericdata_area_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Generated by Django 4.2.17 on 2025-01-08 23:29

from django.db import migrations, models
import django.db.models.deletion
import django_jsonform.models.fields


def backfill_areas_to_generic_data(apps, schema_editor):
# Areas that have been geocoded by the geocoder will have a geocode_data field with data like
# {
# "data": {
# "area_fields": {
# "STC": "E06000014",
# "WD23": "E05010327"
# }
# }
# }
# The last key in the area_fields dict is the area type, and the value is the area code.
# We can use this to find the area and set it on the generic data.
GenericData = apps.get_model("hub", "GenericData")
Area = apps.get_model("hub", "Area")
for generic_data in GenericData.objects.filter(
area=None,
geocode_data__data__area_fields__isnull=False,
# i.e. it has been geocoded successfully
postcode_data__isnull=False
):
area_fields: dict = generic_data.geocode_data.get("data", {}).get("area_fields", {})
if len(area_fields.keys()) > 0:
area_type, gss = area_fields.popitem()
area = Area.objects.filter(gss=gss).first()
if area:
generic_data.area = area
generic_data.save()


class Migration(migrations.Migration):

dependencies = [
("hub", "0151_area_mapit_generation_high_area_mapit_generation_low"),
]

operations = [
migrations.AddField(
model_name="genericdata",
name="area",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="generic_data",
to="hub.area",
),
),
migrations.AlterField(
model_name="externaldatasource",
name="geocoding_config",
field=django_jsonform.models.fields.JSONField(default=dict),
),
migrations.AlterField(
model_name="genericdata",
name="geocoder",
field=models.CharField(blank=True, max_length=1000, null=True),
),
migrations.RunPython(
code=backfill_areas_to_generic_data
)
]
1 change: 1 addition & 0 deletions hub/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ class GenericData(CommonData):
last_update = models.DateTimeField(auto_now=True)
point = PointField(srid=4326, blank=True, null=True)
polygon = MultiPolygonField(srid=4326, blank=True, null=True)
area = models.ForeignKey("Area", blank=True, null=True, on_delete=models.SET_NULL, related_name="generic_data")
postcode_data = JSONField(blank=True, null=True)
postcode = models.CharField(max_length=1000, blank=True, null=True)
first_name = models.CharField(max_length=300, blank=True, null=True)
Expand Down
8 changes: 4 additions & 4 deletions nextjs/src/__generated__/gql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 93ae656

Please sign in to comment.