Minor fix in OAI-PMH.
[wolnelektury.git] / apps / 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 Count, 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 get_queryset(self):
15         objects = Note.objects.select_related('book').all()
16         filters = {}
17
18         try:
19             self.qualifier = Qualifier.objects.get(qualifier=self.request.GET.get('qual'))
20         except Qualifier.DoesNotExist:
21             self.qualifier = None
22         else:
23             filters['qualifier'] = Q(qualifiers=self.qualifier)
24
25         self.language = self.request.GET.get('lang')
26         if self.language:
27             filters['language'] = Q(language=self.language)
28
29         self.fn_type = self.request.GET.get('type')
30         if self.fn_type:
31             filters['fn_type'] = Q(fn_type=self.fn_type)
32
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]")
37         elif self.letter:
38             objects = objects.filter(sort_key__startswith=self.letter)
39             #filters['letter'] = Q(sort_key__startswith=self.letter)
40
41         self.letters = ["0-9"] + [chr(a) for a in range(ord('a'), ord('z')+1)]
42
43         nobj = objects
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()
48
49         nobj = objects
50         for key, fltr in filters.items():
51             if key != 'language':
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)
56         self.languages = [
57             (lang, lang_names.get(LANGUAGES_3TO2.get(lang, lang), lang))
58             for lang in self.languages]
59
60         nobj = objects
61         for key, fltr in filters.items():
62             if key != 'fn_type':
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]
67
68         for f in filters.values():
69             objects = objects.filter(f)
70
71         return objects
72
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
83         return context