1
0
mirror of https://github.com/django/django.git synced 2025-06-15 16:39:13 +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

@ -4,13 +4,13 @@ register = Library()
def path_breadcrumbs(path, overrides="", front=0, back=0): def path_breadcrumbs(path, overrides="", front=0, back=0):
overs = overrides.split('/') overs = overrides.split('/')
comps = [""] * int(front) + path.split('/')[:-1] comps = [""] * int(front) + path.split('/')[:-1]
backs = int(back) + len(comps) backs = int(back) + len(comps)
overs.extend( [None for x in range(len(overs) -1 ,len(comps)) ] ) overs.extend([None for x in range(len(overs) -1, len(comps))])
text = [] text = []
for comp, ov in zip(comps,overs): for comp, ov in zip(comps, overs):
label = ov or comp label = ov or comp
text.append("<a href='%s'>%s</a>&rsaquo;\n" % ( "../" * backs , label ) ) text.append("<a href='%s'>%s</a>&rsaquo;\n" % ("../" * backs, label))
backs -= 1 backs -= 1
return "".join(text) return "".join(text)
path_breadcrumbs = register.simple_tag(path_breadcrumbs) path_breadcrumbs = register.simple_tag(path_breadcrumbs)

View File

@ -4,28 +4,25 @@ from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.admin.filterspecs import FilterSpec from django.contrib.admin.filterspecs import FilterSpec
from django.core import formfields, template from django.core import formfields, template
from django.core.template import loader from django.core.template import loader
from django.db import models
from django.db.models.fields import BoundField, BoundFieldLine, BoundFieldSet from django.db.models.fields import BoundField, BoundFieldLine, BoundFieldSet
from django.db.models.query import handle_legacy_orderlist
from django.core.exceptions import Http404, ImproperlyConfigured, ObjectDoesNotExist, PermissionDenied from django.core.exceptions import Http404, ImproperlyConfigured, ObjectDoesNotExist, PermissionDenied
from django.core.extensions import DjangoContext as Context from django.core.extensions import DjangoContext as Context
from django.core.extensions import get_object_or_404, render_to_response from django.core.extensions import get_object_or_404, render_to_response
from django.core.paginator import ObjectPaginator, InvalidPage from django.core.paginator import ObjectPaginator, InvalidPage
from django.conf.settings import ADMIN_MEDIA_PREFIX
try: try:
from django.contrib.admin.models import LogEntry, ADDITION, CHANGE, DELETION from django.contrib.admin.models import LogEntry, ADDITION, CHANGE, DELETION
except ImportError: except ImportError:
raise ImproperlyConfigured, "You don't have 'django.contrib.admin' in INSTALLED_APPS." raise ImproperlyConfigured, "You don't have 'django.contrib.admin' in INSTALLED_APPS."
from django.db import models
from django.utils.html import strip_tags
from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect
from django.utils.text import capfirst, get_text_list
from django.utils import dateformat from django.utils import dateformat
from django.utils.dates import MONTHS from django.utils.dates import MONTHS
from django.utils.html import escape from django.utils.html import escape, strip_tags
from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect
from django.utils.text import capfirst, get_text_list
import operator import operator
from itertools import izip from itertools import izip
from django.db.models.query import handle_legacy_orderlist
# The system will display a "Show all" link only if the total result count # The system will display a "Show all" link only if the total result count
# is less than or equal to this setting. # is less than or equal to this setting.
MAX_SHOW_ALL_ALLOWED = 200 MAX_SHOW_ALL_ALLOWED = 200
@ -59,7 +56,7 @@ def matches_app(mod, comps):
modcomps = mod.__name__.split('.')[:-1] #HACK: leave off 'models' modcomps = mod.__name__.split('.')[:-1] #HACK: leave off 'models'
for c, mc in izip(comps, modcomps): for c, mc in izip(comps, modcomps):
if c != mc: if c != mc:
return ([],False) return ([], False)
return (comps[len(modcomps):], True) return (comps[len(modcomps):], True)
def find_model(mod, remaining): def find_model(mod, remaining):
@ -92,13 +89,13 @@ def get_app_label(mod):
def get_model_and_app(path): def get_model_and_app(path):
comps = path.split('/') comps = path.split('/')
comps = comps[:-1] # remove '' after final / comps = comps[:-1] # remove '' after final /
for mod in models.get_installed_models(): for mod in models.get_installed_models():
remaining, matched = matches_app(mod, comps) remaining, matched = matches_app(mod, comps)
if matched and len(remaining) > 0: if matched and len(remaining) > 0:
# print "matched ", mod # print "matched ", mod
# print "left", remaining # print "left", remaining
return ( find_model(mod, remaining), get_app_label(mod) ) return ( find_model(mod, remaining), get_app_label(mod) )
raise Http404 # Couldn't find app raise Http404 # Couldn't find app
_model_urls = {} _model_urls = {}
@ -108,7 +105,7 @@ def url_for_model(model):
return _model_urls[model] return _model_urls[model]
except KeyError: except KeyError:
comps = model.__module__.split('.') comps = model.__module__.split('.')
for mod in models.get_installed_models(): for mod in models.get_installed_models():
remaining, matched = matches_app(mod, comps) remaining, matched = matches_app(mod, comps)
if matched and len(remaining) > 0: if matched and len(remaining) > 0:
comps = comps[: - len(remaining)] + remaining[1:] comps = comps[: - len(remaining)] + remaining[1:]
@ -166,7 +163,7 @@ class ChangeList(object):
self.model, self.app_label = get_model_and_app(path) self.model, self.app_label = get_model_and_app(path)
# _get_mod_opts(app_label, module_name) # _get_mod_opts(app_label, module_name)
self.opts = self.model._meta self.opts = self.model._meta
if not request.user.has_perm(self.app_label + '.' + self.opts.get_change_permission()): if not request.user.has_perm(self.app_label + '.' + self.opts.get_change_permission()):
raise PermissionDenied raise PermissionDenied
@ -312,7 +309,7 @@ def change_list(request, path):
c = Context(request, { c = Context(request, {
'title': cl.title, 'title': cl.title,
'is_popup': cl.is_popup, 'is_popup': cl.is_popup,
'cl' : cl, 'cl': cl,
'path': path[:path.rindex('/')] 'path': path[:path.rindex('/')]
}) })
c.update({'has_add_permission': c['perms'][cl.app_label][cl.opts.get_add_permission()]}), c.update({'has_add_permission': c['perms'][cl.app_label][cl.opts.get_add_permission()]}),
@ -323,7 +320,7 @@ change_list = staff_member_required(change_list)
use_raw_id_admin = lambda field: isinstance(field.rel, (models.ManyToOne, models.ManyToMany)) and field.rel.raw_id_admin use_raw_id_admin = lambda field: isinstance(field.rel, (models.ManyToOne, models.ManyToMany)) and field.rel.raw_id_admin
def get_javascript_imports(opts,auto_populated_fields, ordered_objects, field_sets): def get_javascript_imports(opts, auto_populated_fields, ordered_objects, field_sets):
# Put in any necessary JavaScript imports. # Put in any necessary JavaScript imports.
js = ['js/core.js', 'js/admin/RelatedObjectLookups.js'] js = ['js/core.js', 'js/admin/RelatedObjectLookups.js']
if auto_populated_fields: if auto_populated_fields:
@ -476,14 +473,14 @@ def render_change_form(model, manipulator, app_label, context, add=False, change
"admin/%s/change_form" % app_label , "admin/%s/change_form" % app_label ,
"admin/change_form"], context_instance=context) "admin/change_form"], context_instance=context)
def log_add_message(user, opts,manipulator,new_object): def log_add_message(user, opts, manipulator, new_object):
pk_value = getattr(new_object, opts.pk.attname) pk_value = getattr(new_object, opts.pk.attname)
LogEntry.objects.log_action(user.id, opts.get_content_type_id(), pk_value, str(new_object), ADDITION) LogEntry.objects.log_action(user.id, opts.get_content_type_id(), pk_value, str(new_object), ADDITION)
def add_stage(request, path, show_delete=False, form_url='', post_url='../change/', post_url_continue='../%s/', object_id_override=None): def add_stage(request, path, show_delete=False, form_url='', post_url='../change/', post_url_continue='../%s/', object_id_override=None):
model, app_label = get_model_and_app(path) model, app_label = get_model_and_app(path)
opts = model._meta opts = model._meta
if not request.user.has_perm(app_label + '.' + opts.get_add_permission()): if not request.user.has_perm(app_label + '.' + opts.get_add_permission()):
raise PermissionDenied raise PermissionDenied
manipulator = model.AddManipulator() manipulator = model.AddManipulator()
@ -496,9 +493,9 @@ def add_stage(request, path, show_delete=False, form_url='', post_url='../change
if not errors and not request.POST.has_key("_preview"): if not errors and not request.POST.has_key("_preview"):
new_object = manipulator.save(new_data) new_object = manipulator.save(new_data)
log_add_message(request.user, opts,manipulator,new_object) log_add_message(request.user, opts, manipulator, new_object)
msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name':opts.verbose_name, 'obj':new_object} msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': opts.verbose_name, 'obj': new_object}
pk_value = getattr(new_object,opts.pk.attname) pk_value = getattr(new_object, opts.pk.attname)
# Here, we distinguish between different save types by checking for # Here, we distinguish between different save types by checking for
# the presence of keys in request.POST. # the presence of keys in request.POST.
if request.POST.has_key("_continue"): if request.POST.has_key("_continue"):
@ -531,16 +528,16 @@ def add_stage(request, path, show_delete=False, form_url='', post_url='../change
'form': form, 'form': form,
'is_popup': request.REQUEST.has_key('_popup'), 'is_popup': request.REQUEST.has_key('_popup'),
'show_delete': show_delete, 'show_delete': show_delete,
'path' : path , 'path': path ,
}) })
if object_id_override is not None: if object_id_override is not None:
c['object_id'] = object_id_override c['object_id'] = object_id_override
return render_change_form(model, manipulator, app_label, c, add=True) return render_change_form(model, manipulator, app_label, c, add=True)
add_stage = staff_member_required(add_stage) add_stage = staff_member_required(add_stage)
def log_change_message(user, opts,manipulator,new_object): def log_change_message(user, opts, manipulator, new_object):
pk_value = getattr(new_object, opts.pk.column) pk_value = getattr(new_object, opts.pk.column)
# Construct the change message. # Construct the change message.
change_message = [] change_message = []
@ -556,7 +553,6 @@ def log_change_message(user, opts,manipulator,new_object):
LogEntry.objects.log_action(user.id, opts.get_content_type_id(), pk_value, str(new_object), CHANGE, change_message) LogEntry.objects.log_action(user.id, opts.get_content_type_id(), pk_value, str(new_object), CHANGE, change_message)
def change_stage(request, path, object_id): def change_stage(request, path, object_id):
model, app_label = get_model_and_app(path) model, app_label = get_model_and_app(path)
opts = model._meta opts = model._meta
#mod, opts = _get_mod_opts(app_label, module_name) #mod, opts = _get_mod_opts(app_label, module_name)
@ -580,9 +576,9 @@ def change_stage(request, path, object_id):
manipulator.do_html2python(new_data) manipulator.do_html2python(new_data)
if not errors and not request.POST.has_key("_preview"): if not errors and not request.POST.has_key("_preview"):
new_object = manipulator.save(new_data) new_object = manipulator.save(new_data)
log_change_message(request.user,opts,manipulator,new_object) log_change_message(request.user, opts, manipulator, new_object)
msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': opts.verbose_name, 'obj':new_object} msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': opts.verbose_name, 'obj': new_object}
pk_value = getattr(new_object,opts.pk.attname) pk_value = getattr(new_object, opts.pk.attname)
if request.POST.has_key("_continue"): if request.POST.has_key("_continue"):
request.user.add_message(msg + ' ' + _("You may edit it again below.")) request.user.add_message(msg + ' ' + _("You may edit it again below."))
if request.REQUEST.has_key('_popup'): if request.REQUEST.has_key('_popup'):
@ -632,11 +628,10 @@ def change_stage(request, path, object_id):
'form': form, 'form': form,
'object_id': object_id, 'object_id': object_id,
'original': manipulator.original_object, 'original': manipulator.original_object,
'is_popup' : request.REQUEST.has_key('_popup'), 'is_popup': request.REQUEST.has_key('_popup'),
'path' : path , 'path': path ,
}) })
return render_change_form(model, manipulator, app_label, c, change=True)
return render_change_form(model,manipulator, app_label, c, change=True)
def _nest_help(obj, depth, val): def _nest_help(obj, depth, val):
current = obj current = obj

View File

@ -1,18 +1,15 @@
import django.db.models.manipulators import django.db.models.manipulators
import django.db.models.manager 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.fields.related import OneToOne, ManyToOne
from django.db.models.related import RelatedObject from django.db.models.related import RelatedObject
from django.db.models.query import orderlist2sql from django.db.models.query import orderlist2sql
from django.db.models.options import Options from django.db.models.options import Options
from django.db import connection, backend from django.db import connection, backend
from django.db.models import signals from django.db.models import signals
from django.dispatch import dispatcher from django.dispatch import dispatcher
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.utils.functional import curry from django.utils.functional import curry
import re import re
import types import types
import sys 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". # 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() get_verbose_name = lambda class_name: re.sub('([A-Z])', ' \\1', class_name).lower().strip()
class ModelBase(type): class ModelBase(type):
"Metaclass for all models" "Metaclass for all models"
def __new__(cls, name, bases, attrs): def __new__(cls, name, bases, attrs):
@ -42,13 +38,13 @@ class ModelBase(type):
except KeyError: except KeyError:
meta_attrs = {} meta_attrs = {}
# Create the class, we need to add the options to it. # Create the class.
new_class = type.__new__(cls, name, bases, { '__module__' : attrs.pop('__module__') }) new_class = type.__new__(cls, name, bases, {'__module__': attrs.pop('__module__')})
opts = Options( opts = Options(
module_name = meta_attrs.pop('module_name', get_module_name(name)), module_name = meta_attrs.pop('module_name', get_module_name(name)),
# If the verbose_name wasn't given, use the class 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 = meta_attrs.pop('verbose_name', get_verbose_name(name)),
verbose_name_plural = meta_attrs.pop('verbose_name_plural', ''), verbose_name_plural = meta_attrs.pop('verbose_name_plural', ''),
db_table = meta_attrs.pop('db_table', ''), db_table = meta_attrs.pop('db_table', ''),
@ -81,12 +77,10 @@ class ModelBase(type):
# Cache the app label. # Cache the app label.
opts.app_label = 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(): for obj_name, obj in attrs.items():
new_class.add_to_class(obj_name, obj) new_class.add_to_class(obj_name, obj)
# Give the class a docstring -- its definition. # Give the class a docstring -- its definition.
if new_class.__doc__ is None: if new_class.__doc__ is None:
new_class.__doc__ = "%s.%s(%s)" % (opts.module_name, name, ", ".join([f.name for f in opts.fields])) 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() opts._prepare()
new_class._prepare() new_class._prepare()
# Populate the _MODELS member on the module the class is in. # Populate the _MODELS member on the module the class is in.
app_package.__dict__.setdefault('_MODELS', []).append(new_class) app_package.__dict__.setdefault('_MODELS', []).append(new_class)
return new_class return new_class
def cmp_cls(x, y): def cmp_cls(x, y):
@ -117,13 +108,6 @@ def cmp_cls(x, y):
class Model(object): class Model(object):
__metaclass__ = ModelBase __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): def __repr__(self):
return '<%s object>' % self.__class__.__name__ return '<%s object>' % self.__class__.__name__
@ -134,7 +118,7 @@ class Model(object):
return not self.__eq__(other) return not self.__eq__(other)
def __init__(self, *args, **kwargs): 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: if kwargs:
for f in self._meta.fields: for f in self._meta.fields:
if isinstance(f.rel, ManyToOne): 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] raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]
for i, arg in enumerate(args): for i, arg in enumerate(args):
setattr(self, self._meta.fields[i].attname, arg) 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): def _prepare(cls):
# Creates some methods once self._meta has been populated. # 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_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) 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) #RelatedField.do_pending_lookups(cls)
_prepare = classmethod(_prepare) _prepare = classmethod(_prepare)
@ -182,7 +173,7 @@ class Model(object):
# Run any pre-save hooks. # Run any pre-save hooks.
if hasattr(self, '_pre_save'): if hasattr(self, '_pre_save'):
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] non_pks = [f for f in self._meta.fields if not f.primary_key]
cursor = connection.cursor() cursor = connection.cursor()
@ -261,8 +252,7 @@ class Model(object):
def delete(self, ignore_objects=None): 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." 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 = ignore_objects and dict([(o.__class,o.__get_pk_val) for o in ignore_objects]) or {}
ignore_objects and dict([ (o.__class,o.__get_pk_val) for o in ignore_objects ]) or {}
seen_objs = {} seen_objs = {}
self.__collect_sub_objects(seen_objs, ignore_objects) self.__collect_sub_objects(seen_objs, ignore_objects)
@ -282,7 +272,7 @@ class Model(object):
if hasattr(instance, '_pre_delete'): if hasattr(instance, '_pre_delete'):
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(): for related in cls._meta.get_all_related_many_to_many_objects():
cursor.execute("DELETE FROM %s WHERE %s=%%s" % \ 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(f.get_m2m_db_table(cls._meta)),
backend.quote_name(cls._meta.object_name.lower() + '_id')), backend.quote_name(cls._meta.object_name.lower() + '_id')),
[pk_val]) [pk_val])
for field in cls._meta.fields: for field in cls._meta.fields:
if field.rel and field.null and field.rel.to in seen_cls: if field.rel and field.null and field.rel.to in seen_cls:
cursor.execute("UPDATE %s SET %s = NULL WHERE %s =%%s" % \ cursor.execute("UPDATE %s SET %s = NULL WHERE %s=%%s" % \
( backend.quote_name(cls._meta.db_table), (backend.quote_name(cls._meta.db_table), backend.quote_name(field.column),
backend.quote_name(field.column), backend.quote_name(cls._meta.pk.column)), [pk_val])
backend.quote_name(cls._meta.pk.column)),
[pk_val] )
seen_tups.reverse() seen_tups.reverse()
@ -312,7 +299,7 @@ class Model(object):
setattr(self, cls._meta.pk.attname, None) 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'): if hasattr(instance, '_post_delete'):
instance._post_delete() instance._post_delete()
@ -321,7 +308,6 @@ class Model(object):
delete.alters_data = True delete.alters_data = True
def _get_FIELD_display(self, field): def _get_FIELD_display(self, field):
value = getattr(self, field.attname) value = getattr(self, field.attname)
return dict(field.choices).get(value, value) return dict(field.choices).get(value, value)
@ -504,7 +490,6 @@ class Model(object):
_add_related.alters_data = True _add_related.alters_data = True
# Handles related many-to-many object retrieval. # Handles related many-to-many object retrieval.
# Examples: Album.get_song(), Album.get_song_list(), Album.get_song_count() # 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): 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]) cursor.executemany(sql, [(this_id, i) for i in id_list])
connection.commit() connection.commit()
############################################ ############################################
# HELPER FUNCTIONS (CURRIED MODEL METHODS) # # HELPER FUNCTIONS (CURRIED MODEL METHODS) #
############################################ ############################################
@ -568,5 +549,3 @@ def method_get_order(ordered_obj, self):
def get_absolute_url(opts, func, self): def get_absolute_url(opts, func, self):
return settings.ABSOLUTE_URL_OVERRIDES.get('%s.%s' % (opts.app_label, opts.module_name), 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 from django.utils.translation import gettext_lazy, ngettext
import datetime, os import datetime, os
# Random entropy string used by "default" param. # Random entropy string used by "default" param.
NOT_PROVIDED = 'oijpwojefiojpanv' 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): def manipulator_validator_unique(f, opts, self, field_data, all_data):
"Validates that the value is unique for this field." "Validates that the value is unique for this field."
lookup_type = f.get_validator_unique_lookup_type() lookup_type = f.get_validator_unique_lookup_type()
try: try:
old_obj = self.__class__._default_manager.get_object(**{lookup_type: field_data}) old_obj = self.__class__._default_manager.get_object(**{lookup_type: field_data})
except ObjectDoesNotExist: except ObjectDoesNotExist:
@ -97,7 +94,6 @@ class Field(object):
help_text='', db_column=None): help_text='', db_column=None):
self.name = name self.name = name
self.verbose_name = verbose_name self.verbose_name = verbose_name
self.primary_key = primary_key self.primary_key = primary_key
self.maxlength, self.unique = maxlength, unique self.maxlength, self.unique = maxlength, unique
self.blank, self.null = blank, null self.blank, self.null = blank, null
@ -118,9 +114,8 @@ class Field(object):
self.creation_counter = Field.creation_counter self.creation_counter = Field.creation_counter
Field.creation_counter += 1 Field.creation_counter += 1
def __cmp__(self,other ): 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) return cmp(self.creation_counter, other.creation_counter)
def set_attributes_from_name(self, name): def set_attributes_from_name(self, name):
@ -292,7 +287,6 @@ class Field(object):
first_choice = include_blank and blank_choice or [] first_choice = include_blank and blank_choice or []
if self.choices: if self.choices:
return first_choice + list(self.choices) return first_choice + list(self.choices)
rel_model = self.rel.to rel_model = self.rel.to
return first_choice + [(getattr(x, rel_model._meta.pk.attname), str(x)) 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)] 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 empty_strings_allowed = False
def __init__(self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs): 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 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: if auto_now or auto_now_add:
kwargs['editable'] = False kwargs['editable'] = False
kwargs['blank'] = True kwargs['blank'] = True

View File

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