mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-26 03:21:05 +00:00
Merge pull request #3183 from bookwyrm-social/erase_user_command
Adds management command to clear all deleted user data
This commit is contained in:
commit
597378bb78
3 changed files with 61 additions and 1 deletions
43
bookwyrm/management/commands/erase_deleted_user_data.py
Normal file
43
bookwyrm/management/commands/erase_deleted_user_data.py
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
""" Erase any data stored about deleted users """
|
||||||
|
import sys
|
||||||
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
|
from bookwyrm import models
|
||||||
|
from bookwyrm.models.user import erase_user_data
|
||||||
|
|
||||||
|
# pylint: disable=missing-function-docstring
|
||||||
|
class Command(BaseCommand):
|
||||||
|
"""command-line options"""
|
||||||
|
|
||||||
|
help = "Remove Two Factor Authorisation from user"
|
||||||
|
|
||||||
|
def add_arguments(self, parser): # pylint: disable=no-self-use
|
||||||
|
parser.add_argument(
|
||||||
|
"--dryrun",
|
||||||
|
action="store_true",
|
||||||
|
help="Preview users to be cleared without altering the database",
|
||||||
|
)
|
||||||
|
|
||||||
|
def handle(self, *args, **options): # pylint: disable=unused-argument
|
||||||
|
|
||||||
|
# Check for anything fishy
|
||||||
|
bad_state = models.User.objects.filter(is_deleted=True, is_active=True)
|
||||||
|
if bad_state.exists():
|
||||||
|
raise CommandError(
|
||||||
|
f"{bad_state.count()} user(s) marked as both active and deleted"
|
||||||
|
)
|
||||||
|
|
||||||
|
deleted_users = models.User.objects.filter(is_deleted=True)
|
||||||
|
self.stdout.write(f"Found {deleted_users.count()} deleted users")
|
||||||
|
if options["dryrun"]:
|
||||||
|
self.stdout.write("\n".join(u.username for u in deleted_users[:5]))
|
||||||
|
if deleted_users.count() > 5:
|
||||||
|
self.stdout.write("... and more")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
self.stdout.write("Erasing user data:")
|
||||||
|
for user_id in deleted_users.values_list("id", flat=True):
|
||||||
|
erase_user_data.delay(user_id)
|
||||||
|
self.stdout.write(".", ending="")
|
||||||
|
|
||||||
|
self.stdout.write("")
|
||||||
|
self.stdout.write("Tasks created successfully")
|
|
@ -523,6 +523,20 @@ class KeyPair(ActivitypubMixin, BookWyrmModel):
|
||||||
return super().save(*args, **kwargs)
|
return super().save(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
@app.task(queue=MISC)
|
||||||
|
def erase_user_data(user_id):
|
||||||
|
"""Erase any custom data about this user asynchronously
|
||||||
|
This is for deleted historical user data that pre-dates data
|
||||||
|
being cleared automatically"""
|
||||||
|
user = User.objects.get(id=user_id)
|
||||||
|
user.erase_user_data()
|
||||||
|
user.save(
|
||||||
|
broadcast=False,
|
||||||
|
update_fields=["email", "avatar", "preview_image", "summary", "name"],
|
||||||
|
)
|
||||||
|
user.erase_user_statuses(broadcast=False)
|
||||||
|
|
||||||
|
|
||||||
@app.task(queue=MISC)
|
@app.task(queue=MISC)
|
||||||
def set_remote_server(user_id, allow_external_connections=False):
|
def set_remote_server(user_id, allow_external_connections=False):
|
||||||
"""figure out the user's remote server in the background"""
|
"""figure out the user's remote server in the background"""
|
||||||
|
|
3
bw-dev
3
bw-dev
|
@ -246,6 +246,9 @@ case "$CMD" in
|
||||||
remove_remote_user_preview_images)
|
remove_remote_user_preview_images)
|
||||||
runweb python manage.py remove_remote_user_preview_images
|
runweb python manage.py remove_remote_user_preview_images
|
||||||
;;
|
;;
|
||||||
|
erase_deleted_user_data)
|
||||||
|
runweb python manage.py erase_deleted_user_data "$@"
|
||||||
|
;;
|
||||||
copy_media_to_s3)
|
copy_media_to_s3)
|
||||||
awscommand "bookwyrm_media_volume:/images"\
|
awscommand "bookwyrm_media_volume:/images"\
|
||||||
"s3 cp /images s3://${AWS_STORAGE_BUCKET_NAME}/images\
|
"s3 cp /images s3://${AWS_STORAGE_BUCKET_NAME}/images\
|
||||||
|
|
Loading…
Reference in a new issue