Fix load site data command ingestion logic and update fixtures
This commit is contained in:
parent
625c637f99
commit
2f165e4b38
3 changed files with 1185 additions and 1217 deletions
|
@ -2,29 +2,29 @@
|
||||||
{
|
{
|
||||||
"model": "api.habitat",
|
"model": "api.habitat",
|
||||||
"pk": 1,
|
"pk": 1,
|
||||||
"fields": {
|
|
||||||
"name": "RURAL"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"model": "api.habitat",
|
|
||||||
"pk": 2,
|
|
||||||
"fields": {
|
"fields": {
|
||||||
"name": "URBAN"
|
"name": "URBAN"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"model": "api.habitat",
|
||||||
|
"pk": 2,
|
||||||
|
"fields": {
|
||||||
|
"name": "RIPARIAN"
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"model": "api.habitat",
|
"model": "api.habitat",
|
||||||
"pk": 3,
|
"pk": 3,
|
||||||
"fields": {
|
"fields": {
|
||||||
"name": "SALT MARSH/TIDAL LAGOONS & COASTAL BANKS"
|
"name": "STORMWATER TREATMENT TRAIN"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"model": "api.habitat",
|
"model": "api.habitat",
|
||||||
"pk": 4,
|
"pk": 4,
|
||||||
"fields": {
|
"fields": {
|
||||||
"name": "TRANSPORT CORRIDORS"
|
"name": "OPEN WETLAND/LAKE"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -38,35 +38,35 @@
|
||||||
"model": "api.habitat",
|
"model": "api.habitat",
|
||||||
"pk": 6,
|
"pk": 6,
|
||||||
"fields": {
|
"fields": {
|
||||||
"name": "RIPARIAN"
|
"name": "DUNE SYSTEM"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"model": "api.habitat",
|
"model": "api.habitat",
|
||||||
"pk": 7,
|
"pk": 7,
|
||||||
"fields": {
|
"fields": {
|
||||||
"name": "MATAURANGA MAORI"
|
"name": "SALT MARSH/TIDAL LAGOONS & COASTAL BANKS"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"model": "api.habitat",
|
"model": "api.habitat",
|
||||||
"pk": 8,
|
"pk": 8,
|
||||||
"fields": {
|
"fields": {
|
||||||
"name": "OPEN WETLAND/LAKE"
|
"name": "MATAURANGA MAORI"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"model": "api.habitat",
|
"model": "api.habitat",
|
||||||
"pk": 9,
|
"pk": 9,
|
||||||
"fields": {
|
"fields": {
|
||||||
"name": "STORMWATER TREATMENT TRAIN"
|
"name": "TRANSPORT CORRIDORS"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"model": "api.habitat",
|
"model": "api.habitat",
|
||||||
"pk": 10,
|
"pk": 10,
|
||||||
"fields": {
|
"fields": {
|
||||||
"name": "DUNE SYSTEM"
|
"name": "RURAL"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
File diff suppressed because it is too large
Load diff
|
@ -1,21 +1,8 @@
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
|
|
||||||
from pathlib import Path
|
from ._spreadsheet_constants import *
|
||||||
|
|
||||||
import right_tree.api.data
|
|
||||||
from ._spreadsheet_helpers import *
|
|
||||||
from right_tree.api.models import Habitat, Zone
|
from right_tree.api.models import Habitat, Zone
|
||||||
|
|
||||||
SPREADSHEET_FILENAME = 'plant_data.xlsx'
|
|
||||||
SITE_DATA_START_COL = 25
|
|
||||||
HABITAT_ROW = 1
|
|
||||||
ZONE_NAME_ROW = 2
|
|
||||||
ZONE_VARIANT_ROW = 3
|
|
||||||
ZONE_REFINED_VARIANT_ROW = 5
|
|
||||||
|
|
||||||
DATA_DIR_PATH = Path(right_tree.api.data.__file__).resolve().parent
|
|
||||||
SPREADSHEET = get_spreadsheet(DATA_DIR_PATH, SPREADSHEET_FILENAME)
|
|
||||||
|
|
||||||
|
|
||||||
def load_habitat_zone_data(sheet):
|
def load_habitat_zone_data(sheet):
|
||||||
""" Loads habitat and zone objects from data defined in the spreadsheet.
|
""" Loads habitat and zone objects from data defined in the spreadsheet.
|
||||||
|
@ -26,28 +13,30 @@ def load_habitat_zone_data(sheet):
|
||||||
|
|
||||||
current_habitat = current_zone = current_variant = current_refined_variant = None
|
current_habitat = current_zone = current_variant = current_refined_variant = None
|
||||||
|
|
||||||
for col in sheet.iter_cols(min_col=SITE_DATA_START_COL, min_row=ZONE_NAME_ROW, max_row=ZONE_REFINED_VARIANT_ROW, values_only=True):
|
for col in sheet.iter_cols(min_col=SITE_DATA_START_COL, max_col=SITE_DATA_STOP_COL, min_row=HABITAT_ROW, max_row=ZONE_REFINED_VARIANT_ROW, values_only=True):
|
||||||
habitat, zone_name, zone_variant, zone_refined_variant = col
|
habitat, zone_name, zone_variant, zone_refined_variant = col
|
||||||
|
|
||||||
if habitat is not None:
|
if habitat is not None:
|
||||||
current_habitat = habitat
|
current_habitat = habitat
|
||||||
current_zone = current_variant = current_refined_variant = None
|
current_zone = current_variant = current_refined_variant = None
|
||||||
|
|
||||||
current_zone = zone_name if zone_name is not None else current_zone
|
if zone_name is not None:
|
||||||
current_variant = zone_variant if zone_variant is not None else current_variant
|
current_zone = zone_name
|
||||||
|
current_variant = current_refined_variant = None
|
||||||
|
|
||||||
|
if zone_variant is not None:
|
||||||
|
current_variant = zone_variant
|
||||||
|
current_refined_variant = None
|
||||||
|
|
||||||
current_refined_variant = zone_refined_variant if zone_refined_variant is not None else current_refined_variant
|
current_refined_variant = zone_refined_variant if zone_refined_variant is not None else current_refined_variant
|
||||||
|
|
||||||
habitats.add(current_habitat)
|
habitats.add(current_habitat)
|
||||||
zones.add((current_zone, current_variant, current_refined_variant))
|
zones.add((current_zone, current_variant, current_refined_variant))
|
||||||
|
|
||||||
print("Creating Habitat Objects")
|
|
||||||
print("--------------------------")
|
|
||||||
for habitat in habitats:
|
for habitat in habitats:
|
||||||
habitat_obj = Habitat(name=habitat)
|
habitat_obj = Habitat(name=habitat)
|
||||||
habitat_obj.save()
|
habitat_obj.save()
|
||||||
|
|
||||||
print("Creating Zone Objects")
|
|
||||||
print("--------------------------")
|
|
||||||
for zone, variant, refined_variant in zones:
|
for zone, variant, refined_variant in zones:
|
||||||
zone_obj = Zone(name=zone, variant=variant,
|
zone_obj = Zone(name=zone, variant=variant,
|
||||||
refined_variant=refined_variant)
|
refined_variant=refined_variant)
|
||||||
|
@ -58,5 +47,4 @@ class Command(BaseCommand):
|
||||||
help = 'Ingests the site spreadsheet data into the database'
|
help = 'Ingests the site spreadsheet data into the database'
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
self.stdout.write('Creating habitat/zone fixtures...')
|
|
||||||
load_habitat_zone_data(SPREADSHEET)
|
load_habitat_zone_data(SPREADSHEET)
|
||||||
|
|
Loading…
Reference in a new issue