Aim to achieve - Get call stacks where specific model classes are used. (Example - MyModel.objects.save(), MyModel.objects.filter(), etc)
My Approach -
Add this in order to override Save and Delete.
In order to override get_query_set, I am overriding Model classes by
In both CallStackManager and CallStackMixin I am calling capture_call_stack which eventually logs calls.
ex.
Also, I am defining a decorator @donottrack functionality where obvious calls to model classes are made. So that I would just log the new unique call stacks.
Definition for @donottrack is here -
Class MyModelClass(CallStackMixin, models.Model)objects = CallStackManager()In both CallStackManager and CallStackMixin I am calling capture_call_stack which eventually logs calls.
ex.
class CallStackManager(Manager):
""" A Manager class which overrides the default Manager class for getting call stacks
"""
def get_query_set(self):
"""overriding the default queryset API method
"""
capture_call_stack(type(self))
return super(CallStackManager, self).get_query_set()Also, I am defining a decorator @donottrack functionality where obvious calls to model classes are made. So that I would just log the new unique call stacks.
Definition for @donottrack is here -
def donottrack(*classes_not_to_be_tracked):
"""function decorator which deals with toggling call stack
Args:
classes_not_to_be_tracked: model classes where tracking is undesirable
Returns:
wrapped function
"""
@wrapt.decorator
def real_donottrack(wrapped, instance, args, kwargs): # pylint: disable=W0613
"""takes function to be decorated and returns wrapped function
Args:
function - wrapped function i.e. real_donottrack
"""
global HALT_TRACKING # pylint: disable=W0603
HALT_TRACKING.append(classes_not_to_be_tracked)
HALT_TRACKING[-1] = list(set([x for sublist in HALT_TRACKING for x in sublist]))
return_value = wrapped(*args, **kwargs)
HALT_TRACKING.pop()
return return_value
return real_donottrackHALT_TRACKING here is global var which keeps track of model classes not to be tracked scoped to that particular function.
Also, using Wrapt in order to retain identity of wrapped functions.
Strangely, HALT_TRACKING.pop() gets executed before the call to Save, delete or get_query_set is made.
Any idea why it is so?
Also, using Wrapt in order to retain identity of wrapped functions.
Strangely, HALT_TRACKING.pop() gets executed before the call to Save, delete or get_query_set is made.
Any idea why it is so?
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2f5b709f-9749-40b3-99fe-416bfe0e79d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment