def wrapped(*args, **kwargs): name = 'rc_{}.{}'.format(func.__module__, func.__name__) cached = get_variable(name) if cached: return cached result = func(*args, **kwargs) # Avoid cache forever in tasks, shell or tests. if get_variable('request'): set_variable(name, result) return result
def test_view(request): # test exception if request.GET.get('exception'): raise Exception if request != get_variable('request'): raise AssertionError('Bad request value.') if request.user != get_variable('user'): raise AssertionError('Bad user value.') # test callback if request.GET.get('callback') and request.GET['callback'] != get_variable( 'callback' ): raise AssertionError('Bad callback value.') return HttpResponse('')
def log_post_delete(sender, instance, **kwargs): if get_variable('disable_logging'): return SimpleLog = get_log_model() instance._log = SimpleLog.log( instance, action_flag=SimpleLog.DELETE, old=instance._old_values, new=None )
def log_post_save(sender, instance, created, **kwargs): if get_variable('disable_logging'): return if not hasattr(instance, '_log'): SimpleLog = get_log_model() instance._log = SimpleLog.log( instance, action_flag=SimpleLog.ADD if created else SimpleLog.CHANGE, commit=False )
def save_logs_on_commit(): all_logs = get_variable('simple_log_instances', {}).values() for log in [x for x in all_logs if not x.pk]: log.old = getattr(log.instance, '_old_value', None) log.new = serialize_instance(log.instance) if log.is_delete or log.old != log.new: log.save() if settings.SAVE_RELATED and any( [x.pk for x in all_logs if not x.disable_related] ): save_related(all_logs) del_variable('simple_log_instances')
def add_to_thread(cls, instance, obj): in_commit = save_logs_on_commit in [ f[1] for f in connection.run_on_commit ] instances = get_variable('simple_log_instances', {}) # prevent memory usage in non transaction test cases if not in_commit and instances: del_variable('simple_log_instances') instances = {} instances[instance] = obj set_variable('simple_log_instances', instances) if not in_commit: connection.on_commit(save_logs_on_commit)
def log_m2m_change(sender, instance, action, **kwargs): if get_variable('disable_logging'): return SimpleLog = get_log_model() if action in ('pre_add', 'pre_remove', 'pre_clear'): SimpleLog.set_initial(instance) if action in ('post_add', 'post_remove', 'post_clear'): if not hasattr(instance, '_log'): instance._log = SimpleLog.log( instance, action_flag=SimpleLog.CHANGE, commit=False )
def log(cls, instance, commit=True, with_initial=False, force_save=False, **kwargs): if with_initial: cls.set_initial(instance) obj = cls(**cls.get_log_params(instance, **kwargs)) obj.force_save = force_save obj.instance = instance obj.disable_related = get_variable('disable_related', False) if commit: obj.save() cls.add_to_thread(obj) return obj
def create_parent_logs(self): new_instances = get_variable('simple_log_instances', {}) for field in self.instance.parent_model_fields: parent_instance = getattr(self.instance, field, None) if parent_instance: parent_log = new_instances.get(parent_instance) if not parent_log: parent_log = self.log( parent_instance, commit=False, action_flag=self.CHANGE, with_initial=True, ) self._related_objects = self._get_related_objects() + [ parent_log ]
def add_to_thread(cls, obj): in_commit = 'sl_in_commit' in \ [f[1].__name__ for f in connection.run_on_commit] logs = get_variable('logs', []) # prevent memory usage in non transaction test cases if not in_commit and logs: del_variable('logs') logs = [] logs.append(obj) set_variable('logs', logs) if not in_commit: def sl_in_commit(): save_logs_on_commit(logs) connection.on_commit(sl_in_commit)
def log(cls, instance, commit=True, with_initial=False, force_save=False, using=None, **kwargs): if with_initial: cls.set_initial(instance, using) try: obj = cls(**cls.get_log_params(instance, **kwargs)) obj.instance = instance instance._log = obj obj.force_save = force_save obj.disable_related = get_variable('disable_related', False) if hasattr(instance, 'parent_model_fields'): obj.create_parent_logs() if commit: obj.save() cls.add_to_thread(instance, obj) return obj except Exception: logger.exception("Can't create log instance.")
def get_current_request_default(): return get_variable('request')
def log_pre_save_delete(sender, instance, **kwargs): if get_variable('disable_logging'): return SimpleLog = get_log_model() SimpleLog.set_initial(instance)
def test_get_variable_default(self): self.assertIsNone(get_variable('non_exist')) self.assertEqual(get_variable('non_exist', 'default'), 'default')
def test_get_variable(self): REQUEST_VARS.test = 'get_test' self.assertEqual(get_variable('test'), 'get_test') delattr(REQUEST_VARS, 'test')
def is_log_needed(instance, raw): return settings.ENABLED and not (get_variable('disable_logging') or (instance.pk and instance in get_variable( 'simple_log_instances', {})) or (raw and settings.EXCLUDE_RAW))