Ticket #5177: ctremove.4.diff

File ctremove.4.diff, 3.6 KB (added by Rob Hudson <treborhudson@…>, 17 years ago)
  • AUTHORS

    diff --git a/AUTHORS b/AUTHORS
    index bc5c44b..fa2e89f 100644
    a b answer newbie questions, and generally made Django that much better:  
    143143    Sung-Jin Hong <serialx.net@gmail.com>
    144144    Richard House <Richard.House@i-logue.com>
    145145    Robert Rock Howard <http://djangomojo.com/>
     146    Rob Hudson <http://rob.cogit8.org/>
    146147    Jason Huggins <http://www.jrandolph.com/blog/>
    147148    Hyun Mi Ae
    148149    Tom Insam
  • django/contrib/contenttypes/management.py

    diff --git a/django/contrib/contenttypes/management.py b/django/contrib/contenttypes/management.py
    index cb52e08..49083d6 100644
    a b  
    1 """
    2 Creates content types for all installed models.
    3 """
    4 
     1from django.contrib.contenttypes.models import ContentType
    52from django.dispatch import dispatcher
    63from django.db.models import get_apps, get_models, signals
    74from django.utils.encoding import smart_unicode
    85
    9 def create_contenttypes(app, created_models, verbosity=2):
    10     from django.contrib.contenttypes.models import ContentType
     6def update_contenttypes(app, created_models, verbosity=2):
     7    """
     8    Creates content types for models in the given app, removing any model
     9    entries that no longer have a matching model class.
     10    """
    1111    ContentType.objects.clear_cache()
     12    content_types = list(ContentType.objects.filter(app_label=app.__name__.split('.')[-2]))
    1213    app_models = get_models(app)
    1314    if not app_models:
    1415        return
    1516    for klass in app_models:
    1617        opts = klass._meta
    1718        try:
    18             ContentType.objects.get(app_label=opts.app_label,
    19                 model=opts.object_name.lower())
     19            ct = ContentType.objects.get(app_label=opts.app_label,
     20                                         model=opts.object_name.lower())
     21            content_types.remove(ct)
    2022        except ContentType.DoesNotExist:
    2123            ct = ContentType(name=smart_unicode(opts.verbose_name_raw),
    2224                app_label=opts.app_label, model=opts.object_name.lower())
    2325            ct.save()
    2426            if verbosity >= 2:
    2527                print "Adding content type '%s | %s'" % (ct.app_label, ct.model)
     28    # 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()
    2635
    27 def create_all_contenttypes(verbosity=2):
     36def update_all_contenttypes(verbosity=2):
    2837    for app in get_apps():
    29         create_contenttypes(app, None, verbosity)
     38        update_contenttypes(app, None, verbosity)
    3039
    31 dispatcher.connect(create_contenttypes, signal=signals.post_syncdb)
     40dispatcher.connect(update_contenttypes, signal=signals.post_syncdb)
    3241
    3342if __name__ == "__main__":
    34     create_all_contenttypes()
     43    update_all_contenttypes()
  • django/contrib/contenttypes/models.py

    diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py
    index cda47a1..1413586 100644
    a b class ContentTypeManager(models.Manager):  
    2525        """
    2626        Clear out the content-type cache. This needs to happen during database
    2727        flushes to prevent caching of "stale" content type IDs (see
    28         django.contrib.contenttypes.management.create_contenttypes for where
     28        django.contrib.contenttypes.management.update_contenttypes for where
    2929        this gets called).
    3030        """
    3131        global CONTENT_TYPE_CACHE
Back to Top