Created
March 12, 2020 00:39
-
-
Save escattone/4e353404faae10dae2fff8754b292fe3 to your computer and use it in GitHub Desktop.
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
def delete_user(user): | |
# Protected references to users need to be manually deleted first. | |
user.key_set.all().delete() | |
# Some records are worth keeping prior to deleting the user | |
# but "re-assign" to the anonymous user. | |
anon, _ = User.objects.get_or_create(username="Anonymous") | |
user.revisionakismetsubmission_set.update(sender=anon) | |
user.documentdeletionlog_set.update(user=anon) | |
user.documentspamattempt_set.update(user=anon) | |
user.documentspam_reviewed.update(reviewer=anon) | |
user.bans.update(user=anon) | |
user.bans_issued.update(by=anon) | |
user.delete() | |
persona_users = list(User.objects.filter(socialaccount__provider='persona').exclude(socialaccount__provider='github').exclude(socialaccount__provider='google')) | |
print(f"found {len(persona_users)} persona-only users") | |
num_deleted = 0 | |
for user in persona_users: | |
if Revision.objects.filter(creator=user).exists() or AttachmentRevision.objects.filter(creator=user).exists(): | |
continue | |
try: | |
delete_user(user) | |
except Exception as e: | |
print(f"error while deleting {user.username} ({e})") | |
else: | |
num_deleted += 1 | |
print(f"deleted {num_deleted} persona-only users") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I loaded this within a Django shell (
./manage.py shell_plus
) on a productionweb
pod to remove all MDN Persona-only users who had made no contributions.