Skip to content

Instantly share code, notes, and snippets.

@hirokiky
Last active December 18, 2015 00:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirokiky/5697905 to your computer and use it in GitHub Desktop.
Save hirokiky/5697905 to your computer and use it in GitHub Desktop.
A customed FileSystemStorage of Django providing asynchronous deletion of files.
from django.db import models
from .storage import AsyncFileSystemStorage
a_fs = AsyncFileSystemStorage()
class UploadedFile(models.Model):
image = models.ImageField('Uploaded image file', upload_to='.', storage=a_fs)
description = models.TextField('Description of uploaded image file')
def delete(self, *args, **kwargs):
self.image.delete()
super(File, self).delete()
from django.core.files.storage import FileSystemStorage
from .tasks import async_file_delete
class AsyncFileSystemStorage(FileSystemStorage):
def delete(self, name):
file_path = self.path(name)
async_file_delete.delay(file_path)
import os
from celery import task
@task
def async_file_delete(file_path):
if os.path.exists(file_path):
try:
os.remove(file_path)
except OSError:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment