From 33f0938af485bb0a106d0e09f363027eaa87d061 Mon Sep 17 00:00:00 2001 From: Minali24 Date: Tue, 26 Mar 2019 17:43:47 +0530 Subject: [PATCH] Search working from header in home page also. All checkboxes are initially checked to show results from all resource types --- BasicArticle/search_indexes.py | 3 +- Community/forms.py | 3 ++ Community/search_indexes.py | 2 +- Community/views.py | 9 +++- Media/search_indexes.py | 2 + static/js/our_search_code.js | 85 +++++++++++----------------------- templates/base.html | 14 ++++-- templates/search_result.html | 78 ++++++++++++++++++++----------- 8 files changed, 106 insertions(+), 90 deletions(-) diff --git a/BasicArticle/search_indexes.py b/BasicArticle/search_indexes.py index c561fa6d..6c6885a3 100644 --- a/BasicArticle/search_indexes.py +++ b/BasicArticle/search_indexes.py @@ -7,7 +7,8 @@ class ArticlesIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, model_attr='title') # created_by = indexes.CharField(model_attr='created_by') - created_at = indexes.DateTimeField(model_attr='created_at') + created_at = indexes.DateTimeField(model_attr='created_at', faceted=True) + views = indexes.IntegerField(model_attr='views', faceted=True) state = indexes.CharField(model_attr='state') def get_model(self): diff --git a/Community/forms.py b/Community/forms.py index 6f55c1ea..5727b41f 100644 --- a/Community/forms.py +++ b/Community/forms.py @@ -108,6 +108,7 @@ def __init__(self, *args, **kwargs): def search(self): sqs = super(FacetedProductSearchForm, self).search() if self.categorys: + print("checking>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") query = None for category in self.categorys: if query: @@ -115,5 +116,7 @@ def search(self): else: query = u'' query += u'"%s"' % sqs.query.clean(category) + print("query >>>>>>>>>>> ", query) sqs = sqs.narrow(u'category_exact:%s' % query) + print("sqs >>>>>>>>>>>> ", str(sqs)) return sqs diff --git a/Community/search_indexes.py b/Community/search_indexes.py index e17a5858..37213fe0 100644 --- a/Community/search_indexes.py +++ b/Community/search_indexes.py @@ -8,7 +8,7 @@ class CommunityIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.EdgeNgramField(document=True, model_attr='name') category = indexes.CharField(model_attr='category', faceted=True) - created_at = indexes.DateTimeField(model_attr='created_at') + created_at = indexes.DateTimeField(model_attr='created_at', faceted=True) # for spelling suggestions suggestions = indexes.FacetCharField() diff --git a/Community/views.py b/Community/views.py index d67d465d..527383e9 100644 --- a/Community/views.py +++ b/Community/views.py @@ -757,10 +757,11 @@ def community_media_create(request): class FacetedSearchView(BaseFacetedSearchView, TemplateView): form_class = FacetedProductSearchForm - facet_fields = ['category'] + facet_fields = ['category', 'created_at', 'views'] template_name = 'search_result.html' #paginate_by = 3 - #context_object_name = 'object_list' + # context_object_name = 'object_list' + def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) @@ -781,5 +782,9 @@ def get_context_data(self, **kwargs): context['query'] = self.request.GET['query'] except: pass + # print("context ====================================== ", str(context)) + # print("context ====================================== ", str(context['object_list'])) + # print("context ====================================== ", str(context['facets'])) + object_list = context['object_list'] return context diff --git a/Media/search_indexes.py b/Media/search_indexes.py index 7d7c44dd..fd1fe0f2 100644 --- a/Media/search_indexes.py +++ b/Media/search_indexes.py @@ -8,6 +8,8 @@ class MediaIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.EdgeNgramField(document=True, model_attr='title') #category = indexes.CharField(model_attr='category', faceted=True) + created_at = indexes.DateTimeField(model_attr='created_at', faceted=True) + views = indexes.IntegerField(model_attr='views', faceted=True) mediafile = indexes.CharField(model_attr='mediafile') state = indexes.CharField(model_attr='state') diff --git a/static/js/our_search_code.js b/static/js/our_search_code.js index 69a2ac59..08d53282 100644 --- a/static/js/our_search_code.js +++ b/static/js/our_search_code.js @@ -27,61 +27,6 @@ function getParameterByName(name, url) { return decodeURIComponent(results[2].replace(/\+/g, " ")); } - - - -function onFacetChangeApplied(){ - - var url = window.location.href.split("?")[0]; - var search_query = getParameterByName('q'); - var community_query = getParameterByName('community'); - var article_query = getParameterByName('article'); - var image_query = getParameterByName('image'); - var audio_query = getParameterByName('audio'); - var video_query = getParameterByName('video'); - if (community_query !== null){ - community_query = "&community=community" - } - else{ - community_query = "" - } - if (article_query !== null){ - article_query = "&article=article" - } - else{ - article_query = "" - } - if (image_query !== null){ - image_query = "&image=image" - } - else{ - image_query = "" - } - if (audio_query !== null){ - audio_query = "&audio=audio" - } - else{ - audio_query = "" - } - if (video_query !== null){ - video_query = "&video=video" - } - else{ - video_query = "" - } - var url_with_search_query = url + '?q=' + search_query + community_query + audio_query + article_query + video_query + image_query - $('input:checkbox.facet').each(function () { - var sThisVal = (this.checked ? $(this).val() : null); - var sThisName = (this.checked ? $(this).attr('name') : null); - if(sThisVal !== null){ - url_with_search_query += '&'+encodeURIComponent(sThisName)+'='+encodeURIComponent(sThisVal); - } - }); - location.href = url_with_search_query; - return true; -} - - function getQueryParams(){ var vars = {}, hash; var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); @@ -93,14 +38,40 @@ function getQueryParams(){ return vars; } +function onFacetChangeApplied(){ + + var url = window.location.href.split("?")[0]; + var all_params = getQueryParams(); + var url_with_search_query = url + "?"; + var first_param = false; + $.each( all_params, function( key, value ) { + id = decodeURIComponent(key).replace(/\s/g,''); + if (first_param == true){ + url_with_search_query += "&" + } + url_with_search_query += encodeURIComponent(value) + "=" + encodeURIComponent(key); + first_param = true; + }); + $('input:checkbox.facet').each(function () { + var sThisVal = (this.checked ? $(this).val() : null); + var sThisName = (this.checked ? $(this).attr('name') : null); + if(sThisVal !== null){ + url_with_search_query += '&'+encodeURIComponent(sThisName)+'='+encodeURIComponent(sThisVal); + } + }); + + location.href = url_with_search_query; + return true; +} $( document ).ready(function() { var all_params = getQueryParams(); $.each( all_params, function( key, value ) { id = decodeURIComponent(key).replace(/\s/g,''); $('#'+id).attr('checked', 'checked'); - if (value == 'q'){ - $('#q').attr("value",key); + if (value == 'q' || value == 'q1'){ + var query_id = value; + $('#'+query_id).attr("value",key); } }); diff --git a/templates/base.html b/templates/base.html index e4be0a59..79851c0d 100644 --- a/templates/base.html +++ b/templates/base.html @@ -197,10 +197,18 @@