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 Count, 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 get_queryset(self):
15 objects = Note.objects.all()
19 self.qualifier = Qualifier.objects.get(qualifier=self.request.GET.get('qual'))
20 except Qualifier.DoesNotExist:
23 filters['qualifier'] = Q(qualifiers=self.qualifier)
25 self.language = self.request.GET.get('lang')
27 filters['language'] = Q(language=self.language)
29 self.fn_type = self.request.GET.get('type')
31 filters['fn_type'] = Q(fn_type=self.fn_type)
33 self.letter = self.request.GET.get('ltr')
34 if self.letter == "0-9":
35 objects = objects.filter(sort_key__regex=r"^[0-9]")
36 #filters['letter'] = Q(sort_key__regex=r"^[0-9]")
38 objects = objects.filter(sort_key__startswith=self.letter)
39 #filters['letter'] = Q(sort_key__startswith=self.letter)
41 self.letters = ["0-9"] + [chr(a) for a in range(ord('a'), ord('z')+1)]
44 for key, fltr in filters.items():
45 if key != 'qualifier':
46 nobj = nobj.filter(fltr)
47 self.qualifiers = Qualifier.objects.filter(note__in=nobj).distinct()
50 for key, fltr in filters.items():
52 nobj = nobj.filter(fltr)
53 self.languages = nobj.order_by('language').values_list(
54 'language', flat=True).distinct()
55 lang_names = dict(settings.LANGUAGES)
57 (lang, lang_names.get(LANGUAGES_3TO2.get(lang, lang), lang))
58 for lang in self.languages]
61 for key, fltr in filters.items():
63 nobj = nobj.filter(fltr)
64 self.fn_types = nobj.order_by('fn_type').values_list(
65 'fn_type', flat=True).distinct()
66 self.fn_types = [(fnt, FN_TYPES[fnt]) for fnt in self.fn_types]
68 for f in filters.values():
69 objects = objects.filter(f)
73 def get_context_data(self, **kwargs):
74 context = super(NotesView, self).get_context_data(**kwargs)
75 context['letters'] = self.letters
76 context['letter'] = self.letter
77 context['qualifiers'] = self.qualifiers
78 context['qualifier'] = self.qualifier
79 context['languages'] = self.languages
80 context['language'] = self.language
81 context['fn_types'] = self.fn_types
82 context['fn_type'] = self.fn_type