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 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 query_user_album(user_id): result = dict() album = AlbumModel.query_album_by_user_id(user_id) if not album: return result image_model_list = ImageModel.query_album_image_list(user_id, album.id) small_list = [] big_list = [] for image_model in image_model_list: small = big = dict() small["url"] = ImageModel.generate_image_url(image_model, 'b') small["width"] = 240 small["height"] = 240 small["image_id"] = image_model.image_id small_list.append(small) big["url"] = ImageModel.generate_image_url(image_model, 'f') big["width"] = 800 big["height"] = 800 big["image_id"] = image_model.image_id big_list.append(big) result["small"] = small_list result["big"] = big_list return result
def format_private_video_model(video_model=None, image_model=None, video_meta_model=None, pay=0): if not video_model: return dict() if not image_model: image_model = ImageModel.query_image_by_id(video_model.cover_id) if not image_model: return dict() if not video_meta_model: video_meta_model = VideoMetaModel.query_video_meta( video_model.video_id) if not video_meta_model: return dict() result = dict() result["pay"] = pay result["price"] = video_meta_model.price / 100 video_dict = VideoModel.format_video_info(video_model) video_dict["video_id"] = video_model.video_id if not pay: video_dict["video_img"] = ImageModel.generate_image_url( image_model, 'x') video_dict["video_url"] = "" result["video"] = video_dict return result
def format_user_avatar(user_model, img_model=None, size='b'): if not user_model: return "" if not img_model: img_model = ImageModel.query_image_by_id(user_model.avatar) if not img_model: return "" return ImageModel.generate_image_url(img_model, size=size)
def format_video_info(video_model): if not video_model or not isinstance(video_model, VideoModel): return dict() from app.models.social.image import ImageModel from heron import app cover_model = ImageModel.query_image_by_id(video_model.cover_id) result = video_model.format_model( attr_list=["video_width", "video_height"]) result["video_url"] = app.config["VIDEO_HOST"] + video_model.video_url result["video_img"] = ImageModel.generate_image_url(cover_model, 'f') return result
def query_recent_share_photo(user_id, login_user, limit=3): """ 查询最近发布的三条动态的图片 """ query = ShareModel.query.filter_by(user_id=user_id, type_id=10) if user_id == login_user: query = query.filter(ShareModel.status.in_(status_private)) else: query = query.filter(ShareModel.status.in_(status_public)) result = query.order_by(ShareModel.share_id.desc()).limit(limit).all() if not result: return [] share_ids_list = array_column(result, "share_id") query = ImageModel.query.filter_by(status=1).filter(ImageModel.share_id.in_(share_ids_list)) image_list = query.order_by(ImageModel.image_id.desc()).limit(limit).all() if not image_list: image_list = [] result = list() for image_model in image_list: url = ImageModel.generate_image_url(image_model, 'b') if url: result.append(url) return result
def format_user_info(user, full=False): if not user: return dict() user_info_dict = user.to_dict(filter_params=not full) if not user.avatar: user_info_dict["avatar"] = "" user_info_dict["big_avatar"] = "" else: img = ImageModel.query_image_by_id(user.avatar) user_info_dict["avatar"] = UserInfoModel.format_user_avatar( user, img, 'b') user_info_dict["big_avatar"] = UserInfoModel.format_user_avatar( user, img, 'f') user_personal_info = UserPersonalInfoModel.query_personal_info_by_user_id( user.user_id) user_info_dict[ "age"] = user_personal_info.age if user_personal_info else 0 user_id_relation = UserIdRelationModel.query_user_id_relation( user.user_id) user_info_dict["huanxin_uid"] = user_id_relation.str_id return user_info_dict
def post(self): image_uploader = UploadImage() image_uploader.save_images() result = list() for image_dict in image_uploader.images: image_model = image_dict.get("image", None) if not image_model: continue image_dict = image_model.format_model(attr_list=["image_id", "image_width", "image_height"]) image_dict["image_url"] = ImageModel.generate_image_url(image_model) image_dict["tiny_img"] = ImageModel.generate_image_url(image_model, 'e') result.append(image_dict) return json_success_response(result[0])
def query_social_page_model(user_id, auto_format=True): social_page_model = SocialPageModel.query.filter_by(user_id=user_id, status=1).first() if not auto_format: return social_page_model if not social_page_model: social_page_model = SocialPageModel() result = social_page_model.to_dict(filter_params=True) if social_page_model.cover_type == 1: image_model = ImageModel.query_image_by_id(social_page_model.cover) result["cover"] = ImageModel.generate_image_url(image_model, 'f') elif social_page_model.cover_type == 2: pass return result
def check_share_image_price(share_id, user_id): share_image_list = ImageModel.query_share_images(share_id) user_info = UserInfoModel.query_user_model_by_id(user_id) # 认证用户每张照片1块钱, 普通用户每张照片5毛 image_count = len(share_image_list) if user_info.identified == 1: return image_count * 100 else: return image_count * 0.5 * 100
def config_photo(params, image_model_list): album = AlbumModel.query_album_by_user_id(params["user_id"]) user_info = UserInfoModel.query_user_model_by_id(params["user_id"]) if not album: album = AlbumModel() album.user_id = params["user_id"] album.status = 1 album.album_name = user_info.nickname + "的相册" if user_info else "" album.album_cover = "" db.session.add(album) db.session.commit() small_list = [] big_list = [] for image in image_model_list: image = image.get("image", None) if not image: continue image.album_id = album.id image.user_id = params["user_id"] small_list.append({ "url": ImageModel.generate_image_url(image, 'b'), "width": 240, "height": 240, "image_id": image.image_id, }) big_list.append({ "url": ImageModel.generate_image_url(image, "f"), "width": 800, "height": 800, "image_id": image.image_id, }) db.session.commit() return {"small": small_list, "big": big_list}
def get(self, params): result = {"offset": 0, "want_buy": 0, "result_data": list()} # 私密照片 if params["type"] == 11: # 自己看自己的私密照片 if params["user_id"] == g.account["user_id"]: result["result_data"] = ImageModel.query_private_image( params["user_id"], g.account["user_id"], params["last_id"]) # 查看别人的私密照片 else: # 第一次请求,需要生成一个随机量 if params["offset"] == 0 and params["last_id"] == 0: params["offset"] = int( PrivateLibraryHandler.random_private_library_offset( params["type"], params["user_id"])) result["result_data"] = ImageModel.query_random_private_image( params["user_id"], g.account["user_id"], params["last_id"], params["offset"]) result["want_buy"] = RedisModel.query_new_message( params["user_id"], RedisModel.private_image_want) # 私密视频 else: if params["user_id"] == g.account["user_id"]: result["result_data"] = VideoModel.query_private_video( params["user_id"], g.account["user_id"], params["last_id"]) else: if params["offset"] == 0 and params["last_id"] == 0: params["offset"] = int( PrivateLibraryHandler.random_private_library_offset( params["type"], params["user_id"])) result["result_data"] = VideoModel.query_random_private_video( params["user_id"], g.account["user_id"], params["last_id"], params["offset"]) result["want_buy"] = RedisModel.query_new_message( params["user_id"], RedisModel.private_video_want) return json_success_response(result)
def upload_private_images(img_list, user_id): img_model_list = list() for img_dict in img_list: img_model = img_dict.get("image", None) if not img_model: continue img_model.user_id = user_id img_model.type = 11 try: db.session.commit() except: continue img_model_list.append(img_model) return ImageModel.format_private_image_model(img_model_list, user_id, user_id)
def format_private_video_list(video_list, user_id, login_user_id): """ 格式化私密视频列表 :param video_list: 原始私密视频模型 :param user_id: 私密视频所属用户id :param login_user_id: 登录用户id :return: 格式化后的私密视频列表 """ image_model_list = ImageModel.query_image_by_image_id_list( array_column(video_list, "cover_id")) image_model_dict = array_column_key(image_model_list, "image_id") video_id_list = array_column(video_list, "video_id") video_meta_list = VideoMetaModel.query_video_meta_list(video_id_list) video_meta_dict = array_column_key(video_meta_list, "video_id") video_pay_dict = dict() if user_id != login_user_id: video_pay_list = OrderVideoModel.query_order_video_list( login_user_id, video_id_list) video_pay_dict = array_column_key(video_pay_list, "video_id") result = [] for video_model in video_list: image_model = image_model_dict.get(video_model.cover_id, None) video_meta_model = video_meta_dict.get(video_model.video_id, None) if user_id == login_user_id: video_pay = 1 else: order_video = video_pay_dict.get(video_model.video_id, None) video_pay = 1 if order_video else 0 item = VideoModel.format_private_video_model( video_model, image_model, video_meta_model, video_pay) if item: result.append(item) return result
def post(self): upload_video = UploadVideo() upload_image = UploadImage() if not upload_video.videos: return json_fail_response(2202) if not upload_image.images: return json_fail_response(2203) upload_video.save_videos() upload_image.save_images() img_model = upload_image.images[0]["image"] video_model = upload_video.videos[0]["video"] video_model.cover_id = img_model.image_id db.session.commit() video_info = VideoModel.format_video_info(video_model) video_info["video_id"] = video_model.video_id video_info["tiny_img"] = ImageModel.generate_image_url(img_model, "e") return json_success_response(video_info)
def save_image(self, img_info): image = ImageModel() image.image_width = img_info["width"] image.image_height = img_info["height"] image.hash_key = img_info["hash_key"] image.file_name = generate_image_file() image.file_ext = "jpg" image.mime_type = img_info["mime_type"] image.file_size = img_info["file_size"] image.status = 1 pil_img = img_info["pil_image"] if image.mime_type in ("RGBA", "LA"): background = Image.new(pil_img.mode[:-1], pil_img.size, "#ffffff") background.paste(pil_img, pil_img.split()[-1]) pil_img = background from heron import app prefix_path = app.config["UPLOAD_IMG_PATH"] for size in self.image_size: abs_path = UploadImage.generate_image_file_path( image.hash_key, size) img = pil_img.copy() full_path = os.path.join(prefix_path, abs_path) # 原图 if size == 'o': image.image_o = abs_path img.save(full_path, "jpeg", quality=90) # 小头像 100*100 elif size == 'a': image.image_a = abs_path img.thumbnail((100, 100)) img.save(full_path, "jpeg", quality=90) # 大头像 240*240 elif size == 'b': image.image_b = abs_path img.thumbnail((240, 240)) img.save(full_path, "jpeg", quality=90) # 小方图 elif size == 'c': image.image_c = abs_path img.thumbnail((400, 400)) img.save(full_path, "jpeg", quality=90) # 大方图 elif size == 'd': image.image_d = abs_path img.thumbnail((800, 800)) img.save(full_path, "jpeg", quality=90) # 等比缩放 最大高度600 elif size == 'e': image.image_e = abs_path img.thumbnail((600, 600)) img.save(full_path, "jpeg", quality=90) # 等比例缩放 最大宽高800 elif size == 'f': image.image_f = abs_path img.thumbnail((800, 800)) img.save(full_path, "jpeg", quality=90) # 等比缩放 最大宽高1000 elif size == 'g': image.image_g = abs_path img.thumbnail((1000, 1000)) # TODO: 添加水印 img.save(full_path, "jpeg", quality=90) elif size == 'x': image.image_x = abs_path img = img.filter(ImageFilter.BLUR) img = img.filter(ImageFilter.GaussianBlur(radius=4)) img = img.filter(ImageFilter.MedianFilter) img.save(full_path, "jpeg") # 添加对象到数据库session中 db.session.add(image) img_info["image"] = image
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