2007-11-05 21:59:42 +08:00
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
Convenience routines for creating non-trivial Field subclasses, as well as
|
|
|
|
backwards compatibility utilities.
|
2007-11-05 21:59:42 +08:00
|
|
|
|
|
|
|
Add SubfieldBase as the __metaclass__ for your Field subclass, implement
|
|
|
|
to_python() and the other necessary methods and everything will work seamlessly.
|
|
|
|
"""
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
from inspect import getargspec
|
|
|
|
from warnings import warn
|
|
|
|
|
|
|
|
def call_with_connection(func):
|
|
|
|
arg_names, varargs, varkwargs, defaults = getargspec(func)
|
|
|
|
updated = ('connection' in arg_names or varkwargs)
|
|
|
|
if not updated:
|
|
|
|
warn("A Field class whose %s method hasn't been updated to take a "
|
|
|
|
"`connection` argument." % func.__name__,
|
2010-10-15 10:09:11 +08:00
|
|
|
DeprecationWarning, stacklevel=3)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
def inner(*args, **kwargs):
|
|
|
|
if 'connection' not in kwargs:
|
|
|
|
from django.db import connection
|
|
|
|
kwargs['connection'] = connection
|
|
|
|
warn("%s has been called without providing a connection argument. " %
|
2010-10-11 20:20:07 +08:00
|
|
|
func.__name__, DeprecationWarning,
|
2010-10-15 10:09:11 +08:00
|
|
|
stacklevel=2)
|
2009-12-22 23:18:51 +08:00
|
|
|
if updated:
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
if 'connection' in kwargs:
|
|
|
|
del kwargs['connection']
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return inner
|
|
|
|
|
|
|
|
def call_with_connection_and_prepared(func):
|
|
|
|
arg_names, varargs, varkwargs, defaults = getargspec(func)
|
|
|
|
updated = (
|
|
|
|
('connection' in arg_names or varkwargs) and
|
|
|
|
('prepared' in arg_names or varkwargs)
|
|
|
|
)
|
|
|
|
if not updated:
|
|
|
|
warn("A Field class whose %s method hasn't been updated to take "
|
|
|
|
"`connection` and `prepared` arguments." % func.__name__,
|
2010-10-15 10:09:11 +08:00
|
|
|
DeprecationWarning, stacklevel=3)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
def inner(*args, **kwargs):
|
|
|
|
if 'connection' not in kwargs:
|
|
|
|
from django.db import connection
|
|
|
|
kwargs['connection'] = connection
|
|
|
|
warn("%s has been called without providing a connection argument. " %
|
2010-10-11 20:20:07 +08:00
|
|
|
func.__name__, DeprecationWarning,
|
2010-10-15 10:09:11 +08:00
|
|
|
stacklevel=2)
|
2009-12-22 23:18:51 +08:00
|
|
|
if updated:
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
if 'connection' in kwargs:
|
|
|
|
del kwargs['connection']
|
|
|
|
if 'prepared' in kwargs:
|
|
|
|
del kwargs['prepared']
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return inner
|
|
|
|
|
|
|
|
class LegacyConnection(type):
|
|
|
|
"""
|
|
|
|
A metaclass to normalize arguments give to the get_db_prep_* and db_type
|
|
|
|
methods on fields.
|
|
|
|
"""
|
2010-12-04 13:59:38 +08:00
|
|
|
def __new__(cls, name, bases, attrs):
|
|
|
|
new_cls = super(LegacyConnection, cls).__new__(cls, name, bases, attrs)
|
2009-12-22 23:18:51 +08:00
|
|
|
for attr in ('db_type', 'get_db_prep_save'):
|
|
|
|
setattr(new_cls, attr, call_with_connection(getattr(new_cls, attr)))
|
|
|
|
for attr in ('get_db_prep_lookup', 'get_db_prep_value'):
|
|
|
|
setattr(new_cls, attr, call_with_connection_and_prepared(getattr(new_cls, attr)))
|
|
|
|
return new_cls
|
|
|
|
|
|
|
|
class SubfieldBase(LegacyConnection):
|
2007-11-05 21:59:42 +08:00
|
|
|
"""
|
|
|
|
A metaclass for custom Field subclasses. This ensures the model's attribute
|
|
|
|
has the descriptor protocol attached to it.
|
|
|
|
"""
|
2010-12-04 13:59:38 +08:00
|
|
|
def __new__(cls, name, bases, attrs):
|
|
|
|
new_class = super(SubfieldBase, cls).__new__(cls, name, bases, attrs)
|
2007-11-05 21:59:42 +08:00
|
|
|
new_class.contribute_to_class = make_contrib(
|
2010-11-03 02:40:53 +08:00
|
|
|
new_class, attrs.get('contribute_to_class')
|
|
|
|
)
|
2007-11-05 21:59:42 +08:00
|
|
|
return new_class
|
|
|
|
|
|
|
|
class Creator(object):
|
|
|
|
"""
|
|
|
|
A placeholder class that provides a way to set the attribute on the model.
|
|
|
|
"""
|
|
|
|
def __init__(self, field):
|
|
|
|
self.field = field
|
|
|
|
|
|
|
|
def __get__(self, obj, type=None):
|
|
|
|
if obj is None:
|
|
|
|
raise AttributeError('Can only be accessed via an instance.')
|
2009-12-22 23:18:51 +08:00
|
|
|
return obj.__dict__[self.field.name]
|
2007-11-05 21:59:42 +08:00
|
|
|
|
|
|
|
def __set__(self, obj, value):
|
2007-11-30 03:30:49 +08:00
|
|
|
obj.__dict__[self.field.name] = self.field.to_python(value)
|
2007-11-05 21:59:42 +08:00
|
|
|
|
2010-11-03 02:40:53 +08:00
|
|
|
def make_contrib(superclass, func=None):
|
2007-11-05 21:59:42 +08:00
|
|
|
"""
|
|
|
|
Returns a suitable contribute_to_class() method for the Field subclass.
|
|
|
|
|
|
|
|
If 'func' is passed in, it is the existing contribute_to_class() method on
|
|
|
|
the subclass and it is called before anything else. It is assumed in this
|
|
|
|
case that the existing contribute_to_class() calls all the necessary
|
|
|
|
superclass methods.
|
|
|
|
"""
|
|
|
|
def contribute_to_class(self, cls, name):
|
|
|
|
if func:
|
|
|
|
func(self, cls, name)
|
|
|
|
else:
|
2010-11-03 02:40:53 +08:00
|
|
|
super(superclass, self).contribute_to_class(cls, name)
|
2007-11-05 21:59:42 +08:00
|
|
|
setattr(cls, self.name, Creator(self))
|
|
|
|
|
|
|
|
return contribute_to_class
|