Ticket #4528: pre_sync_updated.patch

File pre_sync_updated.patch, 4.2 KB (added by Silver_Ghost, 13 years ago)

Updated patch. Tests included.

  • django/core/management/commands/syncdb.py

    diff --git a/django/core/management/commands/syncdb.py b/django/core/management/commands/syncdb.py
    a b  
    55from django.conf import settings
    66from django.core.management.base import NoArgsCommand
    77from django.core.management.color import no_style
    8 from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
     8from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal, emit_pre_sync_signal
    99from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS
    1010from django.utils.datastructures import SortedDict
    1111from django.utils.importlib import import_module
     
    5454
    5555        db = options.get('database')
    5656        connection = connections[db]
     57        emit_pre_sync_signal(verbosity, interactive, db)
    5758        cursor = connection.cursor()
    5859
    5960        # Get a list of already installed *models* so that references work right.
  • django/core/management/sql.py

    diff --git a/django/core/management/sql.py b/django/core/management/sql.py
    a b  
    178178    return output
    179179
    180180
     181def emit_pre_sync_signal(verbosity, interactive, db):
     182    # Emit the pre_sync signal for every application.
     183    for app in models.get_apps():
     184        app_name = app.__name__.split('.')[-2]
     185        if verbosity >= 2:
     186            print "Running pre-sync handlers for application", app_name
     187        models.signals.pre_syncdb.send(sender=app, app=app,
     188                                       verbosity=verbosity,
     189                                       interactive=interactive,
     190                                       db=db)
     191
     192
    181193def emit_post_sync_signal(created_models, verbosity, interactive, db):
    182194    # Emit the post_sync signal for every application.
    183195    for app in models.get_apps():
     
    187199        models.signals.post_syncdb.send(sender=app, app=app,
    188200            created_models=created_models, verbosity=verbosity,
    189201            interactive=interactive, db=db)
     202
  • django/db/models/signals.py

    diff --git a/django/db/models/signals.py b/django/db/models/signals.py
    a b  
    1111pre_delete = Signal(providing_args=["instance", "using"])
    1212post_delete = Signal(providing_args=["instance", "using"])
    1313
     14pre_syncdb = Signal(providing_args=["app", "verbosity", "interactive", "db"])
    1415post_syncdb = Signal(providing_args=["class", "app", "created_models", "verbosity", "interactive"])
    1516
    1617m2m_changed = Signal(providing_args=["action", "instance", "reverse", "model", "pk_set", "using"])
  • new file tests/regressiontests/syncdb/__init__.py

    diff --git a/tests/regressiontests/syncdb/__init__.py b/tests/regressiontests/syncdb/__init__.py
    new file mode 100644
    - +  
     1
  • new file tests/regressiontests/syncdb/tests.py

    diff --git a/tests/regressiontests/syncdb/models.py b/tests/regressiontests/syncdb/models.py
    new file mode 100644
    diff --git a/tests/regressiontests/syncdb/tests.py b/tests/regressiontests/syncdb/tests.py
    new file mode 100644
    - +  
     1from django.db.models import signals
     2from django.dispatch import receiver
     3from django.test import TestCase
     4from django.core import management
     5
     6import models
     7
     8
     9PRE_SYNCDB_ARGS = ['app', 'verbosity', 'interactive', 'db']
     10SYNCDB_VERBOSITY = 1
     11SYNCDB_INTERACTIVE = False
     12
     13
     14class PreSyncdbReceiver(object):
     15    def __init__(self):
     16        self.run_counter = 0
     17        self.args = None
     18
     19    def __call__(self, signal, sender, **kwargs):
     20        self.run_counter = self.run_counter + 1
     21        self.args = kwargs
     22
     23
     24class SyncdbSignalTests(TestCase):
     25    def test_pre_syncdb_signal(self):
     26        r = PreSyncdbReceiver()
     27        signals.pre_syncdb.connect(r, sender=models)
     28        management.call_command('syncdb', verbosity=SYNCDB_VERBOSITY,
     29                                interactive=SYNCDB_INTERACTIVE)
     30
     31        self.assertEqual(r.run_counter, 1)
     32        self.assertItemsEqual(r.args, PRE_SYNCDB_ARGS)
     33        self.assertEqual(r.args['app'], models)
     34        self.assertEqual(r.args['verbosity'], SYNCDB_VERBOSITY)
     35        self.assertEqual(r.args['interactive'], SYNCDB_INTERACTIVE)
     36        self.assertEqual(r.args['db'], 'default')
Back to Top