Commits

Anonymous committed 7ee03d4

added support for emitting a post_syncdb signal.

  • Participants
  • Parent commits 9d96ab1

Comments (0)

Files changed (3)

 
-from django.db import connection, transaction
+from django.db import connection, transaction, models
+from django.dispatch import dispatcher
 
 class DatabaseOperations(object):
 
         Rolls back the current transaction.
         """
         transaction.rollback()
+        
+    def send_create_signal(self, app_label, model_names):
+        """
+        Sends a post_syncdb signal for the model specified.
+        
+        If the model is not found (perhaps it's been deleted?),
+        no signal is sent.
+        
+        TODO: The behavior of django.contrib.* apps seems flawed in that
+        they don't respect created_models.  Rather, they blindly execute
+        over all models within the app sending the signal.  This is a
+        patch we should push Django to make  For now, this should work.
+        """
+        app = models.get_app(app_label)
+        if not app:
+            return
+            
+        created_models = []
+        for model_name in model_names:
+            model = models.get_model(app_label, model_name)
+            if model:
+                created_models.append(model)
+                
+        if created_models:
+            # syncdb defaults -- perhaps take these as options?
+            verbosity = 1
+            interactive = True
+            dispatcher.send(signal=models.signals.post_syncdb, sender=app,
+                app=app, created_models=created_models,
+                verbosity=verbosity, interactive=interactive)
+            

management/commands/migrate.py

 from django.core.management.base import BaseCommand
 from django.core.management.color import no_style
+from django.conf import settings
 from django.db import models
 from optparse import make_option
 from south import migration
         # Turn on db debugging
         from south.db import db
         db.debug = True
+        
+        # NOTE: THIS IS DUPLICATED FROM django.core.management.commands.syncdb
+        # This code imports any module named 'management' in INSTALLED_APPS.
+        # The 'management' module is the preferred way of listening to post_syncdb
+        # signals, and since we're sending those out with create_table migrations,
+        # we need apps to behave correctly.
+        for app_name in settings.INSTALLED_APPS:
+            try:
+                __import__(app_name + '.management', {}, {}, [''])
+            except ImportError, exc:
+                msg = exc.args[0]
+                if not msg.startswith('No module named') or 'management' not in msg:
+                    raise
+        # END DJANGO DUPE CODE
+        
         # Migrate each app
         for app in models.get_apps():
             migrations = migration.get_migrations(app)

management/commands/startmigration.py

                 )
                 backwards += '''
         db.delete_table("%s")''' % m.m2m_db_table()
+                
+            forwards += '''
+        
+        db.send_create_signal('%s', ['%s'])''' % (model._meta.app_label, model._meta.object_name)
+        
         else:
             forwards = '"Write your forwards migration here"'
             backwards = '"Write your backwards migration here"'