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