Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12339 -- Made content type deletion an interactive process to …

…prevent accidentally cascade deleting content from a production database. Thanks to kcarnold for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12782 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 45a56e637ea5e356b2f276a7ddbb4335f3c39b35 1 parent beca4b8
Russell Keith-Magee authored March 15, 2010

Showing 1 changed file with 27 additions and 9 deletions. Show diff stats Hide diff stats

  1. 36  django/contrib/contenttypes/management.py
36  django/contrib/contenttypes/management.py
@@ -25,16 +25,34 @@ def update_contenttypes(app, created_models, verbosity=2, **kwargs):
25 25
             if verbosity >= 2:
26 26
                 print "Adding content type '%s | %s'" % (ct.app_label, ct.model)
27 27
     # The presence of any remaining content types means the supplied app has an
28  
-    # undefined model and can safely be removed, which cascades to also remove
29  
-    # related permissions.
30  
-    for ct in content_types:
31  
-        if verbosity >= 2:
32  
-            print "Deleting stale content type '%s | %s'" % (ct.app_label, ct.model)
33  
-        ct.delete()
34  
-
35  
-def update_all_contenttypes(verbosity=2):
  28
+    # undefined model. Confirm that the content type is stale before deletion.
  29
+    if content_types:
  30
+        if kwargs.get('interactive', False):
  31
+            content_type_display = '\n'.join(['    %s | %s' % (ct.app_label, ct.model) for ct in content_types])
  32
+            ok_to_delete = raw_input("""The following content types are stale and need to be deleted:
  33
+
  34
+%s
  35
+
  36
+Any objects related to these content types by a foreign key will also
  37
+be deleted. Are you sure you want to delete these content types?
  38
+If you're unsure, answer 'no'.
  39
+
  40
+    Type 'yes' to continue, or 'no' to cancel: """ % content_type_display)
  41
+        else:
  42
+            ok_to_delete = False
  43
+
  44
+        if ok_to_delete == 'yes':
  45
+            for ct in content_types:
  46
+                if verbosity >= 2:
  47
+                    print "Deleting stale content type '%s | %s'" % (ct.app_label, ct.model)
  48
+                ct.delete()
  49
+        else:
  50
+            if verbosity >= 2:
  51
+                print "Stale content types remain."
  52
+
  53
+def update_all_contenttypes(verbosity=2, **kwargs):
36 54
     for app in get_apps():
37  
-        update_contenttypes(app, None, verbosity)
  55
+        update_contenttypes(app, None, verbosity, **kwargs)
38 56
 
39 57
 signals.post_syncdb.connect(update_contenttypes)
40 58
 

0 notes on commit 45a56e6

Please sign in to comment.
Something went wrong with that request. Please try again.