def test_Checker_is_first_date_smaller__false(self): payload = False response = Checker().is_first_date_smaller('2020-01-01 00:00:00', '2019-01-01 00:00:00') self.assertEqual( payload, response, 'Checker().is_first_date_smaller does not return False.')
def is_datetime(self, key, config): if ('is_datetime' in config and key in self.request and self.request[key] != ''): if not Checker().is_datetime(self.request[key]): self.handle_validation_error('Datetime invalid at field: ' + key + '.')
def get_limit(self): """Returns the configured limit.""" if ('limit' in self.args and self.args['limit'] and Checker().can_be_integer(self.args['limit'])): self.limit = int(self.args['limit']) return self.limit
def get_page(self): """Returns the current page.""" if ('page' in self.args and self.args['page'] and Checker().can_be_integer(self.args['page'])): self.page = int(self.args['page']) return self.page
def raise_if_has_term_and_not_is_post_page(self, data, session): """Raise an error if the Post has terms but it does not is from a Post_Type with type 'post-page'""" if 'terms' in data and data['terms']: if 'post_type_id' in data and Checker().can_be_integer(data['post_type_id']): post_type = session.query(PostType.type).filter_by(id=int(data['post_type_id'])).first() if post_type and post_type[0]: if post_type[0] != 'post-page': raise BadRequestError('The Post_Type of the Post must be settled as type \'post-page\' to have Terms.')
def compare_dates(self, key, config): if 'compare_dates' in config and config['compare_dates'] != '': if key in self.request and config['compare_dates'] in self.request: publish_date = self.request[config['compare_dates']] expire_date = self.request[key] if publish_date != '' and expire_date != '': if not Checker().is_first_date_smaller( publish_date, expire_date): self.handle_validation_error( 'The \'' + config['compare_dates'] + '\' date must be smaller than \'' + key + '\' date at least at a hour.')
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()
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')
def add_many_to_many_relationship(self, key, instance, data, context_target, session): """From the given key get from the given data the list of context_target id to add into the given instance""" if (key in data and isinstance(data[key], list)): errors = [] for related in data[key]: if (related and Checker().can_be_integer(related)): registered_related = session.query( context_target).filter_by(id=int(related)).first() if (registered_related): getattr(instance, key).append(registered_related) else: errors.append(context_target.__tablename__ + ' ' + str(related) + ' does not exists.') if errors: raise NotFoundError(errors)
def edit_many_to_many_relationship(self, key, instance, data, context_target, session): """From the given key get from the given data the list of context_target id to add/remove into the given instance""" old_relateds = [] new_old_relateds = [] errors = [] if (getattr(instance, key)): for related in getattr(instance, key): old_relateds.append(related.id) if (key in data and isinstance(data[key], list)): for related in data[key]: if (related and Checker().can_be_integer(related)): new_old_relateds.append(related) related_to_delete = list(set(old_relateds) - set(new_old_relateds)) related_to_add = list(set(new_old_relateds) - set(old_relateds)) for related in related_to_delete: registered_related = session.query(context_target).filter_by( id=int(related)).first() if (registered_related): getattr(instance, key).remove(registered_related) for related in related_to_add: registered_related = session.query(context_target).filter_by( id=int(related)).first() if (registered_related): getattr(instance, key).append(registered_related) else: errors.append(context_target.__tablename__ + ' ' + str(related) + ' does not exists.') if errors: raise NotFoundError(errors)
def test_Checker_is_datetime__false(self): payload = False response = Checker().is_datetime('2020/01/01 00:00:00') self.assertEqual(payload, response, 'Checker().is_datetime does not return False.')
def test_Checker_is_datetime__true(self): payload = True response = Checker().is_datetime('2020-01-01 00:00:00') self.assertEqual(payload, response, 'Checker().is_datetime does not return True.')
def test_Checker_is_image_type__false(self): payload = False response = Checker().is_image_type('Not Mimetype') self.assertEqual(payload, response, 'Checker().is_image_type does not return False.')
def test_Checker_is_image_type__true(self): payload = True response = Checker().is_image_type('image/png') self.assertEqual(payload, response, 'Checker().is_image_type does not return True.')
def test_Checker_can_be_integer__false(self): payload = False response = Checker().can_be_integer('Not Integer') self.assertEqual(payload, response, 'Checker().can_be_integer does not return False.')
def test_Checker_can_be_integer__true(self): payload = True response = Checker().can_be_integer(10) self.assertEqual(payload, response, 'Checker().can_be_integer does not return True.')