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:
2023-10-31 11:16:55 +01:00
committed by GitHub
parent 416ca5bbc6
commit 830da80302
12 changed files with 302 additions and 12 deletions

View File

@@ -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