Commit 7d96519f by alexandre

sort of working filters

parent 78a8f90b
......@@ -8,7 +8,14 @@ window.W = window.W || {};
W.ChoiceCollection = Backbone.Collection.extend({
initialize: function(models, options) {
this.url = '/api/scores/' + options.action + '/';
this.action = options.action;
this.url = '/api/scores/' + this.action + '/';
var that = this;
this.listenTo(options.syncWith, 'request', function(collection, xhr, options) {
that.fetch({data: options.data})
});
},
model: W.ChoiceModel,
......
......@@ -1768,7 +1768,7 @@ window.W = window.W || {};
W.ChoiceItemView = Backbone.Marionette.View.extend({
// template: '#filters-template',
template: _.template('<%- label %>'),
template: _.template('<%- label %> (<%- n %>)'),
tagName: 'option',
attributes : function () {
return {
......@@ -1791,6 +1791,13 @@ window.W = window.W || {};
childView: W.ChoiceItemView,
initialize: function (options) {
options.syncWith.listenTo(this, 'filter', function(view, xhr, options) {
this.queryParams[view.collection.action] = view.$el.val().join(",");
this.fetch()
});
},
onFilter: function (view, event) {
console.log(this.$el.val());
// this.collection.queryParams.score_type = $(event.currentTarget).data('value');
......@@ -1850,7 +1857,9 @@ window.W = window.W || {};
search: '#search',
list: '#list',
pagination: '#pagination',
filters: '#filters',
languageFilter: '#language-filter',
scoreTypeFilter: '#score-type-filter',
tagFilter: '#tag-filter',
},
onRender: function () {
......@@ -1878,14 +1887,14 @@ window.W = window.W || {};
});
// ServerSideFilter delegates the searching to the server by submitting a query.
var serverSideFilter = new Backgrid.Extension.ServerSideFilter({
collection: scoreCollection,
// the name of the URL query parameter
name: "score_type",
placeholder: "ex: location:your country" // HTML5 placeholder for the search box
});
// var serverSideFilter = new Backgrid.Extension.ServerSideFilter({
// collection: scoreCollection,
// // the name of the URL query parameter
// name: "score_type",
// placeholder: "ex: location:your country" // HTML5 placeholder for the search box
// });
$("body").before(serverSideFilter.render().el);
// $("body").before(serverSideFilter.render().el);
var mySearchView = new W.SearchView({
collection: scoreCollection
......@@ -1927,26 +1936,23 @@ window.W = window.W || {};
collection: scoreCollection
});
var that = this;
var languageCollection = new W.ChoiceCollection([], {action: "languages"})
var languageCollection = new W.ChoiceCollection([], {action: "language", syncWith: scoreCollection});
var languageChoiceListView = new W.ChoiceListView({collection: languageCollection, syncWith: scoreCollection});
languageCollection.listenTo(scoreCollection, 'request', function(collection, xhr, options) {
languageCollection.fetch({data: options.data})
});
var scoreTypeCollection = new W.ChoiceCollection([], {action: "score_type", syncWith: scoreCollection})
var scoreTypeChoiceListView = new W.ChoiceListView({collection: scoreTypeCollection, syncWith: scoreCollection})
var languageChoiceListView = new W.ChoiceListView({collection: languageCollection})
// $('body').append(bar.render().$el)
scoreCollection.listenTo(languageChoiceListView, 'filter', function(view, xhr, options) {
console.log(view.$el.val());
});
var TagCollection = new W.ChoiceCollection([], {action: "tags", syncWith: scoreCollection})
var TagChoiceListView = new W.ChoiceListView({collection: TagCollection, syncWith: scoreCollection})
var that = this;
myListView.collection.fetch({
success: function () {
that.showChildView('search', mySearchView);
that.showChildView('filters', languageChoiceListView);
that.showChildView('languageFilter', languageChoiceListView);
that.showChildView('scoreTypeFilter', scoreTypeChoiceListView);
that.showChildView('tagFilter', TagChoiceListView);
that.showChildView('list', myListView);
that.showChildView('pagination', myPaginationView);
}
......
<div>
<h1><%- t('Partitions existantes') %></h1>
<div id="search"></div>
<div id="filters"></div>
<div id="language-filter"></div>
<div id="score-type-filter"></div>
<div id="tag-filter"></div>
<div id="list"></div>
<div id="pagination"></div>
</div>
......@@ -37,31 +37,26 @@ class AttachmentViewSet(viewsets.ModelViewSet):
class ScoreViewSetPagination(PageNumberPagination):
page_size_query_param = 'page_size'
def paginate_queryset(self, queryset, request, view=None):
self.filters = {
"languages": queryset.order_by("language").values("language").annotate(n=models.Count("pk"))
}
return super(ScoreViewSetPagination, self).paginate_queryset(queryset, request, view=view)
from django_filters import rest_framework as filters
class TagsFilter(filters.CharFilter):
def filter(self, qs, value):
if value:
tags = [tag.strip() for tag in value.split(',')]
qs = qs.filter(tags__name__in=tags).distinct()
def get_paginated_response(self, data):
return Response(OrderedDict([
('filters', self.filters),
('count', self.page.paginator.count),
('next', self.get_next_link()),
('previous', self.get_previous_link()),
('results', data)
]))
return qs
class ScoreFilter(FilterSet):
is_featured = BooleanFilter()
shared_with = BooleanFilter()
tags = TagsFilter()
class Meta:
model = Score
fields = ('title', 'score_type', 'is_featured', 'shared_with')
fields = ('title', 'score_type', 'is_featured', 'shared_with', 'language', 'tags')
class ScoreAuthentication(authentication.SessionAuthentication):
......@@ -138,7 +133,7 @@ class ScoreViewSet(viewsets.ModelViewSet):
return Response(data)
@action(detail=False)
def languages(self, request):
def language(self, request):
data = self.filter_queryset(self.queryset).order_by("language").values("language").annotate(n=models.Count("pk"))
data = [{'value': i['language'], 'label': i['language'], 'n': i['n']} for i in data]
return Response(data)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment