diff --git a/contentcuration/contentcuration/management/commands/set_content_mimetypes.py b/contentcuration/contentcuration/management/commands/set_content_mimetypes.py index 27af4732fc..48e3eba471 100755 --- a/contentcuration/contentcuration/management/commands/set_content_mimetypes.py +++ b/contentcuration/contentcuration/management/commands/set_content_mimetypes.py @@ -23,12 +23,12 @@ def handle(self, *args, **kwargs): futures = [] with concurrent.futures.ThreadPoolExecutor() as e: - print("Scheduling all metadata update jobs...") # noqa: T201 + self.stdout.write("Scheduling all metadata update jobs...") for blob in blobs: future = e.submit(self._update_metadata, blob) futures.append(future) - print("Waiting for all jobs to finish...") # noqa: T201 + self.stdout.write("Waiting for all jobs to finish...") def _determine_cache_control(self, name): _, ext = os.path.splitext(name) diff --git a/contentcuration/contentcuration/management/commands/setup.py b/contentcuration/contentcuration/management/commands/setup.py index 16478297f0..bd5f2b1389 100644 --- a/contentcuration/contentcuration/management/commands/setup.py +++ b/contentcuration/contentcuration/management/commands/setup.py @@ -55,7 +55,7 @@ def handle(self, *args, **options): email = options["email"] password = options["password"] if not re.match(r"[^@]+@[^@]+\.[^@]+", email): - print("{} is not a valid email".format(email)) # noqa: T201 + self.stderr.write(self.style.ERROR("{} is not a valid email".format(email))) sys.exit() # create the cache table @@ -191,9 +191,11 @@ def handle(self, *args, **options): for legacy_node in legacy_clipboard_nodes: legacy_node.copy_to(target=user1.clipboard_tree) - print( # noqa: T201 - "\n\n\nSETUP DONE: Log in as admin to view data (email: {}, password: {})\n\n\n".format( - email, password + self.stdout.write( + self.style.SUCCESS( + "\n\n\nSETUP DONE: Log in as admin to view data (email: {}, password: {})\n\n\n".format( + email, password + ) ) ) diff --git a/contentcuration/contentcuration/management/commands/setup_perftest_data.py b/contentcuration/contentcuration/management/commands/setup_perftest_data.py index 18fbadae54..fa67010f6a 100644 --- a/contentcuration/contentcuration/management/commands/setup_perftest_data.py +++ b/contentcuration/contentcuration/management/commands/setup_perftest_data.py @@ -20,7 +20,7 @@ def handle(self, *args, **options): self.editor.clipboard_tree.get_descendants().delete() with ContentNode.objects.delay_mptt_updates(): - print("Creating channel...") # noqa: T201 + self.stdout.write("Creating channel...") self.generate_random_channels() # Make sure we have a channel with a lot of root topics to test initial channel load. @@ -33,7 +33,7 @@ def handle(self, *args, **options): self.editor.clipboard_tree = TreeBuilder( levels=2, num_children=25, user=self.editor ).root - print( # noqa: T201 + self.stdout.write( "Created clipboard with {} nodes".format( self.editor.clipboard_tree.get_descendants().count() ) @@ -47,7 +47,7 @@ def generate_random_channels(self, num_channels=1): new_channel.main_tree = TreeBuilder(user=self.editor).root - print( # noqa: T201 + self.stdout.write( "Created channel with {} nodes".format( new_channel.main_tree.get_descendants().count() ) @@ -55,4 +55,4 @@ def generate_random_channels(self, num_channels=1): # make sure we have a trash tree so that can be tested with real data as well. new_channel.trash_tree = TreeBuilder(user=self.editor).root - print("Created channel with id {}".format(new_channel.pk)) # noqa: T201 + self.stdout.write("Created channel with id {}".format(new_channel.pk)) diff --git a/contentcuration/contentcuration/management/commands/test_server_perf.py b/contentcuration/contentcuration/management/commands/test_server_perf.py index b17f0d8081..aa30b1d9cb 100644 --- a/contentcuration/contentcuration/management/commands/test_server_perf.py +++ b/contentcuration/contentcuration/management/commands/test_server_perf.py @@ -34,24 +34,24 @@ def handle(self, *args, **options): ] = objects.get_object_creation_stats_mptt_delay(num_objects, num_runs) object_types.append("ContentNode-mptt-delay") - print() - print("Test results:") + self.stdout.write("") + self.stdout.write("Test results:") for object_type in object_types: run_stats = stats[object_type] - print( + self.stdout.write( "Stats for creating {} {} objects over {} runs: {}".format( num_objects, object_type, num_runs, run_stats ) ) if options["stress_test"]: - print( # noqa: T201 + self.stdout.write( "Running stress test simulating creation / cloning of a channel like KA, " "this will take at least several minutes. Please do not interrupt if possible!" ) stats = objects.get_large_channel_creation_stats() for stat in stats: - print("{}: {}".format(stat, stats[stat])) + self.stdout.write("{}: {}".format(stat, stats[stat])) finally: if objects: diff --git a/contentcuration/contentcuration/perftools/objective.py b/contentcuration/contentcuration/perftools/objective.py index 0405be92d5..be6186328a 100644 --- a/contentcuration/contentcuration/perftools/objective.py +++ b/contentcuration/contentcuration/perftools/objective.py @@ -1,3 +1,4 @@ +import logging import sys import time @@ -8,6 +9,9 @@ # TODO: Investigate more precise timing libraries +logger = logging.getLogger(__name__) + + def print_progress(text): sys.stdout.write("\r" + text) sys.stdout.flush() @@ -34,7 +38,7 @@ def __del__(self): ) def cleanup(self): - print("Performing clean up, please wait...") # noqa: T201 + logger.info("Performing clean up, please wait...") try: if self.root_node: files = File.objects.filter(contentnode=self.root_node) @@ -45,10 +49,9 @@ def cleanup(self): self.root_node = None except Exception: if self.root_node: - print( # noqa: T201 - "Error in cleanup. Root node with id {} may still exist.".format( - self.root_node.pk - ) + logger.error( + "Error in cleanup. Root node with id %s may still exist.", + self.root_node.pk, ) raise diff --git a/contentcuration/contentcuration/utils/db_tools.py b/contentcuration/contentcuration/utils/db_tools.py index e433b16590..67844eb227 100644 --- a/contentcuration/contentcuration/utils/db_tools.py +++ b/contentcuration/contentcuration/utils/db_tools.py @@ -1,4 +1,5 @@ import json +import logging import os import random import string @@ -31,6 +32,8 @@ from contentcuration.models import User from contentcuration.utils.files import duplicate_file +logger = logging.getLogger(__name__) + LICENSE_DESCRIPTION = "Sample text for content with special permissions" SORT_ORDER = 0 @@ -43,10 +46,10 @@ def create_user(email, password, first_name, last_name, admin=False): user.set_password(password) user.first_name = first_name user.last_name = last_name - print( # noqa: T201 - "User created (email: {}, password: {}, admin: {})".format( - email, password, admin - ) + logger.info( + "User created (email: %s, admin: %s)", + email, + admin, ) user.is_staff = admin user.is_admin = admin diff --git a/contentcuration/contentcuration/utils/nodes.py b/contentcuration/contentcuration/utils/nodes.py index aa424ff27e..aea3184822 100644 --- a/contentcuration/contentcuration/utils/nodes.py +++ b/contentcuration/contentcuration/utils/nodes.py @@ -28,6 +28,8 @@ from contentcuration.utils.files import get_thumbnail_encoding from contentcuration.utils.sentry import report_exception +logger = logging.getLogger(__name__) + def map_files_to_node(user, node, data): # noqa: C901 """ @@ -162,7 +164,7 @@ def map_files_to_slideshow_slide_item(user, node, slides, files): if not matching_slide: # TODO(Jacob) Determine proper error type... raise it. - print("NO MATCH") # noqa: T201 + logger.warning("NO MATCH for checksum %s", checksum) file_path = generate_object_storage_name(checksum, filename) storage = default_storage diff --git a/contentcuration/contentcuration/utils/publish.py b/contentcuration/contentcuration/utils/publish.py index 1b8f08131a..65932d8cb6 100644 --- a/contentcuration/contentcuration/utils/publish.py +++ b/contentcuration/contentcuration/utils/publish.py @@ -391,7 +391,7 @@ def recurse_nodes(self, node, inherited_fields): # noqa C901 def create_slideshow_manifest(ccnode, user_id=None): - print("Creating slideshow manifest...") # noqa: T201 + logging.info("Creating slideshow manifest...") preset = ccmodels.FormatPreset.objects.filter(pk="slideshow_manifest")[0] ext = file_formats.JSON