diff --git a/boards/management/commands/enforce_privacy.py b/boards/management/commands/enforce_privacy.py --- a/boards/management/commands/enforce_privacy.py +++ b/boards/management/commands/enforce_privacy.py @@ -1,5 +1,9 @@ +from datetime import datetime, timedelta, date +from datetime import time as dtime + from django.core.management import BaseCommand from django.db import transaction +from django.utils import timezone from boards.models import Post from boards.models.post.manager import NO_IP @@ -8,8 +12,23 @@ from boards.models.post.manager import N class Command(BaseCommand): - help = 'Removes user and IP data from all posts' + help = 'Removes user and IP data from posts' + + def add_arguments(self, parser): + parser.add_argument('--days-before', type=int, + help='Clean posts only before the number of days before today.') @transaction.atomic def handle(self, *args, **options): - Post.objects.all().update(poster_ip=NO_IP) \ No newline at end of file + days_before = options.get('days_before') + + if days_before: + day_end = date.today() - timedelta(1) + + day_time_end = timezone.make_aware(datetime.combine( + day_end, dtime()), timezone.get_current_timezone()) + posts = Post.objects.filter(pub_time__lte=day_time_end) + else: + posts = Post.objects.all() + posts.update(poster_ip=NO_IP) + diff --git a/boards/models/post/manager.py b/boards/models/post/manager.py --- a/boards/models/post/manager.py +++ b/boards/models/post/manager.py @@ -134,7 +134,6 @@ class PostManager(models.Manager): pub_time__lte=day_time_end, pub_time__gte=day_time_start).count() - @transaction.atomic def import_post(self, title: str, text: str, pub_time: str, global_id, opening_post=None, tags=list(), files=list(),