def get(self, args): """Returns a list of data recovered from model. Before applies the received query params arguments.""" fb = FilterBuilder(Configuration, args) fb.set_equals_filters(['language_id']) try: fb.set_and_or_filter('s', 'or', [{ 'field': 'title', 'type': 'like' }, { 'field': 'description', 'type': 'like' }]) except Exception as e: raise BadRequestError(str(e)) query = self.session.query(Configuration).filter( *fb.get_filter()).order_by(*fb.get_order_by()) result = Paginate(query, fb.get_page(), fb.get_limit()) schema = ConfigurationSchema(many=True, exclude=self.get_exclude_fields( args, ['language', 'socials'])) return self.handle_success(result, schema, 'get', 'Configuration')
def get(self, args): """Returns a list of data recovered from model. Before applies the received query params arguments.""" fb = FilterBuilder(Term, args) fb.set_like_filters(['parent_id', 'taxonomy_id', 'language_id']) try: fb.set_and_or_filter('s', 'or', [{ 'field': 'name', 'type': 'like' }, { 'field': 'display_name', 'type': 'like' }, { 'field': 'description', 'type': 'like' }]) except Exception as e: raise BadRequestError(str(e)) query = self.session.query(Term).filter(*fb.get_filter()).order_by( *fb.get_order_by()) result = Paginate(query, fb.get_page(), fb.get_limit()) schema = TermSchema( many=True, exclude=self.get_exclude_fields( args, ['language', 'parent', 'children', 'taxonomy'])) return self.handle_success(result, schema, 'get', 'Term')
def get(self, args): """Returns a list of data recovered from model. Before applies the received query params arguments.""" fb = FilterBuilder(Field, args) fb.set_equals_filters(['type', 'grouper_id', 'post_id']) try: fb.set_and_or_filter('s', 'or', [{ 'field': 'name', 'type': 'like' }, { 'field': 'description', 'type': 'like' }]) except Exception as e: raise BadRequestError(str(e)) self.set_protection_to_child_post(fb) query = self.session.query(Field).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 = FieldSchema(many=True, exclude=self.get_exclude_fields( args, ['post', 'grouper'])) return self.handle_success(result, schema, 'get', 'Field')
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 get_by_id(self, id, args): """Returns a single row found by id recovered from model. Before applies the received query params arguments.""" fb = FilterBuilder(Post, {}) self.set_protection_to_child_post(fb) fb.filter += (FieldContent.id == id,) result = self.session.query(FieldContent).join(*self.joins, isouter=True).filter(*fb.get_filter()).first() schema = FieldContentSchema(many=False) return self.handle_success(result, schema, 'get_by_id', 'FieldContent')
def get(self, args): """Returns a list of data recovered from model. Before applies the received query params arguments.""" fb = FilterBuilder(Blacklist, args) fb.set_equals_filters(['type', 'target']) fb.set_like_filters(['value']) query = self.session.query(Blacklist).filter(*fb.get_filter()).order_by(*fb.get_order_by()) result = Paginate(query, fb.get_page(), fb.get_limit()) schema = BlacklistSchema(many=True) return self.handle_success(result, schema, 'get', 'Blacklist')
def get(self, args): """Returns a list of data recovered from model. Before applies the received query params arguments.""" fb = FilterBuilder(MenuItem, args) fb.set_equals_filters(['type', 'behavior', 'url', 'menu_id', 'parent_id']) fb.set_like_filters(['title']) query = self.session.query(MenuItem).filter(*fb.get_filter()).order_by(*fb.get_order_by()) result = Paginate(query, fb.get_page(), fb.get_limit()) schema = MenuItemSchema(many=True, exclude=self.get_exclude_fields(args, ['parent', 'children', 'menu'])) return self.handle_success(result, schema, 'get', 'MenuItem')
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 get(self, args): """Returns a list of data recovered from model. Before applies the received query params arguments.""" fb = FilterBuilder(FieldText, args) fb.set_like_filters(['content']) fb.set_equals_filters(['field_id', 'grouper_id', 'post_id']) self.set_protection_to_child_post(fb) query = self.session.query(FieldText).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 = FieldTextSchema(many=True) return self.handle_success(result, schema, 'get', 'FieldText')
def get(self, args): """Returns a list of data recovered from model. Before applies the received query params arguments.""" fb = FilterBuilder(Social, args) fb.set_like_filters(['name']) fb.set_equals_filters(['origin', 'user_id']) query = self.session.query(Social).filter(*fb.get_filter()).order_by( *fb.get_order_by()) result = Paginate(query, fb.get_page(), fb.get_limit()) schema = SocialSchema(many=True, exclude=self.get_exclude_fields( args, ['user', 'configuration'])) return self.handle_success(result, schema, 'get', 'Social')
def get(self, args): """Returns a list of data recovered from model. Before applies the received query params arguments.""" fb = FilterBuilder(PostType, args) fb.set_like_filters(['name']) fb.set_equals_filters(['type']) query = self.session.query(PostType).filter(*fb.get_filter()).order_by( *fb.get_order_by()) result = Paginate(query, fb.get_page(), fb.get_limit()) schema = PostTypeSchema(many=True, exclude=self.get_exclude_fields( args, ['template', 'nests', 'taxonomies'])) return self.handle_success(result, schema, 'get', 'PostType')
def get(self, args): """Returns a list of data recovered from model. Before applies the received query params arguments.""" fb = FilterBuilder(Capability, args) fb.set_equals_filters(['type', 'target_id', 'can_write', 'can_read', 'can_delete', 'only_themselves']) fb.set_like_filters(['description']) query = self.session.query(Capability).join(*self.joins).filter(*fb.get_filter()).order_by(*fb.get_order_by()) result = Paginate(query, fb.get_page(), fb.get_limit()) schema = CapabilitySchema(many=True, exclude=self.get_exclude_fields(args, ['roles'])) return self.handle_success(result, schema, 'get', 'Capability')
def get(self, args): """Returns a list of data recovered from model. Before applies the received query params arguments.""" fb = FilterBuilder(Variable, args) try: fb.set_and_or_filter('s', 'or', [{'field':'key', 'type':'like'}, {'field':'value', 'type':'like'}]) except Exception as e: raise BadRequestError(str(e)) query = self.session.query(Variable).filter(*fb.get_filter()).order_by(*fb.get_order_by()) result = Paginate(query, fb.get_page(), fb.get_limit()) schema = VariableSchema(many=True) return self.handle_success(result, schema, 'get', 'Variable')
def get_result_by_unique_key(self, id, context, session): """Return a row by the given key.""" fb = FilterBuilder(context, {}) if context.__tablename__ == 'Post': if not self.the_logged_user: fb.set_range_of_dates_filter() fb.filter += (getattr(context, 'status') == 'publish', ) self.set_can_see_protected() if not self.can_see_protected: fb.filter += (getattr(context, 'is_protected') != True, ) if Checker().can_be_integer(id): fb.filter += (getattr(context, 'id') == id, ) return session.query(context).filter(*fb.get_filter()).first() else: fb.filter += (getattr(context, 'name') == id, ) return session.query(context).filter(*fb.get_filter()).first()
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
def get(self, args): """Returns a list of data recovered from model. Before applies the received query params arguments.""" fb = FilterBuilder(Comment, args) fb.set_like_filters(['comment', 'origin_ip', 'origin_agent']) fb.set_equals_filters( ['status', 'parent_id', 'user_id', 'post_id', 'language_id']) 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']) except Exception as e: raise BadRequestError(str(e)) query = self.session.query(Comment).filter(*fb.get_filter()).order_by( *fb.get_order_by()) result = Paginate(query, fb.get_page(), fb.get_limit()) schema = CommentSchema( many=True, exclude=self.get_exclude_fields( args, ['user', 'language', 'parent', 'children', 'post'])) return self.handle_success(result, schema, 'get', 'Comment')
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')