def open_unbinding(): '''绑定第三方用户''' user_id = get_login_user_id(request) if not user_id: return return_no_authorization() login_user = User.query_user( id=user_id, status=BaseConfig.TYPE_USER_ANONYMOUS ) if login_user: return http_util.return_forbidden('请先登录,在进行解绑') args = request.json need_keys = ['open_id'] key = check_params(args, *need_keys) if key: return return_not_found(key) open_id = args['open_id'] open_user = UserOpen.query_open_user(id=open_id) if open_user: if open_user.user_id != user_id: return http_util.return_forbidden('bound not found') else: UserOpen.update_open_user_by_id( id=open_id, user_id="" ) return http_util.return_model() else: return http_util.return_forbidden('bound not found')
def resource_delete(): """资源详情""" args = request.json key = http_util.check_params(args, 'res_id', 'res_type') if key: return http_util.return_param_not_found(key) login_user_id = http_util.get_login_user_id(request) if not login_user_id: return http_util.return_no_authorization() res_id = http_util.get_param(args, 'res_id') res_type = http_util.get_param_int(args, 'res_type') try: is_del = Resource.delete_resource(res_id, res_type) if not is_del: return http_util.return_internal_server_error("删除失败") # 记录用户行为 Action.create_action( user_id=login_user_id, type=BaseConfig.TYPE_ACTION_DELETE, res_id=res_id, res_type=res_type ) return http_util.return_model() except BaseException as e: app.logger.error(e) return http_util.return_internal_server_error()
def user_attentions(): login_user_id = get_login_user_id(request) args = request.args key = http_util.check_params(args, 'attention_status') if key: return http_util.return_param_not_found(key) user_id = http_util.get_param(args, 'user_id') attention_status = http_util.get_param_int(args, 'attention_status') page = http_util.get_param_int(args, 'page', BaseConfig.DEFAULT_PAGE) per_page = http_util.get_param_int(args, 'per_page', BaseConfig.DEFAULT_PER_PAGE) detail_id = None if user_id: detail_id = user_id else: detail_id = login_user_id if not detail_id: return return_no_authorization() try: res = get_user_attentions( user_id=detail_id, attention_status=attention_status, page=page, per_page=per_page, login_user_id=login_user_id ) return http_util.return_model(res) except BaseException as e: app.logger.error(e) return http_util.return_internal_server_error()
def create_comment(): '''评论''' args = request.json key = check_params(args, 'res_id', 'content') if key: return return_not_found(key) # 获取用户id user_id = get_login_user_id(request) if not user_id: return return_no_authorization() res_id = args['res_id'] content = args['content'] comt_db.create_comment(user_id=user_id, res_id=res_id, content=content) return http_util.return_model()
def open_binding(): '''解绑第三方用户''' user_id = get_login_user_id(request) if not user_id: return return_no_authorization() login_user = User.query_user( id=user_id, status=BaseConfig.TYPE_USER_ANONYMOUS ) if login_user: return http_util.return_forbidden('请先登录,在进行绑定') args = request.json need_keys = ['open_id', 'source', 'name', 'portrait'] key = check_params(args, *need_keys) if key: return http_util.return_param_not_found(key) is_exists = UserOpen.query_open_user( user_id=user_id, source=args.get('source') ) if is_exists: return http_util.return_forbidden('该第三方已经绑定过') open_id = args['open_id'] open_user = UserOpen.query_open_user( id=open_id ) if open_user: if open_user.user_id: return http_util.return_forbidden('该账户已被绑定') UserOpen.update_open_user_by_id( id=open_id, user_id=user_id ) else: args['user_id'] = user_id UserOpen.create_open_user(**args) return return_model()
def create_location(): '''创建坐标''' user_id = http_util.get_login_user_id(request) if not user_id: return http_util.return_no_authorization() args = request.json key = http_util.check_params(args, 'lat', 'lng') if key: return http_util.return_param_not_found(key) user = User.query_user(id=user_id) if not user: return http_util.return_forbidden("this user_id can't found user ") lat = args['lat'] lng = args['lng'] la = models.create_location( user_id=user_id, lat=lat, lng=lng ) delete_poi( geo_id=env_config.GEOTABLE_ID, user_id=user_id ) create_poi( geo_id=env_config.GEOTABLE_ID, lat=lat, lng=lng, user_id=user_id, status=user.status ) User.update_user_by_id( id=user_id, lat=lat, lng=lng ) if la: return http_util.return_model() else: return http_util.return_internal_server_error()
def near(): '''附近坐标''' login_user_id = http_util.get_login_user_id(request) if not login_user_id: return http_util.return_no_authorization() args = request.args page_size = http_util.get_param_int(args, 'per_page', 50) key = http_util.check_params(args, 'radius', 'lat', 'lng') if key: return http_util.return_param_not_found(key) lat = http_util.get_param(args, 'lat') lng = http_util.get_param(args, 'lng') radius = http_util.get_param_int(args, 'radius', 1000) items, total = search_near( geo_id=env_config.GEOTABLE_ID, lat=lat, lng=lng, radius=radius, page_size=50 ) users = [] for item in items: user_id = item['user_id'] location = item['location'] lat = location[1] lng = location[0] user = get_user_detail(id=user_id) if not user: continue user['ext']['lat'] = lat user['ext']['lng'] = lng if login_user_id != user_id: users.append(user) res = http_util.make_page_response(users, total, 1, page_size) return http_util.return_model( data=res )
def attention(action): user_id = get_login_user_id(request) if not user_id: return return_no_authorization() args = request.json key = check_params(args, 'to_user_id') if key: return return_not_found(key) to_user_id = args['to_user_id'] if action == 'on': user_db.on_attontion(user_id=user_id, to_user_id=to_user_id) elif action == 'off': user_db.off_attontion(user_id=user_id, to_user_id=to_user_id) else: return return_404() return return_model()
def decorated_function(*args, **kwargs): if g.user is None: return http_util.return_no_authorization() return f(*args, **kwargs)