diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 948a561339..ed72344893 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,6 +15,10 @@ Change Log Unreleased ---------- +[4.0.4] +-------- +feat: remove content transmission audits without a catalog uuid + [4.0.3] ------- fix: changing sap transmit metadata flow to account for rate limiting diff --git a/enterprise/__init__.py b/enterprise/__init__.py index 3a473809d7..0894a4fcd7 100644 --- a/enterprise/__init__.py +++ b/enterprise/__init__.py @@ -2,6 +2,6 @@ Your project description goes here. """ -__version__ = "4.0.3" +__version__ = "4.0.4" default_app_config = "enterprise.apps.EnterpriseConfig" diff --git a/integrated_channels/integrated_channel/management/commands/remove_null_catalog_transmission_audits.py b/integrated_channels/integrated_channel/management/commands/remove_null_catalog_transmission_audits.py new file mode 100644 index 0000000000..3614bc9c18 --- /dev/null +++ b/integrated_channels/integrated_channel/management/commands/remove_null_catalog_transmission_audits.py @@ -0,0 +1,30 @@ +""" +Remove content transmission audit records that do not contain a catalog UUID. +""" +import logging + +from django.core.management.base import BaseCommand + +from integrated_channels.integrated_channel.management.commands import IntegratedChannelCommandMixin +from integrated_channels.integrated_channel.tasks import remove_null_catalog_transmission_audits + +LOGGER = logging.getLogger(__name__) + + +class Command(IntegratedChannelCommandMixin, BaseCommand): + """ + Remove content transmission audit records that do not contain a catalog UUID. + ./manage.py lms remove_null_catalog_transmission_audits + """ + + def handle(self, *args, **options): + """ + Filter content transmission audit records that do not contain a catalog UUID and remove them. + """ + try: + remove_null_catalog_transmission_audits.delay() + except Exception as exc: # pylint: disable=broad-except + LOGGER.exception( + f'''Failed to remove content transmission audits that do not + contain a catalog UUID. Task failed with exception: {exc}''' + ) diff --git a/integrated_channels/integrated_channel/tasks.py b/integrated_channels/integrated_channel/tasks.py index ac45530356..55f68472f9 100644 --- a/integrated_channels/integrated_channel/tasks.py +++ b/integrated_channels/integrated_channel/tasks.py @@ -91,6 +91,31 @@ def _log_batch_task_finish(task_name, channel_code, job_user_id, )) +@shared_task +@set_code_owner_attribute +def remove_null_catalog_transmission_audits(): + """ + Task to remove content transmission audit records that do not contain a catalog UUID. + """ + start = time.time() + + _log_batch_task_start('remove_null_catalog_transmission_audits', None, None, None) + + deleted_null_catalog_uuids = ContentMetadataItemTransmission.objects.filter( + enterprise_customer_catalog_uuid=None + ).delete() + + duration_seconds = time.time() - start + _log_batch_task_finish( + 'remove_null_catalog_transmission_audits', + channel_code=None, + job_user_id=None, + integrated_channel_full_config=None, + duration_seconds=duration_seconds, + extra_message=f"{deleted_null_catalog_uuids[0]} transmission audits with no catalog UUIDs removed" + ) + + @shared_task @set_code_owner_attribute def remove_duplicate_transmission_audits(): diff --git a/tests/test_management.py b/tests/test_management.py index 9e30713324..3e90a666bd 100644 --- a/tests/test_management.py +++ b/tests/test_management.py @@ -2012,3 +2012,35 @@ def test_invalid_audits(self): assert moodle_config.last_sync_errored_at == old_timestamp assert moodle_config.last_content_sync_errored_at == old_timestamp assert moodle_config.last_learner_sync_errored_at is None + + +@mark.django_db +@ddt.ddt +class TestRemoveNullCatalogTransmissionAuditsManagementCommand(unittest.TestCase, EnterpriseMockMixin): + """ + Test the ``remove_null_catalog_transmission_audits`` management command. + """ + def setUp(self): + self.enterprise_customer_1 = factories.EnterpriseCustomerFactory( + name='Wonka Factory', + ) + self.enterprise_customer_2 = factories.EnterpriseCustomerFactory( + name='Hershey LLC', + ) + factories.ContentMetadataItemTransmissionFactory( + enterprise_customer=self.enterprise_customer_1, + enterprise_customer_catalog_uuid=None + ) + factories.ContentMetadataItemTransmissionFactory( + enterprise_customer=self.enterprise_customer_2, + enterprise_customer_catalog_uuid="d9efab41-5e09-4094-977a-96313b3dca08" + ) + super().setUp() + + def test_normal_run(self): + assert ContentMetadataItemTransmission.objects.all().count() == 2 + call_command('remove_null_catalog_transmission_audits') + assert ContentMetadataItemTransmission.objects.all().count() == 1 + assert ContentMetadataItemTransmission.objects.filter( + enterprise_customer_catalog_uuid=None + ).count() == 0