Add command to initially load habitat and zone data

This commit is contained in:
Dana Lambert 2021-11-04 11:18:42 +13:00
parent 745b0b3007
commit 6b65ac77b3
3 changed files with 74 additions and 0 deletions

View file

@ -108,6 +108,7 @@ A summary of available commands are outlined below. Note that if the command req
| `reset_plants` | Performs the custom `resetplants` command in the backend container. This removes all plant entries from the database. | Yes
| `load_plant_fixtures` | Loads the `/backend/right_tree/api/data/fixtures/plants.json` fixture. Requires the `plants.json` file to be created (`./dev create_plant_fixtures`) and the plant table to be empty (`./dev reset_plants`). | Yes
| `load_plants` | Creates plants fixtures and loads them into a fresh plant table in the database. Requires the fixtures to be applied and shapefiles loaded. | Yes
| `load_sites_from_spreadsheet` | Loads site spreadsheet data the database initially (replaced with fixtures containing further information) | Yes
| `populate_database` | Populates the `right_tree` database with base data (fixtures), provided shapefiles and plant spreadsheet data. Requires the database to be created. | No
| `init_database` | Creates and populates the database | No
| `reset_database` | Removes, recreates and populates the database | No

View file

@ -0,0 +1,62 @@
from django.core.management.base import BaseCommand
from pathlib import Path
import right_tree.api.data
from ._spreadsheet_helpers 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.
"""
habitats = set()
zones = set()
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):
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
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)
zone_obj.save()
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)

11
dev
View file

@ -1,5 +1,11 @@
#!/bin/bash
# Load .env file if it exists
if [ -f .env ]
then
export $(cat .env | sed 's/#.*//g' | xargs)
fi
cmd_create_database() {
echo "Creating right_tree database..."
docker-compose down --remove-orphans --volumes
@ -53,6 +59,11 @@ cmd_load_plants() {
cmd_load_plant_fixtures
}
cmd_load_sites_from_spreadsheet() {
echo "Loading habitats and zones..."
docker-compose exec django-backend python manage.py loadsitedata
}
cmd_populate_database() {
echo "Populating the database..."
docker-compose up -d django-backend postgres