def get(self, args): """Returns a list of data recovered from model. Before applies the received query params arguments.""" fb = FilterBuilder(Role, args) fb.set_equals_filters(['can_access_admin']) fb.set_like_filters(['name', 'description']) if (args['capability_description'] and args['capability_description'] != ''): fb.set_like_filter('capability_description', joined=Capability, joined_key='description') self.joins.append(Role.capabilities) query = self.session.query(Role).join(*self.joins).filter(*fb.get_filter()).order_by(*fb.get_order_by()) result = Paginate(query, fb.get_page(), fb.get_limit()) schema = RoleSchema(many=True, exclude=self.get_exclude_fields(args, ['capabilities'])) return self.handle_success(result, schema, 'get', 'Role')
def get(self, args): """Returns a list of data recovered from model. Before applies the received query params arguments.""" fb = FilterBuilder(Post, args) fb.set_equals_filters(['status', 'user_id', 'parent_id', 'post_type_id', 'language_id']) self.joins.append(FieldContent) self.joins.append(FieldText) if (args['term_id'] and args['term_id'] != '' and Checker().can_be_integer(args['term_id'])): fb.set_like_filter('term_id', joined=Term, joined_key='id') self.joins.append(Post.terms) try: fb.set_date_filter('created', date_modifier=args['date_modifier']) fb.set_between_dates_filter( 'created', not_between=args['not_between'], compare_date_time_one=args['compare_date_time_one'], compare_date_time_two=args['compare_date_time_two'] ) fb.set_and_or_filter('s', 'or', [ {'field': 'name', 'type': 'like'}, {'field': 'title', 'type': 'like'}, {'field': 'description', 'type': 'like'}, {'field': 'content', 'type': 'like', 'kwargs': {'joined': FieldContent}}, {'field': 'content', 'type': 'like', 'kwargs': {'joined': FieldText}} ]) if not self.the_logged_user: fb.set_range_of_dates_filter() fb.filter += (Post.status == 'publish',) self.set_can_see_protected() if not self.can_see_protected: fb.filter += (Post.is_protected != True,) except Exception as e: raise BadRequestError(str(e)) query = self.session.query(Post).join(*self.joins, isouter=True).filter(*fb.get_filter()).order_by(*fb.get_order_by()) result = Paginate(query, fb.get_page(), fb.get_limit()) schema = PostSchema(many=True, exclude=self.get_exclude_fields(args, [ 'user', 'language', 'parent', 'children', 'post_type', 'nests', 'groupers', 'terms'])) return self.handle_success(result, schema, 'get', 'Post')
class FilterBuilderTests(unittest.TestCase): def setUp(self): self.args = { 'name': 'a', 'title': 'b', 'status': 'c', 'created': '2020-01-01T00:00:00', 'page': 1, 'limit': 10, 'order_by': 'id', 'order': 'desc' } self.kwa = {} self.fb = FilterBuilder(Post, self.args) def test_FilterBuilder_get_context_attr__default(self): payload = Post.id response = self.fb.get_context_attr('id', self.kwa) self.assertEqual( payload, response, 'ErrorHandler().get_context_attr does not return \'Post.id\'.') def test_FilterBuilder_get_context_attr__joined(self): kwa = {'joined': Language, 'joined_key': 'id'} fb = FilterBuilder(Post, self.args) payload = Language.id response = fb.get_context_attr('id', kwa) self.assertEqual( payload, response, 'ErrorHandler().get_context_attr does not return \'Language.id\'.') def test_FilterBuilder_set_equals_filter(self): payload = '"Post".name = :name_1' self.fb.set_equals_filter('name') response = str(self.fb.filter[0]) self.assertEqual( payload, response, 'ErrorHandler().set_equals_filter does not return \'"Post".name = :name_1\'.' ) def test_FilterBuilder_set_equals_filters(self): payload_1 = '"Post".name = :name_1' payload_2 = '"Post".status = :status_1' self.fb.set_equals_filters(['name', 'status']) response_1 = str(self.fb.filter[0]) response_2 = str(self.fb.filter[1]) self.assertEqual( payload_1, response_1, 'ErrorHandler().set_equals_filters does not return \'"Post".name = :name_1\'.' ) self.assertEqual( payload_2, response_2, 'ErrorHandler().set_equals_filters does not return \'"Post".status = :status_1\'.' ) def test_FilterBuilder_set_like_filter(self): payload = '"Post".name LIKE :name_1' self.fb.set_like_filter('name') response = str(self.fb.filter[0]) self.assertEqual( payload, response, 'ErrorHandler().set_like_filter does not return \'"Post".name LIKE :name_1\'.' ) def test_FilterBuilder_set_like_filters(self): payload_1 = '"Post".name LIKE :name_1' payload_2 = '"Post".status LIKE :status_1' self.fb.set_like_filters(['name', 'status']) response_1 = str(self.fb.filter[0]) response_2 = str(self.fb.filter[1]) self.assertEqual( payload_1, response_1, 'ErrorHandler().set_like_filter does not return \'"Post".name LIKE :name_1\'.' ) self.assertEqual( payload_2, response_2, 'ErrorHandler().set_like_filter does not return \'"Post".status LIKE :status_1\'.' ) def test_FilterBuilder_set_and_or_filter(self): payload = '"Post".name LIKE :name_1 OR "Post".status LIKE :status_1' self.fb.set_and_or_filter('name', 'or', [{ 'field': 'name', 'type': 'like' }, { 'field': 'status', 'type': 'like' }]) response = str(self.fb.filter[0]) self.assertEqual( payload, response, 'ErrorHandler().set_and_or_filter does not return \'"Post".name LIKE :name_1 OR "Post".type LIKE :type_1\'.' ) def test_FilterBuilder_set_and_or_filter_exception(self): payload = Exception response = None try: self.fb.set_and_or_filter('name', 'or', [{ 'field': 'xxx', 'type': 'like' }, { 'field': 'xxx', 'type': 'like' }]) except Exception as e: response = type(e) self.assertEqual( payload, response, 'ErrorHandler().set_and_or_filter does not return an Exception.') def test_FilterBuilder_set_date_filter(self): payload = '"Post".created >= :created_1' self.fb.set_date_filter('created') response = str(self.fb.filter[0]) self.assertEqual( payload, response, 'ErrorHandler().set_date_filter does not return \'"Post".created >= :created_1\'.' ) def test_FilterBuilder_set_date_filter__exception(self): payload = Exception response = None try: self.fb.set_date_filter('name') except Exception as e: response = type(e) self.assertEqual( payload, response, 'ErrorHandler().set_date_filter does not return an Exception.') def test_FilterBuilder_set_between_dates_filter(self): payload = '"Post".created BETWEEN :created_1 AND :created_2' self.fb.set_between_dates_filter( 'created', compare_date_time_one='2020-01-01T00:00:00', compare_date_time_two='2020-01-01T00:00:00') response = str(self.fb.filter[0]) self.assertEqual( payload, response, 'ErrorHandler().set_between_dates_filter does not return \'"Post".created BETWEEN :created_1 AND :created_2\'.' ) def test_FilterBuilder_set_between_dates_filter__not_between(self): payload = '"Post".created NOT BETWEEN :created_1 AND :created_2' self.fb.set_between_dates_filter( 'created', compare_date_time_one='2020-01-01T00:00:00', compare_date_time_two='2020-01-01T00:00:00', not_between='1') response = str(self.fb.filter[0]) self.assertEqual( payload, response, 'ErrorHandler().set_between_dates_filter does not return \'"Post".created BETWEEN :created_1 AND :created_2\'.' ) def test_FilterBuilder_set_between_dates_filter__exception(self): payload = Exception response = None try: self.fb.set_between_dates_filter( 'created', compare_date_time_one='2020-01-01T00:00:00', compare_date_time_two='xxxx') except Exception as e: response = type(e) self.assertEqual( payload, response, 'ErrorHandler().set_between_dates_filter does not return an Exception.' ) def test_FilterBuilder_get_filter(self): payload = () response = self.fb.get_filter() self.assertEqual(payload, response, 'ErrorHandler().get_filter does not return a tuple.') def test_FilterBuilder_get_page(self): payload = 1 response = self.fb.get_page() self.assertEqual(payload, response, 'ErrorHandler().get_page does not return 1.') def test_FilterBuilder_get_limit(self): payload = 10 response = self.fb.get_limit() self.assertEqual(payload, response, 'ErrorHandler().get_limit does not return 10.') def test_FilterBuilder_get_order_by(self): payload = '"Post".id DESC' response = str(self.fb.get_order_by()[0]) self.assertEqual( payload, response, 'ErrorHandler().get_order_by does not return \'"Post".id DESC\'.') def test_FilterBuilder_set_range_of_dates_filter(self): payload = '"Post".publish_on <= :publish_on_1 AND "Post".expire_on >= :expire_on_1' self.fb.set_range_of_dates_filter() response = str(self.fb.filter[0]) self.assertEqual( payload, response, 'ErrorHandler().set_range_of_dates_filter does not return \'"Post".publish_on <= :publish_on_1 AND "Post".expire_on >= :expire_on_1\'.' ) def tearDown(self): pass