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.
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
13 class NotesView(ListView):
14 def __init__(self, **kwargs):
15 super(NotesView, self).__init__(**kwargs)
17 self.qualifiers = None
25 def get_queryset(self):
26 objects = Note.objects.all()
30 self.qualifier = Qualifier.objects.get(qualifier=self.request.GET.get('qual'))
31 except Qualifier.DoesNotExist:
34 filters['qualifier'] = Q(qualifiers=self.qualifier)
36 self.language = self.request.GET.get('lang')
38 filters['language'] = Q(language=self.language)
40 self.fn_type = self.request.GET.get('type')
42 filters['fn_type'] = Q(fn_type=self.fn_type)
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]")
49 objects = objects.filter(sort_key__startswith=self.letter)
50 # filters['letter'] = Q(sort_key__startswith=self.letter)
52 self.letters = ["0-9"] + [chr(a) for a in range(ord('a'), ord('z')+1)]
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()
61 for key, fltr in filters.items():
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)
68 (lang, lang_names.get(LANGUAGES_3TO2.get(lang, lang), lang))
69 for lang in self.languages]
72 for key, fltr in filters.items():
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]
79 for f in filters.values():
80 objects = objects.filter(f)
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