async def post(self): try: libs.validator.space_id(self.post_data.get("space_id")) libs.validator.account_id(self.post_data.get("account_id")) except libs.validator.ValidationError as e: self.send_to_client(-1, message=e.__str__()) return growth_list_key = 'growth_list:{}'.format( zbase62.decode(self.post_data.get('space_id'))) growth_list = rd.redis_get(growth_list_key) growth_list = json.loads(str(growth_list, encoding="utf-8")) if isinstance( growth_list, bytes) else growth_list if not growth_list: pb_growth_list = await RecordServer().get_growth_list( account_id=zbase62.decode(self.post_data.get("account_id")), space_id=zbase62.decode(self.post_data.get('space_id')), creator_id=self.current_user.account_id, request_id=self.request_id) if pb_growth_list: growth_list = [] for growth in pb_growth_list.growth_list: growth_list.append(GrowthDetail(growth).growth_detail) rd.redis_set(growth_list_key, json.dumps(growth_list), Config().redis.get("expire_list")) if growth_list is not None: self.send_to_client(0, message='成功获取生长记录详情', response=growth_list) return raise HTTPError(500, "get growth list failed", reason="服务器出错,请联系客服解决")
async def post(self): try: libs.validator.space_id(self.post_data.get('space_id')) libs.validator.vaccine_id(self.post_data.get('vaccine_id')) libs.validator.record_at(self.post_data.get('date_of_inoculation')) libs.validator.injection_no(self.post_data.get('injection_no')) except libs.validator.ValidationError as e: self.send_to_client(-1, message=e.__str__()) return today = datetime.datetime.now().date() date = datetime.datetime.strptime(self.post_data.get("date_of_inoculation"), "%Y-%m-%d").date() if date < today: self.send_to_client(-1, message='计划接种日期应大于当前日期') return pb_space_detail = await SpaceServer().get_space_details(space_id=zbase62.decode(self.post_data.get('space_id')), request_id=self.request_id) if pb_space_detail: result = await VaccineServer().update_plan_inject(account_id=pb_space_detail.owner_id, vaccine_id=self.post_data.get("vaccine_id"), injection_no=self.post_data.get("injection_no"), date_of_inoculation=self.post_data.get( "date_of_inoculation"), request_id=self.request_id, space_id=zbase62.decode(self.post_data.get('space_id')), creator_id=self.current_user.account_id) rd.redis_del('injection_detail:{}'.format(zbase62.decode(self.post_data.get('space_id')))) pb_detail_list = await VaccineServer().get_vaccine_detail_list(request_id=self.request_id) pb_injection_log = await VaccineServer().get_injection_details(account_id=pb_space_detail.owner_id, request_id=self.request_id, space_id=zbase62.decode( self.post_data.get('space_id')), creator_id=self.current_user.account_id) inject_dict = {} if pb_injection_log: for inject in pb_injection_log.inject_detail: i = InjectDetail(inject).inject_detail i["date_of_inoculation"] = time.strftime("%Y-%m-%d", time.localtime(inject.date_of_inoculation.seconds)) inject_dict[(i.get("vaccine_id"), i.get("injection_no"))] = i detail_list = [] if pb_detail_list: for vaccine in pb_detail_list.vaccines: v = Vaccine(vaccine).vaccine vd = inject_dict.get((v.get("vaccine_id"), v.get("injection_no"))) v['is_injected'] = vd.get("is_injected") if vd else None v['date_of_inoculation'] = vd.get("date_of_inoculation") if vd else None detail_list.append(v) if result: self.send_to_client(0, message='设置成功', response=detail_list) return raise HTTPError(500, "update plan inject failed", reason="设置失败,请联系客服解决") raise HTTPError(500, "get space failed", reason="服务器出错,请联系客服解决")
async def post(self): if (self.post_data.get("height") or self.post_data.get("weight") or self.post_data.get("head_circumference")) is None: self.send_to_client(-1, message='请至少输入一项数据') return try: libs.validator.space_id(self.post_data.get("space_id")) libs.validator.growth_id(self.post_data.get("growth_id")) libs.validator.record_at(self.post_data.get("record_at")) if self.post_data.get("height"): libs.validator.height(float(self.post_data.get("height"))) if self.post_data.get("weight"): libs.validator.weight(float(self.post_data.get("weight"))) if self.post_data.get("head_circumference"): libs.validator.head_circumference( float(self.post_data.get("head_circumference"))) except libs.validator.ValidationError as e: self.send_to_client(-1, message=e.__str__()) return record_at = arrow.get(self.post_data.get('record_at')) record_at_utc = record_at.utcfromtimestamp( record_at.timestamp).datetime if bool(self.post_data.get("height") and self.post_data.get("weight")): # bmi值异常检测 if self.post_data.get("weight") / (self.post_data.get("height") * self.post_data.get("height") / 10000) > 60: self.send_to_client(-1, message='bmi值异常, 请正确输入宝宝的体重身高信息') return is_done, message = await RecordServer().update_record( growth_id=self.post_data.get("growth_id"), height=self.post_data.get("height"), weight=self.post_data.get("weight"), head_circumference=self.post_data.get("head_circumference"), record_at=record_at_utc, space_id=zbase62.decode(self.post_data.get('space_id')), creator_id=self.current_user.account_id, request_id=self.request_id) if is_done: rd.redis_del('growth_list:{}'.format( zbase62.decode(self.post_data.get('space_id')))) self.send_to_client(0, message='修改成功') else: self.send_to_client( -1, message=message if message else '服务器出错,请联系客服解决')
async def post(self): try: validator.msg_title(self.post_data.get('msg_title')) validator.word(self.post_data.get('msg_content')) validator.account_id(self.post_data.get('from_id')) except validator.ValidationError as e: self.send_to_client(-1, message=e.__str__()) return group_message_id = await ZIDServer().generate(self.request_id) if group_message_id is None: raise HTTPError(500, "zid-server generate failed", reason="服务器出错,请联系客服解决") result = await MsgBoxServer().create_group_message( group_message_id=group_message_id, product_id=Config().product, msg_title=self.post_data.get("msg_title"), msg_content=self.post_data.get("msg_content"), from_id=zbase62.decode(self.post_data.get("from_id")), request_id=self.request_id) if result: self.send_to_client(-1, '消息发送失败!') return self.send_to_client(0, '消息发送成功!') return
async def post(self): # todo: 接种时间逻辑性校验 try: libs.validator.space_id(self.post_data.get('space_id')) except libs.validator.ValidationError as e: self.send_to_client(-1, message=e.__str__()) return injection_detail_key = 'injection_detail:{}'.format(zbase62.decode(self.post_data.get('space_id'))) injection_detail = rd.redis_get(injection_detail_key) injection_detail = json.loads(str(injection_detail, encoding="utf-8")) if isinstance(injection_detail, bytes) else injection_detail if not injection_detail: pb_space_detail = await SpaceServer().get_space_details( space_id=zbase62.decode(self.post_data.get('space_id')), request_id=self.request_id) if pb_space_detail: pb_detail_list = await VaccineServer().get_vaccine_detail_list(request_id=self.request_id) pb_injection_log = await VaccineServer().get_injection_details(account_id=pb_space_detail.owner_id, request_id=self.request_id, space_id=zbase62.decode( self.post_data.get('space_id')), creator_id=self.current_user.account_id) inject_dict = {} if pb_injection_log: for inject in pb_injection_log.inject_detail: i = InjectDetail(inject).inject_detail i["date_of_inoculation"] = time.strftime("%Y-%m-%d", time.localtime(inject.date_of_inoculation.seconds)) inject_dict[(i.get("vaccine_id"), i.get("injection_no"))] = i if pb_detail_list: injection_detail = [] for vaccine in pb_detail_list.vaccines: v = Vaccine(vaccine).vaccine vd = inject_dict.get((v.get("vaccine_id"), v.get("injection_no"))) v['is_injected'] = vd.get("is_injected") if vd else None v['date_of_inoculation'] = vd.get("date_of_inoculation") if vd else None injection_detail.append(v) rd.redis_set(injection_detail_key, json.dumps(injection_detail), Config().redis.get("expire_list")) if injection_detail is not None: self.send_to_client(0, message='成功获取接种详情', response=injection_detail) return raise HTTPError(500, "get space failed", reason="服务器出错,请联系客服解决")
async def post(self): try: validator.account_id(self.post_data.get('from_id')) validator.message_id(self.post_data.get('message_id')) except validator.ValidationError as e: self.send_to_client(-1, message=e.__str__()) return result = await MsgBoxServer().set_message_as_unread( message_id=zbase62.decode(self.post_data.get("message_id")), to_id=zbase62.decode(self.post_data.get("from_id")), request_id=self.request_id) rd.redis_del('message_center_list:{}'.format( zbase62.decode(self.post_data.get("from_id")))) if result: self.send_to_client(-1, '设置消息未读失败!') return self.send_to_client(0, '设置消息未读成功!') return
async def post(self): try: libs.validator.space_id(self.post_data.get("space_id")) libs.validator.growth_id(self.post_data.get("growth_id")) except libs.validator.ValidationError as e: self.send_to_client(-1, message=e.__str__()) return is_done, message = await RecordServer().delete_record( growth_id=self.post_data.get("growth_id"), space_id=zbase62.decode(self.post_data.get('space_id')), creator_id=self.current_user.account_id, request_id=self.request_id) if is_done: rd.redis_del('growth_list:{}'.format( zbase62.decode(self.post_data.get('space_id')))) self.send_to_client(0, message='删除成功') else: self.send_to_client( -1, message=message if message else '服务器出错,请联系客服解决')
async def post(self): try: validator.account_id(self.post_data.get('from_id')) validator.space_id(self.post_data.get('space_id')) validator.msgtype(self.post_data.get('msg_type')) except validator.ValidationError as e: self.send_to_client(-1, message=e.__str__()) return result = await MsgBoxServer().set_all_message_as_read( product_id=Config().product, to_id=zbase62.decode(self.post_data.get("from_id")), msg_type=self.post_data.get("msg_type"), space_id=zbase62.decode(self.post_data.get("space_id")), request_id=self.request_id) rd.redis_del('message_center_list:{}'.format( zbase62.decode(self.post_data.get("from_id")))) if result: self.send_to_client(-1, '设置所有消息已读失败!') return self.send_to_client(0, '设置所有消息已读成功!') return
async def post(self): if (self.post_data.get("height") or self.post_data.get("weight") or self.post_data.get("head_circumference")) is None: self.send_to_client(-1, message='请至少输入一项数据') return try: libs.validator.space_id(self.post_data.get("space_id")) libs.validator.record_at(self.post_data.get("record_at")) if self.post_data.get("height"): libs.validator.height(float(self.post_data.get("height"))) if self.post_data.get("weight"): libs.validator.weight(float(self.post_data.get("weight"))) if self.post_data.get("head_circumference"): libs.validator.head_circumference( float(self.post_data.get("head_circumference"))) except libs.validator.ValidationError as e: self.send_to_client(-1, message=e.__str__()) return record_at = arrow.get(self.post_data.get('record_at')) record_at_utc = record_at.utcfromtimestamp( record_at.timestamp).datetime record_at_timestamp = record_at.timestamp if record_at_timestamp > time.time(): self.send_to_client(-1, message='记录日期有误') return if self.post_data.get("height") and self.post_data.get("weight"): # bmi值异常检测 if self.post_data.get("weight") / (self.post_data.get("height") * self.post_data.get("height") / 10000) > 60: self.send_to_client(-1, message='bmi值异常, 请正确输入宝宝的体重身高信息') return is_done, message = await RecordServer().add_record( account_id=zbase62.decode(self.post_data.get("account_id")), height=self.post_data.get("height"), weight=self.post_data.get("weight"), head_circumference=self.post_data.get("head_circumference"), record_at=record_at_utc, space_id=zbase62.decode(self.post_data.get('space_id')), creator_id=self.current_user.account_id, request_id=self.request_id) if is_done: rd.redis_del('growth_list:{}'.format( zbase62.decode(self.post_data.get('space_id')))) # to feed word_list = [] if self.post_data.get("weight") is not None: word_list.append("体重:{}KG".format( self.post_data.get("weight"))) if self.post_data.get("height") is not None: word_list.append("身高:{}CM".format( self.post_data.get("height"))) if self.post_data.get("head_circumference") is not None: word_list.append("头围:{}CM".format( self.post_data.get("head_circumference"))) feed_id = await ZIDServer().generate(self.request_id) if feed_id is None: raise HTTPError(500, "zid-server generate failed", reason="服务器出错,请联系客服解决") await FeedServer().create_feed( feed_id=feed_id, space_id=[zbase62.decode(self.post_data.get('space_id'))], # privacy=self.post_data.get('privacy'), #todo: 第一版本先不加权限 record_at=record_at_utc, creator_id=self.current_user.account_id, word='\n'.join(word_list), feed_type=pbfeed_pb2.FEED_TYPE_GROWTH, feed_size=0, request_id=self.request_id) self.send_to_client(0, message='添加成功') else: self.send_to_client( -1, message=message if message else '服务器出错,请联系客服解决')
async def post(self): try: validator.space_id(self.post_data.get('space_id')) validator.msgtype(self.post_data.get('msg_type')) except validator.ValidationError as e: self.send_to_client(-1, message=e.__str__()) return message_list = [] type_name = {0: "日记", 1: "照片", 2: "视频", 3: "音频", 4: "故事", 5: "生长发育"} pbmsg = await MsgBoxServer().query_message( product_id=Config().product, msg_type=self.post_data.get("msg_type"), space_id=zbase62.decode(self.post_data.get("space_id")), to_id=self.current_user.account_id, from_message_id=zbase62.decode( self.post_data.get("from_message_id")) if self.post_data.get("from_message_id") else (1 << 63) - 1, per_page=Config().msgbox.get("per_page"), request_id=self.request_id) avatar = "" if pbmsg: for msg in MsgBox(pbmsg).msgbox.get("messages"): del msg["product_id"], msg["to_id"], msg["from_id"], msg[ "space_id"], msg["message_id"] = zbase62.encode(int(msg.get("message_id"))) feed_id = msg.get("message_body").get("msg_content").get( "feed_id") feed_type = None if msg.get("message_body").get("msg_content").get("feed_id"): msg['message_body']['msg_content'][ 'feed_id'] = zbase62.encode( int( msg.get("message_body").get("msg_content").get( "feed_id"))) feed_type = await FeedServer().get_feed_type( feed_id=int(feed_id), request_id=self.request_id) title = "" content = "" snapshot = {} if msg.get("message_body").get("msg_title") == "post": pb_space_detail = await SpaceServer().get_space_details( space_id=zbase62.decode( self.post_data.get("space_id")), request_id=self.request_id) owner_userinfo = await AccountServer().get_userinfo( account_id=pb_space_detail.owner_id, app_id=self.app_id_int64, request_id=self.request_id) title = "{} 有新{}".format(owner_userinfo.nickname, type_name.get(feed_type)) avatar_name = owner_userinfo.avatar if avatar_name: avatar, error = await backend.backend_user_media.get_public_url( avatar_name) else: avatar = "" feed = await FeedServer().get_feed( feed_id=int(feed_id), space_id=zbase62.decode( self.post_data.get("space_id")), account_id=self.current_user.account_id, request_id=self.request_id) content = Feed(feed).feed.get("word") if Feed( feed).feed.get("word") else "" msg["feed_id"] = msg["message_body"]["msg_content"][ "feed_id"] if Feed(feed).feed.get("feed_type") == 1: snapshot = { "url": Feed(feed).feed.get("files")[0]["url"], "width": Feed(feed).feed.get("files")[0]["width"], "height": Feed(feed).feed.get("files")[0]["height"] } if Feed(feed).feed.get("feed_type") == 2: snapshot = { "url": Feed(feed).feed.get("files")[0]["cover_image_url"], "width": Feed(feed).feed.get("files")[0]["width"], "height": Feed(feed).feed.get("files")[0]["height"] } if msg.get("message_body").get("msg_title") == "comment": comment = await FeedServer().get_comment( comment_id=int( msg.get("message_body").get("msg_content").get( "comment_id")), space_id=zbase62.decode( self.post_data.get("space_id")), account_id=self.current_user.account_id, request_id=self.request_id) if comment.word is "": continue commenter_userinfo = await AccountServer().get_userinfo( account_id=comment.author, app_id=self.app_id_int64, request_id=self.request_id) title = "{} 发表了新评论".format(commenter_userinfo.nickname) content = comment.word avatar_name = commenter_userinfo.avatar if avatar_name: avatar, error = await backend.backend_user_media.get_public_url( avatar_name) else: avatar = "" if msg.get("message_body").get("msg_title") == "like": liker_userinfo = await AccountServer().get_userinfo( account_id=int( msg.get("message_body").get("msg_content").get( "like_id")), app_id=self.app_id_int64, request_id=self.request_id) title = "{} 赞了一下".format(liker_userinfo.nickname) avatar_name = liker_userinfo.avatar if avatar_name: avatar, error = await backend.backend_user_media.get_public_url( avatar_name) else: avatar = "" content = "" if msg.get("message_body").get("msg_title") == "follow": pb_space_detail = await SpaceServer().get_space_details( space_id=zbase62.decode( self.post_data.get("space_id")), request_id=self.request_id) owner_userinfo = await AccountServer().get_userinfo( account_id=pb_space_detail.owner_id, app_id=self.app_id_int64, request_id=self.request_id) follower_userinfo = await AccountServer().get_userinfo( account_id=int( msg.get("message_body").get("msg_content").get( "follower_id")), app_id=self.app_id_int64, request_id=self.request_id) title = "{} 有一个新粉丝了".format(owner_userinfo.nickname) avatar_name = owner_userinfo.avatar if avatar_name: avatar, error = await backend.backend_user_media.get_public_url( avatar_name) else: avatar = "" content = "{} 关注了{}".format(follower_userinfo.nickname, owner_userinfo.nickname) if msg.get("message_body").get("msg_content").get( "comment_id"): msg['message_body']['msg_content'][ 'comment_id'] = zbase62.encode( int( msg.get("message_body").get("msg_content").get( "comment_id"))) msg["msg_content"] = msg["message_body"]["msg_content"] # system message if msg["msg_type"] == 4: title = msg.get("message_body").get( "msg_title") if msg.get("message_body").get( "msg_title") else "" content = msg.get("message_body").get("msg_content").get( "body") if msg.get("message_body").get( "msg_content").get("body") else "" msg["type"] = msg["message_body"]["msg_content"]["type"] if msg["msg_type"] == 3: msg["action"] = msg["message_body"]["msg_content"][ "action"] msg["snapshot"] = snapshot del msg["message_body"] del msg["msg_content"] message_list.append({ "title": title, "content": content, "from_id": zbase62.encode(self.current_user.account_id), "space_id": self.post_data.get("space_id"), "avatar": avatar, "msg": msg, }) self.send_to_client(0, '查询消息成功!', response={ "message_list": message_list, "is_last_page": MsgBox(pbmsg).msgbox.get("is_last_page") }) return else: self.send_to_client(-1, '查询消息失败!') return
async def post(self): message_center_list = [] type_name = { 0: "日记", 1: "照片", 2: "视频", 3: "音频", 4: "故事", 5: "生长发育" } # feed 类型 system_pbmsg = await MsgBoxServer().query_message( product_id=Config().product, to_id=self.current_user.account_id, per_page=1, space_id=0, request_id=self.request_id, msg_type=Config().msgbox.get("msg_type").get("system"), ) if system_pbmsg: if MsgBox(system_pbmsg).msgbox.get("messages"): unread = await MsgBoxServer().get_unread_count( product_id=Config().product, msg_type=Config().msgbox.get("msg_type").get("system"), to_id=self.current_user.account_id, request_id=self.request_id) last_message = MsgBox(system_pbmsg).msgbox.get("messages")[-1] message_center_list.append({ "title": "系统消息", "space_id": zbase62.encode(0), "avatar": "", "unread": unread, "from_id": zbase62.encode(self.current_user.account_id), "latest_message": { "msg_type": Config().msgbox.get("msg_type").get("system"), "content": last_message.get("message_body").get( "msg_content").get("body") if last_message.get("message_body").get("msg_content") else "", "create_at": last_message.get("create_at"), "message_id": zbase62.encode(int(last_message.get("message_id"))) } }) # 获取所有关注的space空间 pb_space = await SpaceServer().get_all_space( follower_id=self.current_user.account_id, request_id=self.request_id) for space in pb_space.space_details: if space.status == 0: pb_userinfo = await AccountServer().get_userinfo( account_id=space.owner_id, app_id=self.app_id_int64, request_id=self.request_id) title = "{}的动态".format(pb_userinfo.nickname) userinfo = UserInfo(pb_userinfo) avatar_name = userinfo.__dict__.get('userinfo').get('avatar') if avatar_name: baby_avatar, error = await backend.backend_user_media.get_public_url( avatar_name) else: baby_avatar = "" unread = 0 # 未读消息数 pbmsg = await MsgBoxServer().query_message( product_id=Config().product, to_id=self.current_user.account_id, per_page=1, request_id=self.request_id, msg_type=Config().msgbox.get("msg_type").get("feed"), space_id=space.space_id) if pbmsg: if MsgBox(pbmsg).msgbox.get("messages"): last_message = MsgBox(pbmsg).msgbox.get("messages")[0] last_message['message_id'] = zbase62.encode( int(last_message.get("message_id"))) del last_message['product_id'], last_message[ 'space_id'] feed_type = None if last_message.get("message_body").get( "msg_content").get("feed_id"): feed_id = MsgBox(pbmsg).msgbox.get( "messages")[-1].get("message_body").get( "msg_content").get("feed_id") last_message['message_body']['msg_content'][ 'feed_id'] = zbase62.encode( int( last_message.get("message_body").get( "msg_content").get("feed_id"))) feed_type = await FeedServer().get_feed_type( feed_id=int(feed_id), request_id=self.request_id) content = "" if last_message.get("message_body").get( "msg_title") == "post": content = "{} 有新{}".format(pb_userinfo.nickname, type_name[feed_type]) if last_message.get("message_body").get( "msg_title") == "comment": comment = await FeedServer().get_comment( comment_id=int( last_message.get("message_body").get( "msg_content").get("comment_id")), space_id=space.space_id, account_id=self.current_user.account_id, request_id=self.request_id) commenter_userinfo = await AccountServer( ).get_userinfo(account_id=comment.author, app_id=self.app_id_int64, request_id=self.request_id) content = "{} 发表了评论".format( commenter_userinfo.nickname) if last_message.get("message_body").get( "msg_title") == "like": liker_userinfo = await AccountServer( ).get_userinfo(account_id=int( last_message.get("message_body").get( "msg_content").get("like_id")), app_id=self.app_id_int64, request_id=self.request_id) content = "{} 赞了一下{}的{}".format( liker_userinfo.nickname, pb_userinfo.nickname, type_name[feed_type]) if last_message.get("message_body").get( "msg_title") == "follow": follower_userinfo = await AccountServer( ).get_userinfo(account_id=int( last_message.get("message_body").get( "msg_content").get("follower_id")), app_id=self.app_id_int64, request_id=self.request_id) content = "{} 关注了{}".format( follower_userinfo.nickname, pb_userinfo.nickname) if last_message.get("message_body").get( "msg_content").get("comment_id"): last_message['message_body']['msg_content'][ 'comment_id'] = zbase62.encode( int( last_message.get("message_body").get( "msg_content").get("comment_id"))) unread = await MsgBoxServer().get_unread_count( product_id=Config().product, msg_type=Config().msgbox.get("msg_type").get( "feed"), space_id=space.space_id, to_id=self.current_user.account_id, request_id=self.request_id) message_center_list.append({ "title": title, "space_id": zbase62.encode(space.space_id), "avatar": baby_avatar, "unread": unread, "from_id": zbase62.encode(self.current_user.account_id), "latest_message": { "content": content, "create_at": last_message.get("create_at"), "message_id": last_message.get("message_id"), "msg_type": last_message.get("msg_type") } }) if message_center_list is not None: self.send_to_client(0, '获取消息成功', response=sorted( message_center_list, key=lambda x: zbase62.decode(x[ 'latest_message']['message_id']), reverse=True)) else: self.send_to_client(-1, '获取消息失败')