From 064e541400f652aaa3ba6f954a97fafecb013648 Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Mon, 24 Apr 2017 14:23:16 +0200 Subject: [PATCH] working (and ugly) filtering for resources --- apps/catalogue/filters.py | 31 +++++++++++++++++++ .../templates/catalogue/finished.html | 6 +++- .../templates/catalogue/upcoming.html | 6 +++- apps/catalogue/views.py | 7 +++-- redakcja/settings/common.py | 1 + requirements.txt | 1 + 6 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 apps/catalogue/filters.py diff --git a/apps/catalogue/filters.py b/apps/catalogue/filters.py new file mode 100644 index 00000000..e0069bba --- /dev/null +++ b/apps/catalogue/filters.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# +# This file is part of MIL/PEER, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# +import django_filters +from django.utils.functional import lazy +from django_filters.filters import ModelMultipleChoiceFilter + +from catalogue.models import Document, Category + + +def tag_filter(dc_tag): + category = Category.objects.get(dc_tag=dc_tag) + return ModelMultipleChoiceFilter( + queryset=category.tag_set.all(), label=lazy(lambda: category.label, unicode)(), method='filter_by_tag') + + +class DocumentFilterSet(django_filters.FilterSet): + language = tag_filter('language') + license = tag_filter('rights') + audience = tag_filter('audience') + + class Meta: + model = Document + fields = [] + + def filter_by_tag(self, queryset, name, value): + if not value: + return queryset + return queryset.filter(tags__in=value).distinct() diff --git a/apps/catalogue/templates/catalogue/finished.html b/apps/catalogue/templates/catalogue/finished.html index 6b861903..16188ccc 100644 --- a/apps/catalogue/templates/catalogue/finished.html +++ b/apps/catalogue/templates/catalogue/finished.html @@ -4,7 +4,11 @@ {% block inner_content %}

{% trans "Finished resources" %}

- {% for doc in objects_list %} +
+ {{ filter_set.form.as_p }} + +
+ {% for doc in filter_set.qs %} {% include "catalogue/resource_box.html" with link_url='catalogue_html' %} {% endfor %} {% endblock %} diff --git a/apps/catalogue/templates/catalogue/upcoming.html b/apps/catalogue/templates/catalogue/upcoming.html index 9a8ae8c5..1ac70456 100644 --- a/apps/catalogue/templates/catalogue/upcoming.html +++ b/apps/catalogue/templates/catalogue/upcoming.html @@ -4,7 +4,11 @@ {% block inner_content %}

{% trans "Upcoming resources" %}

- {% for doc in objects_list %} +
+ {{ filter_set.form.as_p }} + +
+ {% for doc in filter_set.qs %} {% include "catalogue/resource_box.html" with link_url='catalogue_preview' %} {% endfor %} {% endblock %} diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index 86dc74db..64e39f67 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -24,6 +24,7 @@ from django.views.decorators.http import require_POST from unidecode import unidecode from catalogue import forms +from catalogue.filters import DocumentFilterSet from catalogue.forms import TagMultipleForm, TagSingleForm from catalogue.helpers import active_tab from catalogue.models import Category @@ -510,12 +511,14 @@ def fork(request, pk): def upcoming(request): + f = DocumentFilterSet(request.GET, queryset=Document.objects.filter(deleted=False).filter(publish_log=None)) return render(request, "catalogue/upcoming.html", { - 'objects_list': Document.objects.filter(deleted=False).filter(publish_log=None), + 'filter_set': f, }) def finished(request): + f = DocumentFilterSet(request.GET, queryset=Document.objects.filter(deleted=False).exclude(publish_log=None)) return render(request, "catalogue/finished.html", { - 'objects_list': Document.objects.filter(deleted=False).exclude(publish_log=None), + 'filter_set': f, }) diff --git a/redakcja/settings/common.py b/redakcja/settings/common.py index 83e720df..69d93345 100644 --- a/redakcja/settings/common.py +++ b/redakcja/settings/common.py @@ -128,6 +128,7 @@ INSTALLED_APPS = ( 'constance', 'constance.backends.database', 'honeypot', + 'django_filters', 'catalogue', # 'cover', diff --git a/requirements.txt b/requirements.txt index 5ef9001d..953c7d5c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -26,6 +26,7 @@ django-email-extras django-modeltranslation>=0.12,<0.13 django-constance[database] django-honeypot +django-filter celery>=3.1.12,<3.2 kombu>3.0,<3.1 -- 2.20.1