API: Add support for searching plays by name

This allows users to query the API like so:

    /api/v1/plays?name=deploy

The search above would return plays with "deploy" in the name of the
play.

Change-Id: I88cabd33c0a7d42f36eccc7a556c71749f3ff616
This commit is contained in:
David Moreau Simard 2020-04-13 14:52:36 -04:00
parent 36aacd09cf
commit 821b0fe880
No known key found for this signature in database
GPG Key ID: 938880DAFC753E80
2 changed files with 12 additions and 0 deletions

View File

@ -83,6 +83,7 @@ class PlayFilter(DateFilter):
status = django_filters.MultipleChoiceFilter(
field_name="status", choices=ara_models.Play.STATUS, lookup_expr="iexact"
)
name = django_filters.CharFilter(field_name="name", lookup_expr="icontains")
# fmt: off
order = django_filters.OrderingFilter(

View File

@ -98,6 +98,17 @@ class PlayTestCase(APITestCase):
self.assertEqual(1, len(request.data["results"]))
self.assertEqual(play.name, request.data["results"][0]["name"])
def test_get_plays_by_name(self):
# Create a playbook and two plays
playbook = factories.PlaybookFactory()
play = factories.PlayFactory(name="first_play", playbook=playbook)
factories.TaskFactory(name="second_play", playbook=playbook)
# Query for the first play name and expect one result
request = self.client.get("/api/v1/plays?name=%s" % play.name)
self.assertEqual(1, len(request.data["results"]))
self.assertEqual(play.name, request.data["results"][0]["name"])
def test_get_play_by_uuid(self):
play = factories.PlayFactory(name="play1", uuid="6b838b6f-cfc7-4e11-a264-73df8683ee0e")
factories.PlayFactory(name="play2")