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.db.models import Count, Q
7 from .constants import FN_TYPES
8 from .models import Note, Qualifier
11 class NotesView(ListView):
12 def get_queryset(self):
13 objects = Note.objects.select_related('book').all()
17 self.qualifier = Qualifier.objects.get(qualifier=self.request.GET.get('qual'))
18 except Qualifier.DoesNotExist:
21 filters['qualifier'] = Q(qualifiers=self.qualifier)
23 self.language = self.request.GET.get('lang')
25 filters['language'] = Q(language=self.language)
27 self.fn_type = self.request.GET.get('type')
29 filters['fn_type'] = Q(fn_type=self.fn_type)
31 self.letter = self.request.GET.get('ltr')
32 if self.letter == "0-9":
33 objects = objects.filter(sort_key__regex=r"^[0-9]")
34 #filters['letter'] = Q(sort_key__regex=r"^[0-9]")
36 objects = objects.filter(sort_key__startswith=self.letter)
37 #filters['letter'] = Q(sort_key__startswith=self.letter)
39 self.letters = ["0-9"] + [chr(a) for a in range(ord('a'), ord('z')+1)]
42 for key, fltr in filters.items():
43 if key != 'qualifier':
44 nobj = nobj.filter(fltr)
45 self.qualifiers = Qualifier.objects.filter(note__in=nobj).distinct()
48 for key, fltr in filters.items():
50 nobj = nobj.filter(fltr)
51 self.languages = nobj.order_by('language').values_list(
52 'language', flat=True).distinct()
55 for key, fltr in filters.items():
57 nobj = nobj.filter(fltr)
58 self.fn_types = nobj.order_by('fn_type').values_list(
59 'fn_type', flat=True).distinct()
60 self.fn_types = [(fnt, FN_TYPES[fnt]) for fnt in self.fn_types]
62 for f in filters.values():
63 objects = objects.filter(f)
67 def get_context_data(self, **kwargs):
68 context = super(NotesView, self).get_context_data(**kwargs)
69 context['letters'] = self.letters
70 context['letter'] = self.letter
71 context['qualifiers'] = self.qualifiers
72 context['qualifier'] = self.qualifier
73 context['languages'] = self.languages
74 context['language'] = self.language
75 context['fn_types'] = self.fn_types
76 context['fn_type'] = self.fn_type