diff --git a/zds/utils/management/commands/remove_one_year_old_ip_addresses.py b/zds/utils/management/commands/remove_one_year_old_ip_addresses.py new file mode 100644 index 0000000000..e0713e65ac --- /dev/null +++ b/zds/utils/management/commands/remove_one_year_old_ip_addresses.py @@ -0,0 +1,14 @@ +from datetime import datetime, timedelta + +from django.core.management.base import BaseCommand + +from zds.utils.models import Comment + + +class Command(BaseCommand): + help = "Removes IP addresses that are more than one year old" + + def handle(self, *args, **options): + one_year_ago = datetime.now() - timedelta(days=365) + Comment.objects.filter(pubdate__lte=one_year_ago).exclude(ip_address="").update(ip_address="") + self.stdout.write(self.style.SUCCESS(f"Successfully removed IP addresses that are more than one year old!")) diff --git a/zds/utils/management/tests.py b/zds/utils/management/tests.py index 27e1805c33..db53c39abd 100644 --- a/zds/utils/management/tests.py +++ b/zds/utils/management/tests.py @@ -1,12 +1,13 @@ +from django.contrib.auth.models import User, Permission from django.core.management import call_command from django.test import TestCase -from django.contrib.auth.models import User, Permission +from zds.gallery.models import Gallery, UserGallery from zds.member.models import Profile +from zds.member.tests.factories import ProfileFactory from zds.forum.models import Forum, Topic, ForumCategory -from zds.utils.models import Tag, Category as TCategory, CategorySubCategory, SubCategory, Licence +from zds.forum.tests.factories import TopicFactory, PostFactory, ForumFactory, ForumCategoryFactory from zds.tutorialv2.models.help_requests import HelpWriting -from zds.member.tests.factories import ProfileFactory from zds.tutorialv2.models.database import ( PublishableContent, PublishedContent, @@ -14,8 +15,8 @@ Validation as CValidation, ) from zds.tutorialv2.tests import TutorialTestMixin, override_for_contents -from zds.gallery.models import Gallery, UserGallery from zds.utils.management.commands.load_fixtures import Command as FixtureCommand +from zds.utils.models import Tag, Category as TCategory, CategorySubCategory, SubCategory, Licence @override_for_contents() @@ -61,3 +62,23 @@ def test_profiler(self): result = self.client.get("/?prof", follow=True) self.assertEqual(result.status_code, 200) + + def test_remove_old_ip_addresses(self): + category = ForumCategoryFactory(position=1) + forum = ForumFactory(category=category, position_in_category=1) + user = ProfileFactory().user + topic = TopicFactory(forum=forum, author=user) + old_post = PostFactory(topic=topic, author=user, position=1) + old_post.pubdate = old_post.pubdate.replace(year=1999) + old_post.save() + recent_post = PostFactory(topic=topic, author=user, position=2) + + self.assertNotEqual(old_post.ip_address, "") + self.assertNotEqual(recent_post.ip_address, "") + + call_command("remove_one_year_old_ip_addresses") + old_post.refresh_from_db() + recent_post.refresh_from_db() + + self.assertEqual(old_post.ip_address, "") + self.assertNotEqual(recent_post.ip_address, "")