Skip to content

Instantly share code, notes, and snippets.

@ianfitzpatrick
Last active May 1, 2024 06:10
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/e35617e42501b29f5e2a57dd08a7bdcc to your computer and use it in GitHub Desktop.
Save ianfitzpatrick/e35617e42501b29f5e2a57dd08a7bdcc to your computer and use it in GitHub Desktop.
Find Orphaned Group Notifications
# Find orphaned group invite/request notifications
from django.core.paginator import Paginator
from notifications.constants import NOTIFY_INVITE, NOTIFY_REQUEST
from notifications.models import Notification
problem_objs = []
objs = Notification.objects.filter(sub_type__in=[NOTIFY_INVITE, NOTIFY_REQUEST])
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')
try:
if not obj.owner.id in obj.content_object.memberships.all().values_list('user__id', flat=True):
problem_objs.append(obj)
except AttributeError:
print(f'\n\n{obj.id}\n\n')
problem_objs.append(obj)
i += chunk_size
to_delete = Notification.objects.filter(id__in=[obj.id for obj 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