Skip to content

Commit

Permalink
Search working from header in home page also. All checkboxes are init…
Browse files Browse the repository at this point in the history
…ially checked to show results from all resource types
  • Loading branch information
Minali24 committed Mar 26, 2019
1 parent 1289074 commit 33f0938
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 90 deletions.
3 changes: 2 additions & 1 deletion BasicArticle/search_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions Community/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,15 @@ 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:
query += u' OR '
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
2 changes: 1 addition & 1 deletion Community/search_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 7 additions & 2 deletions Community/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

2 changes: 2 additions & 0 deletions Media/search_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
85 changes: 28 additions & 57 deletions static/js/our_search_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('&');
Expand All @@ -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);
}
});

Expand Down
14 changes: 11 additions & 3 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,18 @@
<span class="sep"></span>
<i class="fa fa-search search-btn"></i>
<div class="search-box">
<form method ='post' action="/search/">
{% csrf_token %}
<form method ='get' action="/search/">
<table style="text-align:center;">
<tr>
<label class="checkbox-inline hide"><input id="community" checked type="checkbox" name="community" value="community"> Community</label>
<label class="checkbox-inline hide"><input id="article" checked type="checkbox" name="article" value="article"> Article</label>
<label class="checkbox-inline hide"><input id="image" checked type="checkbox" name="image" value="image"> Image</label>
<label class="checkbox-inline hide"><input id="audio" checked type="checkbox" name="audio" value="audio"> Audio</label>
<label class="checkbox-inline hide"><input id="video" checked type="checkbox" name="video" value="video"> Video</label>
</tr>
</table>
<div class="input-group">
<input type="text" placeholder="Search" class="form-control" id="searchcriteria" name="searchcriteria">
<input type="text" placeholder="Search" class="form-control" name="q" id="q1">
<span class="input-group-btn">
<button class="btn btn-primary" type="submit">Search</button>
</span>
Expand Down
78 changes: 52 additions & 26 deletions templates/search_result.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ <h2>Search</h2><br>
<form method="get" action=".">
<table style="text-align:center;">
<tr>
<label class="checkbox-inline"><input id="community" type="checkbox" name="community" value="community"> Community</label>
<label class="checkbox-inline"><input id="article" type="checkbox" name="article" value="article"> Article</label>
<label class="checkbox-inline"><input id="image" type="checkbox" name="image" value="image"> Image</label>
<label class="checkbox-inline"><input id="audio" type="checkbox" name="audio" value="audio"> Audio</label>
<label class="checkbox-inline"><input id="video" type="checkbox" name="video" value="video"> Video</label>
<label class="checkbox-inline"><input id="community" checked type="checkbox" name="community" value="community"> Community</label>
<label class="checkbox-inline"><input id="article" checked type="checkbox" name="article" value="article"> Article</label>
<label class="checkbox-inline"><input id="image" checked type="checkbox" name="image" value="image"> Image</label>
<label class="checkbox-inline"><input id="audio" checked type="checkbox" name="audio" value="audio"> Audio</label>
<label class="checkbox-inline"><input id="video" checked type="checkbox" name="video" value="video"> Video</label>
</tr>
<!-- {{ form.as_table }} -->
<tr>
Expand All @@ -23,35 +23,61 @@ <h2>Search</h2><br>
</tr>
<hr>
<tr>

</tr>
</table>
</form>


<hr>
<hr>

<div class="container row">
{% if facets.fields %}
<div class="col-md-3 facet">
<h3>Filters</h3>
{% if facets.fields.category %}
<dt>Filter by Category</dt>
{% for category in facets.fields.category %}
{% if category.1 != 0 %}
<dl>
<input class="facet" id="{{category.0|cut:" "}}" type="checkbox" name="category" value="{{ category.0 }}"
data-toggle="toggle" /> {{ category.0 }} ({{ category.1 }})
</dl>
{% endif %}
{% endfor %}
{% endif %}
</dl>
<div>
<input class="btn btn-primary btn-xs" type="submit" value="apply filter" onclick="return onFacetChangeApplied();" />
</div>
<dl>
{% if facets.fields.category and community %}
<dt>Filter by Category</dt>
{% for category in facets.fields.category %}
{% if category.1 != 0 %}
<dl>
<input class="facet" id="{{category.0|cut:" "}}" type="checkbox" name="category" value="{{ category.0 }}" data-toggle="toggle" /> {{ category.0 }} ({{ category.1 }})
</dl>
{% endif %}
{% endfor %}
<div>
<input class="btn btn-primary btn-xs" type="submit" value="apply filter" onclick="return onFacetChangeApplied();" />
</div>
{% endif %}
<hr>
{% if facets.fields.created_at %}
<dt>Filter by Creation Date</dt>
{% for date in facets.fields.created_at %}
{% if date.1 != 0 %}
<dl>
<input class="facet" id="{{date.0|cut:" "}}" type="checkbox" name="date" value="{{ date.0 }}" data-toggle="toggle" /> {{ date.0 }} ({{ date.1 }})
</dl>
{% endif %}
{% endfor %}
<div>
<input class="btn btn-primary btn-xs" type="submit" value="apply filter" onclick="return onFacetChangeApplied();" />
</div>
{% endif %}
<hr>
{% if facets.fields.views %}
<dt>Filter by Views</dt>
{% for view in facets.fields.views %}
{% if view.1 != 0 %}
<dl>
<input class="facet" id="{{view.0|cut:" "}}" type="checkbox" name="view" value="{{ view.0 }}" data-toggle="toggle" /> {{ view.0 }} ({{ view.1 }})
</dl>
{% endif %}
{% endfor %}
<div>
<input class="btn btn-primary btn-xs" type="submit" value="apply filter" onclick="return onFacetChangeApplied();" />
</div>
{% endif %}
<hr>
{% endif %}
</div>


</form>


<div class="col-md-9">
Expand Down

0 comments on commit 33f0938

Please sign in to comment.