From 8acf06010fe51cd5ac8d1c0c9ccf7076a6a84906 Mon Sep 17 00:00:00 2001 From: astex <0astex@gmail.com> Date: Wed, 20 Apr 2016 12:47:03 -0400 Subject: [PATCH 1/4] Add a project_id filter to the contact index. --- app/controllers/contact.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/controllers/contact.py b/app/controllers/contact.py index 9e337d9..33c618d 100644 --- a/app/controllers/contact.py +++ b/app/controllers/contact.py @@ -9,6 +9,16 @@ class ContactController(RestController): """The contact controller.""" Model = Contact constant_fields = ['sales_force_id'] + filters = { + 'project_id': lambda d: ProjectContact.project_id == d['project_id']} + + def query(self, filter_data): + """Construct a query with joins based on filter_data.""" + q = super(ContactController, self).query(filter_data) + if 'project_id' in filter_data: + q = q\ + .join(ProjectContact, ProjectContact.contact_id == Contact.id) + return q def get_form(self, filter_data): """Return the contact form.""" -- GitLab From 854911f2eb221869cc6cda5765e5990aa925a76a Mon Sep 17 00:00:00 2001 From: astex <0astex@gmail.com> Date: Wed, 20 Apr 2016 12:47:15 -0400 Subject: [PATCH 2/4] Add tests for the project_id filter in the contact index. --- app/tests/test_contact.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/tests/test_contact.py b/app/tests/test_contact.py index 2be613b..8721fc9 100644 --- a/app/tests/test_contact.py +++ b/app/tests/test_contact.py @@ -2,6 +2,7 @@ from sqlalchemy import and_ from app.lib.database import db from app.tests.base import RestTestCase +from app.models.project import Project from app.models.contact import Contact, ContactMethod, ProjectContact @@ -15,6 +16,21 @@ class TestContact(RestTestCase): model = self.env.contact self._test_index() + def test_index_project_id(self): + """Tests /contact/?project_id=... GET.""" + # Construct a model that should be in the response. + model = self.env.contact + project = self.env.project + self.env.add(ProjectContact( + project_id=project.id, + contact_id=model.id)) + # Contruct a model that should not be in the response. + _ = self.env.contact + + 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 /contact/ GET.""" model = self.env.contact -- GitLab From 2a04d7c7ad1991c848759800b3ac0334e32789e5 Mon Sep 17 00:00:00 2001 From: astex <0astex@gmail.com> Date: Wed, 20 Apr 2016 12:55:17 -0400 Subject: [PATCH 3/4] Add a project_id filter to the contact method index. --- app/controllers/contact.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/controllers/contact.py b/app/controllers/contact.py index 33c618d..5045240 100644 --- a/app/controllers/contact.py +++ b/app/controllers/contact.py @@ -29,6 +29,17 @@ class ContactMethodController(RestController): """The contact method controller.""" Model = ContactMethod constant_fields = ['contact_id', 'method'] + filters = { + 'project_id': lambda d: ProjectContact.project_id == d['project_id']} + + def query(self, filter_data): + """Construct a query with joins based on filter data.""" + q = super(ContactMethodController, self).query(filter_data) + if 'project_id' in filter_data: + q = q\ + .join(Contact, Contact.id == ContactMethod.contact_id)\ + .join(ProjectContact, ProjectContact.contact_id == Contact.id) + return q def get_form(self, filter_data): """Return the contact method form.""" -- GitLab From ae1373dd1e7043915d1d1d964fb9836e60c5c9f9 Mon Sep 17 00:00:00 2001 From: astex <0astex@gmail.com> Date: Wed, 20 Apr 2016 12:55:31 -0400 Subject: [PATCH 4/4] Add a test for the project_id filter in the contact method index. --- app/tests/test_contact.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app/tests/test_contact.py b/app/tests/test_contact.py index 8721fc9..76af056 100644 --- a/app/tests/test_contact.py +++ b/app/tests/test_contact.py @@ -95,6 +95,21 @@ class TestContactMethod(RestTestCase): model = self.env.email_contact_method self._test_index() + def test_index_project_id(self): + """Tests /contact/method/?project_id=... GET.""" + # Construct a model that should be in the response. + model = self.env.email_contact_method + project = self.env.project + self.env.add(ProjectContact( + project_id=project.id, + contact_id=model.contact_id)) + # Construct a model that should not be in the response. + _ = self.env.email_contact_method + + 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 /contact/method/ GET.""" model = self.env.email_contact_method -- GitLab