1 from functools import wraps
3 from django.db.models import Count
8 View decorator, which puts tab info on a request.
12 def wrapped(request, *args, **kwargs):
13 request.catalogue_active_tab = tab
14 return f(request, *args, **kwargs)
19 class ChunksList(object):
20 def __init__(self, chunk_qs):
21 self.chunk_qs = chunk_qs.annotate(
22 book_length=Count('book__chunk')).select_related(
23 'book', 'stage__name',
26 self.book_qs = chunk_qs.values('book_id')
28 def __getitem__(self, key):
29 if isinstance(key, slice):
30 return self.get_slice(key)
31 elif isinstance(key, int):
32 return self.get_slice(slice(key, key+1))[0]
34 raise TypeError('Unsupported list index. Must be a slice or an int.')
37 return self.book_qs.count()
39 def get_slice(self, slice_):
40 book_ids = [x['book_id'] for x in self.book_qs[slice_]]
41 chunk_qs = self.chunk_qs.filter(book__in=book_ids)
45 for chunk in chunk_qs:
46 if chunk.book != book:
48 chunks_list.append(ChoiceChunks(book, [chunk], chunk.book_length))
50 chunks_list[-1].chunks.append(chunk)
54 class ChoiceChunks(object):
56 Associates the given chunks iterable for a book.
61 def __init__(self, book, chunks, book_length):
64 self.book_length = book_length