253f1291ede51bb9ae4e9c3c7e1ff01c14b2b26b
[redakcja.git] / apps / wiki / helpers.py
1 from django import http
2 from django.db.models import Count
3 from django.utils import simplejson as json
4 from django.utils.functional import Promise
5 from datetime import datetime
6 from functools import wraps
7
8
9 class ExtendedEncoder(json.JSONEncoder):
10
11     def default(self, obj):
12         if isinstance(obj, Promise):
13             return unicode(obj)
14
15         if isinstance(obj, datetime):
16             return datetime.ctime(obj) + " " + (datetime.tzname(obj) or 'GMT')
17
18         return json.JSONEncoder.default(self, obj)
19
20
21 # shortcut for JSON reponses
22 class JSONResponse(http.HttpResponse):
23
24     def __init__(self, data={}, **kwargs):
25         # get rid of mimetype
26         kwargs.pop('mimetype', None)
27
28         data = json.dumps(data, cls=ExtendedEncoder)
29         super(JSONResponse, self).__init__(data, mimetype="application/json", **kwargs)
30
31
32 # return errors
33 class JSONFormInvalid(JSONResponse):
34     def __init__(self, form):
35         super(JSONFormInvalid, self).__init__(form.errors, status=400)
36
37
38 class JSONServerError(JSONResponse):
39     def __init__(self, *args, **kwargs):
40         kwargs['status'] = 500
41         super(JSONServerError, self).__init__(*args, **kwargs)
42
43
44 def ajax_login_required(view):
45     @wraps(view)
46     def authenticated_view(request, *args, **kwargs):
47         if not request.user.is_authenticated():
48             return http.HttpResponse("Login required.", status=401, mimetype="text/plain")
49         return view(request, *args, **kwargs)
50     return authenticated_view
51
52
53 def ajax_require_permission(permission):
54     def decorator(view):
55         @wraps(view)
56         def authorized_view(request, *args, **kwargs):
57             if not request.user.has_perm(permission):
58                 return http.HttpResponse("Access Forbidden.", status=403, mimetype="text/plain")
59             return view(request, *args, **kwargs)
60         return authorized_view
61     return decorator
62
63 import collections
64
65 def recursive_groupby(iterable):
66     """
67 #    >>> recursive_groupby([1,2,3,4,5])
68 #    [1, 2, 3, 4, 5]
69
70     >>> recursive_groupby([[1]])
71     [1]
72
73     >>> recursive_groupby([('a', 1),('a', 2), 3, ('b', 4), 5])
74     ['a', [1, 2], 3, 'b', [4], 5]
75
76     >>> recursive_groupby([('a', 'x', 1),('a', 'x', 2), ('a', 'x', 3)])
77     ['a', ['x', [1, 2, 3]]]
78
79     """
80
81     def _generator(iterator):
82         group = None
83         grouper = None
84
85         for item in iterator:
86             if not isinstance(item, collections.Sequence):
87                 if grouper is not None:
88                     yield grouper
89                     if len(group):
90                         yield recursive_groupby(group)
91                     group = None
92                     grouper = None
93                 yield item
94                 continue
95             elif len(item) == 1:
96                 if grouper is not None:
97                     yield grouper
98                     if len(group):
99                         yield recursive_groupby(group)
100                     group = None
101                     grouper = None
102                 yield item[0]
103                 continue
104             elif not len(item):
105                 continue
106
107             if grouper is None:
108                 group = [item[1:]]
109                 grouper = item[0]
110                 continue
111
112             if grouper != item[0]:
113                 if grouper is not None:
114                     yield grouper
115                     if len(group):
116                         yield recursive_groupby(group)
117                     group = None
118                     grouper = None
119                 group = [item[1:]]
120                 grouper = item[0]
121                 continue
122
123             group.append(item[1:])
124
125         if grouper is not None:
126             yield grouper
127             if len(group):
128                 yield recursive_groupby(group)
129             group = None
130             grouper = None
131
132     return list(_generator(iterable))
133
134
135 def active_tab(tab):
136     """
137         View decorator, which puts tab info on a request.
138     """
139     def wrapper(f):
140         @wraps(f)
141         def wrapped(request, *args, **kwargs):
142             request.wiki_active_tab = tab
143             return f(request, *args, **kwargs)
144         return wrapped
145     return wrapper
146
147
148 class ChunksList(object):
149     def __init__(self, chunk_qs):
150         self.chunk_qs = chunk_qs.annotate(
151             book_length=Count('book__chunk')).select_related(
152             'book', 'stage__name',
153             'user')
154
155         self.book_ids = [x['book_id'] for x in chunk_qs.values('book_id')]
156
157     def __getitem__(self, key):
158         if isinstance(key, slice):
159             return self.get_slice(key)
160         elif isinstance(key, int):
161             return self.get_slice(slice(key, key+1))[0]
162         else:
163             raise TypeError('Unsupported list index. Must be a slice or an int.')
164
165     def __len__(self):
166         return len(self.book_ids)
167
168     def get_slice(self, slice_):
169         book_ids = self.book_ids[slice_]
170         chunk_qs = self.chunk_qs.filter(book__in=book_ids)
171
172         chunks_list = []
173         book = None
174         for chunk in chunk_qs:
175             if chunk.book != book:
176                 book = chunk.book
177                 chunks_list.append(ChoiceChunks(book, [chunk], chunk.book_length))
178             else:
179                 chunks_list[-1].chunks.append(chunk)
180         return chunks_list
181
182
183 class ChoiceChunks(object):
184     """
185         Associates the given chunks iterable for a book.
186     """
187
188     chunks = None
189
190     def __init__(self, book, chunks, book_length):
191         self.book = book
192         self.chunks = chunks
193         self.book_length = book_length
194