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