When playing with the thought of adding images to my books DB i thought: I need random names, and would like to scale them ..
So I looked a bit and found django-stdimage. I was pretty happy with what it could do, but the uuid4 names themselves seemed a bit .. not what I wanted .. So I came up with adding the objects pk into the filename as well.
There were some nice ways already to generate filenames, but none exactly what I wanted.
Here is my own class UploadToClassNameDirPKUUID
:
class UploadToClassNameDirPKUUID(UploadToClassNameDir):
def __call__(self, instance, filename):
# slightly modified from the UploadToUUId class from stdimage.utils
if instance.pk:
self.kwargs.update({
'name': '{}-{}'.format(instance.pk, uuid4().hex),
})
else:
# no pk found so just get uuid4.hex
self.kwargs.update({
'name': uuid4().hex
})
return super().__call__(instance, filename)
Basically the same as UploadToClassNameDirUUID, but with instance.pk added in the front of the filename - this is purely convenience for me so I have the 2 pictures for my book (front&back) identifiable in the directory without looking both up in my DB. One could maybe argue it would "expose" the pk, but first in this case I do not really care as the app is not public and 2nd: anyone who can access my django-admin (which is what I use for data entry,..) would see the pk anyway so whatever ;)
Share on Twitter Share on Facebook
Comments
There are currently no comments
New Comment