feat: make media uploaded files generate uuid name

This commit is contained in:
Dariusz Niemczyk 2024-01-14 21:47:19 +01:00
parent 81417f58be
commit ab631aeb90
No known key found for this signature in database
1 changed files with 9 additions and 1 deletions

View File

@ -1,4 +1,5 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import os
import uuid import uuid
import re import re
@ -129,9 +130,16 @@ class Item(models.Model, TreeModelMixin):
ordering = ("path",) ordering = ("path",)
class ImageFieldWithUuid(models.ImageField):
def generate_filename(self, instance, filename):
ext = filename.split(".")[-1]
filename = "%s.%s" % (uuid.uuid4(), ext)
return super().generate_filename(instance, filename)
class ItemImage(models.Model): class ItemImage(models.Model):
item = models.ForeignKey(Item, related_name="images", on_delete=models.CASCADE) item = models.ForeignKey(Item, related_name="images", on_delete=models.CASCADE)
image = models.ImageField() image = ImageFieldWithUuid()
def __str__(self): def __str__(self):
return "{}".format(self.image.name) return "{}".format(self.image.name)