mirror of
https://github.com/django/django.git
synced 2025-06-05 03:29:12 +00:00
magic-removal: More small code cleanups
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1727 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
fc27500389
commit
5907e9337f
@ -1,10 +1,9 @@
|
||||
from django.db.models import signals
|
||||
from django.db.models.fields import Field, IntegerField
|
||||
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 import signals
|
||||
|
||||
from django.dispatch import dispatcher
|
||||
|
||||
# Values for Relation.edit_inline.
|
||||
@ -17,30 +16,28 @@ class RelatedField(object):
|
||||
pending_lookups = {}
|
||||
|
||||
dispatcher.connect(
|
||||
lambda sender: RelatedField.do_pending_lookups(sender) ,
|
||||
lambda sender: RelatedField.do_pending_lookups(sender),
|
||||
signal = signals.class_prepared,
|
||||
weak = False)
|
||||
|
||||
|
||||
def add_lookup(cls, rel_cls, field):
|
||||
name = field.rel.to
|
||||
module = rel_cls.__module__
|
||||
key = (module, name)
|
||||
cls.pending_lookups.setdefault(key,[]).append( (rel_cls, field) )
|
||||
cls.pending_lookups.setdefault(key, []).append((rel_cls, field))
|
||||
add_lookup = classmethod(add_lookup)
|
||||
|
||||
def do_pending_lookups(cls, other_cls):
|
||||
key = (other_cls.__module__, other_cls.__name__)
|
||||
for (rel_cls,field) in cls.pending_lookups.setdefault(key,[]):
|
||||
for rel_cls, field in cls.pending_lookups.setdefault(key,[]):
|
||||
field.rel.to = other_cls
|
||||
field.do_related_class(other_cls, rel_cls)
|
||||
do_pending_lookups = classmethod(do_pending_lookups)
|
||||
|
||||
|
||||
def contribute_to_class(self, cls, name):
|
||||
sup = super(RelatedField,self)
|
||||
sup = super(RelatedField, self)
|
||||
if hasattr(sup, 'contribute_to_class'):
|
||||
sup.contribute_to_class(cls,name)
|
||||
sup.contribute_to_class(cls, name)
|
||||
other = self.rel.to
|
||||
if isinstance(other, basestring):
|
||||
if other == RECURSIVE_RELATIONSHIP_CONSTANT:
|
||||
@ -59,7 +56,6 @@ class RelatedField(object):
|
||||
related = RelatedObject(other._meta, cls, self)
|
||||
self.contribute_to_related_class(other, related)
|
||||
|
||||
|
||||
#HACK
|
||||
class SharedMethods(RelatedField):
|
||||
def get_attname(self):
|
||||
@ -74,7 +70,7 @@ class SharedMethods(RelatedField):
|
||||
# EXAMPLES: Choice.get_poll(), Story.get_dateline()
|
||||
setattr(cls, 'get_%s' % self.name, curry(cls._get_foreign_key_object, field_with_rel=self))
|
||||
|
||||
class ForeignKey(SharedMethods,Field):
|
||||
class ForeignKey(SharedMethods, Field):
|
||||
empty_strings_allowed = False
|
||||
def __init__(self, to, to_field=None, **kwargs):
|
||||
try:
|
||||
@ -124,7 +120,7 @@ class ForeignKey(SharedMethods,Field):
|
||||
else:
|
||||
field_objs = [formfields.SelectField]
|
||||
params['choices'] = self.get_choices_default()
|
||||
return (field_objs,params)
|
||||
return (field_objs, params)
|
||||
|
||||
def get_manipulator_field_objs(self):
|
||||
rel_field = self.rel.get_related_field()
|
||||
@ -133,7 +129,7 @@ class ForeignKey(SharedMethods,Field):
|
||||
else:
|
||||
return [formfields.IntegerField]
|
||||
|
||||
def get_db_prep_save(self,value):
|
||||
def get_db_prep_save(self, value):
|
||||
if value == '' or value == None:
|
||||
return None
|
||||
else:
|
||||
@ -173,7 +169,6 @@ class ForeignKey(SharedMethods,Field):
|
||||
func = lambda self, *args, **kwargs: self._add_related(related.model, related.field, *args, **kwargs)
|
||||
setattr(cls, 'add_%s' % rel_obj_name, func)
|
||||
|
||||
|
||||
class OneToOneField(SharedMethods, IntegerField):
|
||||
def __init__(self, to, to_field=None, **kwargs):
|
||||
kwargs['verbose_name'] = kwargs.get('verbose_name', 'ID')
|
||||
@ -205,7 +200,7 @@ class OneToOneField(SharedMethods, IntegerField):
|
||||
rel_class=related.model, rel_field=related.field))
|
||||
|
||||
|
||||
class ManyToManyField(RelatedField,Field):
|
||||
class ManyToManyField(RelatedField, Field):
|
||||
def __init__(self, to, **kwargs):
|
||||
kwargs['verbose_name'] = kwargs.get('verbose_name', None)
|
||||
kwargs['rel'] = ManyToMany(to, kwargs.pop('singular', None),
|
||||
@ -333,13 +328,11 @@ class ManyToManyFieldNew(RelatedField):
|
||||
id_to = self.from_._meta.db_table
|
||||
id_from = self.to._meta.db_table
|
||||
|
||||
M2M.add_to_class(id_from, ForeignKey(to=self.from_) )
|
||||
M2M.add_to_class(id_to, ForeignKey(to=self.to) )
|
||||
M2M.add_to_class(id_from, ForeignKey(to=self.from_))
|
||||
M2M.add_to_class(id_to, ForeignKey(to=self.to))
|
||||
M2M._meta.db_table = '%s_%s' % (self.from_._meta.db_table, self.name)
|
||||
M2M._meta.unique_together = ((id_to, id_from),)
|
||||
M2M.__name__ = "M2M_%s_%s_%s" % (self.name,self.from_.__name__, self.to.__name__)
|
||||
|
||||
|
||||
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,
|
||||
@ -372,7 +365,6 @@ class OneToOne(ManyToOne):
|
||||
self.lookup_overrides = lookup_overrides or {}
|
||||
self.raw_id_admin = raw_id_admin
|
||||
|
||||
|
||||
class ManyToMany:
|
||||
def __init__(self, to, singular=None, num_in_admin=0, related_name=None,
|
||||
filter_interface=None, limit_choices_to=None, raw_id_admin=False):
|
||||
|
@ -6,8 +6,8 @@ from django.db.models import signals
|
||||
|
||||
def add_manipulators(sender):
|
||||
cls = sender
|
||||
cls.add_to_class( 'AddManipulator', ModelAddManipulator)
|
||||
cls.add_to_class( 'ChangeManipulator', ModelChangeManipulator)
|
||||
cls.add_to_class('AddManipulator', ModelAddManipulator)
|
||||
cls.add_to_class('ChangeManipulator', ModelChangeManipulator)
|
||||
|
||||
dispatcher.connect(
|
||||
add_manipulators,
|
||||
@ -39,14 +39,12 @@ class ManipulatorDescriptor(object):
|
||||
return self.man
|
||||
|
||||
def get_base_manipulator(self, type):
|
||||
|
||||
if hasattr(type, 'MANIPULATOR'):
|
||||
man = type.MANIPULATOR
|
||||
else:
|
||||
man = self.empty
|
||||
return man
|
||||
|
||||
|
||||
class AutomaticManipulator(Manipulator):
|
||||
def _prepare(cls, model):
|
||||
cls.model = model
|
||||
@ -279,9 +277,6 @@ class ModelChangeManipulator(AutomaticManipulator):
|
||||
if self.opts.get_ordered_objects():
|
||||
self.fields.append(formfields.CommaSeparatedIntegerField(field_name="order_"))
|
||||
|
||||
|
||||
|
||||
|
||||
def manipulator_validator_unique_together(field_name_list, opts, self, field_data, all_data):
|
||||
from django.utils.text import get_text_list
|
||||
field_list = [opts.get_field(field_name) for field_name in field_name_list]
|
||||
|
Loading…
x
Reference in New Issue
Block a user