Code

Ticket #6064: 6064-using-signals.diff

File 6064-using-signals.diff, 7.8 KB (added by floguy, 7 years ago)

Changed the implementation method to emit a signal instead of execute a tuple from the settings.

Line 
1Index: django/db/backends/ado_mssql/base.py
2===================================================================
3--- django/db/backends/ado_mssql/base.py        (revision 6888)
4+++ django/db/backends/ado_mssql/base.py        (working copy)
5@@ -5,6 +5,8 @@
6 """
7 
8 from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseOperations, util
9+from django.dispatch import dispatcher
10+from django.db.backends import signals
11 try:
12     import adodbapi as Database
13 except ImportError, e:
14@@ -109,4 +111,5 @@
15             # TODO: Handle DATABASE_PORT.
16             conn_string = "PROVIDER=SQLOLEDB;DATA SOURCE=%s;UID=%s;PWD=%s;DATABASE=%s" % (settings.DATABASE_HOST, settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME)
17             self.connection = Database.connect(conn_string)
18+        dispatcher.send(signal=signals.connection_created, sender=self.__class__)
19         return self.connection.cursor()
20Index: django/db/backends/mysql_old/base.py
21===================================================================
22--- django/db/backends/mysql_old/base.py        (revision 6888)
23+++ django/db/backends/mysql_old/base.py        (working copy)
24@@ -5,6 +5,8 @@
25 """
26 
27 from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseOperations, util
28+from django.dispatch import dispatcher
29+from django.db.backends import signals
30 from django.utils.encoding import force_unicode
31 try:
32     import MySQLdb as Database
33@@ -194,6 +196,7 @@
34                     self.connection.set_character_set('utf8')
35         else:
36             cursor = self.connection.cursor()
37+        dispatcher.send(signal=signals.connection_created, sender=self.__class__)
38         return cursor
39 
40     def make_debug_cursor(self, cursor):
41Index: django/db/backends/postgresql/base.py
42===================================================================
43--- django/db/backends/postgresql/base.py       (revision 6888)
44+++ django/db/backends/postgresql/base.py       (working copy)
45@@ -6,6 +6,8 @@
46 
47 from django.utils.encoding import smart_str, smart_unicode
48 from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, util
49+from django.dispatch import dispatcher
50+from django.db.backends import signals
51 from django.db.backends.postgresql.operations import DatabaseOperations
52 try:
53     import psycopg as Database
54@@ -102,6 +104,7 @@
55             cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE])
56         cursor.execute("SET client_encoding to 'UNICODE'")
57         cursor = UnicodeCursorWrapper(cursor, 'utf-8')
58+        dispatcher.send(signal=signals.connection_created, sender=self.__class__)
59         return cursor
60 
61 def typecast_string(s):
62Index: django/db/backends/sqlite3/base.py
63===================================================================
64--- django/db/backends/sqlite3/base.py  (revision 6888)
65+++ django/db/backends/sqlite3/base.py  (working copy)
66@@ -7,6 +7,8 @@
67 """
68 
69 from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseOperations, util
70+from django.dispatch import dispatcher
71+from django.db.backends import signals
72 try:
73     try:
74         from sqlite3 import dbapi2 as Database
75@@ -112,6 +114,7 @@
76             self.connection.create_function("django_extract", 2, _sqlite_extract)
77             self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
78             self.connection.create_function("regexp", 2, _sqlite_regexp)
79+        dispatcher.send(signal=signals.connection_created, sender=self.__class__)
80         return self.connection.cursor(factory=SQLiteCursorWrapper)
81 
82     def close(self):
83Index: django/db/backends/mysql/base.py
84===================================================================
85--- django/db/backends/mysql/base.py    (revision 6888)
86+++ django/db/backends/mysql/base.py    (working copy)
87@@ -5,6 +5,8 @@
88 """
89 
90 from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseOperations, util
91+from django.dispatch import dispatcher
92+from django.db.backends import signals
93 try:
94     import MySQLdb as Database
95 except ImportError, e:
96@@ -180,6 +182,7 @@
97                 kwargs['port'] = int(settings.DATABASE_PORT)
98             kwargs.update(self.options)
99             self.connection = Database.connect(**kwargs)
100+        dispatcher.send(signal=signals.connection_created, sender=self.__class__)
101         cursor = self.connection.cursor()
102         return cursor
103 
104Index: django/db/backends/oracle/base.py
105===================================================================
106--- django/db/backends/oracle/base.py   (revision 6888)
107+++ django/db/backends/oracle/base.py   (working copy)
108@@ -5,6 +5,8 @@
109 """
110 
111 from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseOperations, util
112+from django.dispatch import dispatcher
113+from django.db.backends import signals
114 from django.utils.datastructures import SortedDict
115 from django.utils.encoding import smart_str, force_unicode
116 import datetime
117@@ -432,6 +434,7 @@
118         # Set oracle date to ansi date format.
119         cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD'")
120         cursor.execute("ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'")
121+        dispatcher.send(signal=signals.connection_created, sender=self.__class__)
122         return cursor
123 
124 class FormatStylePlaceholderCursor(Database.Cursor):
125Index: django/db/backends/signals.py
126===================================================================
127--- django/db/backends/signals.py       (revision 0)
128+++ django/db/backends/signals.py       (revision 0)
129@@ -0,0 +1 @@
130+connection_created = object()
131\ No newline at end of file
132Index: django/db/backends/postgresql_psycopg2/base.py
133===================================================================
134--- django/db/backends/postgresql_psycopg2/base.py      (revision 6888)
135+++ django/db/backends/postgresql_psycopg2/base.py      (working copy)
136@@ -6,6 +6,8 @@
137 
138 from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures
139 from django.db.backends.postgresql.operations import DatabaseOperations as PostgresqlDatabaseOperations
140+from django.dispatch import dispatcher
141+from django.db.backends import signals
142 from django.utils.safestring import SafeUnicode
143 try:
144     import psycopg2 as Database
145@@ -73,4 +75,5 @@
146         cursor.tzinfo_factory = None
147         if set_tz:
148             cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE])
149+        dispatcher.send(signal=signals.connection_created, sender=self.__class__)
150         return cursor
151Index: tests/modeltests/signals/models.py
152===================================================================
153--- tests/modeltests/signals/models.py  (revision 6888)
154+++ tests/modeltests/signals/models.py  (working copy)
155@@ -2,8 +2,10 @@
156 Testing signals before/after saving and deleting.
157 """
158 
159+from django.dispatch import dispatcher
160 from django.db import models
161-from django.dispatch import dispatcher
162+from django.db import connection
163+from django.db.backends.signals import connection_created
164 
165 class Person(models.Model):
166     first_name = models.CharField(max_length=20)
167@@ -32,12 +34,22 @@
168     print 'post_delete signal,', instance
169     print 'instance.id is None: %s' % (instance.id == None) 
170 
171+def connection_created_test(sender, **kwargs):
172+    print 'connection_created signal'
173+
174 __test__ = {'API_TESTS':"""
175+>>> dispatcher.connect(connection_created_test, signal=connection_created)
176 >>> dispatcher.connect(pre_save_test, signal=models.signals.pre_save)
177 >>> dispatcher.connect(post_save_test, signal=models.signals.post_save)
178 >>> dispatcher.connect(pre_delete_test, signal=models.signals.pre_delete)
179 >>> dispatcher.connect(post_delete_test, signal=models.signals.post_delete)
180 
181+>>> cursor = connection.cursor()
182+connection_created signal
183+
184+>>> dispatcher.disconnect(connection_created_test, signal=connection_created)
185+>>> cursor = connection.cursor()
186+
187 >>> p1 = Person(first_name='John', last_name='Smith')
188 >>> p1.save()
189 pre_save signal, John Smith