Skip to content

Commit 322a50a

Browse files
committed
[1.1.X] 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.
Backport of r12782 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12783 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent c64669a commit 322a50a

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

django/contrib/contenttypes/management.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,34 @@ def update_contenttypes(app, created_models, verbosity=2, **kwargs):
2525
if verbosity >= 2:
2626
print "Adding content type '%s | %s'" % (ct.app_label, ct.model)
2727
# 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):
3654
for app in get_apps():
37-
update_contenttypes(app, None, verbosity)
55+
update_contenttypes(app, None, verbosity, **kwargs)
3856

3957
signals.post_syncdb.connect(update_contenttypes)
4058

0 commit comments

Comments
 (0)