From 7bc102f8df0345ebb3bf64c72703e8d50fe2aa9b Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Fri, 25 May 2018 12:57:12 +0200 Subject: [PATCH] improved search filter --- cookbook/filters.py | 12 +++++++++++- cookbook/models.py | 2 +- cookbook/templates/index.html | 9 ++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/cookbook/filters.py b/cookbook/filters.py index 5bbb8fd21..91972678b 100644 --- a/cookbook/filters.py +++ b/cookbook/filters.py @@ -1,10 +1,20 @@ import django_filters -from cookbook.models import Recipe +from cookbook.forms import MultiSelectWidget +from cookbook.models import Recipe, Keyword class RecipeFilter(django_filters.FilterSet): name = django_filters.CharFilter(lookup_expr='contains') + keywords = django_filters.ModelMultipleChoiceFilter(queryset=Keyword.objects.all(), widget=MultiSelectWidget, method='filter_keywords') + + @staticmethod + def filter_keywords(queryset, name, value): + if not name == 'keywords': + return queryset + for x in value: + queryset = queryset.filter(keywords=x) + return queryset class Meta: model = Recipe diff --git a/cookbook/models.py b/cookbook/models.py index 26b91489e..4f9fa6a49 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -40,7 +40,7 @@ class Recipe(models.Model): @property def all_tags(self): - return ', '.join([x.name for x in self.keywords.all()]) + return ', '.join([(x.icon + x.name) for x in self.keywords.all()]) class RecipeImport(models.Model): diff --git a/cookbook/templates/index.html b/cookbook/templates/index.html index 0090df706..e5380e99d 100644 --- a/cookbook/templates/index.html +++ b/cookbook/templates/index.html @@ -1,10 +1,15 @@ {% extends "base.html" %} {% load django_tables2 %} {% load crispy_forms_tags %} +{% load static %} {% load i18n %} {% block title %}{% trans "Cookbook" %}{% endblock %} +{% block extra_head %} + {{ filter.form.media }} +{% endblock %} + {% block content %}
@@ -14,9 +19,11 @@ {% trans "Search" %}
-
+ {{ filter.form|crispy }} + {% trans 'Reset' %}