From 6675120b0d26672898c9bf49a513d859a97bd9c2 Mon Sep 17 00:00:00 2001 From: Tobias Bengfort Date: Mon, 6 Dec 2021 17:31:47 +0100 Subject: [PATCH 1/2] consistently use self.stdout in commands --- .../management/commands/attribute_export.py | 2 +- .../commands/create_demo_content.py | 26 +++++++++---------- .../test_create_demo_content.py | 6 +++-- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/castellum/recruitment/management/commands/attribute_export.py b/castellum/recruitment/management/commands/attribute_export.py index da017ecc3..2734421e2 100644 --- a/castellum/recruitment/management/commands/attribute_export.py +++ b/castellum/recruitment/management/commands/attribute_export.py @@ -43,4 +43,4 @@ class Command(BaseCommand): else: subject = Subject.objects.get(pk=options['subject_uuid']) s = exporter.get_data(descriptions, [(subject.pk, subject)]) - print(s) + self.stdout.write(s) diff --git a/castellum/recruitment/management/commands/create_demo_content.py b/castellum/recruitment/management/commands/create_demo_content.py index e3bbc335c..c514fb672 100644 --- a/castellum/recruitment/management/commands/create_demo_content.py +++ b/castellum/recruitment/management/commands/create_demo_content.py @@ -207,8 +207,8 @@ def generate_geolocations(contacts): ) for contact in contacts) -def generate_studies(count): - print('Generating studies') +def generate_studies(count, stdout): + stdout.write('Generating studies') studies = [fake_study() for i in range(count)] for study in studies: study.save() @@ -218,16 +218,16 @@ def generate_studies(count): study.executiontag_set.create(name=name, color=color) -def generate_subjects(count): +def generate_subjects(count, stdout): # NOTE: We have ForeignKeys to Address and Subject. Unfortunately, bulk_create does not set # the id on the instances, so we have to get them from the database after bulk_create. This # assumes that the database was empty before. - print('Generating subjects') + stdout.write('Generating subjects') Subject.objects.bulk_create([fake_subject() for i in range(count)], batch_size=200) subjects = Subject.objects.all()[:count] - print('Generating contacts') + stdout.write('Generating contacts') contacts = [fake_contact(subjects[i]) for i in range(count)] contacts[-1].first_name = 'Muhammad' contacts[-1].first_name_phonetic = phonetic('Muhammad') @@ -238,14 +238,14 @@ def generate_subjects(count): Contact.objects.bulk_create(contacts) contacts = Contact.objects.all()[:count] - print('Generating addresses') + stdout.write('Generating addresses') Address.objects.bulk_create(fake_address(contact) for contact in contacts) if 'castellum.geofilters' in settings.INSTALLED_APPS: - print('Generating geolocations') + stdout.write('Generating geolocations') generate_geolocations(contacts) - print('Generating consents') + stdout.write('Generating consents') try: document = ConsentDocument.objects.filter(is_valid=True).latest() except ConsentDocument.DoesNotExist: @@ -254,8 +254,8 @@ def generate_subjects(count): Consent.objects.bulk_create(consents) -def generate_streets(count): - print('Generating streets') +def generate_streets(count, stdout): + stdout.write('Generating streets') streets = [Street(name=fake.street_name()) for i in range(count)] Street.objects.bulk_create(streets, batch_size=200) @@ -273,8 +273,8 @@ class Command(BaseCommand): raise CommandError('Demo content not created in production environment.') if not Study.objects.exists(): - generate_studies(options['study_count']) + generate_studies(options['study_count'], self.stdout) if not Subject.objects.exists(): - generate_subjects(options['subject_count']) + generate_subjects(options['subject_count'], self.stdout) if not Street.objects.exists(): - generate_streets(options['street_count']) + generate_streets(options['street_count'], self.stdout) diff --git a/tests/recruitment/management_commands/test_create_demo_content.py b/tests/recruitment/management_commands/test_create_demo_content.py index 5d9e3c113..17f84c368 100644 --- a/tests/recruitment/management_commands/test_create_demo_content.py +++ b/tests/recruitment/management_commands/test_create_demo_content.py @@ -1,3 +1,5 @@ +import sys + import pytest from castellum.recruitment.management.commands.create_demo_content import generate_studies @@ -8,11 +10,11 @@ from castellum.subjects.models import Subject @pytest.mark.django_db def test_generate_studies(): - generate_studies(3) + generate_studies(3, sys.stdout) assert Study.objects.count() == 3 @pytest.mark.django_db def test_generate_subjects(): - generate_subjects(3) + generate_subjects(3, sys.stdout) assert Subject.objects.count() == 3 -- GitLab From 5467657307601aef266b67c00723010d1ba880b5 Mon Sep 17 00:00:00 2001 From: Tobias Bengfort Date: Mon, 6 Dec 2021 17:31:58 +0100 Subject: [PATCH 2/2] lint for print() statements --- setup.cfg | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.cfg b/setup.cfg index 078308fa8..f37b978ce 100644 --- a/setup.cfg +++ b/setup.cfg @@ -41,6 +41,7 @@ test = lxml flake8 flake8-sfs + flake8-print isort pospell dev = -- GitLab