working (and ugly) filtering for resources
authorJan Szejko <janek37@gmail.com>
Mon, 24 Apr 2017 12:23:16 +0000 (14:23 +0200)
committerJan Szejko <janek37@gmail.com>
Mon, 24 Apr 2017 12:23:16 +0000 (14:23 +0200)
apps/catalogue/filters.py [new file with mode: 0644]
apps/catalogue/templates/catalogue/finished.html
apps/catalogue/templates/catalogue/upcoming.html
apps/catalogue/views.py
redakcja/settings/common.py
requirements.txt

diff --git a/apps/catalogue/filters.py b/apps/catalogue/filters.py
new file mode 100644 (file)
index 0000000..e0069bb
--- /dev/null
@@ -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()
index 6b86190..16188cc 100644 (file)
@@ -4,7 +4,11 @@
 
 {% block inner_content %}
     <h1>{% trans "Finished resources" %}</h1>
-    {% for doc in objects_list %}
+    <form action="" method="get">
+        {{ filter_set.form.as_p }}
+        <input type="submit" />
+    </form>
+    {% for doc in filter_set.qs %}
         {% include "catalogue/resource_box.html" with link_url='catalogue_html' %}
     {% endfor %}
 {% endblock %}
index 9a8ae8c..1ac7045 100644 (file)
@@ -4,7 +4,11 @@
 
 {% block inner_content %}
     <h1>{% trans "Upcoming resources" %}</h1>
-    {% for doc in objects_list %}
+    <form action="" method="get">
+        {{ filter_set.form.as_p }}
+        <input type="submit" />
+    </form>
+    {% for doc in filter_set.qs %}
         {% include "catalogue/resource_box.html" with link_url='catalogue_preview' %}
     {% endfor %}
 {% endblock %}
index 86dc74d..64e39f6 100644 (file)
@@ -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,
     })
index 83e720d..69d9334 100644 (file)
@@ -128,6 +128,7 @@ INSTALLED_APPS = (
     'constance',
     'constance.backends.database',
     'honeypot',
+    'django_filters',
 
     'catalogue',
     # 'cover',
index 5ef9001..953c7d5 100644 (file)
@@ -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