From dd4bed785732b7418fcc5cc10838d1ed71246aa3 Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Mon, 15 May 2017 17:17:15 +0100 Subject: [PATCH] Implement haystack --- .gitignore | 51 +++++++++++++++++++ shop/search_indexes.py | 16 ++++++ .../search/indexes/shop/product_text.txt | 3 ++ shop/urls.py | 4 +- 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 shop/search_indexes.py create mode 100644 shop/templates/search/indexes/shop/product_text.txt diff --git a/.gitignore b/.gitignore index 1113749..0b06d8b 100644 --- a/.gitignore +++ b/.gitignore @@ -105,3 +105,54 @@ ENV/ .ropeproject # End of https://www.gitignore.io/api/dj,django,python + +# Created by https://www.gitignore.io/api/emacs + +### Emacs ### +# -*- mode: gitignore; -*- +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + +# cask packages +.cask/ +dist/ + +# Flycheck +flycheck_*.el + +# server auth directory +/server/ + +# projectiles files +.projectile + +# directory configuration +.dir-locals.el + +# End of https://www.gitignore.io/api/emacs diff --git a/shop/search_indexes.py b/shop/search_indexes.py new file mode 100644 index 0000000..44504ef --- /dev/null +++ b/shop/search_indexes.py @@ -0,0 +1,16 @@ +import datetime +from haystack import indexes +from .models import * + +class ProductIndex(indexes.SearchIndex, indexes.Indexable): + text = indexes.CharField(document=True, use_template=True) + product_name = indexes.CharField(model_attr='product_name') + price = indexes.DecimalField(model_attr='price') + currency = indexes.CharField(model_attr='price_currency') + + + def get_model(self): + return Product + + def index_queryset(self, using=None): + return self.get_model().objects.filter(approved=True) diff --git a/shop/templates/search/indexes/shop/product_text.txt b/shop/templates/search/indexes/shop/product_text.txt new file mode 100644 index 0000000..4fb2e73 --- /dev/null +++ b/shop/templates/search/indexes/shop/product_text.txt @@ -0,0 +1,3 @@ +{{ object.product_name }} +{{ object.product_discription }} +{{ object.seller.username }} diff --git a/shop/urls.py b/shop/urls.py index 00d6129..a7e725d 100644 --- a/shop/urls.py +++ b/shop/urls.py @@ -1,11 +1,11 @@ # pylint: disable=C0321 -from django.conf.urls import url +from django.conf.urls import url, include from . import views app_name = 'shop' urlpatterns = [ url(r'^$', views.homepage, name='homepage'), - url(r'^search/$', views.search, name='search'), + url(r'^search/', views.search, name='search'), url(r'^product/(?P\d*[1-9]\d*)/$', views.view_product, name='viewproduct'), url(