def format_offer_info(offer_model, share_model, login_user_id): if not offer_model: return dict() icon_info = RewardIconModel.query_icon_with_id(offer_model.present_id) if not share_model: from app.models.social.share import ShareModel share_model = ShareModel.query_share_model(offer_model.share_id) result = dict() result["present_url"] = icon_info.url if icon_info else "" result["price"] = share_model.price / 100 result["total_number"] = offer_model.total_number result["join_number"] = offer_model.join_number result["status"] = offer_model.status result["offer_status"] = offer_model.offer_status result["limit_time"] = (offer_model.due_time - datetime.datetime.now()).seconds offer_enter = OfferListModel.check_offer_enter_status(offer_model.offer_id, login_user_id) result["enter_status"] = 1 if offer_enter else 0 if offer_model.offer_status == 3: winner_info = UserInfoModel.query_user_model_by_id(offer_model.winner_user_id) result["winner"] = UserInfoModel.format_user_info(winner_info) elif offer_model.offer_status in [1, 2]: result["list"] = OfferListModel.query_offer_list(offer_model.offer_id) return result
def black_able(black_user_id): user = UserInfoModel.query_user_model_by_id(black_user_id) user_info = UserInfoModel.format_user_info(user) if user_info and user_info.get("role_id", 0) == 8: return {"data": 0, "message": "不能拉黑系统管理员"} return None
def query_share_info(share_id): share_info = ShareModel.query_share_model(share_id) if not share_info or share_info.status not in status_public: return None user_info = UserInfoModel.query_user_model_by_id(share_info.user_id) if share_info.type_id == 10: image = ImageModel.query.filter_by( share_id=share_id, status=1).order_by(ImageModel.image_id.asc()).first() image_url = ImageModel.generate_image_url(image, 'c') elif share_info.type_id == 11: image = ImageModel.query.filter_by( share_id=share_id, status=1).order_by(ImageModel.image_id.asc()).first() image_url = ImageModel.generate_image_url(image, 'x') elif share_info.type_id == 30: image_url = "" else: image_url = UserInfoModel.format_user_info(user_info)["avatar"] result = { "image": image_url, "content": share_info.content, "nickname": user_info.nickname, } return result
def get(self, *args): user_id = g.account["user_id"] user_info = UserInfoModel.query_user_model_by_id(user_id) if not user_info: return json_success_response({}) result = dict() result["user_info"] = UserInfoModel.format_user_info(user_info) result["profit_today"] = 0 result["ok_percent"] = UserInfoModel.calculate_ok_percent(user_id) invite_code = UserInviteCodeModel.query_user_invite_code(user_id) result["invite_url"] = user_invite_url + invite_code result["new_visitor"] = RedisModel.query_new_message( user_id, RedisModel.new_visitor) result["follow_add"] = 0 social_meta = SocialMetaModel.query_social_meta_info( g.account["user_id"]) result["wechat_info"] = MeHandler.format_wechat_info( g.account["user_id"], social_meta.wechat_want if social_meta else 0) return json_success_response(result)
def get(self, params): login_user_id = g.account["user_id"] RedisModel.reset_new_message(login_user_id, RedisModel.new_visitor) visitor_list = list() while not visitor_list: visitor_list = VisitorRecordModel.query_visitor_list_by_day( login_user_id, 20, params["last_id"]) params["last_id"] += 1 # 最多查询30天 if params["last_id"] > 30: break visitor_dict_list = list() for visitor in visitor_list: item = dict() user_info = UserInfoModel.query_user_model_by_id( visitor.visitor_user_id) item["user_info"] = UserInfoModel.format_user_info( user_info) if user_info else {} item["time"] = visitor.created_time visitor_dict_list.append(item) return json_success_response({ "last_id": params["last_id"], "list": visitor_dict_list })
def format_home_users(user_list=list()): """ 格式化首页的用户 """ result = [] if not user_list: return result image_list = ImageModel.query_image_by_image_id_list( array_column(user_list, "image_id")) image_list = array_column_key(image_list, "user_id") for user in user_list: item = dict() # 用户信息 user_info = UserInfoModel.query_user_model_by_id(user.user_id) item["user_info"] = UserInfoModel.format_user_info(user_info) image_model = image_list.get(user.user_id, None) item["cover"] = ImageModel.generate_image_url( image_model, "c") if image_model else "" item["last_id"] = 0 item["offset"] = 0 item["relation_type"] = 0 result.append(item) return result
def get(self, params): params["limit"] = self.per_page user_list = [] field = "" # 关注 if params["type"] == 2: user_list = FollowModel.query_follow_list(params) field = "follow_id" # 粉丝 elif params["type"] == 3: user_list = FollowModel.query_fans_list(params) field = "user_id" # 好友 elif params["type"] == 4: user_list = FollowModel.query_friends_list(params) field = "follow_id" if not user_list: return json_success_response(user_list) other_user_id_list = array_column(user_list, field) user_relations_dict = FollowModel.query_relation_to_user_list(g.account["user_id"], other_user_id_list) result = list() for follow_model in user_list: item = dict() item["id"] = follow_model.id user_id = getattr(follow_model, field) item["relation_type"] = user_relations_dict.get(user_id, 0) user_model = UserInfoModel.query_user_model_by_id(user_id) item["user_info"] = UserInfoModel.format_user_info(user_model) result.append(item) return json_success_response(result)
def format_offer_list(offer_list): result = list() if not offer_list: return result for offer_list_model in offer_list: item = offer_list_model.to_dict(filter_params=True) user_info = UserInfoModel.query_user_model_by_id( offer_list_model.apply_user_id) item["user_info"] = UserInfoModel.format_user_info(user_info) result.append(item) return result
def post(self): image_list = UploadImage() image_list.save_images() if not image_list.images: return json_fail_response(2401) image_model = image_list.images[0]["image"] user_info = UserInfoModel.query_user_model_by_id( g.account["user_id"], True) if not user_info: return json_fail_response(2101) # 缓存旧的头像 old_avatar = user_info.avatar # 更新头像 user_info.avatar = image_model.image_id # 头像绑定到图片模型 image_model.user_id = user_info.user_id # 解绑旧的头像 if old_avatar: ImageModel.query.filter_by(image_id=old_avatar).update( dict(user_id=0)) db.session.commit() user_model = UserInfoModel.query_user_model_by_id( g.account["user_id"], True) AccountDataModel.query_request_account_by_token( g.account["access_token"], True) user_info = UserInfoModel.format_user_info(user_model) small_dict = { "url": user_info.get("avatar", ""), "width": 240, "height": 240, } big_dict = { "url": user_info.get("big_avatar", ""), "width": 800, "height": 800 } result = {"small": small_dict, "big": big_dict} return json_success_response(result)
def format_ease_mob_user(login_user_id, user_id): user_info = UserInfoModel.query_user_model_by_id(user_id) has_trade = UserConsumerModel.query_trade_relationship( user_id, login_user_id) follow_status = FollowModel.query_relation_to_user( login_user_id, user_id) remark_name = RemarkNameModel.query_remark_name(login_user_id, user_id) result = { "user_info": UserInfoModel.format_user_info(user_info), "had_trade": has_trade, "follow_status": follow_status, "black_status": 0, "remark_name": remark_name.remark_nickname if remark_name else "" } return result
def get(self, params): query = LikeModel.query.filter_by(share_id=params["share_id"], status=1) if params["last_id"]: query = query.filter(LikeModel.like_id < params["last_id"]) like_model_list = query.order_by( LikeModel.like_id.desc()).limit(20).all() result = list() for model in like_model_list: user_info = UserInfoModel.query_user_model_by_id(model.user_id) user_info = UserInfoModel.format_user_info(user_info) user_info["like_id"] = model.like_id result.append(user_info) return json_success_response(result)
def get(self): result = {"new_comment": 0, "image_url": ""} new_comment = RedisModel.query_new_message(g.account["user_id"], RedisModel.new_comment) result["new_comment"] = new_comment if new_comment: relation_list = CommentRelationModel.query_relation_info( g.account["user_id"], per_page=3, last_cid=0) for model in relation_list: if model.user_id != g.account["user_id"]: user_info = UserInfoModel.query_user_model_by_id( model.user_id) result["image_url"] = UserInfoModel.format_user_info( user_info)["avatar"] break return json_success_response(result)
def get(self, params): RedisModel.reset_new_message(g.account["user_id"], RedisModel.new_comment) comment_list = CommentRelationModel.query_relation_info( g.account["user_id"], 20, params["last_id"]) if not comment_list: return json_success_response(list()) comment_id_list = array_column(comment_list, "comment_id") comment_info_list = CommentModel.query_comment_list(comment_id_list) comment_info_dict = array_column_key(comment_info_list, "comment_id") if not comment_info_list: return json_success_response(list()) result = list() for comment_relation_model in comment_list: share_info = GetCommentHandler.query_share_info( comment_relation_model.share_id) if not share_info: continue if comment_info_dict[comment_relation_model. comment_id].user_id == g.account["user_id"]: continue user_info = UserInfoModel.query_user_model_by_id( comment_relation_model.user_id) item = dict() item["share_id"] = comment_relation_model.share_id item["share_img"] = share_info["image"] item["share_content"] = share_info["content"] item["share_nickname"] = share_info["nickname"] item["content"] = comment_info_dict[ comment_relation_model.comment_id].content item["created_time"] = comment_relation_model.created_time item["status"] = comment_relation_model.status item["comment_id"] = comment_relation_model.comment_id item["user_info"] = UserInfoModel.format_user_info(user_info) result.append(item) return json_success_response(result)
def format_like_info(like_model_list=list()): result = [] if not like_model_list: return result from app.models.account.user_info import UserInfoModel for model in like_model_list: user_info = UserInfoModel.query_user_model_by_id(model.user_id) user_info = UserInfoModel.format_user_info(user_info) item_dict = { "avatar": user_info.get("avatar", ""), "like_id": model.like_id, } result.append(item_dict) return result
def format_user_wechat_data(order_meta_list, params, buyer_type="", seller_type=""): order_info_list = OrderInfoModel.query_order_info_model( array_column(order_meta_list, "order_id")) order_info_dict = array_column_key(order_info_list, "order_id") result = list() for order_meta in order_meta_list: item = dict() user_info_model = UserInfoModel.query_user_model_by_id( getattr(order_meta, seller_type, default=0)) item["user_info"] = UserInfoModel.format_user_info( user_info_model) if user_info_model else dict() item["id"] = order_meta.id item["order_id"] = order_meta.order_id order_info = order_info_dict.get(order_meta.order_id) item["price"] = order_info.order_price / 100 if order_info else 0 item["time"] = order_meta["created_time"] wechat_status = UserWeChatModel.format_wechat_status( order_meta, params["user_id"]) item["wechat_complain_status"] = wechat_status.get( "wechat_complain_status", 0) item["wechat_order_status"] = wechat_status.get( "wechat_order_status", 0) if buyer_type == "seller_id": item["wechat"] = order_info.order_message if order_info else "" result.append(item) return result
def get(self): user_id = g.account["user_id"] result = {"user_id": user_id} # Aha 账号 aha_account = AhaAccountModel.query_aha_account_by_id(user_id) result = dict(result, **aha_account) # 社交信息 params = ["vocation_name", "school_name", "language", "emotional_state", "live_region_name"] user_social = UserSocialInfoModel.query_user_social_info(user_id) user_social_dic = user_social.format_model(params) result = dict(result, **user_social_dic) # 个人信息 params = ["weight", "height", "star_sign", "age"] user_personal_model = UserPersonalInfoModel.query_personal_info_by_user_id(user_id) user_personal_dic = user_personal_model.format_model(params) result = dict(result, **user_personal_dic) # 用户信息 user_info_model = UserInfoModel.query_user_model_by_id(user_id) user_info_dic = UserInfoModel.format_user_info(user_info_model) result = dict(result, **user_info_dic) # social_page_dic = SocialPageModel.query_social_page_model(user_id) result["user_banner_type"] = social_page_dic.get("cover_type", 1) result["user_banner"] = social_page_dic.get("cover", "") if social_page_dic["cover_type"] == 2: result["orientation"] = social_page_dic.get("orientation", "") result["user_banner_cover"] = social_page_dic.get("video_img", "") result["album"] = UserInfoModel.query_user_album(user_id) return json_success_response(result)
def format_share_model(share_info_list=list(), account=0): """ 统一的格式化动态模型 1. 动态格式化 2. 图片格式化 3. 评论格式化 4. 点赞格式化 """ result_list = [] if not share_info_list: return result_list share_id_list = array_column(share_info_list, "share_id") # 获取到share_meta模型(一次sql比多次sql要好) share_meta_list = ShareMetaModel.query_share_meta_model_list(share_id_list, auto_format=False) share_meta_dict = array_column_key(share_meta_list, "share_id") img_model_dict = ImageModel.query_share_image_list(share_id_list) like_status_list = ShareModel.query_share_check_list(account, share_id_list) # 逐条格式化 for share_model in share_info_list: share_dic = share_model.to_dict() share_meta = share_meta_dict.get(share_model.share_id, None) share_dic["like_status"] = 1 if like_status_list.get(share_model.share_id, None) else 0 share_dic["like_count"] = share_meta.like if share_meta else 0 share_dic["comment_count"] = share_meta.comment if share_meta else 0 share_dic["click"] = share_meta.click if share_meta else 0 share_dic["image"] = dict() share_dic["video"] = dict() # 格式化用户信息 from app.models.account.user_info import UserInfoModel user_model = UserInfoModel.query_user_model_by_id(share_model.user_id) share_dic["user_info"] = UserInfoModel.format_user_info(user_model) # 格式化图片信息 if share_model.type_id in [8, 10, 11, 40, 50, 51]: img_model_list = img_model_dict.get(share_model.share_id, None) share_dic["image"] = { "big": ImageModel.format_image_model(img_model_list, size='f'), "small": ImageModel.format_image_model(img_model_list, size='c'), } # 格式化视频信息 elif share_model.type_id in [30, 31]: video_model = VideoModel.query_share_video_info(share_model.share_id) share_dic["video"] = VideoModel.format_video_info(video_model) share_dic["like_list"] = LikeModel.query_like_list(share_model.share_id, limit=5) # 删除无用数据 del share_dic["data"] # 格式化悬赏数据 share_dic["offer_info"] = dict() if share_model.type_id == 90: offer_model = OfferModel.query_offer_mode_with_offer_id(share_model.offer_id) share_dic["offer_info"] = OfferModel.format_offer_info(offer_model, share_model, account) result_list.append(share_dic) return result_list
def post(self, params): share_model = ShareModel.query_share_model(params["share_id"], status=status_private) if not share_model: return json_fail_response(2405) like_model = LikeModel.query_like_model(g.account["user_id"], params["share_id"]) user_info = UserInfoModel.query_user_model_by_id(g.account["user_id"]) if not like_model: if params["status"] != 1: result = { "data": 0, "message": "操作失败", } return json_success_response(result) like_model = LikeModel() like_model.share_id = params["share_id"] like_model.user_id = g.account["user_id"] like_model.status = 1 db.session.add(like_model) db.session.commit() result = { "data": 1, "status": params["status"], "user_info": UserInfoModel.format_user_info(user_info), "message": "点赞成功", } ShareMetaModel.update_share_meta_model(share_model.share_id, share_model.user_id, ["like"]) elif like_model.status != params["status"]: like_model.status = params["status"] db.session.commit() result = { "data": 1, "status": params["status"], "user_info": UserInfoModel.format_user_info(user_info), "message": "点赞成功" if params["status"] == 1 else "取消点赞成功", } ShareMetaModel.update_share_meta_model(share_model.share_id, share_model.user_id, ["like"], params["status"] == 1) else: if params["status"] == 1: result = { "data": 0, "status": params["status"], "user_info": UserInfoModel.format_user_info(user_info), "message": "已点赞", } else: result = { "data": 0, "status": params["status"], "user_info": UserInfoModel.format_user_info(user_info), "message": "未点赞", } share_meta = ShareMetaModel.query.filter_by( share_id=share_model.share_id).first() result["like_count"] = share_meta.like return json_success_response(result)
def get(self, params): if params["user_id"] == 0: return json_fail_response(2101) user_info = UserInfoModel.query_user_model_by_id(params["user_id"]) result = UserInfoModel.format_user_info(user_info) social_meta_info = SocialMetaModel.query_social_meta_info( params["user_id"]) result["share_num"] = social_meta_info.share if social_meta_info else 0 result[ "follow_num"] = social_meta_info.following if social_meta_info else 0 result[ "fans_num"] = social_meta_info.follower if social_meta_info else 0 result["follow_status"] = FollowModel.query_relation_to_user( g.account["user_id"], params["user_id"]) result["album"] = UserInfoModel.query_user_album(params["user_id"]) social_page_info = SocialPageModel.query_social_page_model( params["user_id"]) result["user_banner"] = social_page_info.get("cover", "") result["user_banner_type"] = social_page_info.get("cover_type", 1) if result["user_banner_type"] == 2: result["orientation"] = "" result["user_banner_cover"] = "" if params["user_id"] == g.account["user_id"]: result["profit"] = IndexHandler.query_user_profit( params["user_id"]) if result["identified"] == 1: result["identify_title"] = "官方认证:" + result["identify_title"] result["wechat_info"] = MeHandler.format_wechat_info( params["user_id"], social_meta_info.wechat_want if social_meta_info else 0) user_location = UserLocationModel.query.filter( UserLocationModel.id == user_info.location_id).first() result["latitude"] = user_location.latitude if user_location else 0 result["longitude"] = user_location.longitude if user_location else 0 result["changeable_aha"] = 1 result["aha_id"] = '' aha_info = AhaAccountModel.query_aha_account_by_id(params['user_id'], auto_formated=False) if aha_info: result["changeable_aha"] = aha_info.changeable_aha if aha_info.changeable_aha == 0: result["aha_id"] = aha_info.aha_id # 最近的动态 result["share_info"] = ShareModel.query_recent_share_photo( params['user_id'], g.account["user_id"]) # 是否有交易记录 result["had_trade"] = 1 # 备注昵称 result["remark_name"] = "" last_login = OpenLogModel.query_last_login(params["user_id"]) result["last_login"] = 0 if last_login: result["last_login"] = time.time() - time.mktime( last_login.created_time.timetuple()) # 插入访客记录 if g.account["user_id"] != 0 and g.account["user_id"] != params[ "user_id"]: RedisModel.add_new_message(params["user_id"], RedisModel.new_visitor) VisitorRecordModel(user_id=params["user_id"], visitor_user_id=g.account["user_id"]) return json_success_response(result)
def query_share_comment_list(share_id, login_user_id=0, last_id=0, per_page=20): """ TODO:待完成 """ query = CommentModel.query.filter_by(share_id=share_id, status=1) if last_id: query = query.filter(CommentModel.comment_id < last_id) share_model = ShareModel.query_share_model(share_id, status=status_private) # 如果访问者不是发帖者,则只能显示自己的悄悄话和公开评论 if share_model.user_id != login_user_id: query = query.filter( or_( CommentModel.type == 0, and_(CommentModel.type == 1, CommentModel.user_id == login_user_id), )) comment_list = query.filter(CommentModel.status == 1).order_by( CommentModel.comment_id.desc()).limit(per_page).all() if not comment_list: return [] comment_dict_list = array_column_key(comment_list, "comment_id") # 获取所有的父评论id并去重 layer_comment_id_list = list( set(array_column(comment_list, "layer_comment_id"))) # 查找所有父评论的详情 query = CommentModel.query.filter( CommentModel.comment_id.in_(layer_comment_id_list), CommentModel.status == 1) layer_comment_model_list = query.order_by( CommentModel.comment_id.desc()).all() for layer_comment_model in layer_comment_model_list: if layer_comment_model.type == 1 and login_user_id not in [ layer_comment_model.user_id ]: continue comment_model = comment_dict_list.get( layer_comment_model.comment_id, None) if not comment_model: continue result = list() for comment_model in comment_list: item = comment_model.to_dict() user_model = UserInfoModel.query_user_model_by_id( comment_model.user_id) item["user_info"] = UserInfoModel.format_user_info(user_model) if not item.get("layer_info", None): item["layer_info"] = [] result.append(item) return result
def post(self, params): share_info = ShareModel.query_share_model(params["share_id"], status=status_private) if not share_info: return json_fail_response(2405) comment_model = None # 回复某一条评论 if params["reply_comment_id"]: comment_model = CommentModel.query_comment_by_id(params["reply_comment_id"]) if not comment_model: return json_fail_response(2406) if comment_model.layer_comment_id: layer_comment_model = CommentModel.query_comment_by_id(comment_model.layer_comment_id) if not layer_comment_model: return json_fail_response(2406) comment = CommentModel() comment.share_id = params["share_id"] comment.user_id = g.account["user_id"] comment.reply_comment_id = params["reply_comment_id"] comment.content = params["content"] comment.type = params["type"] comment.status = 1 if params["reply_comment_id"]: if not comment_model.layer_comment_id: comment.layer_comment_id = comment_model.comment_id else: comment.layer_comment_id = comment_model.layer_comment_id if comment_model.type == 1: comment.type = 1 db.session.add(comment) db.session.commit() ShareMetaModel.update_share_meta_model(params["share_id"], share_info.user_id, ["comment"]) result = comment.to_dict() user = UserInfoModel.query_user_model_by_id(g.account["user_id"]) result["user_info"] = UserInfoModel.format_user_info(user) if comment.reply_comment_id: reply_user_info = UserInfoModel.query_user_model_by_id(comment_model.user_id) result["reply_user_info"] = UserInfoModel.format_user_info(reply_user_info) # 回复的动态的所有者不是自己 if comment_model.user_id != g.account["user_id"]: CommentRelationModel(g.account["user_id"], comment_model.user_id, share_info.share_id, comment.comment_id, comment_model.comment_id) RedisModel.add_new_message(comment_model.user_id, RedisModel.new_comment) # 动态的主人不是自己 if share_info.user_id != g.account["user_id"]: if comment_model and comment_model.user_id == share_info.user_id: pass else: CommentRelationModel(g.account["user_id"], share_info.user_id, share_info.share_id, comment.comment_id, params["reply_comment_id"]) RedisModel.add_new_message(share_info.user_id, RedisModel.new_comment) share_meta = ShareMetaModel.query_share_meta_model(params["share_id"]) result["comment_count"] = share_meta.comment if share_meta else 0 # 回复的数据结构 if params["reply_comment_id"]: result = { "comment_id": comment.comment_id, "share_id": comment.share_id, "user_id": comment.user_id, "reply_comment_id": comment.reply_comment_id, "content": comment.content, "type": comment.type, "status": comment.status, "created_time": comment.created_time, "nickname": user.nickname, "reply_nickname": reply_user_info.nickname, } return json_success_response(result)