-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨(back) add delete outdated object commands
Once an object reached its retention date it should be deleted
- Loading branch information
Showing
5 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/backend/marsha/bbb/management/commands/delete_outdated_classrooms.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""Delete outdated classrooms that have reached their retention date.""" | ||
from django.core.management.base import BaseCommand | ||
|
||
from marsha.bbb.models import Classroom | ||
from marsha.core.management.commands.delete_outdated_videos import ( | ||
delete_outdated_models, | ||
) | ||
|
||
|
||
class Command(BaseCommand): | ||
"""Delete outdated classrooms that have reached their retention date.""" | ||
|
||
help = "Deletes outdated Classroom once they reached their retention date." | ||
|
||
def handle(self, *args, **options): | ||
""" | ||
call delete_outdated_models to delete Video object. | ||
""" | ||
delete_outdated_models(self.stdout, Classroom) |
40 changes: 40 additions & 0 deletions
40
src/backend/marsha/bbb/tests/test_delete_outdated_classrooms.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Test delete_outdated_classrooms command.""" | ||
from datetime import date, datetime | ||
from unittest.mock import patch | ||
|
||
from django.core.management import call_command | ||
from django.test import TestCase | ||
from django.utils import timezone | ||
|
||
from marsha.bbb.factories import ClassroomFactory | ||
from marsha.bbb.models import Classroom | ||
|
||
|
||
class DeleteOutdatedClassroomsTestCase(TestCase): | ||
""" | ||
Test case for the delete_outdated_classrooms command. | ||
""" | ||
|
||
def setUp(self): | ||
""" | ||
Set up the test case with classrooms. | ||
""" | ||
|
||
self.classroom_1 = ClassroomFactory( | ||
retention_date=date(2022, 1, 1), | ||
) | ||
self.classroom_2 = ClassroomFactory( | ||
retention_date=date(2021, 1, 1), | ||
) | ||
self.classroom_3 = ClassroomFactory( | ||
retention_date=date(2023, 1, 1), | ||
) | ||
self.classroom_4 = ClassroomFactory() | ||
|
||
def test_delete_outdated_videos(self): | ||
""" | ||
Test the delete_outdated_classrooms command. | ||
""" | ||
with patch.object(timezone, "now", return_value=datetime(2022, 1, 2)): | ||
call_command("delete_outdated_classrooms") | ||
self.assertEqual(Classroom.objects.count(), 2) |
43 changes: 43 additions & 0 deletions
43
src/backend/marsha/core/management/commands/delete_outdated_videos.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Delete outdated videos that have reached their retention date.""" | ||
from django.core.management.base import BaseCommand | ||
from django.utils import timezone | ||
|
||
from marsha.core.models import Video | ||
|
||
|
||
def delete_outdated_models(stdout, model): | ||
""" | ||
Deletes outdated model objects once they reached their retention date. | ||
Object with a retention date have their save method overridden to | ||
update their related s3 lifecycle policy. | ||
Parameters: | ||
---------- | ||
stdout (file): The output stream to write log messages. | ||
model (Model): The model class whose outdated objects need to be deleted. | ||
""" | ||
|
||
now = timezone.now() | ||
|
||
# Find outdated model objects | ||
outdated_objects = model.objects.filter(retention_date__lt=now) | ||
|
||
stdout.write( | ||
f"Deleting outdated {outdated_objects.count()} {model.__name__} objects..." | ||
) | ||
|
||
outdated_objects.delete() | ||
|
||
stdout.write(f"Successfully deleted outdated {model.__name__} objects.") | ||
|
||
|
||
class Command(BaseCommand): | ||
"""Delete outdated videos that have reached their retention date.""" | ||
|
||
help = "Deletes outdated videos once they reached their retention date." | ||
|
||
def handle(self, *args, **options): | ||
""" | ||
call delete_outdated_models to delete Video object. | ||
""" | ||
delete_outdated_models(self.stdout, Video) |
40 changes: 40 additions & 0 deletions
40
src/backend/marsha/core/tests/management_commands/test_delete_outdated_videos.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Test delete_outdated_videos command.""" | ||
from datetime import date, datetime | ||
from unittest.mock import patch | ||
|
||
from django.core.management import call_command | ||
from django.test import TestCase | ||
from django.utils import timezone | ||
|
||
from marsha.core.factories import VideoFactory | ||
from marsha.core.models import Video | ||
|
||
|
||
class DeleteOutdatedVideosTestCase(TestCase): | ||
""" | ||
Test case for the delete_outdated_videos command. | ||
""" | ||
|
||
def setUp(self): | ||
""" | ||
Set up the test case with videos. | ||
""" | ||
|
||
self.video_1 = VideoFactory( | ||
retention_date=date(2022, 1, 1), | ||
) | ||
self.video_2 = VideoFactory( | ||
retention_date=date(2021, 1, 1), | ||
) | ||
self.video_3 = VideoFactory( | ||
retention_date=date(2023, 1, 1), | ||
) | ||
self.video_4 = VideoFactory() | ||
|
||
def test_delete_outdated_videos(self): | ||
""" | ||
Test the delete_outdated_videos command. | ||
""" | ||
with patch.object(timezone, "now", return_value=datetime(2022, 1, 2)): | ||
call_command("delete_outdated_videos") | ||
self.assertEqual(Video.objects.count(), 2) |