Skip to content

Commit

Permalink
Merge #1574
Browse files Browse the repository at this point in the history
1574: Support ability to sort by action name r=mythmon a=peterbe

Fixes #1573 

I hope this doesn't screw up your pending work on v3 refactoring @mythmon 

The ultimate motive with this is to support [better filtering/sorting by action in delivery console](mozilla/delivery-console#151).

Co-authored-by: Peter Bengtsson <mail@peterbe.com>
  • Loading branch information
bors[bot] and peterbe committed Oct 5, 2018
2 parents 616f817 + 93b9cd6 commit 714ec1c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
5 changes: 4 additions & 1 deletion normandy/base/api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ class AliasedOrderingFilter(filters.OrderingFilter):

def get_valid_fields(self, *args, **kwargs):
valid_fields = super().get_valid_fields(*args, **kwargs)
return valid_fields + list(self.aliases.values())
for alias, mapping in self.aliases.items():
valid_fields.append((alias, mapping[1]))
return valid_fields

def get_ordering(self, *args, **kwargs):
ordering = super().get_ordering(*args, **kwargs)
if ordering is not None:
return list(map(self.replace_alias, ordering))

return ordering

def replace_alias(self, term):
Expand Down
1 change: 1 addition & 0 deletions normandy/recipes/api/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class RecipeOrderingFilter(AliasedOrderingFilter):
aliases = {
"last_updated": ("latest_revision__updated", "Last Updated"),
"name": ("latest_revision__name", "Name"),
"action": ("latest_revision__action__name", "Action"),
}


Expand Down
1 change: 1 addition & 0 deletions normandy/recipes/api/v3/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class RecipeOrderingFilter(AliasedOrderingFilter):
aliases = {
"last_updated": ("latest_revision__updated", "Last Updated"),
"name": ("latest_revision__name", "Name"),
"action": ("latest_revision__action__name", "Action"),
}


Expand Down
21 changes: 21 additions & 0 deletions normandy/recipes/tests/api/v2/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,27 @@ def test_order_name(self, api_client):
assert res.status_code == 200
assert [r["id"] for r in res.data["results"]] == [r2.id, r1.id]

def test_order_by_action_name(self, api_client):
r1 = RecipeFactory(name="a")
r1.action.name = "Bee"
r1.action.save()
r2 = RecipeFactory(name="b")
r2.action.name = "Cee"
r2.action.save()
r3 = RecipeFactory(name="c")
r3.action.name = "Ahh"
r3.action.save()

res = api_client.get("/api/v2/recipe/?ordering=action")
assert res.status_code == 200
# Expected order is ['Ahh', 'Bee', 'Cee']
assert [r["id"] for r in res.data["results"]] == [r3.id, r1.id, r2.id]

res = api_client.get("/api/v2/recipe/?ordering=-action")
assert res.status_code == 200
# Expected order is ['Cee', 'Bee', 'Ahh']
assert [r["id"] for r in res.data["results"]] == [r2.id, r1.id, r3.id]

def test_order_bogus(self, api_client):
"""Test that filtering by an unknown key doesn't change the sort order"""
RecipeFactory(name="a")
Expand Down
21 changes: 21 additions & 0 deletions normandy/recipes/tests/api/v3/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,27 @@ def test_order_name(self, api_client):
assert res.status_code == 200
assert [r["id"] for r in res.data["results"]] == [r2.id, r1.id]

def test_order_by_action_name(self, api_client):
r1 = RecipeFactory(name="a")
r1.action.name = "Bee"
r1.action.save()
r2 = RecipeFactory(name="b")
r2.action.name = "Cee"
r2.action.save()
r3 = RecipeFactory(name="c")
r3.action.name = "Ahh"
r3.action.save()

res = api_client.get("/api/v3/recipe/?ordering=action")
assert res.status_code == 200
# Expected order is ['Ahh', 'Bee', 'Cee']
assert [r["id"] for r in res.data["results"]] == [r3.id, r1.id, r2.id]

res = api_client.get("/api/v3/recipe/?ordering=-action")
assert res.status_code == 200
# Expected order is ['Cee', 'Bee', 'Ahh']
assert [r["id"] for r in res.data["results"]] == [r2.id, r1.id, r3.id]

def test_order_bogus(self, api_client):
"""Test that filtering by an unknown key doesn't change the sort order"""
RecipeFactory(name="a")
Expand Down

0 comments on commit 714ec1c

Please sign in to comment.