python - Decorators on Django Template Filters? -
I have a template filter that works a lot easier and works well, but I have a decorator I want to use. Unfortunately, the decorator causes a dirty deggo error that does not make any sense ...
The code that works:
@ register.filter (Name = "has_network") with defn (does not work): @ Register.filter (name = "has_network") @cache_function (30) defNetwork (profile, network): hnworkfunk = gateway (profile, "has_% s"% network) returns hnworkfunction ()
Here is an error:
TemplateSyntaxError at /
Rendering An exception was caught when doing: Pop from the empty list
I have tried to establish a break point inside the decorator and I am sure that it is not being called ... < / P>
But just in case the decorator (I know someone will ask for it)
I changed the decorator (temporarily) with a fake decorator N off, but i still get the same error
def cache_function (cache_ti Meout): DEF Cover (FN): DEF DECORATOR (* AGR, ** QUARGE): Returns FN (* ALGS, **
Editing Confirmed : This is the reason because decorator * args
and ** kwargs
? Do I think that pop ()
is being called to ensure that at least one ARG is taken?
The decorator fixes this problem:
Unfortunately this is a ruins of the usual nature: Decorator: / What to do now?
Last reply: Add an extra log for decorator which indicates what is being decorated
Something might be more beautiful, but it works.
django.core.cache Import cache from django.db.models.query Import Query Try: Exclude CPickle import dumps: hashlib import sha1 cache_miss = object () class Pickle with CantPickleAQuerySet (exception) From import dumps: def cache_function (cache_timeout, func_type = 'generic'): def wrapper (fn): DEF decoration (or * args, ** quarges): try: cache_indentifier = "% s% s% s% s "Exception:% (fn .___ module__, fn .___ name__, dump (RGS), dump (case) except the exception:" Print Error:% s \ n Failed to generate cache key:% s (% e, Fn.__ module__, fn .___name__) retire Fn (* args, ** quaerge) cache_key = key = sha1 (cache_indentifier) .hexdigest () value = Cache.get (cache_key, cache_miss) If the value is cache_miss: value = fn (* args, ** kwargs) if ifinstance (value , QuerySet): Increase the CantPickleAQuerySet ("You can not cache a query but you can cache a list! Just change your query (the value you are returning) to the list so that 'Return list (queries) ) Try: cache.set (cache_key, value, cache_timeout) Except exception, e: print "error:% s \ nFailed cache For:% s \ nvalue:% s "% (e, cache_indentifiers, value) return value no_arg2 = object () def filter_decorator (arg1, arg2 = no_arg2): if arg2 is no_arg2: return decorator (arg1) other: return Decorator (Arg1, arg2) If func_type == 'generic': return decorator elif func_type == 'filter': return filter_decorator return wrapper
Comments
Post a Comment