Skip to content

Commit

Permalink
Ajoute une commande pour supprimer les vieilles adresses IP
Browse files Browse the repository at this point in the history
  • Loading branch information
Situphen committed Sep 26, 2024
1 parent 9a2d086 commit 8988ebc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
14 changes: 14 additions & 0 deletions zds/utils/management/commands/remove_one_year_old_ip_addresses.py
Original file line number Diff line number Diff line change
@@ -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!"))
29 changes: 25 additions & 4 deletions zds/utils/management/tests.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
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,
ContentReaction,
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()
Expand Down Expand Up @@ -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, "")

0 comments on commit 8988ebc

Please sign in to comment.