document: correct method delete (#16186)
This commit is contained in:
parent
1d18bde01c
commit
fd1fefe082
|
@ -17,7 +17,7 @@ from django.utils.translation import ugettext_lazy as _
|
|||
from django.utils.text import slugify
|
||||
from django.utils.http import urlquote
|
||||
from django.dispatch import receiver
|
||||
from django.db.models.signals import post_save
|
||||
from django.db.models.signals import post_save, post_delete
|
||||
from django.core.files.storage import default_storage
|
||||
|
||||
from jsonfield import JSONField
|
||||
|
@ -171,11 +171,6 @@ class Document(models.Model):
|
|||
magic_object.close()
|
||||
super(Document, self).save(*args, **kwargs)
|
||||
|
||||
def delete(self):
|
||||
'''Delete file on model delete'''
|
||||
self.document_file.delete()
|
||||
super(Document, self).delete()
|
||||
|
||||
@property
|
||||
def thumbnail_path(self):
|
||||
name = os.path.basename(self.content.name)
|
||||
|
@ -217,3 +212,9 @@ def create_thumbnail(sender, instance, created, **kwargs):
|
|||
instance.content.file.name,
|
||||
instance.thumbnail_full_path])
|
||||
threading.Thread(target=do).start()
|
||||
|
||||
@receiver(post_delete, sender=Document)
|
||||
def delete_file(sender, instance, **kwargs):
|
||||
if instance.content:
|
||||
if os.path.isfile(instance.content.path):
|
||||
os.remove(instance.content.path)
|
||||
|
|
|
@ -1,3 +1,12 @@
|
|||
import os
|
||||
import pytest
|
||||
|
||||
from django.core.files.base import ContentFile
|
||||
|
||||
from fargo.fargo.models import Document
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
def login(app, username='admin', password='admin', user=None):
|
||||
login_page = app.get('/login/')
|
||||
login_form = login_page.forms[0]
|
||||
|
@ -10,3 +19,12 @@ def login(app, username='admin', password='admin', user=None):
|
|||
resp = login_form.submit()
|
||||
assert resp.status_int == 302
|
||||
return app
|
||||
|
||||
def test_document_delete(app):
|
||||
f = ContentFile('A test file, ez pz.', 'test_file.txt')
|
||||
doc = Document.objects.get_by_file(f)
|
||||
file_path = doc.content.path
|
||||
assert os.path.isfile(file_path)
|
||||
|
||||
doc.delete()
|
||||
assert not os.path.isfile(file_path)
|
||||
|
|
Loading…
Reference in New Issue