|
| 1 | +import json |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import boto3 |
| 5 | +import environ |
| 6 | +from distributions.export_models import ExportDataset |
| 7 | +from distributions.export_models import ExportProvider |
| 8 | +from distributions.models import Dataset |
| 9 | +from provider.models import Provider |
| 10 | +from utils.command import CustomBaseCommand |
| 11 | + |
| 12 | +from django.core import serializers |
| 13 | +from django.core.management.base import CommandParser |
| 14 | + |
| 15 | +env = environ.Env() |
| 16 | +boto3.setup_default_session(profile_name="swisstopo-swissgeo-dev") |
| 17 | + |
| 18 | +SAMPLE_IDS = [ |
| 19 | + "ch.bafu.schutzgebiete-luftfahrt", |
| 20 | + "ch.swisstopo.lubis-luftbilder-dritte-kantone", |
| 21 | + "ch.bav.sachplan-infrastruktur-schiene_anhorung", |
| 22 | + "ch.agroscope.korridore-feuchtgebietsarten_qualitaet", |
| 23 | + "ch.meteoschweiz.messwerte-pollen-buche-1h", |
| 24 | +] |
| 25 | + |
| 26 | + |
| 27 | +class Command(CustomBaseCommand): |
| 28 | + help = "Exports datasets from the system to DynamoDB" |
| 29 | + |
| 30 | + def add_arguments(self, parser: CommandParser) -> None: |
| 31 | + super().add_arguments(parser) |
| 32 | + parser.add_argument( |
| 33 | + "--clear", |
| 34 | + action="store_true", |
| 35 | + help="Delete existing objects before importing", |
| 36 | + ) |
| 37 | + parser.add_argument( |
| 38 | + "--dry-run", |
| 39 | + action="store_true", |
| 40 | + help="Dry run, abort transaction in the end", |
| 41 | + ) |
| 42 | + parser.add_argument( |
| 43 | + "--sample", |
| 44 | + action="store_true", |
| 45 | + help="Export a sample of datasets (10) instead of all datasets", |
| 46 | + ) |
| 47 | + parser.add_argument( |
| 48 | + "--providers", |
| 49 | + action="store_true", |
| 50 | + help="Import providers", |
| 51 | + ) |
| 52 | + parser.add_argument( |
| 53 | + "--attributions", |
| 54 | + action="store_true", |
| 55 | + help="Import attributions", |
| 56 | + ) |
| 57 | + parser.add_argument( |
| 58 | + "--datasets", |
| 59 | + action="store_true", |
| 60 | + help="Import datasets", |
| 61 | + ) |
| 62 | + parser.add_argument( |
| 63 | + "--target-env", |
| 64 | + type=str, |
| 65 | + choices=["dev", "int", "prod"], |
| 66 | + default="dev", |
| 67 | + help="Specify the target environment", |
| 68 | + ) |
| 69 | + parser.add_argument( |
| 70 | + "--profile-name", |
| 71 | + type=str, |
| 72 | + nargs="?", |
| 73 | + default=None, |
| 74 | + help="Specify the profile name (only needed locally)", |
| 75 | + ) |
| 76 | + parser.add_argument( |
| 77 | + "--debug", |
| 78 | + action="store_true", |
| 79 | + help="Enable debug logging", |
| 80 | + ) |
| 81 | + |
| 82 | + def handle(self, *args: Any, **options: Any) -> None: |
| 83 | + """Main entry point of command.""" |
| 84 | + if options["profile_name"]: |
| 85 | + self.session = boto3.Session(profile_name=options["profile_name"]) # pylint: disable=attribute-defined-outside-init |
| 86 | + else: |
| 87 | + self.session = boto3.Session() # pylint: disable=attribute-defined-outside-init |
| 88 | + |
| 89 | + if options["datasets"]: |
| 90 | + self.export_datasets(*args, **options) |
| 91 | + if options["providers"]: |
| 92 | + self.export_providers(*args, **options) |
| 93 | + |
| 94 | + def export_datasets(self, *args: Any, **options: Any) -> None: |
| 95 | + |
| 96 | + dynamodb_client = self.session.client("dynamodb", region_name="eu-central-1") |
| 97 | + |
| 98 | + if options["sample"]: |
| 99 | + self.print("Exporting only a sample of datasets (10)...") |
| 100 | + qs = Dataset.objects.filter(dataset_id__in=SAMPLE_IDS) |
| 101 | + else: |
| 102 | + qs = Dataset.objects.all() |
| 103 | + datasets = json.loads( |
| 104 | + serializers.serialize( |
| 105 | + "json", qs, use_natural_foreign_keys=True, use_natural_primary_keys=True |
| 106 | + ) |
| 107 | + ) |
| 108 | + |
| 109 | + for dataset in datasets: |
| 110 | + exp_item = ExportDataset(**dataset["fields"]) |
| 111 | + item = exp_item.as_dynamodb_item() |
| 112 | + self.print(f"Exporting dataset {exp_item.dataset_id} to DynamoDB") |
| 113 | + dynamodb_client.put_item( |
| 114 | + TableName=f"harvest-datasets-{options['target_env']}", Item=item |
| 115 | + ) |
| 116 | + |
| 117 | + def export_providers(self, *args: Any, **options: Any) -> None: |
| 118 | + dynamodb_client = self.session.client("dynamodb", region_name="eu-central-1") |
| 119 | + |
| 120 | + qs = Provider.objects.all() |
| 121 | + providers = json.loads( |
| 122 | + serializers.serialize( |
| 123 | + "json", qs, use_natural_foreign_keys=True, use_natural_primary_keys=True |
| 124 | + ) |
| 125 | + ) |
| 126 | + |
| 127 | + for provider in providers: |
| 128 | + exp_item = ExportProvider(**provider["fields"]) |
| 129 | + item = exp_item.as_dynamodb_item() |
| 130 | + self.print(f"Exporting provider {exp_item.provider_id} to DynamoDB") |
| 131 | + dynamodb_client.put_item( |
| 132 | + TableName=f"harvest-providers-{options['target_env']}", Item=item |
| 133 | + ) |
0 commit comments