1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
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
12 class NotesView(ListView):
13 def __init__(self, **kwargs):
14 super(NotesView, self).__init__(**kwargs)
16 self.qualifiers = None
24 def get_queryset(self):
25 objects = Note.objects.all()
29 self.qualifier = Qualifier.objects.get(qualifier=self.request.GET.get('qual'))
30 except Qualifier.DoesNotExist:
33 filters['qualifier'] = Q(qualifiers=self.qualifier)
35 self.language = self.request.GET.get('lang')
37 filters['language'] = Q(language=self.language)
39 self.fn_type = self.request.GET.get('type')
41 filters['fn_type'] = Q(fn_type=self.fn_type)
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]")
48 objects = objects.filter(sort_key__startswith=self.letter)
49 # filters['letter'] = Q(sort_key__startswith=self.letter)
51 self.letters = ["0-9"] + [chr(a) for a in range(ord('a'), ord('z')+1)]
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()
60 for key, fltr in filters.items():
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)
67 (lang, lang_names.get(LANGUAGES_3TO2.get(lang, lang), lang))
68 for lang in self.languages]
71 for key, fltr in filters.items():
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]
78 for f in filters.values():
79 objects = objects.filter(f)
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