magic-removal: slight event cleanup

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1721 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Robert Wittams 2005-12-18 00:59:11 +00:00
parent 4f609faf3f
commit 39c6f4fb88
7 changed files with 53 additions and 36 deletions

View File

@ -16,7 +16,7 @@ from django.db.models.fields.related import *
from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
from django.db.models.exceptions import FieldDoesNotExist, BadKeywordArguments
from django.db.models.signals import Signals
from django.db.models import signals
# Admin stages.

View File

@ -1,12 +1,13 @@
from django.db.models.manipulators import ModelAddManipulator, ModelChangeManipulator
import django.db.models.manipulators
import django.db.models.manager
from django.db.models.fields import AutoField
from django.db.models.fields.related import OneToOne, ManyToOne
from django.db.models.related import RelatedObject
from django.db.models.manager import Manager
from django.db.models.query import orderlist2sql
from django.db.models.options import Options
from django.db import connection, backend
from django.db.models.signals import Signals
from django.db.models import signals
from django.dispatch import dispatcher
from django.core.exceptions import ObjectDoesNotExist
@ -133,7 +134,7 @@ class Model(object):
return not self.__eq__(other)
def __init__(self, *args, **kwargs):
dispatcher.send( signal = Signals.pre_init, sender = self.__class__, args=args, kwargs=kwargs)
dispatcher.send( signal = signals.pre_init, sender = self.__class__, args=args, kwargs=kwargs)
if kwargs:
for f in self._meta.fields:
if isinstance(f.rel, ManyToOne):
@ -164,26 +165,15 @@ class Model(object):
raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]
for i, arg in enumerate(args):
setattr(self, self._meta.fields[i].attname, arg)
dispatcher.send( signal = Signals.post_init, sender = self.__class__, instance=self)
dispatcher.send( signal = signals.post_init, sender = self.__class__, instance=self)
def _prepare(cls):
if not hasattr(cls, '_default_manager'):
# Create the default manager, if needed.
if hasattr(cls, 'objects'):
raise ValueError, "Model %s must specify a custom Manager, because it has a field named 'objects'" % name
cls.add_to_class('objects', Manager())
cls.add_to_class( 'AddManipulator', ModelAddManipulator)
cls.add_to_class( 'ChangeManipulator', ModelChangeManipulator)
# Creates some methods once self._meta has been populated.
if cls._meta.order_with_respect_to:
cls.get_next_in_order = curry(cls._get_next_or_previous_in_order, is_next=True)
cls.get_previous_in_order = curry(cls._get_next_or_previous_in_order, is_next=False)
dispatcher.send( signal = Signals.class_prepared, sender = cls)
dispatcher.send( signal = signals.class_prepared, sender = cls)
#RelatedField.do_pending_lookups(cls)
_prepare = classmethod(_prepare)
@ -192,7 +182,7 @@ class Model(object):
# Run any pre-save hooks.
if hasattr(self, '_pre_save'):
self._pre_save()
dispatcher.send( signal=Signals.pre_save, sender = self.__class__, instance = self )
dispatcher.send( signal=signals.pre_save, sender = self.__class__, instance = self )
non_pks = [f for f in self._meta.fields if not f.primary_key]
cursor = connection.cursor()
@ -237,7 +227,7 @@ class Model(object):
connection.commit()
# Run any post-save hooks.
dispatcher.send(signal=Signals.pre_save, sender = self.__class__, instance = self )
dispatcher.send(signal=signals.pre_save, sender = self.__class__, instance = self )
if hasattr(self, '_post_save'):
self._post_save()
@ -292,7 +282,7 @@ class Model(object):
if hasattr(instance, '_pre_delete'):
instance._pre_delete()
dispatcher.send(signal=Signals.pre_delete, sender = cls, instance = instance )
dispatcher.send(signal=signals.pre_delete, sender = cls, instance = instance )
for related in cls._meta.get_all_related_many_to_many_objects():
cursor.execute("DELETE FROM %s WHERE %s=%%s" % \
@ -322,7 +312,7 @@ class Model(object):
setattr(self, cls._meta.pk.attname, None)
dispatcher.send(signal=Signals.post_delete, sender = cls, instance = instance )
dispatcher.send(signal=signals.post_delete, sender = cls, instance = instance )
if hasattr(instance, '_post_delete'):
instance._post_delete()

View File

@ -1,4 +1,4 @@
from django.db.models.signals import Signals
from django.db.models import signals
from django.dispatch import dispatcher
from django.conf import settings
from django.core import formfields, validators
@ -507,7 +507,7 @@ class FileField(Field):
setattr(cls, 'save_%s_file' % self.name, curry(cls._save_FIELD_file, field=self))
dispatcher.connect(
self.delete_file,
signal = Signals.post_delete,
signal = signals.post_delete,
sender = cls
)

View File

@ -3,7 +3,7 @@ from django.db.models.related import RelatedObject
from django.utils.translation import gettext_lazy, string_concat
from django.utils.functional import curry
from django.core import formfields
from django.db.models.signals import Signals
from django.db.models import signals
from django.dispatch import dispatcher
@ -18,7 +18,7 @@ class RelatedField(object):
dispatcher.connect(
lambda sender: RelatedField.do_pending_lookups(sender) ,
signal = Signals.class_prepared,
signal = signals.class_prepared,
weak = False)
@ -320,7 +320,7 @@ class ManyToManyFieldNew(RelatedField):
# We need to wait until the class we were in was fully defined
dispatcher.connect(
self.from_prepared,
signal = Signals.class_prepared,
signal = signals.class_prepared,
sender = self.from_
)
@ -340,6 +340,7 @@ class ManyToManyFieldNew(RelatedField):
M2M.__name__ = "M2M_%s_%s_%s" % (self.name,self.from_.__name__, self.to.__name__)
class ManyToOne:
def __init__(self, to, field_name, num_in_admin=3, min_num_in_admin=None,
max_num_in_admin=None, num_extra_on_change=1, edit_inline=False,

View File

@ -3,11 +3,26 @@ from django.utils.functional import curry
from django.db import backend, connection
from django.db.models.query import Q, parse_lookup, fill_table_cache, get_cached_row
from django.db.models.query import handle_legacy_orderlist, orderlist2sql, orderfield2column
from django.dispatch import dispatcher
from django.db.models import signals
# Size of each "chunk" for get_iterator calls.
# Larger values are slightly faster at the expense of more storage space.
GET_ITERATOR_CHUNK_SIZE = 100
def ensure_default_manager(sender):
cls = sender
if not hasattr(cls, '_default_manager'):
# Create the default manager, if needed.
if hasattr(cls, 'objects'):
raise ValueError, "Model %s must specify a custom Manager, because it has a field named 'objects'" % name
cls.add_to_class('objects', Manager())
dispatcher.connect(
ensure_default_manager,
signal=signals.class_prepared
)
class Manager(object):
# Tracks each time a Manager instance is created. Used to retain order.
creation_counter = 0

View File

@ -1,6 +1,18 @@
from django.core.exceptions import ObjectDoesNotExist
from django.core.formfields import Manipulator
from django.db.models.fields import FileField, AutoField
from django.dispatch import dispatcher
from django.db.models import signals
def add_manipulators(sender):
cls = sender
cls.add_to_class( 'AddManipulator', ModelAddManipulator)
cls.add_to_class( 'ChangeManipulator', ModelChangeManipulator)
dispatcher.connect(
add_manipulators,
signal=signals.class_prepared
)
class ManipulatorDescriptor(object):
class empty:

View File

@ -1,4 +1,3 @@
class Signals(object):
class_prepared = object()
pre_init= object()