Remove model field utilities that were needed because of brokenness in the old schema
- Legacy-Id: 6833
This commit is contained in:
parent
947c973724
commit
924d58f935
|
@ -1,43 +0,0 @@
|
|||
from django.db import models
|
||||
|
||||
class InvalidToNoneOverrider(object):
|
||||
"""Converts invalid ids to None before returning them to Django."""
|
||||
def __init__(self, cls, fieldname, null_values):
|
||||
self.fieldname = fieldname
|
||||
self.real_field = getattr(cls, fieldname)
|
||||
self.null_values = null_values
|
||||
|
||||
def __get__(self, instance, instance_type=None):
|
||||
if instance is None: # calls on the class
|
||||
return self
|
||||
|
||||
v = getattr(instance, u"%s_id" % self.fieldname)
|
||||
if v == None or v in self.null_values:
|
||||
return None
|
||||
else:
|
||||
# forward to real field
|
||||
return self.real_field.__get__(instance, instance_type)
|
||||
|
||||
def __set__(self, instance, value):
|
||||
# forward to real field
|
||||
self.real_field.__set__(instance, value)
|
||||
|
||||
class BrokenForeignKey(models.ForeignKey):
|
||||
"""ForeignKey for when some null values aren't NULL in the database.
|
||||
|
||||
Django is strict with foreign keys, invalid ids result in
|
||||
DoesNotExist in inconvenient places. With this field, invalid ids
|
||||
are overridden to return None. Takes a keyword argument
|
||||
'null_values' to determine which ids should be considered
|
||||
invalid and equivalent to NULL."""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.broken_null_values = kwargs.pop('null_values', (0,))
|
||||
super(self.__class__, self).__init__(*args, **kwargs)
|
||||
|
||||
def broken_foreign_key_class_prepared_handler(sender, **kwargs):
|
||||
for f in sender._meta.fields:
|
||||
if type(f) == BrokenForeignKey:
|
||||
setattr(sender, f.name, InvalidToNoneOverrider(sender, f.name, f.broken_null_values))
|
||||
|
||||
models.signals.class_prepared.connect(broken_foreign_key_class_prepared_handler)
|
|
@ -1,42 +0,0 @@
|
|||
# Copyright The IETF Trust 2007, All Rights Reserved
|
||||
|
||||
# Caching accessor for the reverse of a ForeignKey relatinoship
|
||||
# Started by axiak on #django
|
||||
class FKAsOneToOne(object):
|
||||
def __init__(self, field, reverse = False, query = None):
|
||||
self.field = field
|
||||
self.reverse = reverse
|
||||
self.query = query
|
||||
|
||||
def __get_attr(self, instance):
|
||||
if self.reverse:
|
||||
field_name = '%s_set' % self.field
|
||||
else:
|
||||
field_name = self.field
|
||||
return getattr(instance, field_name)
|
||||
|
||||
def __get__(self, instance, Model):
|
||||
if not hasattr(instance, '_field_values'):
|
||||
instance._field_values = {}
|
||||
try:
|
||||
return instance._field_values[self.field]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
if self.reverse:
|
||||
value_set = self.__get_attr(instance).all()
|
||||
if self.query:
|
||||
value_set = value_set.filter(self.query)
|
||||
try:
|
||||
instance._field_values[self.field] = value_set[0]
|
||||
except IndexError:
|
||||
instance._field_values[self.field] = None
|
||||
else:
|
||||
instance._field_values[self.field] = self.__get_attr(instance)
|
||||
|
||||
return instance._field_values[self.field]
|
||||
|
||||
# We don't try to be smart and define __set__ to adjust the other
|
||||
# end of the relation since that could require setting several
|
||||
# fields, failing silently with a naive implementation. Updating
|
||||
# the other end is the responsibility of the caller.
|
|
@ -1,34 +0,0 @@
|
|||
from django.core.exceptions import ObjectDoesNotExist
|
||||
|
||||
class CachedLookupField(object):
|
||||
"""Django field for looking up and caching another object, like a
|
||||
ForeignKey only you must supply a function for doing the lookup
|
||||
yourself (and there's no reverse magic). Useful in case a real foreign
|
||||
key is missing. "lookup" is called on the first access to the field
|
||||
and gets the instance as sole argument; it should return an object
|
||||
or throw a DoesNotExist exception (which is normalized to None), e.g.
|
||||
|
||||
class A(django.db.models.Model):
|
||||
foo = CachedLookupField(lookup=lambda self: Foo.objects.get(key=self.key))
|
||||
key = CharField()
|
||||
"""
|
||||
|
||||
def __init__(self, lookup):
|
||||
self.lookup = lookup
|
||||
self.value = None
|
||||
self.value_cached = False
|
||||
|
||||
def __get__(self, instance, instance_type=None):
|
||||
if not instance_type:
|
||||
return self
|
||||
|
||||
if not self.value_cached:
|
||||
try:
|
||||
self.value = self.lookup(instance)
|
||||
except ObjectDoesNotExist:
|
||||
self.value = None
|
||||
self.value_cached = True
|
||||
|
||||
return self.value
|
||||
|
||||
|
Loading…
Reference in a new issue