Ticket #12339: interactive_contenttype_update.diff

File interactive_contenttype_update.diff, 2.3 KB (added by Kenneth Arnold, 15 years ago)
  • contrib/contenttypes/management.py

     
    2626            if verbosity >= 2:
    2727                print "Adding content type '%s | %s'" % (ct.app_label, ct.model)
    2828    # The presence of any remaining content types means the supplied app has an
    29     # undefined model and can safely be removed, which cascades to also remove
    30     # related permissions.
    31     for ct in content_types:
    32         if verbosity >= 2:
    33             print "Deleting stale content type '%s | %s'" % (ct.app_label, ct.model)
    34         ct.delete()
     29    # undefined model. It may not be safe to remove it, however, if other
     30    # models reference it by foreign key.
     31    if content_types:
     32        if kwargs.get('interactive', False):
     33            content_type_display = ', '.join(['%s | %s' % (ct.app_label, ct.model) for ct in content_types])
     34            ok_to_delete = raw_input("""The following content types are stale:
     35%s
     36Would you like to delete them? If another object references one of these by a foreign key, it will also get deleted, which may not be what you want.
     37If you're unsure, answer 'no'.
    3538
    36 def update_all_contenttypes(verbosity=2):
     39    Type 'yes' to continue, or 'no' to cancel: """ % content_type_display)
     40        else:
     41            ok_to_delete = False
     42
     43        if ok_to_delete == 'yes':
     44            for ct in content_types:
     45                if verbosity >= 2:
     46                    print "Deleting stale content type '%s | %s'" % (ct.app_label, ct.model)
     47                ct.delete()
     48        else:
     49            if verbosity >= 2:
     50                print """
     51Stale content types remain. To clean them up, call ct.delete on each of the
     52following objects after verifying that they are not referenced anywhere:
     53
     54>>> from django.contrib.contenttypes.models import ContentType"""
     55                for ct in content_types:
     56                    print ">>> ct = ContentType.objects.get(app_label=%r, model=%r)" % (ct.app_label, ct.model)
     57
     58
     59def update_all_contenttypes(verbosity=2, **kwargs):
    3760    for app in get_apps():
    38         update_contenttypes(app, None, verbosity)
     61        update_contenttypes(app, None, verbosity, **kwargs)
    3962
    4063signals.post_syncdb.connect(update_contenttypes)
    4164
Back to Top