Update for Django 4.
[redakcja.git] / src / documents / templatetags / book_list.py
1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 import re
5 from django.db.models import Q, Count, F, Max
6 from django import template
7 from django.utils.translation import gettext_lazy as _
8 from django.contrib.auth.models import User
9 from documents.models import Book, Chunk, Image, Project
10
11 register = template.Library()
12
13
14 class ChunksList(object):
15     def __init__(self, chunk_qs):
16         self.chunk_qs = chunk_qs.select_related('book', 'book__project', 'stage', 'user')
17         self.book_qs = chunk_qs.values('book_id')
18
19     def __getitem__(self, key):
20         if isinstance(key, slice):
21             return self.get_slice(key)
22         elif isinstance(key, int):
23             return self.get_slice(slice(key, key+1))[0]
24         else:
25             raise TypeError('Unsupported list index. Must be a slice or an int.')
26
27     def __len__(self):
28         return self.book_qs.count()
29
30     def get_slice(self, slice_):
31         book_ids = [x['book_id'] for x in self.book_qs[slice_]]
32         chunk_qs = self.chunk_qs.filter(book__in=book_ids)
33
34         chunks_list = []
35         book = None
36         for chunk in chunk_qs:
37             if chunk.book != book:
38                 book = chunk.book
39                 chunks_list.append(ChoiceChunks(book, [chunk]))
40             else:
41                 chunks_list[-1].chunks.append(chunk)
42         return chunks_list
43
44
45 class ChoiceChunks(object):
46     """
47         Associates the given chunks iterable for a book.
48     """
49
50     chunks = None
51
52     def __init__(self, book, chunks):
53         self.book = book
54         self.chunks = chunks
55
56
57 def foreign_filter(qs, value, filter_field, model, model_field='slug', unset='-'):
58     if value == unset:
59         return qs.filter(**{filter_field: None})
60     if not value:
61         return qs
62     try:
63         obj = model._default_manager.get(**{model_field: value})
64     except model.DoesNotExist:
65         return qs.none()
66     else:
67         return qs.filter(**{filter_field: obj})
68
69
70 def search_filter(qs, value, filter_fields):
71     if not value:
72         return qs
73
74     for word in value.split():
75         m = re.match(r'(.+):(.+)', word)
76         if m is not None:
77             field = m.group(1)
78             value = m.group(2)
79             q = Book.q_dc(field, field + 's', value, 'book__')
80         else:
81             q = Q(**{"%s__icontains" % filter_fields[0]: value})
82             for field in filter_fields[1:]:
83                 q |= Q(**{"%s__icontains" % field: value})
84         qs = qs.filter(q)
85
86     return qs
87
88
89 _states = [
90         ('publishable', _('publishable'), Q(book___new_publishable=True)),
91         ('changed', _('changed'), Q(_changed=True)),
92         ('published', _('published'), Q(book___published=True)),
93         ('unpublished', _('unpublished'), Q(book___published=False)),
94         ('empty', _('empty'), Q(head=None)),
95     ]
96 _states_options = [s[:2] for s in _states]
97 _states_dict = dict([(s[0], s[2]) for s in _states])
98
99
100 def document_list_filter(request, **kwargs):
101
102     def arg_or_GET(field):
103         return kwargs.get(field, request.GET.get(field))
104
105     if arg_or_GET('all'):
106         chunks = Chunk.objects.all()
107     else:
108         chunks = Chunk.visible_objects.all()
109
110     chunks = chunks.order_by('book__title', 'book', 'number')
111
112     if not request.user.is_authenticated:
113         chunks = chunks.filter(book__public=True)
114
115     state = arg_or_GET('status')
116     if state in _states_dict:
117         chunks = chunks.filter(_states_dict[state])
118
119     chunks = foreign_filter(chunks, arg_or_GET('user'), 'user', User, 'username')
120     chunks = foreign_filter(chunks, arg_or_GET('stage'), 'stage', Chunk.tag_model, 'slug')
121     chunks = search_filter(chunks, arg_or_GET('title'), ['book__title', 'title'])
122     chunks = foreign_filter(chunks, arg_or_GET('project'), 'book__project', Project, 'pk')
123     return chunks
124
125
126 @register.inclusion_tag('documents/book_list/book_list.html', takes_context=True)
127 def book_list(context, user=None):
128     request = context['request']
129
130     if user:
131         filters = {"user": user}
132         new_context = {"viewed_user": user}
133     else:
134         filters = {}
135         new_context = {
136             "users": User.objects.annotate(
137                 count=Count('chunk')).filter(count__gt=0).order_by(
138                 '-count', 'last_name', 'first_name'),
139             "other_users": User.objects.annotate(
140                 count=Count('chunk')).filter(count=0).annotate(m=Max('chunkchange__created_at')).order_by(F('m').desc(nulls_last=True), 'last_name', 'first_name'),
141             "active_users": User.objects.annotate(m=Max('chunkchange__created_at')).order_by(F('m').desc(nulls_last=True), 'last_name', 'first_name'),
142         }
143
144     new_context.update({
145         "filters": True,
146         "request": request,
147         "books": ChunksList(document_list_filter(request, **filters)),
148         "stages": Chunk.tag_model.objects.all(),
149         "states": _states_options,
150         "projects": Project.objects.all(),
151     })
152
153     return new_context
154
155
156
157 _image_states = [
158         ('publishable', _('publishable'), Q(_new_publishable=True)),
159         ('changed', _('changed'), Q(_changed=True)),
160         ('published', _('published'), Q(_published=True)),
161         ('unpublished', _('unpublished'), Q(_published=False)),
162         ('empty', _('empty'), Q(head=None)),
163     ]
164 _image_states_options = [s[:2] for s in _image_states]
165 _image_states_dict = dict([(s[0], s[2]) for s in _image_states])
166
167 def image_list_filter(request, **kwargs):
168
169     def arg_or_GET(field):
170         return kwargs.get(field, request.GET.get(field))
171
172     images = Image.objects.all().select_related('user', 'stage', 'project')
173
174     if not request.user.is_authenticated:
175         images = images.filter(public=True)
176
177     state = arg_or_GET('status')
178     if state in _image_states_dict:
179         images = images.filter(_image_states_dict[state])
180
181     images = foreign_filter(images, arg_or_GET('user'), 'user', User, 'username')
182     images = foreign_filter(images, arg_or_GET('stage'), 'stage', Image.tag_model, 'slug')
183     images = search_filter(images, arg_or_GET('title'), ['title', 'title'])
184     images = foreign_filter(images, arg_or_GET('project'), 'project', Project, 'pk')
185     return images
186
187
188 @register.inclusion_tag('documents/image_table.html', takes_context=True)
189 def image_list(context, user=None):
190     request = context['request']
191
192     if user:
193         filters = {"user": user}
194         new_context = {"viewed_user": user}
195     else:
196         filters = {}
197         new_context = {
198             "users": User.objects.annotate(
199                 count=Count('image')).filter(count__gt=0).order_by(
200                 '-count', 'last_name', 'first_name'),
201             "other_users": User.objects.annotate(
202                 count=Count('image')).filter(count=0).order_by(
203                 'last_name', 'first_name'),
204                 }
205
206     new_context.update({
207         "filters": True,
208         "request": request,
209         "objects": image_list_filter(request, **filters),
210         "stages": Image.tag_model.objects.all(),
211         "states": _image_states_options,
212         "projects": Project.objects.all(),
213     })
214
215     return new_context