def update_user(): '''修改用户信息''' headers = request.headers user_id = g.user_id args = request.json try: mobile = args.get('mobile') if mobile: mobile_exists = User.query_user(mobile=mobile) if mobile_exists: return http_util.return_forbidden('手机号已经绑定其他用户') u = User.update_user_by_id(id=user_id, **args) if u: # 记录用户操作 Action.create_action( user_id=user_id, type=BaseConfig.TYPE_ACTION_UPDATE, res_id=user_id, res_type=BaseConfig.TYPE_USER, ext=args ) Search.sync_index_by_id(user_id, BaseConfig.TYPE_USER) return return_model() else: return http_util.return_internal_server_error() except BaseException as e: app.logger.error(e) return http_util.return_internal_server_error()
def delete_user_by_id(user_id): """删除用户""" user = User.query_user(id=user_id) if user: User.update_user_by_id(id=user_id, is_del=1) # 检查是否为第三方用户 uos = UserOpen.query_items(user_id=user_id) print(len(uos)) if uos: for uo in uos: UserOpen.update_open_user_by_id(id=uo.id, user_id="") return True return False
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 init_request(): g.user = None g.user_id = None user_id = get_login_user_id(request) if user_id: g.user = User.query_user(id=user_id) if g.user: g.user_id = g.user.id
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 get_similar_users(user_id): items = [] for item in User.get_similar_users(user_id): user_id = None if g.user: user_id = g.user.id item['attention_status'] = get_user_attention_status( user_id, item['user_id']) items.append(item) return items # list(map(map_user, User.get_similar_users(user_id)))
def save_image(): user_id = g.user_id args = request.json urls = http_util.get_param(args, 'urls') if urls: for url in urls: image = image_db.create_image(user_id=user_id, url=url) # 记录用户上传图片行为 Action.create_action(user_id=user_id, type=BaseConfig.TYPE_ACTION_UPLOAD, res_id=image.id, res_type=BaseConfig.TYPE_IMAGE) detail = Image.get_image_detail(image.id) User.update_user_by_id(id=user_id, last_upload=detail) return http_util.return_model() else: return http_util.return_param_not_found('urls')
def save_video(): user_id = g.user_id args = request.json url = http_util.get_param(args, 'url', "") poster = http_util.get_param(args, 'poster', "") video = Video.create_video(user_id=user_id, url=url, poster=poster) # 记录用户上传视频 Action.create_action(user_id=user_id, type=BaseConfig.TYPE_ACTION_UPLOAD, res_id=video.id, res_type=BaseConfig.TYPE_VIDEO_PLAY) detail = Video.get_video_detail(video.id) User.update_user_by_id(id=user_id, last_upload=detail) return http_util.return_model()
def sync_index_by_id(cls, res_id, res_type): """根据id刷新索引""" item = None if res_type == BaseConfig.TYPE_USER: item = User.get_detail(res_id) args = make_search_args(res_type) args['id'] = item['res_id'] item = format_item(item) args['body'] = item print(args) es.sync_index(**args)
def create_anonymous(): '''获取匿名用户''' try: user = User.create_anonymous_user() authorization = generate_authorization(user.id) return return_model( header={"authorization": authorization} ) except BaseException as e: app.logger.error(e) return http_util.return_internal_server_error()
def get_user_detail(id, source_include=[], source_exclude=[], params=None): """获取用户详情""" user = User.query_user(id=id) item = User.get_detail(id) # 查找第三方用户 if 'opens' in source_include: opens = [] for open_user in user.opens: opens.append({"open_id": open_user.id, "source": open_user.source}) item['opens'] = opens login_user_id = params.get('login_user_id', None) if login_user_id: # 拼装关注状态 attention_status = get_user_attention_status(login_user_id, id) item['attention_status'] = attention_status # 观看数量 if 'view_count' in source_include: views = Action.query_user_views(id) item['ext']['view_count'] = len(views) # 关注数 if 'attention_count' in source_include: attens = UserAttention.query_user_attentions(to_user_id=id) item['ext']['attention_count'] = len(attens) # 获取最后一次地理位置 item['ext']['last_location_time'] = Location.get_user_last_location_time( id) if source_exclude: for field in source_exclude: del item[field] return item
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 list(): '''首页列表''' args = request.args type = http_util.get_param_int(args, 'type', BaseConfig.TYPE_HOME_PAGE_LIST) 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) paginate = User.query_paginate(page=page, status=BaseConfig.TYPE_USER_NORMAL, per_page=per_page) details = [] for item in paginate.items: detail = item.to_json() details.append(detail) res = http_util.make_page_response(details, paginate.total, page, per_page) return return_model(data=res)
def sync_index_by_type(type): """根据type刷新索引""" args = make_search_args(type) items = [] if type == BaseConfig.TYPE_USER: items = User.get_normal_users() elif type == BaseConfig.TYPE_VIDEO_PLAY: items = Video.get_videos() elif type == BaseConfig.TYPE_ARTICLE: items = Article.get_articles() elif type == BaseConfig.TYPE_AUDIO: items = Audio.get_items() elif type == BaseConfig.TYPE_COLLECTION: items = Collection.get_items() else: return False es.delete_index_by_type(**args) for item in items: args['id'] = item['res_id'] item = format_item(item) args['body'] = item es.sync_index(**args)
def signin(): args = request.json key = check_params(args, 'mobile', 'code') if key: return return_not_found(key) mobile = args['mobile'] code = args['code'] is_exist = sms.verify_code( mobile=mobile, code=code ) if mobile.startswith('110') and code == '0000': is_exist = True if mobile == '15890687745' and code == '0000': is_exist = True if not is_exist: return return_model(message='code {} is not found '.format(code), status=404) try: user = User.query_user(mobile=mobile) # 如果用户不存在则创建 if not user: # 获取token携带用户 login_user_id = get_login_user_id(request) login_user = User.query_user(id=login_user_id) # 如果登陆用户不存在,则用手机创建用户 if not login_user or login_user.mobile: user = User.create_user( mobile=mobile, status=BaseConfig.TYPE_USER_NORMAL ) else: user = user_db.update_user_by_id( user_id=login_user_id, mobile=mobile, status=BaseConfig.TYPE_USER_NORMAL ) authorization = generate_authorization(user.id) # 记录用户登录操作 Action.create_action( user_id=user.id, type=BaseConfig.TYPE_ACTION_LOGIN, res_id=user.id, res_type=BaseConfig.TYPE_USER, ext=args ) return return_model( header={"authorization": authorization} ) except BaseException as e: app.logger.error(e) return http_util.return_internal_server_error()
def signin_open(): '''第三方登录''' 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) login_user_id = get_login_user_id(request) try: open_id = args['open_id'] if login_user_id: luo = UserOpen.query_open_user( id=open_id, user_id=login_user_id ) if luo: return http_util.return_forbidden('当前用户已登录,不能重复登录') login_user = User.query_user( id=login_user_id, status=BaseConfig.TYPE_USER_NORMAL ) if login_user: return http_util.return_forbidden('当前用户已登录,不能重复登录') open_user = UserOpen.query_open_user(id=open_id) if open_user: # 如果第三方用户没有绑定用户,则生成用户并绑定 if not open_user.user_id: binding_user_id = "" if login_user_id: binding_user_id = login_user_id User.update_user_by_id( id=login_user_id, name=open_user.name, portrait=open_user.portrait, status=BaseConfig.TYPE_USER_NORMAL ) else: user = User.create_user( name=args.get('name'), portrait=args.get('portrait') ) binding_user_id = user.id open_user = UserOpen.update_open_user_by_id( id=open_id, user_id=binding_user_id ) else: # 创建第三方用户 if login_user_id: args['user_id'] = login_user_id open_user = UserOpen.create_open_user(**args) User.update_user_by_id( id=login_user_id, name=open_user.name, portrait=open_user.portrait, status=BaseConfig.TYPE_USER_NORMAL ) else: open_user = UserOpen.create_open_user_and_user(**args) user_id = open_user.user_id authorization = generate_authorization(user_id) # 记录用户登录操作 Action.create_action( user_id=user_id, type=BaseConfig.TYPE_ACTION_LOGIN, res_id=user_id, res_type=BaseConfig.TYPE_USER, ext=args ) return return_model( header={"authorization": authorization} ) except BaseException as e: app.logger.error(e) return http_util.return_internal_server_error()