[damned-lies] Delete attached files along with their action



commit 59947b348087a5f94fe389bd87f539c80c3eeffd
Author: Claude Paroz <claude 2xlibre net>
Date:   Thu Jan 27 20:58:10 2011 +0100

    Delete attached files along with their action
    
    See http://code.djangoproject.com/ticket/6456
    Django does not automatically delete file fields any more.

 vertimus/models.py         |   22 +++++++++++++---------
 vertimus/tests/__init__.py |    6 +++---
 2 files changed, 16 insertions(+), 12 deletions(-)
---
diff --git a/vertimus/models.py b/vertimus/models.py
index baa4110..df3a09a 100644
--- a/vertimus/models.py
+++ b/vertimus/models.py
@@ -26,7 +26,7 @@ from django.contrib.sites.models import Site
 from django.core import mail, urlresolvers
 from django.db import models
 from django.db.models import Max
-from django.db.models.signals import post_save, pre_delete
+from django.db.models.signals import post_save, post_delete
 from django.utils.translation import get_language, activate, ugettext, ugettext_lazy as _
 
 from stats.models import Branch, Domain, Statistics
@@ -421,7 +421,7 @@ class ActionDbArchived(models.Model):
             ).filter(max_created__lt=datetime.now()-timedelta(days=days)):
             # Call each action delete() so as file is also deleted
             for act in ActionDbArchived.objects.filter(sequence=action['sequence']):
-                action.delete()
+                act.delete()
 
 class ActionAbstract(object):
     """Abstract class"""
@@ -830,16 +830,20 @@ def merge_uploaded_file(sender, instance, **kwargs):
         instance.merge_file_with_pot(potfile)
 post_save.connect(merge_uploaded_file, sender=ActionDb)
 
-def delete_merged_file(sender, instance, **kwargs):
+def delete_action_files(sender, instance, **kwargs):
     """
-    pre_delete callback for ActionDb that deletes the merged file from upload
+    post_delete callback for ActionDb that deletes the file + the merged file from upload
     directory.
     """
-    if instance.file and instance.file.path.endswith('.po'):
-        merged_file = instance.file.path[:-3] + ".merged.po"
-        if os.access(merged_file, os.W_OK):
-             os.remove(merged_file)
-pre_delete.connect(delete_merged_file, sender=ActionDb)
+    if instance.file:
+        if instance.file.path.endswith('.po'):
+            merged_file = instance.file.path[:-3] + ".merged.po"
+            if os.access(merged_file, os.W_OK):
+                 os.remove(merged_file)
+        if os.access(instance.file.path, os.W_OK):
+             os.remove(instance.file.path)
+post_delete.connect(delete_action_files, sender=ActionDb)
+post_delete.connect(delete_action_files, sender=ActionDbArchived)
 
 """ The following string is just reproduced from a template so as a translator comment
     can be added (comments are not supported in templates) """
diff --git a/vertimus/tests/__init__.py b/vertimus/tests/__init__.py
index 2087c75..f02298c 100644
--- a/vertimus/tests/__init__.py
+++ b/vertimus/tests/__init__.py
@@ -298,7 +298,7 @@ class VertimusTest(TeamsAndRolesTests):
         state.save()
 
         file_path = os.path.join(settings.MEDIA_ROOT, action.file.name)
-        self.assert_(os.access(file_path, os.W_OK))
+        self.assertTrue(os.access(file_path, os.W_OK))
 
         action = ActionAbstract.new_by_name('TC')
         state = state.apply_action(action, self.pc, "To commit.")
@@ -312,13 +312,13 @@ class VertimusTest(TeamsAndRolesTests):
         state = state.apply_action(action, self.pc, "Committed.")
         state.save()
 
-        self.assert_(not os.access(file_path, os.F_OK))
+        self.assertTrue(not os.access(file_path, os.F_OK), "%s not deleted" % file_path)
 
         # Remove test file
         action_db_archived = ActionDbArchived.objects.get(comment="Done.")
         filename_archived = os.path.join(settings.MEDIA_ROOT, action_db_archived.file.name)
         action_db_archived.delete()
-        self.assert_(not os.access(filename_archived, os.F_OK))
+        self.assertTrue(not os.access(filename_archived, os.F_OK), "%s not deleted" % filename_archived)
 
     def test_action_tr(self):
         state = StateDb(branch=self.b, domain=self.d, language=self.l, name='Translated').get_state()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]