Skip to content
This repository has been archived by the owner on Jan 28, 2020. It is now read-only.

Commit

Permalink
Merge d4226ec into 81b1cb3
Browse files Browse the repository at this point in the history
  • Loading branch information
amir-qayyum-khan committed Mar 18, 2016
2 parents 81b1cb3 + d4226ec commit 9ad9761
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 18 deletions.
8 changes: 4 additions & 4 deletions rest/tests/test_vocabulary.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ def test_vocabulary_pagination(self):
weight=1000,
)).data for i in range(40)]

expected.sort(key=lambda x: x["id"])
expected.sort(key=lambda x: x["name"])

resp = self.client.get(
'{repo_base}{repo_slug}/vocabularies/'.format(
Expand All @@ -494,8 +494,8 @@ def test_vocabulary_pagination(self):
self.assertEqual(40, vocabularies['count'])

# Sort both lists in preparation for comparisons.
expected.sort(key=lambda x: x["id"])
from_api = sorted(vocabularies['results'], key=lambda x: x["id"])
expected.sort(key=lambda x: x["name"])
from_api = sorted(vocabularies['results'], key=lambda x: x["name"])

expected_count = 20
self.assertEqual(expected_count, len(from_api))
Expand All @@ -511,7 +511,7 @@ def test_vocabulary_pagination(self):
))
self.assertEqual(HTTP_200_OK, resp.status_code)
vocabularies = as_json(resp)
from_api = sorted(vocabularies['results'], key=lambda x: x["id"])
from_api = sorted(vocabularies['results'], key=lambda x: x["name"])
self.assertEqual(expected_count, len(from_api))
self.assertEqual(40, vocabularies['count'])
self.assertEqual(
Expand Down
3 changes: 2 additions & 1 deletion rest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied as DjangoPermissionDenied
from django.db import transaction
from django.db.models.functions import Lower
from rest_framework import status
from rest_framework.exceptions import ValidationError
from rest_framework.generics import (
Expand Down Expand Up @@ -194,7 +195,7 @@ def get_queryset(self):
if learning_resource_type is not None:
queryset = queryset.filter(
learning_resource_types__name=learning_resource_type)
return queryset.order_by('id')
return queryset.order_by(Lower('name'))

def get_success_headers(self, data):
"""Add Location header for model create."""
Expand Down
33 changes: 29 additions & 4 deletions ui/jstests/taxonomy/test_taxonomy_component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,12 @@ define(['QUnit', 'jquery', 'lodash', 'taxonomy_component', 'react',
assert.ok(TaxonomyComponent, "class object not found");
var vocabularyWithoutTerms = {
"id": 3,
"slug": "difficulty2",
"name": "difficulty2",
"description": "easy",
"slug": "apple",
"name": "Apple",
"description": "fruit",
"vocabulary_type": "f",
"required": false,
"weight": 2147483647,
"weight": 2147483640,
};
var refreshCount = 0;
var refreshFromAPI = function () {
Expand Down Expand Up @@ -330,11 +330,36 @@ define(['QUnit', 'jquery', 'lodash', 'taxonomy_component', 'react',
component.state.vocabularies.length,
2
);

// Assert that the vocabularies are sorted by case insensitive names
assert.equal(
component.state.vocabularies[0].vocabulary.name,
"difficulty"
);
assert.equal
(component.state.vocabularies[1].vocabulary.name,
"TestA"
);
assert.equal(refreshCount, 1);

component.addOrUpdateVocabulary(vocabularyWithoutTerms);
component.forceUpdate(function () {
assert.equal(component.state.vocabularies.length, 3);

// Assert that the vocabularies are sorted by case insensitive names
assert.equal(
component.state.vocabularies[0].vocabulary.name,
"Apple"
);
assert.equal(
component.state.vocabularies[1].vocabulary.name,
"difficulty"
);
assert.equal(
component.state.vocabularies[2].vocabulary.name,
"TestA"
);

// If the vocabulary already exists the count should not change.
component.addOrUpdateVocabulary(vocabularyWithoutTerms);
component.forceUpdate(function () {
Expand Down
9 changes: 0 additions & 9 deletions ui/static/ui/js/taxonomy/add_vocabulary.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,6 @@ define('add_vocabulary', ['react', 'lodash', 'jquery', 'uri',
// Reset state (and eventually update the vocab tab
thiz.props.updateParent(data);
thiz.resetForm();
if (!thiz.isEditModeOpen()) {
var scrollableDiv = $(
".cd-panel-2 .cd-panel-container .cd-panel-content"
);
scrollableDiv.animate(
{scrollTop: scrollableDiv.prop('scrollHeight')},
500
);
}
return thiz.props.refreshFromAPI();
}).fail(function (data) {
var jsonData = data && data.responseJSON;
Expand Down
10 changes: 10 additions & 0 deletions ui/static/ui/js/taxonomy/taxonomy_component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ define('taxonomy_component', ['react', 'lodash', 'jquery',
};
vocabularies = vocabularies.concat([newVocab]);
}
if (!_.isEmpty(vocabularies)) {
// sort vocabularies
vocabularies.sort(function (vocabLeft, vocabRight) {
if (vocabRight) {
return vocabLeft.vocabulary.name.toLowerCase().
localeCompare(vocabRight.vocabulary.name.toLowerCase());
}
return 0;
});
}
this.setState({vocabularies: vocabularies});
},
addTerm: function (vocabId, newTerm) {
Expand Down

0 comments on commit 9ad9761

Please sign in to comment.