mirror of
https://github.com/daniviga/django-ram.git
synced 2025-08-04 13:17:50 +02:00
Keep media folder clean (#28)
* Reorg roster, portal and bookshelf media * Extend media reorg to consists * Delete roster and bookshelf images on delte. Do not delete others data that might be dedup! * Bump version
This commit is contained in:
49
ram/bookshelf/migrations/0009_alter_bookimage_image.py
Normal file
49
ram/bookshelf/migrations/0009_alter_bookimage_image.py
Normal file
@@ -0,0 +1,49 @@
|
||||
# Generated by Django 4.2.6 on 2023-10-30 13:16
|
||||
|
||||
import os
|
||||
import sys
|
||||
import shutil
|
||||
import ram.utils
|
||||
import bookshelf.models
|
||||
|
||||
from django.db import migrations, models
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
def move_images(apps, schema_editor):
|
||||
sys.stdout.write("\n Processing files. Please await...")
|
||||
for r in bookshelf.models.BookImage.objects.all():
|
||||
fname = os.path.basename(r.image.path)
|
||||
new_image = bookshelf.models.book_image_upload(r, fname)
|
||||
new_path = os.path.join(settings.MEDIA_ROOT, new_image)
|
||||
os.makedirs(os.path.dirname(new_path), exist_ok=True)
|
||||
try:
|
||||
shutil.move(r.image.path, new_path)
|
||||
except FileNotFoundError:
|
||||
sys.stderr.write(" !! FileNotFoundError: {}\n".format(new_image))
|
||||
pass
|
||||
r.image.name = new_image
|
||||
r.save()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("bookshelf", "0008_alter_author_options_alter_publisher_options"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name="bookimage",
|
||||
name="image",
|
||||
field=models.ImageField(
|
||||
blank=True,
|
||||
null=True,
|
||||
storage=ram.utils.DeduplicatedStorage,
|
||||
upload_to=bookshelf.models.book_image_upload,
|
||||
),
|
||||
),
|
||||
migrations.RunPython(
|
||||
move_images,
|
||||
reverse_code=migrations.RunPython.noop
|
||||
),
|
||||
]
|
@@ -1,3 +1,5 @@
|
||||
import os
|
||||
import shutil
|
||||
from uuid import uuid4
|
||||
from django.db import models
|
||||
from django.conf import settings
|
||||
@@ -70,13 +72,31 @@ class Book(models.Model):
|
||||
def get_absolute_url(self):
|
||||
return reverse("book", kwargs={"uuid": self.uuid})
|
||||
|
||||
def delete(self, *args, **kwargs):
|
||||
shutil.rmtree(
|
||||
os.path.join(
|
||||
settings.MEDIA_ROOT, "images", "books", str(self.uuid)
|
||||
),
|
||||
ignore_errors=True
|
||||
)
|
||||
super(Book, self).delete(*args, **kwargs)
|
||||
|
||||
|
||||
def book_image_upload(instance, filename):
|
||||
return os.path.join(
|
||||
"images",
|
||||
"books",
|
||||
str(instance.book.uuid),
|
||||
filename
|
||||
)
|
||||
|
||||
|
||||
class BookImage(Image):
|
||||
book = models.ForeignKey(
|
||||
Book, on_delete=models.CASCADE, related_name="image"
|
||||
)
|
||||
image = models.ImageField(
|
||||
upload_to="images/books/", # FIXME, find a better way to replace this
|
||||
upload_to=book_image_upload,
|
||||
storage=DeduplicatedStorage,
|
||||
null=True,
|
||||
blank=True
|
||||
|
Reference in New Issue
Block a user