Merge pull request #519 from mozilla/505-fix-explore-filters

Implement filtering.
This commit is contained in:
Chris McAvoy 2013-06-06 15:15:14 -07:00
Родитель 15b8f33ab8 12c9c1673c
Коммит e849180bd6
6 изменённых файлов: 17 добавлений и 31 удалений

8
api.js
Просмотреть файл

@ -13,7 +13,7 @@ var DEFAULT_QUERY = {
function middleware (method, default_query) { function middleware (method, default_query) {
if (!_.isFunction(method)) if (!_.isFunction(method))
method = this[method]; method = this[method];
if (!_.isFunction(method)) { if (!_.isFunction(method)) {
throw new Error('Supplied method ' + method + ' not valid'); throw new Error('Supplied method ' + method + ' not valid');
@ -174,7 +174,7 @@ function paginate(key, dataFn) {
if (err) if (err)
return callback(err, data); return callback(err, data);
if (typeof data[key].length !== 'number') if (typeof data[key].length !== 'number')
return callback(new errors.BadGateway('Unpageable data returned from upstream'), data); return callback(new errors.BadGateway('Unpageable data returned from upstream'), data);
var pages = Math.ceil(data[key].length / pageSize); var pages = Math.ceil(data[key].length / pageSize);
@ -239,7 +239,7 @@ module.exports = function Api(origin, globalFilters, config) {
}, },
/* TODO: writable is set to true for mocking, but it would /* TODO: writable is set to true for mocking, but it would
be nice to revisit and try to remove that line. */ be nice to revisit and try to remove that line. */
writable: true writable: true
}); });
}, this); }, this);
@ -273,5 +273,3 @@ module.exports = function Api(origin, globalFilters, config) {
}); });
}; };

Просмотреть файл

@ -126,4 +126,4 @@ module.exports = function(app) {
next(); next();
}); });
} }

Просмотреть файл

@ -44,7 +44,6 @@ module.exports = function (app) {
app.get('/explore', badger.middleware('getPrograms'), function (req, res, next) { app.get('/explore', badger.middleware('getPrograms'), function (req, res, next) {
var data = req.remote; var data = req.remote;
res.render('programs/list.html', { res.render('programs/list.html', {
filters: getFilters(req.query, ['categories', 'orgs', 'ageRanges', 'activityTypes']), filters: getFilters(req.query, ['categories', 'orgs', 'ageRanges', 'activityTypes']),
items: data.programs, items: data.programs,
@ -385,4 +384,4 @@ module.exports = function (app) {
app.get('/orgs/:orgName/unfavorite', function (req, res, next) { app.get('/orgs/:orgName/unfavorite', function (req, res, next) {
return res.redirect('/login', 303); return res.redirect('/login', 303);
}); });
}; };

Просмотреть файл

@ -169,23 +169,6 @@ function filterBadges (data, query) {
return data; return data;
} }
function filterPrograms (data, query) {
var category = confirmFilterValue(query.category, categories),
org = confirmFilterValue(query.org, orgs),
ageGroup = confirmFilterValue(query.age, ageRanges),
activityType = confirmFilterValue(query.activity, activityTypes);
if (!category && !org && !ageGroup && !activityType)
return data;
return applyFilter(data, {
'categories': category,
'issuer.shortname': org,
'ageRange': ageGroup,
'activityType': activityType
});
}
function getJWTToken(email) { function getJWTToken(email) {
var claims = { var claims = {
prn: email, prn: email,
@ -234,7 +217,13 @@ var openbadger = new Api(ENDPOINT, {
getPrograms: { getPrograms: {
func: function getPrograms (query, callback) { func: function getPrograms (query, callback) {
this.get('/programs', function(err, data) { var qs = {
category: query.category,
org: query.org,
age: query.age,
activity: query.activity,
};
this.get('/programs', {qs: qs}, function(err, data) {
if (err) if (err)
return callback(err, data); return callback(err, data);
@ -243,7 +232,6 @@ var openbadger = new Api(ENDPOINT, {
}); });
}); });
}, },
filters: filterPrograms,
paginate: true, paginate: true,
key: 'programs' key: 'programs'
}, },

Просмотреть файл

@ -211,10 +211,11 @@ test('getProgram', function(t) {
}); });
test('getPrograms', function(t) { test('getPrograms', function(t) {
const CALLBACK_INDEX = 2;
t.test('on error', function(t) { t.test('on error', function(t) {
var getStub = mock.expects('get'); var getStub = mock.expects('get');
getStub.callsArgWith(1, 500, 'error of some sort'); getStub.callsArgWith(CALLBACK_INDEX, 500, 'error of some sort');
openbadger.getPrograms(DEFAULT_QUERY, function(err, data) { openbadger.getPrograms(DEFAULT_QUERY, function(err, data) {
t.same(err, 500, 'error'); t.same(err, 500, 'error');
t.similar(data, 'error of some sort', 'data'); t.similar(data, 'error of some sort', 'data');
@ -224,7 +225,7 @@ test('getPrograms', function(t) {
t.test('with data', function(t) { t.test('with data', function(t) {
var getStub = mock.expects('get'); var getStub = mock.expects('get');
getStub.callsArgWith(1, null, DATA['programs']); getStub.callsArgWith(CALLBACK_INDEX, null, DATA['programs']);
openbadger.getPrograms(DEFAULT_QUERY, function(err, data) { openbadger.getPrograms(DEFAULT_QUERY, function(err, data) {
t.notOk(err, 'no error'); t.notOk(err, 'no error');
t.same(data.programs.length, 3, 'data length'); t.same(data.programs.length, 3, 'data length');
@ -237,7 +238,7 @@ test('getPrograms', function(t) {
t.test('paginates', function(t) { t.test('paginates', function(t) {
var getStub = mock.expects('get'); var getStub = mock.expects('get');
getStub.callsArgWith(1, null, DATA['programs']); getStub.callsArgWith(CALLBACK_INDEX, null, DATA['programs']);
openbadger.getPrograms({ pageSize: 2, page: 1 }, function(err, data) { openbadger.getPrograms({ pageSize: 2, page: 1 }, function(err, data) {
t.notOk(err, 'no error'); t.notOk(err, 'no error');
t.same(data.programs.length, 2, 'paginated'); t.same(data.programs.length, 2, 'paginated');

Просмотреть файл

@ -4,4 +4,4 @@
{% block item_actions %} {% block item_actions %}
{{ super() }} {{ super() }}
{% endblock %} {% endblock %}