test fix
[redakcja.git] / apps / catalogue / helpers.py
1 from datetime import date
2 from functools import wraps
3
4 from django.db.models import Count
5
6
7 def active_tab(tab):
8     """
9         View decorator, which puts tab info on a request.
10     """
11     def wrapper(f):
12         @wraps(f)
13         def wrapped(request, *args, **kwargs):
14             request.catalogue_active_tab = tab
15             return f(request, *args, **kwargs)
16         return wrapped
17     return wrapper
18
19
20 def cached_in_field(field_name):
21     def decorator(f):
22         @property
23         @wraps(f)
24         def wrapped(self, *args, **kwargs):
25             value = getattr(self, field_name)
26             if value is None:
27                 value = f(self, *args, **kwargs)
28                 type(self)._default_manager.filter(pk=self.pk).update(**{field_name: value})
29             return value
30         return wrapped
31     return decorator
32
33
34 def parse_isodate(isodate):
35     try:
36         return date(*[int(p) for p in isodate.split('-')])
37     except (AttributeError, TypeError, ValueError):
38         raise ValueError("Not a date in ISO format.")