From b9b30379613043cba8bd2a4f1d5037fc28290117 Mon Sep 17 00:00:00 2001 From: astex <0astex@gmail.com> Date: Fri, 29 Apr 2016 13:52:36 -0400 Subject: [PATCH 1/2] Add a project_id filter to the client controller. --- app/controllers/client.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/controllers/client.py b/app/controllers/client.py index e9ab583..e7a081b 100644 --- a/app/controllers/client.py +++ b/app/controllers/client.py @@ -1,6 +1,8 @@ """Controllers for managing clients.""" from werkzeug.exceptions import BadRequest + from app.controllers.base import RestController +from app.models.project import Project from app.models.client import Client from app.forms.client import ClientForm @@ -9,6 +11,15 @@ class ClientController(RestController): """The client controller.""" Model = Client constant_fields = ['sales_force_id'] + filters = { + 'project_id': lambda d: Project.id == d['project_id']} + + def query(self, filter_data): + """Construct a query with additional joins based on filter_data.""" + q = super(ClientController, self).query(filter_data) + if 'project_id' in filter_data: + q = q.join(Project, Project.client_id == Client.id) + return q def get_form(self, filter_data): """Return the client form.""" -- GitLab From fd5183b1deafe50d230aab65eee4bed53c348677 Mon Sep 17 00:00:00 2001 From: astex <0astex@gmail.com> Date: Fri, 29 Apr 2016 13:52:51 -0400 Subject: [PATCH 2/2] Add a unit test for the /client/?project_id=... GET endpoint. --- app/tests/test_client.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/tests/test_client.py b/app/tests/test_client.py index 0dca409..8cd2451 100644 --- a/app/tests/test_client.py +++ b/app/tests/test_client.py @@ -15,6 +15,20 @@ class TestClient(RestTestCase): model = self.env.client self._test_index() + def test_index_project_id(self): + """Tests /client/?project_id=... GET.""" + # Construct a model that should be returned. + project = self.env.project + model = db.session.query(Client)\ + .filter(Client.id == project.client_id)\ + .first() + # Construct a model that should not be returned. + _ = self.env.client + + response_data = self._test_index({'project_id': project.id}) + self.assertEqual(len(response_data), 1) + self.assertEqual(response_data[0]['id'], model.id) + def test_get(self): """Tests /client/ GET.""" model = self.env.client -- GitLab