7bc248193f5a58ec2d5a4564b8f6a3a43c3cb3c2
[redakcja.git] / apps / catalogue / helpers.py
1 from functools import wraps
2
3 from django.db.models import Count
4
5
6 def active_tab(tab):
7     """
8         View decorator, which puts tab info on a request.
9     """
10     def wrapper(f):
11         @wraps(f)
12         def wrapped(request, *args, **kwargs):
13             request.catalogue_active_tab = tab
14             return f(request, *args, **kwargs)
15         return wrapped
16     return wrapper
17
18
19 def cached_in_field(field_name):
20     def decorator(f):
21         @property
22         @wraps(f)
23         def wrapped(self, *args, **kwargs):
24             value = getattr(self, field_name)
25             if value is None:
26                 value = f(self, *args, **kwargs)
27                 type(self)._default_manager.filter(pk=self.pk).update(**{field_name: value})
28             return value
29         return wrapped
30     return decorator