1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

magic-removal: Various small code cleanups

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1726 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty
2005-12-18 20:36:41 +00:00
parent 10df9db788
commit fc27500389
5 changed files with 67 additions and 104 deletions

View File

@@ -1,18 +1,15 @@
import django.db.models.manipulators
import django.db.models.manager
from django.db.models.fields import AutoField
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.query import orderlist2sql
from django.db.models.options import Options
from django.db import connection, backend
from django.db.models import signals
from django.dispatch import dispatcher
from django.core.exceptions import ObjectDoesNotExist
from django.utils.functional import curry
import re
import types
import sys
@@ -27,7 +24,6 @@ get_module_name = lambda class_name: class_name.lower() + 's'
# Calculate the verbose_name by converting from InitialCaps to "lowercase with spaces".
get_verbose_name = lambda class_name: re.sub('([A-Z])', ' \\1', class_name).lower().strip()
class ModelBase(type):
"Metaclass for all models"
def __new__(cls, name, bases, attrs):
@@ -42,13 +38,13 @@ class ModelBase(type):
except KeyError:
meta_attrs = {}
# Create the class, we need to add the options to it.
new_class = type.__new__(cls, name, bases, { '__module__' : attrs.pop('__module__') })
# Create the class.
new_class = type.__new__(cls, name, bases, {'__module__': attrs.pop('__module__')})
opts = Options(
module_name = meta_attrs.pop('module_name', get_module_name(name)),
# If the verbose_name wasn't given, use the class name,
# converted from InitialCaps to "lowercase with spaces".
# converted from "InitialCaps" to "lowercase with spaces".
verbose_name = meta_attrs.pop('verbose_name', get_verbose_name(name)),
verbose_name_plural = meta_attrs.pop('verbose_name_plural', ''),
db_table = meta_attrs.pop('db_table', ''),
@@ -81,12 +77,10 @@ class ModelBase(type):
# Cache the app label.
opts.app_label = app_label
#Add all attributes to the class
# Add all attributes to the class.
for obj_name, obj in attrs.items():
new_class.add_to_class(obj_name, obj)
# Give the class a docstring -- its definition.
if new_class.__doc__ is None:
new_class.__doc__ = "%s.%s(%s)" % (opts.module_name, name, ", ".join([f.name for f in opts.fields]))
@@ -97,12 +91,9 @@ class ModelBase(type):
opts._prepare()
new_class._prepare()
# Populate the _MODELS member on the module the class is in.
app_package.__dict__.setdefault('_MODELS', []).append(new_class)
return new_class
def cmp_cls(x, y):
@@ -117,13 +108,6 @@ def cmp_cls(x, y):
class Model(object):
__metaclass__ = ModelBase
def add_to_class(cls, name, attribute):
if hasattr(attribute, 'contribute_to_class'):
attribute.contribute_to_class(cls, name)
else:
setattr(cls, name, attribute)
add_to_class = classmethod(add_to_class)
def __repr__(self):
return '<%s object>' % self.__class__.__name__
@@ -134,7 +118,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):
@@ -165,7 +149,14 @@ 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 add_to_class(cls, name, attribute):
if hasattr(attribute, 'contribute_to_class'):
attribute.contribute_to_class(cls, name)
else:
setattr(cls, name, attribute)
add_to_class = classmethod(add_to_class)
def _prepare(cls):
# Creates some methods once self._meta has been populated.
@@ -173,7 +164,7 @@ class Model(object):
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)
@@ -182,7 +173,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()
@@ -261,8 +252,7 @@ class Model(object):
def delete(self, ignore_objects=None):
assert getattr(self, self._meta.pk.attname) is not None, "%r can't be deleted because it doesn't have an ID."
ignore_objects = \
ignore_objects and dict([ (o.__class,o.__get_pk_val) for o in ignore_objects ]) or {}
ignore_objects = ignore_objects and dict([(o.__class,o.__get_pk_val) for o in ignore_objects]) or {}
seen_objs = {}
self.__collect_sub_objects(seen_objs, ignore_objects)
@@ -282,7 +272,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" % \
@@ -294,14 +284,11 @@ class Model(object):
(backend.quote_name(f.get_m2m_db_table(cls._meta)),
backend.quote_name(cls._meta.object_name.lower() + '_id')),
[pk_val])
for field in cls._meta.fields:
if field.rel and field.null and field.rel.to in seen_cls:
cursor.execute("UPDATE %s SET %s = NULL WHERE %s =%%s" % \
( backend.quote_name(cls._meta.db_table),
backend.quote_name(field.column),
backend.quote_name(cls._meta.pk.column)),
[pk_val] )
cursor.execute("UPDATE %s SET %s = NULL WHERE %s=%%s" % \
(backend.quote_name(cls._meta.db_table), backend.quote_name(field.column),
backend.quote_name(cls._meta.pk.column)), [pk_val])
seen_tups.reverse()
@@ -312,7 +299,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()
@@ -321,7 +308,6 @@ class Model(object):
delete.alters_data = True
def _get_FIELD_display(self, field):
value = getattr(self, field.attname)
return dict(field.choices).get(value, value)
@@ -504,7 +490,6 @@ class Model(object):
_add_related.alters_data = True
# Handles related many-to-many object retrieval.
# Examples: Album.get_song(), Album.get_song_list(), Album.get_song_count()
def _get_related_many_to_many(self, method_name, rel_class, rel_field, **kwargs):
@@ -529,10 +514,6 @@ class Model(object):
cursor.executemany(sql, [(this_id, i) for i in id_list])
connection.commit()
############################################
# HELPER FUNCTIONS (CURRIED MODEL METHODS) #
############################################
@@ -568,5 +549,3 @@ def method_get_order(ordered_obj, self):
def get_absolute_url(opts, func, self):
return settings.ABSOLUTE_URL_OVERRIDES.get('%s.%s' % (opts.app_label, opts.module_name), func)(self)

View File

@@ -8,7 +8,6 @@ from django.utils.text import capfirst
from django.utils.translation import gettext_lazy, ngettext
import datetime, os
# Random entropy string used by "default" param.
NOT_PROVIDED = 'oijpwojefiojpanv'
@@ -35,9 +34,7 @@ def manipulator_valid_rel_key(f, self, field_data, all_data):
def manipulator_validator_unique(f, opts, self, field_data, all_data):
"Validates that the value is unique for this field."
lookup_type = f.get_validator_unique_lookup_type()
try:
old_obj = self.__class__._default_manager.get_object(**{lookup_type: field_data})
except ObjectDoesNotExist:
@@ -97,7 +94,6 @@ class Field(object):
help_text='', db_column=None):
self.name = name
self.verbose_name = verbose_name
self.primary_key = primary_key
self.maxlength, self.unique = maxlength, unique
self.blank, self.null = blank, null
@@ -118,9 +114,8 @@ class Field(object):
self.creation_counter = Field.creation_counter
Field.creation_counter += 1
def __cmp__(self,other ):
#This is because bisect does not take a comparison function. grrr.
# This is needed because bisect does not take a comparison function.
return cmp(self.creation_counter, other.creation_counter)
def set_attributes_from_name(self, name):
@@ -292,7 +287,6 @@ class Field(object):
first_choice = include_blank and blank_choice or []
if self.choices:
return first_choice + list(self.choices)
rel_model = self.rel.to
return first_choice + [(getattr(x, rel_model._meta.pk.attname), str(x))
for x in rel_model._default_manager.get_list(**rel_model._meta.limit_choices_to)]
@@ -365,7 +359,7 @@ class DateField(Field):
empty_strings_allowed = False
def __init__(self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs):
self.auto_now, self.auto_now_add = auto_now, auto_now_add
#HACKs : auto_now_add/auto_now should be done as a default or a pre_save...
#HACKs : auto_now_add/auto_now should be done as a default or a pre_save.
if auto_now or auto_now_add:
kwargs['editable'] = False
kwargs['blank'] = True

View File

@@ -15,7 +15,6 @@ class Options:
order_with_respect_to=None, module_constants=None):
# Move many-to-many related fields from self.fields into self.many_to_many.
self.fields, self.many_to_many = [], []
self.module_name, self.verbose_name = module_name, verbose_name
self.verbose_name_plural = verbose_name_plural or verbose_name + 's'
self.db_table = db_table
@@ -27,10 +26,9 @@ class Options:
self.object_name, self.app_label = object_name, app_label
self.get_latest_by = get_latest_by
self.order_with_respect_to = order_with_respect_to
self.module_constants = module_constants or {}
self.admin = admin
def contribute_to_class(self, cls, name):
self.model = cls
cls._meta = self
@@ -41,7 +39,7 @@ class Options:
self.ordering = ('_order',)
else:
self.order_with_respect_to = None
# Calculate one_to_one_field.
self.one_to_one_field = None
for f in self.fields:
@@ -71,20 +69,18 @@ class Options:
self.has_auto_field = True
#HACK
self.limit_choices_to = {}
# If the db_table wasn't provided, use the app_label + module_name.
if not self.db_table:
self.db_table = "%s_%s" % (self.app_label, self.module_name)
def add_field(self, field):
# Insert the fields in the order that they were created. The
# "creation_counter" is needed because metaclasses don't preserve the
# attribute order.
# Insert the given field in the order in which it was created, using
# the "creation_counter" attribute of the field.
if field.rel and isinstance(field.rel, ManyToMany):
self.many_to_many.insert(bisect(self.many_to_many, field), field)
else:
self.fields.insert(bisect(self.fields,field),field)
self.fields.insert(bisect(self.fields, field), field)
def __repr__(self):
return '<Options for %s>' % self.module_name
@@ -95,11 +91,10 @@ class Options:
def get_content_type_id(self):
"Returns the content-type ID for this object type."
if not hasattr(self, '_content_type_id'):
import django.models.core
manager = django.models.core.ContentType.objects
self._content_type_id = \
manager.get_object(python_module_name__exact=self.module_name,
package__label__exact=self.app_label).id
from django.models.core import ContentType
self._content_type_id = ContentType.objects.get_object(
python_module_name__exact=self.module_name,
package__label__exact=self.app_label).id
return self._content_type_id
def get_field(self, name, many_to_many=True):
@@ -209,4 +204,4 @@ class Options:
self._field_types[field_type] = True
else:
self._field_types[field_type] = False
return self._field_types[field_type]
return self._field_types[field_type]