def get_user(id): user = userService.get_user_by_id(id) if user is None: return Restful.bad_request('用户ID: %s 不存在' % id) user = safe_model_to_json(enrich_user(user)) return Restful.ok(user)
def update_user_sns(id): user = userService.get_user_by_id(id) if user is None: return Restful.bad_request('用户ID: %s 不存在' % id) userService.spider_user_sns(user) return Restful.ok()
def get_token(): if g.token_used: return Restful.forbidden('请使用用户名和密码') return Restful.ok({ 'userId': g.current_user.id, 'username': g.current_user.username, 'avatar': g.current_user.avatar, 'slogan': g.current_user.slogan, 'token': userService.generate_auth_token(g.current_user.id, 3600*24), 'expiration': 3600*24 })
def register(): try: user = userService.register(request.json) return Restful.created({ 'userId': user.id, 'username': user.username, 'avatar': user.avatar, 'slogan': user.slogan, 'token': userService.generate_auth_token(user.id, 3600*24), 'expiration': 3600*24 }) except ServiceError, e: return Restful.bad_request(e.message)
def get_user_list(): fuzzy = request.args.get('fuzzy', type=str) order = request.args.get('order', type=str) page, paginate_by = get_request_page_args(request) paginate = Paginate(userService.get_user_list(fuzzy=fuzzy), '/user', page, paginate_by) return Restful.ok(paginate.to_json())
def validation_error(e): current_app.logger.error(e) return Restful.bad_request(e.message) # @api.errorhandler(Exception) # def validation_error(e): # current_app.logger.error(e) # return Restful.internal_server_error(e)
def validation_error(e): current_app.logger.error(e) return Restful.bad_request(e.message) # @main.errorhandler(Exception) # def validation_error(e): # current_app.logger.error(e) # return Restful.internal_server_error(e)
def logout(): return Restful.ok()
def auth_error(): # return Restful.bad_request('用户名或密码错误') # qq浏览器返回401时,会弹出默认登录登陆框。 在after_request中改变响应头也没用。 return Restful.forbidden('用户名或密码错误')
def app_page_not_found(e): current_app.logger.error(e) if request.accept_mimetypes.accept_json and not request.accept_mimetypes.accept_html: response = Restful.page_not_found() return response return render_template('404.html'), 404
def app_method_not_allowed(e): current_app.logger.error(e) response = Restful.method_not_allowed() return response
def app_internal_server_error(e): current_app.logger.error(e) response = Restful.internal_server_error(e.message) return response
def modify_user(id): try: user = userService.modify_user_profile(id, request.json) return Restful.ok(user.to_json()) except ServiceError, e: return Restful.bad_request(e.message)
def app_page_not_found(e): current_app.logger.error(e) response = Restful.page_not_found() return response
def app_method_not_allowed(e): current_app.logger.error(e) if request.accept_mimetypes.accept_json and not request.accept_mimetypes.accept_html: response = Restful.method_not_allowed() return response return render_template('405.html'), 405
def app_internal_server_error(e): current_app.logger.error(e) if request.accept_mimetypes.accept_json and not request.accept_mimetypes.accept_html: response = Restful.internal_server_error(e.message) return response return render_template('500.html'), 500
from restful import Restful #------------Test------------------ rest = Restful() @rest.router('/wm/[a]?') def test(): a = rest.getAttribute('a') v = rest.query('u') if not v: v = '' elif v is True: v = '\"true\"' return '{\"test\":' + a + ',\"value\":' + v + '}' @rest.router('/vpc/[b]') def test(): attrB = rest.getAttribute('b') return '{\"vpc\":' + attrB + '}' @rest.router('/post', 'POST') def testPost(): p = rest.query('p') v = rest.query('v') return '{\"q\":' + p + '\"v\":' + v + '}' rest.run()