Some dictionary filtering features.
[wolnelektury.git] / apps / dictionary / views.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from dictionary.models import Note
6 from django.views.generic.list import ListView
7 from django.db.models import Count
8
9
10 class NotesView(ListView):
11     def get_queryset(self):
12         self.letters = ["0-9"] + [chr(a) for a in range(ord('a'), ord('z')+1)]
13         self.letter = self.request.GET.get('ltr')
14
15         self.qualifiers = Note.objects.order_by('qualifier').filter(qualifier__startswith='f').values_list(
16             'qualifier', flat=True).distinct()
17         self.qualifier = self.request.GET.get('qual')
18
19         self.languages = Note.objects.order_by('language').values_list(
20             'language', flat=True).distinct()
21         self.language = self.request.GET.get('lang')
22
23         self.fn_types = Note.objects.order_by('fn_type').values_list(
24             'fn_type', flat=True).distinct()
25         self.fn_type = self.request.GET.get('type')
26
27         objects = Note.objects.select_related('book').all()
28
29         if self.letter == "0-9":
30             objects = objects.filter(sort_key__regex=r"^[0-9]")
31         elif self.letter:
32             objects = objects.filter(sort_key__startswith=self.letter)
33
34         if self.qualifier:
35             objects = objects.filter(qualifier=self.qualifier)
36
37         if self.language:
38             objects = objects.filter(language=self.language)
39
40         if self.fn_type:
41             objects = objects.filter(fn_type=self.fn_type)
42
43         return objects
44
45         # TODO: wewn. wyszukiwarka, czy wg definiendum?
46         # TODO: filtr języka
47
48     def get_context_data(self, **kwargs):
49         context = super(NotesView, self).get_context_data(**kwargs)
50         context['letters'] = self.letters
51         context['letter'] = self.letter
52         context['qualifiers'] = self.qualifiers
53         context['qualifier'] = self.qualifier
54         context['languages'] = self.languages
55         context['language'] = self.language
56         context['fn_types'] = self.fn_types
57         context['fn_type'] = self.fn_type
58         return context