diff --git a/greg/tests/populate_fixtures.py b/greg/management/commands/populate_test_data.py similarity index 91% rename from greg/tests/populate_fixtures.py rename to greg/management/commands/populate_test_data.py index 704680c8d27fa8fa73ef366b56065285c602b763..3226f6bda083241cf4e59e7e9381a53045abe402 100644 --- a/greg/tests/populate_fixtures.py +++ b/greg/management/commands/populate_test_data.py @@ -2,7 +2,7 @@ Adds a few more specific models for testing purposes. Alternative to the similiar script for adding random data to various fields. -WARNING: This script removes all entries in most tables. Do not execute it unless you +WARNING: This command removes all entries in most tables. Do not execute it unless you are absolutely certain you know what you are doing. There are 4 guests: @@ -22,10 +22,11 @@ one of them has denied the other one. """ import datetime -import logging +from django.core.management.base import CommandError from django.db import connection from django.conf import settings +from django.core.management.base import BaseCommand from django.utils import timezone from greg.models import ( @@ -55,8 +56,6 @@ OU_EUROPE_NAME_EN = "Europe" CONSENT_IDENT_MANDATORY = "mandatory" CONSENT_IDENT_OPTIONAL = "optional" -logger = logging.getLogger(__name__) - class DatabasePopulation: """ @@ -66,7 +65,7 @@ class DatabasePopulation: """ def truncate_tables(self): - logger.info("truncating tables...") + print("truncating tables...") with connection.cursor() as cursor: for table in ( "greg_consent", @@ -85,9 +84,9 @@ class DatabasePopulation: "greg_person", "greg_sponsor", ): - logging.info("purging table %s", table) + print("purging table", table) cursor.execute(f"DELETE FROM {table}") - logger.info("...tables purged") + print("...tables purged") def _add_consent_types_and_choices(self): mandatory = ConsentType.objects.create( @@ -391,7 +390,7 @@ class DatabasePopulation: ) def populate_database(self): - logger.info("populating db...") + print("populating db...") # Add the types, sponsors and ous self._add_consent_types_and_choices() self._add_ous_with_identifiers() @@ -402,10 +401,36 @@ class DatabasePopulation: self._add_waiting_person() self._add_invited_person() self._add_expired_person() - logger.info("...done populating db") + print("...done populating db") + + +class Command(BaseCommand): + help = __doc__ + + def add_arguments(self, parser): + parser.add_argument( + "--destructive", + type=str, + required=True, + help="Verify database name. THIS COMMAND IS DESTRUCTIVE.", + ) + + def handle(self, *args, **options): + """ + Handle import of OUs from OrgReg. + - Updates already present OUs with new information + - Creates not present OUs + - Set not present OUs as inactive + """ + db_name = str(settings.DATABASES["default"]["NAME"]) + if options.get("destructive") != db_name: + raise CommandError( + "Must pass {!r} to --destructive, as its tables will be truncated".format( + db_name + ) + ) -if __name__ == "__main__": - database_population = DatabasePopulation() - database_population.truncate_tables() - database_population.populate_database() + database_population = DatabasePopulation() + database_population.truncate_tables() + database_population.populate_database()