from __future__ import unicode_literals

from django.db import models
from django.conf import settings

import django.db.models.signals
import django.dispatch.dispatcher

import web.core

import re
import os
import uuid

class File(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    slug = models.CharField(max_length=64, default=web.core.random_slug_default_length, editable=False)
    author = models.ForeignKey(settings.AUTH_USER_MODEL)
    file = models.FileField(upload_to=web.core.get_file_path)
    created = models.DateTimeField(auto_now_add=True)
    expiry = models.DateTimeField(default=web.core.default_expiry, blank=True)

    def delete_file(self):
        return self.file.delete()

    def __unicode__(self):
        return self.file.name


@django.dispatch.dispatcher.receiver(django.db.models.signals.post_delete, sender=File)
def file_delete(sender, instance, **kwargs):
    instance.delete_file()

from __future__ import unicode_literals

from django.db import models
from django.conf import settings

import django.db.models.signals
import django.dispatch.dispatcher

import web.core

import re
import os
import uuid

class File(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    slug = models.CharField(max_length=64, default=web.core.random_slug_default_length, editable=False)
    author = models.ForeignKey(settings.AUTH_USER_MODEL)
    file = models.FileField(upload_to=web.core.get_file_path)
    created = models.DateTimeField(auto_now_add=True)
    expiry = models.DateTimeField(default=web.core.default_expiry, blank=True)

    def delete_file(self):
        base_path = os.path.dirname(self.file.name)
        os.unlink(self.file.name)
        os.rmdir(base_path)

    def __unicode__(self):
        return self.file.name


@django.dispatch.dispatcher.receiver(django.db.models.signals.post_delete, sender=File)
def file_delete(sender, instance, **kwargs):
    instance.delete_file()

Remove directories when deleting files
Remove directories when deleting files

  from __future__ import unicode_literals
  
  from django.db import models
  from django.conf import settings
  
  import django.db.models.signals
  import django.dispatch.dispatcher
  
  import web.core
  
  import re
  import os
  import uuid
  
  class File(models.Model):
      id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
      slug = models.CharField(max_length=64, default=web.core.random_slug_default_length, editable=False)
      author = models.ForeignKey(settings.AUTH_USER_MODEL)
      file = models.FileField(upload_to=web.core.get_file_path)
      created = models.DateTimeField(auto_now_add=True)
      expiry = models.DateTimeField(default=web.core.default_expiry, blank=True)
  
      def delete_file(self):
-         return self.file.delete()
+         base_path = os.path.dirname(self.file.name)
+         os.unlink(self.file.name)
+         os.rmdir(base_path)
  
      def __unicode__(self):
          return self.file.name
  
  
  @django.dispatch.dispatcher.receiver(django.db.models.signals.post_delete, sender=File)
  def file_delete(sender, instance, **kwargs):
      instance.delete_file()
  
Remove directories when deleting files
## Code Before:
from __future__ import unicode_literals

from django.db import models
from django.conf import settings

import django.db.models.signals
import django.dispatch.dispatcher

import web.core

import re
import os
import uuid

class File(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    slug = models.CharField(max_length=64, default=web.core.random_slug_default_length, editable=False)
    author = models.ForeignKey(settings.AUTH_USER_MODEL)
    file = models.FileField(upload_to=web.core.get_file_path)
    created = models.DateTimeField(auto_now_add=True)
    expiry = models.DateTimeField(default=web.core.default_expiry, blank=True)

    def delete_file(self):
        return self.file.delete()

    def __unicode__(self):
        return self.file.name


@django.dispatch.dispatcher.receiver(django.db.models.signals.post_delete, sender=File)
def file_delete(sender, instance, **kwargs):
    instance.delete_file()

## Instruction:
Remove directories when deleting files
## Code After:
from __future__ import unicode_literals

from django.db import models
from django.conf import settings

import django.db.models.signals
import django.dispatch.dispatcher

import web.core

import re
import os
import uuid

class File(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    slug = models.CharField(max_length=64, default=web.core.random_slug_default_length, editable=False)
    author = models.ForeignKey(settings.AUTH_USER_MODEL)
    file = models.FileField(upload_to=web.core.get_file_path)
    created = models.DateTimeField(auto_now_add=True)
    expiry = models.DateTimeField(default=web.core.default_expiry, blank=True)

    def delete_file(self):
        base_path = os.path.dirname(self.file.name)
        os.unlink(self.file.name)
        os.rmdir(base_path)

    def __unicode__(self):
        return self.file.name


@django.dispatch.dispatcher.receiver(django.db.models.signals.post_delete, sender=File)
def file_delete(sender, instance, **kwargs):
    instance.delete_file()
