public
Created

Django #15363

  • Download Gist
qs.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
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']

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.