Use new epub builder.
[wolnelektury.git] / src / dictionary / views.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from django.views.generic.list import ListView
5 from django.conf import settings
6 from django.db.models import Q
7 from catalogue.constants import LANGUAGES_3TO2
8 from .constants import FN_TYPES
9 from .models import Note, Qualifier
10
11
12 class NotesView(ListView):
13     def __init__(self, **kwargs):
14         super(NotesView, self).__init__(**kwargs)
15         self.qualifier = None
16         self.qualifiers = None
17         self.language = None
18         self.languages = None
19         self.fn_type = None
20         self.fn_types = None
21         self.letter = None
22         self.letters = None
23
24     def get_queryset(self):
25         objects = Note.objects.all()
26         filters = {}
27
28         try:
29             self.qualifier = Qualifier.objects.get(qualifier=self.request.GET.get('qual'))
30         except Qualifier.DoesNotExist:
31             pass
32         else:
33             filters['qualifier'] = Q(qualifiers=self.qualifier)
34
35         self.language = self.request.GET.get('lang')
36         if self.language:
37             filters['language'] = Q(language=self.language)
38
39         self.fn_type = self.request.GET.get('type')
40         if self.fn_type:
41             filters['fn_type'] = Q(fn_type=self.fn_type)
42
43         self.letter = self.request.GET.get('ltr')
44         if self.letter == "0-9":
45             objects = objects.filter(sort_key__regex=r"^[0-9]")
46             # filters['letter'] = Q(sort_key__regex=r"^[0-9]")
47         elif self.letter:
48             objects = objects.filter(sort_key__startswith=self.letter)
49             # filters['letter'] = Q(sort_key__startswith=self.letter)
50
51         self.letters = ["0-9"] + [chr(a) for a in range(ord('a'), ord('z')+1)]
52
53         nobj = objects
54         for key, fltr in filters.items():
55             if key != 'qualifier':
56                 nobj = nobj.filter(fltr)
57         self.qualifiers = Qualifier.objects.filter(note__in=nobj).distinct()
58
59         nobj = objects
60         for key, fltr in filters.items():
61             if key != 'language':
62                 nobj = nobj.filter(fltr)
63         self.languages = nobj.order_by('language').values_list(
64             'language', flat=True).distinct()
65         lang_names = dict(settings.LANGUAGES)
66         self.languages = [
67             (lang, lang_names.get(LANGUAGES_3TO2.get(lang, lang), lang))
68             for lang in self.languages]
69
70         nobj = objects
71         for key, fltr in filters.items():
72             if key != 'fn_type':
73                 nobj = nobj.filter(fltr)
74         self.fn_types = nobj.order_by('fn_type').values_list(
75             'fn_type', flat=True).distinct()
76         self.fn_types = [(fnt, FN_TYPES[fnt]) for fnt in self.fn_types]
77
78         for f in filters.values():
79             objects = objects.filter(f)
80
81         return objects
82
83     def get_context_data(self, **kwargs):
84         context = super(NotesView, self).get_context_data(**kwargs)
85         context['letters'] = self.letters
86         context['letter'] = self.letter
87         context['qualifiers'] = self.qualifiers
88         context['qualifier'] = self.qualifier
89         context['languages'] = self.languages
90         context['language'] = self.language
91         context['fn_types'] = self.fn_types
92         context['fn_type'] = self.fn_type
93         return context