Skip to content

Commit

Permalink
Merge pull request #339 from edx-solutions/dcikatic/SOL-20-filter-cha…
Browse files Browse the repository at this point in the history
…nges

Dcikatic/sol 20 filter changes
  • Loading branch information
Martyn James committed Dec 3, 2014
2 parents 7710af5 + 8f988de commit 6132d42
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 21 deletions.
26 changes: 17 additions & 9 deletions cms/djangoapps/contentstore/views/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,19 @@ def _assets_json(request, course_key):
requested_file_types = settings.FILES_AND_UPLOAD_TYPE_FILTER.get(
requested_filter, None)
filter_params = None
if requested_filter is not None and len(requested_filter) > 0:
if requested_filter:
if requested_filter == 'OTHER':
all_filters = settings.FILES_AND_UPLOAD_TYPE_FILTER
where = []
for all_filter in all_filters:
extension_filters = all_filters[all_filter]
where.extend(["this.displayname.split('.').reverse()[0].toUpperCase() != '{}'.toUpperCase()".format(
where.extend(["this.contentType.toUpperCase() != '{}'.toUpperCase()".format(
extension_filter) for extension_filter in extension_filters])
filter_params = {
"$where": ' && '.join(where),
}
else:
where = ["this.displayname.split('.').reverse()[0].toUpperCase() == '{}'.toUpperCase()".format(
where = ["this.contentType.toUpperCase() == '{}'.toUpperCase()".format(
req_filter) for req_filter in requested_file_types]
filter_params = {
"$where": ' || '.join(where),
Expand All @@ -131,17 +131,21 @@ def _assets_json(request, course_key):

current_page = max(requested_page, 0)
start = current_page * requested_page_size
assets, total_count = _get_assets_for_page(
request, course_key, current_page, requested_page_size, sort, filter_params=filter_params)
options = {
'current_page': current_page,
'page_size': requested_page_size,
'sort': sort,
'filter_params': filter_params
}
assets, total_count = _get_assets_for_page(request, course_key, options)
end = start + len(assets)

# If the query is beyond the final page, then re-query the final page so
# that at least one asset is returned
if requested_page > 0 and start >= total_count:
current_page = int(math.floor((total_count - 1) / requested_page_size))
options.current_page = current_page= int(math.floor((total_count - 1) / requested_page_size))
start = current_page * requested_page_size
assets, total_count = _get_assets_for_page(
request, course_key, current_page, requested_page_size, sort, filter_params=filter_params)
assets, total_count = _get_assets_for_page(request, course_key, options)
end = start + len(assets)

asset_json = []
Expand Down Expand Up @@ -169,10 +173,14 @@ def _assets_json(request, course_key):
})


def _get_assets_for_page(request, course_key, current_page, page_size, sort, filter_params=None):
def _get_assets_for_page(request, course_key, options):
"""
Returns the list of assets for the specified page and page size.
"""
current_page = options['current_page']
page_size = options['page_size']
sort = options['sort']
filter_params = options['filter_params'] if options['filter_params'] else None
start = current_page * page_size

return contentstore().get_all_content_for_course(
Expand Down
2 changes: 1 addition & 1 deletion cms/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,4 +758,4 @@

# Files and Uploads type filter values
FILES_AND_UPLOAD_TYPE_FILTER = {
"Images": ['PNG', 'JPG', 'JPEG'], "Documents": ['PDF'], "Text": ['TXT']}
"Images": ['image/png', 'image/jpeg'], "Documents": ['application/pdf'], "Text": ['application/txt']}
4 changes: 2 additions & 2 deletions cms/static/js/collections/asset.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
define(["backbone.paginator", "js/models/asset"], function(BackbonePaginator, AssetModel) {
var AssetCollection = BackbonePaginator.requestPager.extend({
assetFilter: '',
assetType: '',
model : AssetModel,
paginator_core: {
type: 'GET',
Expand All @@ -18,7 +18,7 @@ define(["backbone.paginator", "js/models/asset"], function(BackbonePaginator, As
'page_size': function() { return this.perPage; },
'sort': function() { return this.sortField; },
'direction': function() { return this.sortDirection; },
'asset_type': function() { return this.assetFilter; },
'asset_type': function() { return this.assetType; },
'format': 'json'
},

Expand Down
2 changes: 1 addition & 1 deletion cms/static/js/spec/views/assets_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ define([ "jquery", "js/common_helpers/ajax_helpers", "js/views/asset", "js/views
var requests;
requests = AjaxHelpers.requests(this);
assetsView.setPage(0);
if (typeof responseData == "undefined"){
if (!responseData){
AjaxHelpers.respondWithJson(requests, mockEmptyAssetsResponse);
}
else{
Expand Down
14 changes: 7 additions & 7 deletions cms/static/js/views/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ define(["jquery", "underscore", "gettext", "js/models/asset", "js/views/paging",
renderPageItems: function() {
var self = this,
assets = this.collection,
hasAssets = this.collection.assetFilter != '' ? true: assets.length > 0,
hasAssets = this.collection.assetType != '' || assets.length > 0,
tableBody = this.getTableBody();
tableBody.empty();
if (hasAssets) {
Expand Down Expand Up @@ -196,16 +196,16 @@ define(["jquery", "underscore", "gettext", "js/models/asset", "js/views/paging",
this.toggleFilterColumnState($this, event);
},

toggleFilterColumnState: function(menu, event){
toggleFilterColumnState: function(menu, event) {
var $subnav = menu.find('.wrapper-nav-sub');
var $title = menu.find('.title');
var titleText = $title.find('.type-filter');
var assetfilter = $(event.currentTarget).data('assetfilter');
if(assetfilter == this.allLabel){
var assettype = $(event.currentTarget).data('assetfilter');
if(assettype == this.allLabel){
titleText.text(titleText.data('alllabel'));
}
else{
titleText.text(assetfilter);
titleText.text(assettype);
}

if ($subnav.hasClass('is-shown')) {
Expand All @@ -226,10 +226,10 @@ define(["jquery", "underscore", "gettext", "js/models/asset", "js/views/paging",
event.preventDefault();
var collection = this.collection;
if($(event.currentTarget).data('assetfilter') == this.allLabel){
collection.assetFilter = '';
collection.assetType = '';
}
else{
collection.assetFilter = $(event.currentTarget).data('assetfilter');
collection.assetType = $(event.currentTarget).data('assetfilter');
}

this.selectFilter('js-asset-type-col');
Expand Down
2 changes: 1 addition & 1 deletion cms/static/js/views/paging_header.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ define(["underscore", "gettext", "js/views/baseview"], function(_, gettext, Base

messageHtml: function() {
var message = gettext('Showing %(current_item_range)s out of %(total_items_count)s, ');
if (this.view.collection.assetFilter){
if (this.view.collection.assetType){
message = gettext(message + 'filtered by %(asset_type)s, ');
}
if (this.view.collection.sortDirection === 'asc') {
Expand Down

0 comments on commit 6132d42

Please sign in to comment.