~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to django/contrib/contenttypes/management.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant, Eddy Mulyono
  • Date: 2008-09-16 12:18:47 UTC
  • mfrom: (1.1.5 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080916121847-mg225rg5mnsdqzr0
Tags: 1.0-1ubuntu1
* Merge from Debian (LP: #264191), remaining changes:
  - Run test suite on build.

[Eddy Mulyono]
* Update patch to workaround network test case failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
Creates content types for all installed models.
3
 
"""
4
 
 
5
 
from django.dispatch import dispatcher
 
1
from django.contrib.contenttypes.models import ContentType
6
2
from django.db.models import get_apps, get_models, signals
 
3
from django.utils.encoding import smart_unicode
7
4
 
8
 
def create_contenttypes(app, created_models, verbosity=2):
9
 
    from django.contrib.contenttypes.models import ContentType
 
5
def update_contenttypes(app, created_models, verbosity=2, **kwargs):
 
6
    """
 
7
    Creates content types for models in the given app, removing any model
 
8
    entries that no longer have a matching model class.
 
9
    """
10
10
    ContentType.objects.clear_cache()
 
11
    content_types = list(ContentType.objects.filter(app_label=app.__name__.split('.')[-2]))
11
12
    app_models = get_models(app)
12
13
    if not app_models:
13
14
        return
14
15
    for klass in app_models:
15
16
        opts = klass._meta
16
17
        try:
17
 
            ContentType.objects.get(app_label=opts.app_label,
18
 
                model=opts.object_name.lower())
 
18
            ct = ContentType.objects.get(app_label=opts.app_label,
 
19
                                         model=opts.object_name.lower())
 
20
            content_types.remove(ct)
19
21
        except ContentType.DoesNotExist:
20
 
            ct = ContentType(name=str(opts.verbose_name),
 
22
            ct = ContentType(name=smart_unicode(opts.verbose_name_raw),
21
23
                app_label=opts.app_label, model=opts.object_name.lower())
22
24
            ct.save()
23
25
            if verbosity >= 2:
24
26
                print "Adding content type '%s | %s'" % (ct.app_label, ct.model)
 
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()
25
34
 
26
 
def create_all_contenttypes(verbosity=2):
 
35
def update_all_contenttypes(verbosity=2):
27
36
    for app in get_apps():
28
 
        create_contenttypes(app, None, verbosity)
 
37
        update_contenttypes(app, None, verbosity)
29
38
 
30
 
dispatcher.connect(create_contenttypes, signal=signals.post_syncdb)
 
39
signals.post_syncdb.connect(update_contenttypes)
31
40
 
32
41
if __name__ == "__main__":
33
 
    create_all_contenttypes()
 
42
    update_all_contenttypes()