Skip to content

Instantly share code, notes, and snippets.

@ianfitzpatrick
Last active May 1, 2024 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianfitzpatrick/4ab1cff0460e905967c8b9fe7a635d09 to your computer and use it in GitHub Desktop.
Save ianfitzpatrick/4ab1cff0460e905967c8b9fe7a635d09 to your computer and use it in GitHub Desktop.
Orphaned Public List Notifications
import sys
from django.core.paginator import Paginator
from notifications.constants import NOTIFY_UPDATE
from lists.models import List
from lists.permissions.permissions import can_read_wishlist_via_shared_group
from notifications.models import Notification
list_ids = List.objects.filter(privacy_level=List.PUBLIC).values_list('id', flat=True)
objs = Notification.objects.filter(sub_type=NOTIFY_UPDATE, object_id__in=list_ids)
# Result: 553,742 notifications, not yet filtered on owner's access
problem_objs = []
len_objs = objs.count()
chunk_size = 25000
i = 0
print(f'{len_objs} Notification objects total are processable.')
paginator = Paginator(objs, chunk_size)
for page_number in paginator.page_range:
page = paginator.page(page_number)
for n, obj in enumerate(page.object_list):
print(f'{i + n+1}/{len_objs}', end='\r')
if not can_read_wishlist_via_shared_group(obj.content_object, obj.owner):
problem_objs.append(obj.id)
i += chunk_size
to_delete = Notification.objects.filter(id__in=problem_objs)
confirmation = input(f'Are you sure you want to delete the selected {to_delete.count()} notifications? (y/n): ')
if confirmation.lower() == 'y':
result = to_delete.delete()
print(result)
else:
print('Deletion cancelled.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment