Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5177 -- Changed content type creation to also remove the types…

… for any orphaned entries (so it's now an "update" feature). Thanks, Rob Hudson.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6287 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7c27f3e7baeb5393aeb06c476fdb52ea3a7cdc65 1 parent 8e970f4
Malcolm Tredinnick authored September 15, 2007
1  AUTHORS
@@ -149,6 +149,7 @@ answer newbie questions, and generally made Django that much better:
149 149
     Sung-Jin Hong <serialx.net@gmail.com>
150 150
     Richard House <Richard.House@i-logue.com>
151 151
     Robert Rock Howard <http://djangomojo.com/>
  152
+    Rob Hudson <http://rob.cogit8.org/>
152 153
     Jason Huggins <http://www.jrandolph.com/blog/>
153 154
     Hyun Mi Ae
154 155
     Tom Insam
33  django/contrib/contenttypes/management.py
... ...
@@ -1,34 +1,43 @@
1  
-"""
2  
-Creates content types for all installed models.
3  
-"""
4  
-
  1
+from django.contrib.contenttypes.models import ContentType
5 2
 from django.dispatch import dispatcher
6 3
 from django.db.models import get_apps, get_models, signals
7 4
 from django.utils.encoding import smart_unicode
8 5
 
9  
-def create_contenttypes(app, created_models, verbosity=2):
10  
-    from django.contrib.contenttypes.models import ContentType
  6
+def 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
+    """
11 11
     ContentType.objects.clear_cache()
  12
+    content_types = list(ContentType.objects.filter(app_label=app.__name__.split('.')[-2]))
12 13
     app_models = get_models(app)
13 14
     if not app_models:
14 15
         return
15 16
     for klass in app_models:
16 17
         opts = klass._meta
17 18
         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)
20 22
         except ContentType.DoesNotExist:
21 23
             ct = ContentType(name=smart_unicode(opts.verbose_name_raw),
22 24
                 app_label=opts.app_label, model=opts.object_name.lower())
23 25
             ct.save()
24 26
             if verbosity >= 2:
25 27
                 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()
26 35
 
27  
-def create_all_contenttypes(verbosity=2):
  36
+def update_all_contenttypes(verbosity=2):
28 37
     for app in get_apps():
29  
-        create_contenttypes(app, None, verbosity)
  38
+        update_contenttypes(app, None, verbosity)
30 39
 
31  
-dispatcher.connect(create_contenttypes, signal=signals.post_syncdb)
  40
+dispatcher.connect(update_contenttypes, signal=signals.post_syncdb)
32 41
 
33 42
 if __name__ == "__main__":
34  
-    create_all_contenttypes()
  43
+    update_all_contenttypes()
2  django/contrib/contenttypes/models.py
@@ -25,7 +25,7 @@ def clear_cache(self):
25 25
         """
26 26
         Clear out the content-type cache. This needs to happen during database
27 27
         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
29 29
         this gets called).
30 30
         """
31 31
         global CONTENT_TYPE_CACHE

0 notes on commit 7c27f3e

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