From 3e6dc009f5c3ac2e4127ceed2017746d59e2c807 Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Tue, 14 May 2024 17:29:05 -0400 Subject: [PATCH] feat: Add a --sync flag to the loaddivisions management command --- CHANGELOG.md | 4 ++++ .../core/management/commands/loaddivisions.py | 14 ++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e414275..e50e538 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +* Add a --sync flag to the loaddivisions management command, to delete divisions that are in the DB but not the CSV, even if the DB contains the CSV. This flag is relevant if you synchronize with a single CSV. + ## 3.3.0 (2023-05-08) * Add last_seen field to database objects diff --git a/opencivicdata/core/management/commands/loaddivisions.py b/opencivicdata/core/management/commands/loaddivisions.py index adc2701..dba6c9a 100644 --- a/opencivicdata/core/management/commands/loaddivisions.py +++ b/opencivicdata/core/management/commands/loaddivisions.py @@ -15,7 +15,7 @@ def to_db(fd): return Division(id=fd.id, name=fd.name, **args) -def load_divisions(country, bulk=False): +def load_divisions(country, bulk=False, sync=False): existing_divisions = Division.objects.filter(country=country) country_division = FileDivision.get("ocd-division/country:{}".format(country)) @@ -35,7 +35,7 @@ def load_divisions(country, bulk=False): if objects_set == existing_divisions_set: print("The CSV and the DB contents are exactly the same; no work to be done!") - elif objects_set.issubset(existing_divisions_set): + elif not sync and objects_set.issubset(existing_divisions_set): print("The DB contains all CSV contents; no work to be done!") else: if bulk: @@ -65,9 +65,15 @@ def add_arguments(self, parser): parser.add_argument( "--bulk", action="store_true", - help="Use bulk_create to add divisions. *Warning* This deletes any existing divisions", + help="Use bulk_create to add divisions. *Warning* This deletes any existing divisions.", + ) + parser.add_argument( + "--sync", + action="store_true", + help="Add divisions from a CSV file, and delete existing divisions that are not in the " + "CSV file. This option only makes sense with a single country.", ) def handle(self, *args, **options): for country in options["countries"]: - load_divisions(country, options["bulk"]) + load_divisions(country, options["bulk"], options["sync"])