Skip to content

Commit 066b76e

Browse files
committed
Add clean redundant tabular items function + add migration to fix AC
names
1 parent b7daec6 commit 066b76e

File tree

8 files changed

+100
-20
lines changed

8 files changed

+100
-20
lines changed

vbos/datasets/admin.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from django.shortcuts import redirect, render, reverse
99
from django.urls import path
1010

11-
from vbos.datasets.utils import GeoJSONProperties
11+
from vbos.datasets.utils import GeoJSONProperties, clean_redundant_tabular_items
1212

1313
from .forms import CSVUploadForm, GeoJSONUploadForm
1414
from .models import (
@@ -137,6 +137,21 @@ def import_file(self, request):
137137
class TabularDatasetAdmin(admin.ModelAdmin):
138138
list_display = ["id", "name", "cluster", "type", "updated"]
139139
list_filter = ["cluster", "type"]
140+
actions = ["clean_redundant_items"]
141+
142+
@admin.action(description="Clean redundant TabularItems for dataset")
143+
def clean_redundant_items(self, request, queryset):
144+
for dataset in queryset:
145+
clean_redundant_tabular_items(dataset)
146+
147+
dataset_names = list(queryset.values_list("name", flat=True))
148+
if len(dataset_names) == 1:
149+
message = f"Cleaned redundant values for: {dataset_names[0]}."
150+
else:
151+
# Join all but last with commas, then add "and" before last item
152+
message = f"Cleaned redundant values for: {', '.join(dataset_names[:-1])} and {dataset_names[-1]}."
153+
154+
messages.success(request, message)
140155

141156

142157
@admin.register(TabularItem)

vbos/datasets/fixtures/area-councils.geojson

Lines changed: 5 additions & 5 deletions
Large diffs are not rendered by default.

vbos/datasets/fixtures/master-import.csv

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,3 @@ Year,Month,Day,Type,Cluster,Indicator,Attribute,Hazard Type,Hazard Response & Re
5454
2024,,,Baseline,Health,Health Facility,hospital,,,,,,,,,,Vanuatu,Tafea,,,number,Ministry of Health,2024,Annually,1
5555
2024,,,Baseline,Health,Health Facility,dispensary,,,,,,,,,,Vanuatu,Tafea,,,number,Ministry of Health,2024,Annually,12
5656
2024,,,Baseline,Health,Health Facility,aidpost,,,,,,,,,,Vanuatu,Tafea,,,number,Ministry of Health,2024,Annually,35
57-
2024,,,Baseline,Health,Health Facility,hospital,,,,,,,,,,Vanuatu,Torba,East Vanualava,,number,Ministry of Health,2024,Annually,1
58-
2024,,,Baseline,Health,Health Facility,dispensary,,,,,,,,,,Vanuatu,Torba,West Vanualava,,number,Ministry of Health,2024,Annually,1
59-
2024,,,Baseline,Health,Health Facility,aidpost,,,,,,,,,,Vanuatu,Torba,East Gaua,,number,Ministry of Health,2024,Annually,4
60-
2024,,,Baseline,Health,Health Facility,aidpost,,,,,,,,,,Vanuatu,Torba,East Vanualava,,number,Ministry of Health,2024,Annually,3
61-
2024,,,Baseline,Health,Health Facility,aidpost,,,,,,,,,,Vanuatu,Torba,Merelava,,number,Ministry of Health,2024,Annually,1
62-
2024,,,Baseline,Health,Health Facility,aidpost,,,,,,,,,,Vanuatu,Torba,Motalava,,number,Ministry of Health,2024,Annually,2
63-
2024,,,Baseline,Health,Health Facility,aidpost,,,,,,,,,,Vanuatu,Torba,Torres,,number,Ministry of Health,2024,Annually,2
64-
2024,,,Baseline,Health,Health Facility,aidpost,,,,,,,,,,Vanuatu,Torba,Ureparapara,,number,Ministry of Health,2024,Annually,1
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from django.core.management.base import BaseCommand
2+
3+
from ...models import TabularDataset
4+
from ...utils import clean_redundant_tabular_items
5+
6+
7+
class Command(BaseCommand):
8+
help = """Clean TabularItem data, removing entries that are redundant."""
9+
10+
def handle(self, *args, **options):
11+
datasets = TabularDataset.objects.all()
12+
13+
for d in datasets:
14+
clean_redundant_tabular_items(d)
15+
16+
self.stdout.write(f"Removed redundant {d.name} tabular items.")

vbos/datasets/management/commands/import_datasets.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
from django.core.management.base import BaseCommand
44

5-
from ...models import TYPE_CHOICES, Cluster, TabularDataset
6-
7-
REVERSE_TYPE_MAPPING = {str(label): value for (value, label) in TYPE_CHOICES.items()}
5+
from ...models import Cluster, TabularDataset
6+
from ...utils import REVERSE_TYPE_MAPPING
87

98

109
class Command(BaseCommand):
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Generated by Django 5.2.5 on 2025-11-14 00:18
2+
3+
from django.db import migrations
4+
5+
6+
def update_area_councils_names(apps, schema_editor):
7+
AreaCouncil = apps.get_model("datasets", "AreaCouncil")
8+
updates = [
9+
["Bigbay Coast", "Big Bay Coast"],
10+
["Bigbay Inland", "Big Bay Inland"],
11+
["East Vanua Lava", "East Vanualava"],
12+
["West Vanua Lava", "West Vanualava"],
13+
["West Vanua Lava", "West Vanualava"],
14+
["Canal - Fanafo", "Canal Fanafo"],
15+
]
16+
for entry in updates:
17+
try:
18+
e = AreaCouncil.objects.get(name=entry[0])
19+
e.name = entry[1]
20+
e.save()
21+
except AreaCouncil.DoesNotExist:
22+
print("AreaCouncil: {entry[0]} not found")
23+
24+
25+
class Migration(migrations.Migration):
26+
dependencies = [
27+
("datasets", "0016_alter_rasterdataset_unique_together_and_more"),
28+
]
29+
30+
operations = [
31+
migrations.RunPython(
32+
update_area_councils_names, reverse_code=migrations.RunPython.noop
33+
),
34+
]

vbos/datasets/test/test_management_commands.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,25 @@ def test_import(self):
3232
TabularDataset.objects.get(name="Health Facility").source,
3333
"Ministry of Health",
3434
)
35-
self.assertEqual(TabularItem.objects.count(), 63)
35+
# Test TabularItem import
36+
self.assertEqual(TabularItem.objects.count(), 55)
3637
self.assertEqual(
3738
TabularItem.objects.filter(dataset__name="Number Schools").count(), 31
3839
)
3940
self.assertEqual(
40-
TabularItem.objects.filter(dataset__name="Health Facility").count(), 32
41+
TabularItem.objects.filter(dataset__name="Health Facility").count(), 24
4142
)
4243
self.assertIn(
43-
"63 tabular items created",
44+
"55 tabular items created",
4445
self.out.getvalue(),
4546
)
47+
48+
# Clean redundant entries
49+
call_command("clean_tabular_data", stdout=self.out)
50+
self.assertEqual(TabularItem.objects.count(), 30)
51+
self.assertEqual(
52+
TabularItem.objects.filter(dataset__name="Number Schools").count(), 10
53+
)
54+
self.assertEqual(
55+
TabularItem.objects.filter(dataset__name="Health Facility").count(), 20
56+
)

vbos/datasets/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,16 @@ def create_tabular_item(csv_row: CSVRow, dataset: TabularDataset):
137137
name__iexact=csv_row.area_council
138138
).first(),
139139
)
140+
141+
142+
def clean_redundant_tabular_items(dataset: TabularDataset):
143+
items = TabularItem.objects.filter(dataset=dataset)
144+
# If a dataset has items with an Area Council value,
145+
# remove all items that don't have the Area Council set
146+
if items.filter(area_council__isnull=False).count() > 0:
147+
items.filter(area_council__isnull=True).delete()
148+
149+
# If a dataset has items with a Province value,
150+
# remove all items that don't have the Province set
151+
if items.filter(province__isnull=False).count() > 0:
152+
items.filter(province__isnull=True).delete()

0 commit comments

Comments
 (0)