Commits

Anonymous committed 47d076e

no longer blows up on syncdb

  • Participants
  • Parent commits 2229a90

Comments (0)

Files changed (1)

helptext/models.py

 from itertools import groupby
 
 from django.contrib.contenttypes.models import ContentType
+from django.core.exceptions import ImproperlyConfigured
 from django.db import models
 
-def register_model(model):
-    for mod, fields in groupby(model._meta.get_fields_with_model(),
-                               lambda x: x[1]):
-        if mod is None:
-            mod=model
-        fields=[x[0] for x in fields]
-        print "fields: ", list(fields)
-        content_type=ContentType.objects.get_for_model(mod)
-        for field in (f for f in fields if f.editable and not f.auto_created):
-            fh=FieldHelp.objects.filter(content_type=content_type,
-                                        field_name=field.name)
-            if fh:
-                assert len(fh)==1
-                fh=fh[0]
-            else:
-                fh=FieldHelp(content_type=content_type,
-                             field_name=field.name,
-                             help_text=field.help_text,
-                             original_help_text=field.help_text)
-                fh.save()
-            field.help_text=FieldHelpProxy(fh)
+def register_app(app_label):
+    model_list=models.get_models(app_label)
+    register_model(*model_list)
+
+
+def _is_synced(*mods):
+    from django.db import connection
+    all_tables=connection.introspection.table_names()
+    return set(connection.introspection.table_name_converter(x._meta.db_table) for x in mods).issubset(all_tables)
+
+def register_model(*model_list):
+    if not ContentType._meta.installed:
+        raise ImproperlyConfigured("Put 'django.contrib.contenttypes' in your INSTALLED_APPS "
+                                   "setting in order to use the helptext application.")
+
+    if not _is_synced(ContentType, FieldHelp):
+        # syncdb not run yet -- return silently
+        return
+
+    for model in model_list:
+
+        for mod, fields in groupby(model._meta.get_fields_with_model(),
+                                   lambda x: x[1]):
+            if mod is None:
+                mod=model
+            if not _is_synced(mod):
+                # probably not synced yet
+                continue
+            fields=[x[0] for x in fields]
+
+            content_type=ContentType.objects.get_for_model(mod)
+            for field in (f for f in fields if f.editable and not f.auto_created):
+                fh=FieldHelp.objects.filter(content_type=content_type,
+                                            field_name=field.name)
+                if fh:
+                    assert len(fh)==1
+                    fh=fh[0]
+                else:
+                    fh=FieldHelp(content_type=content_type,
+                                 field_name=field.name,
+                                 help_text=field.help_text,
+                                 original_help_text=field.help_text)
+                    fh.save()
+                field.help_text=FieldHelpProxy(fh)
 
 class FieldHelpProxy(object):
     def __init__(self, fh):