wcag fixes
[redakcja.git] / apps / catalogue / helpers.py
index c9dc0bd..df64ade 100644 (file)
@@ -1,3 +1,4 @@
+from datetime import date
 from functools import wraps
 
 from django.db.models import Count
@@ -16,50 +17,22 @@ def active_tab(tab):
     return wrapper
 
 
-class ChunksList(object):
-    def __init__(self, chunk_qs):
-        self.chunk_qs = chunk_qs.annotate(
-            book_length=Count('book__chunk')).select_related(
-            'book', 'stage__name',
-            'user')
-
-        self.book_qs = chunk_qs.values('book_id')
-
-    def __getitem__(self, key):
-        if isinstance(key, slice):
-            return self.get_slice(key)
-        elif isinstance(key, int):
-            return self.get_slice(slice(key, key+1))[0]
-        else:
-            raise TypeError('Unsupported list index. Must be a slice or an int.')
-
-    def __len__(self):
-        return self.book_qs.count()
-
-    def get_slice(self, slice_):
-        book_ids = [x['book_id'] for x in self.book_qs[slice_]]
-        chunk_qs = self.chunk_qs.filter(book__in=book_ids)
-
-        chunks_list = []
-        book = None
-        for chunk in chunk_qs:
-            if chunk.book != book:
-                book = chunk.book
-                chunks_list.append(ChoiceChunks(book, [chunk], chunk.book_length))
-            else:
-                chunks_list[-1].chunks.append(chunk)
-        return chunks_list
-
-
-class ChoiceChunks(object):
-    """
-        Associates the given chunks iterable for a book.
-    """
-
-    chunks = None
+def cached_in_field(field_name):
+    def decorator(f):
+        @property
+        @wraps(f)
+        def wrapped(self, *args, **kwargs):
+            value = getattr(self, field_name)
+            if value is None:
+                value = f(self, *args, **kwargs)
+                type(self)._default_manager.filter(pk=self.pk).update(**{field_name: value})
+            return value
+        return wrapped
+    return decorator
 
-    def __init__(self, book, chunks, book_length):
-        self.book = book
-        self.chunks = chunks
-        self.book_length = book_length
 
+def parse_isodate(isodate):
+    try:
+        return date(*[int(p) for p in isodate.split('-')])
+    except (AttributeError, TypeError, ValueError):
+        raise ValueError("Not a date in ISO format.")