X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/f59e7c3de6bd0f85a61a4d9481db60cd7369ae92..f3dbec099e62eec49b0a9e1d293e7f0dc12361c1:/apps/dictionary/views.py?ds=sidebyside diff --git a/apps/dictionary/views.py b/apps/dictionary/views.py index 7b9cd5313..42aab6c36 100755 --- a/apps/dictionary/views.py +++ b/apps/dictionary/views.py @@ -2,16 +2,74 @@ # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # -from django.views.generic.list_detail import object_list -from catalogue.forms import SearchForm -from dictionary.models import Note - -def letter_notes(request, letter=None): - letters = ["0-9"] + [chr(a) for a in range(ord('a'), ord('z')+1)] - objects = Note.objects.all() - if letter == "0-9": - objects = objects.filter(sort_key__regex=r"^[0-9]") - elif letter: - objects = objects.filter(sort_key__startswith=letter) - - return object_list(request, queryset=objects, extra_context=locals()) +from dictionary.models import Note, Qualifier +from django.views.generic.list import ListView +from django.db.models import Count, Q + + +class NotesView(ListView): + def get_queryset(self): + objects = Note.objects.select_related('book').all() + filters = {} + + try: + self.qualifier = Qualifier.objects.get(qualifier=self.request.GET.get('qual')) + except Qualifier.DoesNotExist: + self.qualifier = None + else: + filters['qualifier'] = Q(qualifiers=self.qualifier) + + self.language = self.request.GET.get('lang') + if self.language: + filters['language'] = Q(language=self.language) + + self.fn_type = self.request.GET.get('type') + if self.fn_type: + filters['fn_type'] = Q(fn_type=self.fn_type) + + self.letter = self.request.GET.get('ltr') + if self.letter == "0-9": + objects = objects.filter(sort_key__regex=r"^[0-9]") + #filters['letter'] = Q(sort_key__regex=r"^[0-9]") + elif self.letter: + objects = objects.filter(sort_key__startswith=self.letter) + #filters['letter'] = Q(sort_key__startswith=self.letter) + + self.letters = ["0-9"] + [chr(a) for a in range(ord('a'), ord('z')+1)] + + nobj = objects + for key, fltr in filters.items(): + if key != 'qualifier': + nobj = nobj.filter(fltr) + self.qualifiers = Qualifier.objects.filter(note__in=nobj).distinct() + + nobj = objects + for key, fltr in filters.items(): + if key != 'language': + nobj = nobj.filter(fltr) + self.languages = nobj.order_by('language').values_list( + 'language', flat=True).distinct() + + nobj = objects + for key, fltr in filters.items(): + if key != 'fn_type': + nobj = nobj.filter(fltr) + self.fn_types = nobj.order_by('fn_type').values_list( + 'fn_type', flat=True).distinct() + + for f in filters.values(): + objects = objects.filter(f) + + return objects + + def get_context_data(self, **kwargs): + context = super(NotesView, self).get_context_data(**kwargs) + context['letters'] = self.letters + context['letter'] = self.letter + context['qualifiers'] = self.qualifiers + context['qualifier'] = self.qualifier + context['languages'] = self.languages + context['language'] = self.language + context['fn_types'] = self.fn_types + context['fn_type'] = self.fn_type + return context