Last active
May 8, 2024 22:51
-
-
Save ianfitzpatrick/57a4e2c8814716b60ac90d3a053f8e67 to your computer and use it in GitHub Desktop.
Find Group Invite Notifications Already Approved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
invite_problem_count = 0 | |
request_problem_count = 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} [invite problems: {invite_problem_count}, request problems: {request_problem_count}]', end='\r') | |
try: | |
if obj.sub_type == NOTIFY_INVITE: | |
if not obj.owner.id in obj.content_object.memberships.invites().values_list('user__id', flat=True): | |
problem_objs.append(obj) | |
invite_problem_count += 1 | |
if obj.sub_type == NOTIFY_REQUEST: | |
if not obj.requester.id in obj.content_object.memberships.requests().values_list('user__id', flat=True): | |
if obj.requester.id in obj.content_object.memberships.approved().all().values_list('user__id', flat=True): | |
problem_objs.append(obj) | |
request_problem_count += 1 | |
except AttributeError: | |
print(f'\n\n{obj.id}\n\n') | |
problem_objs.append(obj) | |
if obj.sub_type == NOTIFY_INVITE: | |
invite_problem_count += 1 | |
if obj.sub_type == NOTIFY_REQUEST: | |
request_problem_count += 1 | |
i += chunk_size | |
to_inactive = Notification.objects.filter(id__in=[obj.id for obj in problem_objs]) | |
print(f'{i + n+1}/{len_objs} [invite problems: {invite_problem_count}, request problems: {request_problem_count}]') | |
confirmation = input(f'Are you sure you want to set inactive the selected {to_inactive.count()} notifications? (y/n): ') | |
if confirmation.lower() == 'y': | |
result = to_inactive.update(active=False) | |
print(result) | |
else: | |
print('Update cancelled.') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment