django.contrib.contenttypes.management: 34 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/django/contrib/contenttypes/management.py

Stats: 0 executed, 31 missed, 3 excluded, 45 ignored

  1. from django.contrib.contenttypes.models import ContentType
  2. from django.db.models import get_apps, get_models, signals
  3. from django.utils.encoding import smart_unicode
  4. def update_contenttypes(app, created_models, verbosity=2, **kwargs):
  5. """
  6. Creates content types for models in the given app, removing any model
  7. entries that no longer have a matching model class.
  8. """
  9. ContentType.objects.clear_cache()
  10. app_models = get_models(app)
  11. if not app_models:
  12. return
  13. # They all have the same app_label, get the first one.
  14. app_label = app_models[0]._meta.app_label
  15. app_models = dict(
  16. (model._meta.object_name.lower(), model)
  17. for model in app_models
  18. )
  19. # Get all the content types
  20. content_types = dict(
  21. (ct.model, ct)
  22. for ct in ContentType.objects.filter(app_label=app_label)
  23. )
  24. to_remove = [
  25. ct
  26. for (model_name, ct) in content_types.iteritems()
  27. if model_name not in app_models
  28. ]
  29. cts = ContentType.objects.bulk_create([
  30. ContentType(
  31. name=smart_unicode(model._meta.verbose_name_raw),
  32. app_label=app_label,
  33. model=model_name,
  34. )
  35. for (model_name, model) in app_models.iteritems()
  36. if model_name not in content_types
  37. ])
  38. if verbosity >= 2:
  39. for ct in cts:
  40. print "Adding content type '%s | %s'" % (ct.app_label, ct.model)
  41. # Confirm that the content type is stale before deletion.
  42. if to_remove:
  43. if kwargs.get('interactive', False):
  44. content_type_display = '\n'.join([
  45. ' %s | %s' % (ct.app_label, ct.model)
  46. for ct in to_remove
  47. ])
  48. ok_to_delete = raw_input("""The following content types are stale and need to be deleted:
  49. %s
  50. Any objects related to these content types by a foreign key will also
  51. be deleted. Are you sure you want to delete these content types?
  52. If you're unsure, answer 'no'.
  53. Type 'yes' to continue, or 'no' to cancel: """ % content_type_display)
  54. else:
  55. ok_to_delete = False
  56. if ok_to_delete == 'yes':
  57. for ct in to_remove:
  58. if verbosity >= 2:
  59. print "Deleting stale content type '%s | %s'" % (ct.app_label, ct.model)
  60. ct.delete()
  61. else:
  62. if verbosity >= 2:
  63. print "Stale content types remain."
  64. def update_all_contenttypes(verbosity=2, **kwargs):
  65. for app in get_apps():
  66. update_contenttypes(app, None, verbosity, **kwargs)
  67. signals.post_syncdb.connect(update_contenttypes)
  68. if __name__ == "__main__":
  69. update_all_contenttypes()