mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-01-08 16:25:27 +00:00
Add remove_remote_user_preview_images command
This commit is contained in:
parent
8c34b23c35
commit
09c095cfec
3 changed files with 66 additions and 0 deletions
|
@ -0,0 +1,40 @@
|
|||
""" Generate preview images """
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db.models import Q
|
||||
|
||||
from bookwyrm import models, preview_images
|
||||
|
||||
|
||||
# pylint: disable=line-too-long
|
||||
class Command(BaseCommand):
|
||||
"""Remove preview images for remote users"""
|
||||
|
||||
help = "Remove preview images for remote users"
|
||||
|
||||
# pylint: disable=no-self-use,unused-argument
|
||||
def handle(self, *args, **options):
|
||||
"""generate preview images"""
|
||||
self.stdout.write(
|
||||
" | Hello! I will be removing preview images from remote users."
|
||||
)
|
||||
self.stdout.write(
|
||||
"🧑🚒 ⎨ This might take quite long if your instance has a lot of remote users."
|
||||
)
|
||||
self.stdout.write(" | ✧ Thank you for your patience ✧")
|
||||
|
||||
users = models.User.objects.filter(local=False).exclude(Q(preview_image='')|Q(preview_image=None))
|
||||
|
||||
if len(users) > 0:
|
||||
self.stdout.write(
|
||||
f" → Remote user preview images ({len(users)}): ", ending=""
|
||||
)
|
||||
for user in users:
|
||||
preview_images.remove_user_preview_image_task.delay(user.id)
|
||||
self.stdout.write(".", ending="")
|
||||
self.stdout.write(" OK 🖼")
|
||||
else:
|
||||
self.stdout.write(
|
||||
f" | There was no remote users with preview images."
|
||||
)
|
||||
|
||||
self.stdout.write("🧑🚒 ⎨ I’m all done! ✧ Enjoy ✧")
|
|
@ -475,3 +475,25 @@ def generate_user_preview_image_task(user_id):
|
|||
image = generate_preview_image(texts=texts, picture=avatar)
|
||||
|
||||
save_and_cleanup(image, instance=user)
|
||||
|
||||
|
||||
@app.task(queue=LOW)
|
||||
def remove_user_preview_image_task(user_id):
|
||||
"""remove preview_image for a user"""
|
||||
if not settings.ENABLE_PREVIEW_IMAGES:
|
||||
return
|
||||
|
||||
user = models.User.objects.get(id=user_id)
|
||||
|
||||
try:
|
||||
file_name = user.preview_image.name
|
||||
except ValueError:
|
||||
file_name = None
|
||||
|
||||
# Delete image in model
|
||||
user.preview_image.delete(save=False)
|
||||
user.save(broadcast=False, update_fields=["preview_image"])
|
||||
|
||||
# Delete image file
|
||||
if file_name and default_storage.exists(file_name):
|
||||
default_storage.delete(file_name)
|
||||
|
|
4
bw-dev
4
bw-dev
|
@ -223,6 +223,9 @@ case "$CMD" in
|
|||
generate_preview_images)
|
||||
runweb python manage.py generate_preview_images "$@"
|
||||
;;
|
||||
remove_remote_user_preview_images)
|
||||
runweb python manage.py remove_remote_user_preview_images
|
||||
;;
|
||||
copy_media_to_s3)
|
||||
awscommand "bookwyrm_media_volume:/images"\
|
||||
"s3 cp /images s3://${AWS_STORAGE_BUCKET_NAME}/images\
|
||||
|
@ -300,6 +303,7 @@ case "$CMD" in
|
|||
echo " populate_suggestions"
|
||||
echo " generate_thumbnails"
|
||||
echo " generate_preview_images [--all]"
|
||||
echo " remove_remote_user_preview_images"
|
||||
echo " copy_media_to_s3"
|
||||
echo " sync_media_to_s3"
|
||||
echo " set_cors_to_s3 [cors file]"
|
||||
|
|
Loading…
Reference in a new issue