diff --git a/breccia_mapper/management/__init__.py b/breccia_mapper/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/breccia_mapper/management/commands/__init__.py b/breccia_mapper/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/breccia_mapper/management/commands/selectiveloaddata.py b/breccia_mapper/management/commands/selectiveloaddata.py new file mode 100644 index 0000000..eedc553 --- /dev/null +++ b/breccia_mapper/management/commands/selectiveloaddata.py @@ -0,0 +1,44 @@ +import json +import os + +from django.core.management.commands import loaddata + +from bootstrap_customizer.models import BootstrapTheme + + +def should_add_record(record): + if record['model'] != 'bootstrap_customizer.bootstraptheme': + return True + + return not BootstrapTheme.objects.filter( + pk=record['pk'], + ).exists() + + +class Command(loaddata.Command): + def handle(self, *args, **options): + args = list(args) + + # Read the original JSON file + file_name = args[0] + with open(file_name) as json_file: + json_list = json.load(json_file) + + # Filter out records that already exists + json_list_filtered = list(filter(should_add_record, json_list)) + if not json_list_filtered: + print("All data are already previously loaded") + return + + # Write the updated JSON file + file_dir_and_name, file_ext = os.path.splitext(file_name) + file_name_temp = f"{file_dir_and_name}_temp{file_ext}" + with open(file_name_temp, 'w') as json_file_temp: + json.dump(json_list_filtered, json_file_temp) + + # Pass the request to the actual loaddata (parent functionality) + args[0] = file_name_temp + super().handle(*args, **options) + + # You can choose to not delete the file so that you can see what was added to your records + os.remove(file_name_temp) diff --git a/breccia_mapper/settings.py b/breccia_mapper/settings.py index 4690745..d5c4e21 100644 --- a/breccia_mapper/settings.py +++ b/breccia_mapper/settings.py @@ -202,6 +202,7 @@ THIRD_PARTY_APPS = [ ] FIRST_PARTY_APPS = [ + 'breccia_mapper', 'people', 'activities', 'export',