Skip to content

Commit

Permalink
✨(back) add delete outdated object commands
Browse files Browse the repository at this point in the history
Once an object reached its retention date it should be deleted
  • Loading branch information
polyhb committed Jul 5, 2023
1 parent a9f6e81 commit 5874511
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Versioning](https://semver.org/spec/v2.0.0.html).
- Lifecycle rule on source bucket to expire uploaded
object after 21 days.
- Add retention date and s3 lifecycle rules to classroom / video
- Add a command to delete expired classrooms / videos

### Changed

Expand Down
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 src/backend/marsha/bbb/tests/test_delete_outdated_classrooms.py
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)
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)
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)

0 comments on commit 5874511

Please # to comment.