Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajoute une commande pour supprimer les vieilles adresses IP #6608

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, "")