Ticket #1763: unittest.patch
File unittest.patch, 18.7 KB (added by , 19 years ago) |
---|
-
django/db/models/base.py
7 7 from django.db.models.related import RelatedObject 8 8 from django.db.models.query import orderlist2sql, delete_objects 9 9 from django.db.models.options import Options, AdminOptions 10 from django.db import connection, backend, transaction 10 from django.db import backend, transaction 11 import django.db 11 12 from django.db.models import signals 12 13 from django.db.models.loading import register_models 13 14 from django.dispatch import dispatcher … … 146 147 dispatcher.send(signal=signals.pre_save, sender=self.__class__, instance=self) 147 148 148 149 non_pks = [f for f in self._meta.fields if not f.primary_key] 149 cursor = connection.cursor()150 cursor = django.db.connection.cursor() 150 151 151 152 # First, try an UPDATE. If that doesn't update anything, do an INSERT. 152 153 pk_val = self._get_pk_val() … … 353 354 rel = rel_field.rel.to 354 355 m2m_table = rel_field.m2m_db_table() 355 356 this_id = self._get_pk_val() 356 cursor = connection.cursor()357 cursor = django.db.connection.cursor() 357 358 cursor.execute("DELETE FROM %s WHERE %s = %%s" % \ 358 359 (backend.quote_name(m2m_table), 359 360 backend.quote_name(rel_field.m2m_column_name())), [this_id]) … … 371 372 # ORDERING METHODS ######################### 372 373 373 374 def method_set_order(ordered_obj, self, id_list): 374 cursor = connection.cursor()375 cursor = django.db.connection.cursor() 375 376 # Example: "UPDATE poll_choices SET _order = %s WHERE poll_id = %s AND id = %s" 376 377 sql = "UPDATE %s SET %s = %%s WHERE %s = %%s AND %s = %%s" % \ 377 378 (backend.quote_name(ordered_obj.db_table), backend.quote_name('_order'), … … 382 383 transaction.commit_unless_managed() 383 384 384 385 def method_get_order(ordered_obj, self): 385 cursor = connection.cursor()386 cursor = django.db.connection.cursor() 386 387 # Example: "SELECT id FROM poll_choices WHERE poll_id = %s ORDER BY _order" 387 388 sql = "SELECT %s FROM %s WHERE %s = %%s ORDER BY %s" % \ 388 389 (backend.quote_name(ordered_obj._meta.pk.column), -
django/db/models/manager.py
1 1 from django.utils.functional import curry 2 from django.db import backend , connection2 from django.db import backend 3 3 from django.db.models.query import QuerySet 4 4 from django.dispatch import dispatcher 5 5 from django.db.models import signals -
django/db/models/fields/related.py
1 from django.db import backend, connection,transaction1 from django.db import backend, transaction 2 2 from django.db.models import signals 3 3 from django.db.models.fields import AutoField, Field, IntegerField, get_ul_class 4 4 from django.db.models.related import RelatedObject … … 7 7 from django.core import validators 8 8 from django import forms 9 9 from django.dispatch import dispatcher 10 import django.db 10 11 11 12 # For Python 2.3 12 13 if not hasattr(__builtins__, 'set'): … … 282 283 # source_col_name: the PK colname in join_table for the source object 283 284 # target_col_name: the PK colname in join_table for the target object 284 285 # *objs - objects to add 285 from django.db import connection286 286 287 287 # Add the newly created or already existing objects to the join table. 288 288 # First find out which items are already added, to avoid adding them twice 289 289 new_ids = set([obj._get_pk_val() for obj in objs]) 290 cursor = connection.cursor()290 cursor = django.db.connection.cursor() 291 291 cursor.execute("SELECT %s FROM %s WHERE %s = %%s AND %s IN (%s)" % \ 292 292 (target_col_name, self.join_table, source_col_name, 293 293 target_col_name, ",".join(['%s'] * len(new_ids))), … … 308 308 # source_col_name: the PK colname in join_table for the source object 309 309 # target_col_name: the PK colname in join_table for the target object 310 310 # *objs - objects to remove 311 from django.db import connection312 311 313 312 for obj in objs: 314 313 if not isinstance(obj, self.model): 315 314 raise ValueError, "objects to remove() must be %s instances" % self.model._meta.object_name 316 315 # Remove the specified objects from the join table 317 cursor = connection.cursor()316 cursor = django.db.connection.cursor() 318 317 for obj in objs: 319 318 cursor.execute("DELETE FROM %s WHERE %s = %%s AND %s = %%s" % \ 320 319 (self.join_table, source_col_name, target_col_name), … … 323 322 324 323 def _clear_items(self, source_col_name): 325 324 # source_col_name: the PK colname in join_table for the source object 326 from django.db import connection 327 cursor = connection.cursor() 325 cursor = django.db.connection.cursor() 328 326 cursor.execute("DELETE FROM %s WHERE %s = %%s" % \ 329 327 (self.join_table, source_col_name), 330 328 [self._pk_val]) -
django/db/models/__init__.py
1 1 from django.conf import settings 2 2 from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured 3 3 from django.core import validators 4 from django.db import backend , connection4 from django.db import backend 5 5 from django.db.models.loading import get_apps, get_app, get_models, get_model, register_models 6 6 from django.db.models.query import Q 7 7 from django.db.models.manager import Manager -
django/db/models/query.py
1 from django.db import backend, connection,transaction1 from django.db import backend, transaction 2 2 from django.db.models.fields import DateField, FieldDoesNotExist 3 3 from django.db.models import signals 4 4 from django.dispatch import dispatcher 5 5 from django.utils.datastructures import SortedDict 6 6 import django.db 7 7 import operator 8 8 9 9 # For Python 2.3 … … 152 152 # undefined, so we convert it to a list of tuples. 153 153 extra_select = self._select.items() 154 154 155 cursor = connection.cursor()155 cursor = django.db.connection.cursor() 156 156 select, sql, params = self._get_sql_clause() 157 157 cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) 158 158 fill_cache = self._select_related … … 178 178 counter._limit = None 179 179 counter._select_related = False 180 180 select, sql, params = counter._get_sql_clause() 181 cursor = connection.cursor()181 cursor = django.db.connection.cursor() 182 182 cursor.execute("SELECT COUNT(*)" + sql, params) 183 183 return cursor.fetchone()[0] 184 184 … … 457 457 columns = [f.column for f in self.model._meta.fields] 458 458 field_names = [f.attname for f in self.model._meta.fields] 459 459 460 cursor = connection.cursor()460 cursor = django.db.connection.cursor() 461 461 select, sql, params = self._get_sql_clause() 462 462 select = ['%s.%s' % (backend.quote_name(self.model._meta.db_table), backend.quote_name(c)) for c in columns] 463 463 cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) … … 484 484 sql = 'SELECT %s %s GROUP BY 1 ORDER BY 1 %s' % \ 485 485 (backend.get_date_trunc_sql(self._kind, '%s.%s' % (backend.quote_name(self.model._meta.db_table), 486 486 backend.quote_name(self._field.column))), sql, self._order) 487 cursor = connection.cursor()487 cursor = django.db.connection.cursor() 488 488 cursor.execute(sql, params) 489 489 # We have to manually run typecast_timestamp(str()) on the results, because 490 490 # MySQL doesn't automatically cast the result of date functions as datetime … … 829 829 ordered_classes = seen_objs.keys() 830 830 ordered_classes.reverse() 831 831 832 cursor = connection.cursor()832 cursor = django.db.connection.cursor() 833 833 834 834 for cls in ordered_classes: 835 835 seen_objs[cls] = seen_objs[cls].items() -
django/db/transaction.py
13 13 """ 14 14 15 15 import thread 16 from django.db import connection 16 import django.db 17 17 from django.conf import settings 18 18 19 19 class TransactionManagementError(Exception): … … 120 120 if top: 121 121 top[-1] = flag 122 122 if not flag and is_dirty(): 123 connection._commit()123 django.db.connection._commit() 124 124 set_clean() 125 125 else: 126 126 raise TransactionManagementError("This code isn't under transaction management") … … 130 130 Commits changes if the system is not in managed transaction mode. 131 131 """ 132 132 if not is_managed(): 133 connection._commit()133 django.db.connection._commit() 134 134 else: 135 135 set_dirty() 136 136 … … 139 139 Rolls back changes if the system is not in managed transaction mode. 140 140 """ 141 141 if not is_managed(): 142 connection._rollback()142 django.db.connection._rollback() 143 143 else: 144 144 set_dirty() 145 145 … … 147 147 """ 148 148 Does the commit itself and resets the dirty flag. 149 149 """ 150 connection._commit()150 django.db.connection._commit() 151 151 set_clean() 152 152 153 153 def rollback(): 154 154 """ 155 155 This function does the rollback itself and resets the dirty flag. 156 156 """ 157 connection._rollback()157 django.db.connection._rollback() 158 158 set_clean() 159 159 160 160 ############## -
django/core/context_processors.py
25 25 context_extras = {} 26 26 if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS: 27 27 context_extras['debug'] = True 28 from django.db import connection29 context_extras['sql_queries'] = connection.queries28 import django.db 29 context_extras['sql_queries'] = django.db.connection.queries 30 30 return context_extras 31 31 32 32 def i18n(request): -
django/core/management.py
63 63 64 64 def _get_table_list(): 65 65 "Gets a list of all db tables that are physically installed." 66 from django.db import connection,get_introspection_module67 cursor = connection.cursor()66 from django.db import get_introspection_module 67 cursor = django.db.connection.cursor() 68 68 return get_introspection_module().get_table_list(cursor) 69 69 70 70 # If the foreign key points to an AutoField, a PositiveIntegerField or a … … 244 244 245 245 def get_sql_delete(app): 246 246 "Returns a list of the DROP TABLE SQL statements for the given app." 247 from django.db import backend, connection,models, get_introspection_module247 from django.db import backend, models, get_introspection_module 248 248 introspection = get_introspection_module() 249 249 250 250 # This should work even if a connecton isn't available 251 251 try: 252 cursor = connection.cursor()252 cursor = django.db.connection.cursor() 253 253 except: 254 254 cursor = None 255 255 … … 308 308 # directly into a database client, to avoid locking issues. 309 309 if cursor: 310 310 cursor.close() 311 connection.close()311 django.db.connection.close() 312 312 313 313 return output[::-1] # Reverse it, to deal with table dependencies. 314 314 get_sql_delete.help_doc = "Prints the DROP TABLE SQL statements for the given app name(s)." … … 409 409 410 410 def syncdb(): 411 411 "Creates the database tables for all apps in INSTALLED_APPS whose tables haven't already been created." 412 from django.db import connection,transaction, models, get_creation_module412 from django.db import transaction, models, get_creation_module 413 413 from django.db.models import signals 414 414 from django.conf import settings 415 415 from django.dispatch import dispatcher … … 429 429 430 430 data_types = get_creation_module().DATA_TYPES 431 431 432 cursor = connection.cursor()432 cursor = django.db.connection.cursor() 433 433 434 434 # Get a list of all existing database tables, 435 435 # so we know what needs to be added. … … 543 543 544 544 def install(app): 545 545 "Executes the equivalent of 'get_sql_all' in the current database." 546 from django.db import connection,transaction546 from django.db import transaction 547 547 548 548 app_name = app.__name__.split('.')[-2] 549 549 … … 555 555 sql_list = get_sql_all(app) 556 556 557 557 try: 558 cursor = connection.cursor()558 cursor = django.db.connection.cursor() 559 559 for sql in sql_list: 560 560 cursor.execute(sql) 561 561 except Exception, e: … … 573 573 574 574 def reset(app): 575 575 "Executes the equivalent of 'get_sql_reset' in the current database." 576 from django.db import connection,transaction576 from django.db import transaction 577 577 from cStringIO import StringIO 578 578 app_name = app.__name__.split('.')[-2] 579 579 … … 591 591 Type 'yes' to continue, or 'no' to cancel: """) 592 592 if confirm == 'yes': 593 593 try: 594 cursor = connection.cursor()594 cursor = django.db.connection.cursor() 595 595 for sql in sql_list: 596 596 cursor.execute(sql) 597 597 except Exception, e: … … 670 670 671 671 def inspectdb(): 672 672 "Generator that introspects the tables in the given database name and returns a Django model, one line at a time." 673 from django.db import connection,get_introspection_module673 from django.db import get_introspection_module 674 674 from django.conf import settings 675 675 import keyword 676 676 … … 680 680 object_name = table_name.title().replace('_', '') 681 681 return object_name.endswith('s') and object_name[:-1] or object_name 682 682 683 cursor = connection.cursor()683 cursor = django.db.connection.cursor() 684 684 yield "# This is an auto-generated Django model module." 685 685 yield "# You'll have to do the following manually to clean this up:" 686 686 yield "# * Rearrange models' order" … … 999 999 1000 1000 def createcachetable(tablename): 1001 1001 "Creates the table needed to use the SQL cache backend" 1002 from django.db import backend, connection,transaction, get_creation_module, models1002 from django.db import backend, transaction, get_creation_module, models 1003 1003 data_types = get_creation_module().DATA_TYPES 1004 1004 fields = ( 1005 1005 # "key" is a reserved word in MySQL, so use "cache_key" instead. … … 1026 1026 for i, line in enumerate(table_output): 1027 1027 full_statement.append(' %s%s' % (line, i < len(table_output)-1 and ',' or '')) 1028 1028 full_statement.append(');') 1029 curs = connection.cursor()1029 curs = django.db.connection.cursor() 1030 1030 curs.execute("\n".join(full_statement)) 1031 1031 for statement in index_output: 1032 1032 curs.execute(statement) -
django/core/cache/backends/db.py
1 1 "Database cache backend." 2 2 3 3 from django.core.cache.backends.base import BaseCache 4 from django.db import connection,transaction4 from django.db import transaction 5 5 import base64, time 6 6 from datetime import datetime 7 7 try: … … 25 25 self._cull_frequency = 3 26 26 27 27 def get(self, key, default=None): 28 cursor = connection.cursor()28 cursor = django.db.connection.cursor() 29 29 cursor.execute("SELECT cache_key, value, expires FROM %s WHERE cache_key = %%s" % self._table, [key]) 30 30 row = cursor.fetchone() 31 31 if row is None: … … 40 40 def set(self, key, value, timeout=None): 41 41 if timeout is None: 42 42 timeout = self.default_timeout 43 cursor = connection.cursor()43 cursor = django.db.connection.cursor() 44 44 cursor.execute("SELECT COUNT(*) FROM %s" % self._table) 45 45 num = cursor.fetchone()[0] 46 46 now = datetime.now().replace(microsecond=0) … … 61 61 transaction.commit_unless_managed() 62 62 63 63 def delete(self, key): 64 cursor = connection.cursor()64 cursor = django.db.connection.cursor() 65 65 cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key]) 66 66 transaction.commit_unless_managed() 67 67 68 68 def has_key(self, key): 69 cursor = connection.cursor()69 cursor = django.db.connection.cursor() 70 70 cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key]) 71 71 return cursor.fetchone() is not None 72 72 -
django/contrib/auth/models.py
1 1 from django.core import validators 2 from django.db import backend, connection, models 2 from django.db import backend, models 3 import django.db 3 4 from django.contrib.contenttypes.models import ContentType 4 5 from django.utils.translation import gettext_lazy as _ 5 6 import datetime … … 130 131 "Returns a list of permission strings that this user has through his/her groups." 131 132 if not hasattr(self, '_group_perm_cache'): 132 133 import sets 133 cursor = connection.cursor()134 cursor = django.db.connection.cursor() 134 135 # The SQL below works out to the following, after DB quoting: 135 136 # cursor.execute(""" 136 137 # SELECT ct."app_label", p."codename" -
django/bin/daily_cleanup.py
1 1 "Daily cleanup file" 2 2 3 from django.db import backend, connection, transaction 3 from django.db import backend, transaction 4 import django.db 4 5 5 6 DOCUMENTATION_DIRECTORY = '/home/html/documentation/' 6 7 7 8 def clean_up(): 8 9 # Clean up old database records 9 cursor = connection.cursor()10 cursor = django.db.connection.cursor() 10 11 cursor.execute("DELETE FROM %s WHERE %s < NOW()" % \ 11 12 (backend.quote_name('core_sessions'), backend.quote_name('expire_date'))) 12 13 cursor.execute("DELETE FROM %s WHERE %s < NOW() - INTERVAL '1 week'" % \