From 924d58f935adfa9f3e29eae3a78ad0f5d860ca14 Mon Sep 17 00:00:00 2001 From: Ole Laursen Date: Sat, 7 Dec 2013 12:37:29 +0000 Subject: [PATCH] Remove model field utilities that were needed because of brokenness in the old schema - Legacy-Id: 6833 --- ietf/utils/broken_foreign_key.py | 43 ------------------------------- ietf/utils/cache_foreign_key.py | 42 ------------------------------ ietf/utils/cached_lookup_field.py | 34 ------------------------ 3 files changed, 119 deletions(-) delete mode 100644 ietf/utils/broken_foreign_key.py delete mode 100644 ietf/utils/cache_foreign_key.py delete mode 100644 ietf/utils/cached_lookup_field.py diff --git a/ietf/utils/broken_foreign_key.py b/ietf/utils/broken_foreign_key.py deleted file mode 100644 index da5648282..000000000 --- a/ietf/utils/broken_foreign_key.py +++ /dev/null @@ -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) diff --git a/ietf/utils/cache_foreign_key.py b/ietf/utils/cache_foreign_key.py deleted file mode 100644 index cbf43f8f7..000000000 --- a/ietf/utils/cache_foreign_key.py +++ /dev/null @@ -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. diff --git a/ietf/utils/cached_lookup_field.py b/ietf/utils/cached_lookup_field.py deleted file mode 100644 index 5c56ab7c8..000000000 --- a/ietf/utils/cached_lookup_field.py +++ /dev/null @@ -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 - -