def help(): func_list = [] for rule in app.url_map.iter_rules(): if rule.endpoint != "static": func_list.append({ 'rule': rule.rule, 'methods': [method for method in rule.methods], }) return respond_json(message="All URL endpoints", success=True, data=func_list)
def index(): # capture log with level info store to storage/logs/yyyy-mm-dd.log app.logger.info('Hi, im logger with level info') # perform job with params # for n in range(10): # example_job.perform.queue(n) return respond_json( message="Don't know where to go? Query /help for more information.", success=True, data=None)
def store(): if 'file' not in request.files or request.files['file'].filename == '': return respond_json_with_validation_errors( {'file': ['file doest not exists!']}) file = storage.put('my_files', request.files['file']) return respond_json(success=True, message='Successfully upload files', data={ "file": storage.temporary_url(file), }, code=200)
def index(): users = User.query.order_by(desc(User.username)).paginate( page=request.args.get('page', type=int, default=1), per_page=request.args.get('per_page', type=int, default=15), ) return respond_json( success=True, message=None, data=[user.serialize for user in users.items], meta={ 'pagination': paginator.paginate(users) } )
def send(): try: # please read docs https://pythonhosted.org/Flask-Mail/ message = Message('Hello this is my subject mail') message.recipients = [ '*****@*****.**', ] message.html = 'Hello my name is <b>ilhamarrouf</b>' with app.app_context(): mail.send(message) return respond_json(success=True, message='Successfully send mail...') except Exception as err: app.logger.error(getattr(err, 'message', repr(err))) abort(BadRequest.code)
def account(): return respond_json(success=True, data=current_user.serialize)
def handle_authentication_http_exception(error): return respond_json(success=False, code=error.status_code, message=error.message, errors=error.errors)
def internal_server_error(error): return respond_json( message="Error 500: Internal server error", success=False, code=InternalServerError.code, )
def too_many_request(error): return respond_json( message="429 Too Many Requests", success=False, code=TooManyRequests.code, )
def method_not_allowed(error): return respond_json( message="Error 405: Method not allowed", success=False, code=MethodNotAllowed.code, )
def not_found(error): return respond_json( message="Error 404: Not found", success=False, code=NotFound.code, )
def unauthenticated(error): return respond_json( message="Error 401: Unauthenticated", success=False, code=Unauthorized.code, )
def bad_request(error): return respond_json( message="Error 400: Bad request", success=False, code=BadRequest.code, )
def custom_user_loader_error(identity): return respond_json(success=False, message="Auth user {} not found".format(identity), code=Unauthorized.code)
def expired_token_loader(): return respond_json(success=False, message='Token has expired.', code=Unauthorized.code)
def unauthorized_loader(callback): return respond_json(success=False, message='unauthenticated.', code=Unauthorized.code)