conventionschedule-android/webstorage/webproj/bff/helpers.py

23 lines
760 B
Python

import os
import uuid
from django.utils.deconstruct import deconstructible
from django.core.files.storage import default_storage
from django.db.models import FileField
@deconstructible
class RandomFileName(object):
def __init__(self, path):
self.path = os.path.join(path, "%s%s")
def __call__(self, _, filename):
# @note It's up to the validators to check if it's the correct file type in name or if one even exist.
extension = os.path.splitext(filename)[1]
return self.path % (uuid.uuid4(), extension)
def file_cleanup(sender, instance, **kwargs):
for field in sender._meta.get_fields():
if field and isinstance(field, FileField):
default_storage.delete(getattr(instance, field.name).path)