Created
February 5, 2013 21:35
-
-
Save charettes/4717921 to your computer and use it in GitHub Desktop.
Django #15363
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def deprecated_get_query_set(cls): | |
def get_query_set(self, *args, **kwargs): | |
self.trace.append("%s.get_query_set" % cls.__name__) | |
return cls.get_queryset(self, *args, **kwargs) | |
cls.get_query_set = get_query_set | |
# Account for already defined __getattribute__ | |
getattribute = getattr(cls, '__getattribute__') | |
def __getattribute__(self, name): | |
attribute = getattribute(self, name) | |
if name == 'get_queryset': | |
print(self, cls) | |
if attribute.im_func is cls.get_queryset.im_func: | |
# No defined `get_queryset` method | |
get_query_set = getattribute(self, 'get_query_set') | |
if get_query_set.im_func is not cls.get_query_set.im_func: | |
# Fallback to defined `get_query_set` | |
return get_query_set | |
return attribute | |
cls.__getattribute__ = __getattribute__ | |
return cls | |
@deprecated_get_query_set | |
class Manager(object): | |
def __init__(self): | |
self.trace = [] | |
def get_queryset(self): | |
self.trace.append('Manager.get_queryset') | |
return self.trace | |
class Old(Manager): | |
def get_query_set(self): | |
self.trace.append('Old.get_query_set') | |
return super(Old, self).get_query_set() | |
#assert Old().get_queryset() == ['Old.get_query_set', 'Manager.get_query_set', 'Manager.get_queryset'] | |
#assert Old().get_query_set() == ['Old.get_query_set', 'Manager.get_query_set', 'Manager.get_queryset'] | |
class OldSubclass(Manager): | |
def get_query_set(self): | |
self.trace.append('OldSubclass.get_query_set') | |
return super(OldSubclass, self).get_query_set() | |
#assert OldSubclass().get_queryset() == ['OldSubclass.get_query_set', 'Old.get_query_set', 'Manager.get_query_set', 'Manager.get_queryset'] | |
#assert OldSubclass().get_query_set() == ['OldSubclass.get_query_set', 'Old.get_query_set', 'Manager.get_query_set', 'Manager.get_queryset'] | |
@deprecated_get_query_set | |
class New(Manager): | |
def get_queryset(self): | |
self.trace.append('New.get_queryset') | |
return super(New, self).get_queryset() | |
#assert New().get_queryset() == ['New.get_queryset', 'Manager.get_queryset'] | |
#assert New().get_query_set() == ['New.get_query_set', 'New.get_queryset', 'Manager.get_queryset'] | |
@deprecated_get_query_set | |
class NewSubclass(Manager): | |
def get_queryset(self): | |
self.trace.append('NewSubclass.get_queryset') | |
return super(NewSubclass, self).get_queryset() | |
assert NewSubclass().get_queryset() == ['NewSubclass.get_queryset', 'New.get_queryset', 'Manager.get_queryset'] | |
assert NewSubclass().get_query_set() == ['NewSubclass.get_query_set', 'NewSubclass.get_queryset', 'New.get_queryset', 'Manager.get_queryset'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment