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",
|
||||
"pk": 1,
|
||||
"fields": {
|
||||
"name": "RURAL"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "api.habitat",
|
||||
"pk": 2,
|
||||
"fields": {
|
||||
"name": "URBAN"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "api.habitat",
|
||||
"pk": 2,
|
||||
"fields": {
|
||||
"name": "RIPARIAN"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "api.habitat",
|
||||
"pk": 3,
|
||||
"fields": {
|
||||
"name": "SALT MARSH/TIDAL LAGOONS & COASTAL BANKS"
|
||||
"name": "STORMWATER TREATMENT TRAIN"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "api.habitat",
|
||||
"pk": 4,
|
||||
"fields": {
|
||||
"name": "TRANSPORT CORRIDORS"
|
||||
"name": "OPEN WETLAND/LAKE"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
@ -38,35 +38,35 @@
|
|||
"model": "api.habitat",
|
||||
"pk": 6,
|
||||
"fields": {
|
||||
"name": "RIPARIAN"
|
||||
"name": "DUNE SYSTEM"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "api.habitat",
|
||||
"pk": 7,
|
||||
"fields": {
|
||||
"name": "MATAURANGA MAORI"
|
||||
"name": "SALT MARSH/TIDAL LAGOONS & COASTAL BANKS"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "api.habitat",
|
||||
"pk": 8,
|
||||
"fields": {
|
||||
"name": "OPEN WETLAND/LAKE"
|
||||
"name": "MATAURANGA MAORI"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "api.habitat",
|
||||
"pk": 9,
|
||||
"fields": {
|
||||
"name": "STORMWATER TREATMENT TRAIN"
|
||||
"name": "TRANSPORT CORRIDORS"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "api.habitat",
|
||||
"pk": 10,
|
||||
"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 pathlib import Path
|
||||
|
||||
import right_tree.api.data
|
||||
from ._spreadsheet_helpers import *
|
||||
from ._spreadsheet_constants import *
|
||||
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):
|
||||
""" 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
|
||||
|
||||
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
|
||||
|
||||
if habitat is not None:
|
||||
current_habitat = habitat
|
||||
current_zone = current_variant = current_refined_variant = None
|
||||
|
||||
current_zone = zone_name if zone_name is not None else current_zone
|
||||
current_variant = zone_variant if zone_variant is not None else current_variant
|
||||
if zone_name is not None:
|
||||
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
|
||||
|
||||
habitats.add(current_habitat)
|
||||
zones.add((current_zone, current_variant, current_refined_variant))
|
||||
|
||||
print("Creating Habitat Objects")
|
||||
print("--------------------------")
|
||||
for habitat in habitats:
|
||||
habitat_obj = Habitat(name=habitat)
|
||||
habitat_obj.save()
|
||||
|
||||
print("Creating Zone Objects")
|
||||
print("--------------------------")
|
||||
for zone, variant, refined_variant in zones:
|
||||
zone_obj = Zone(name=zone, variant=variant,
|
||||
refined_variant=refined_variant)
|
||||
|
@ -58,5 +47,4 @@ class Command(BaseCommand):
|
|||
help = 'Ingests the site spreadsheet data into the database'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
self.stdout.write('Creating habitat/zone fixtures...')
|
||||
load_habitat_zone_data(SPREADSHEET)
|
||||
|
|
Loading…
Reference in a new issue