Compare commits
1 Commits
18f0083827
...
v0.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 8174c4cfe5 |
@@ -21,7 +21,7 @@ class ExtractPeopleAgent(BaseAgent):
|
|||||||
"身高(cm) height\n"
|
"身高(cm) height\n"
|
||||||
"婚姻状况 marital_status\n"
|
"婚姻状况 marital_status\n"
|
||||||
"择偶要求 match_requirement\n"
|
"择偶要求 match_requirement\n"
|
||||||
"以上信息需要严格按照 JSON 格式输出 字段名与条目中英文保持一致; 若未识别到以上的某项,则不返回该字段,不要自行填写“未知”,“未填写”等类似字眼。\n"
|
"以上信息需要严格按照 JSON 格式输出 字段名与条目中英文保持一致。\n"
|
||||||
"其中,'年龄 age' 和 '身高(cm) height' 必须是一个整数,不能是一个字符串;\n"
|
"其中,'年龄 age' 和 '身高(cm) height' 必须是一个整数,不能是一个字符串;\n"
|
||||||
"并且,'性别 gender' 根据识别结果,必须从 男,女,未知 三选一填写。\n"
|
"并且,'性别 gender' 根据识别结果,必须从 男,女,未知 三选一填写。\n"
|
||||||
"除了上述基本信息,还有一个字段\n"
|
"除了上述基本信息,还有一个字段\n"
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from datetime import datetime
|
|
||||||
from sqlalchemy import Column, Integer, String, Text, DateTime, func
|
from sqlalchemy import Column, Integer, String, Text, DateTime, func
|
||||||
from utils.rldb import RLDBBaseModel
|
from utils.rldb import RLDBBaseModel
|
||||||
from utils.error import ErrorCode, error
|
from utils.error import ErrorCode, error
|
||||||
@@ -27,37 +26,6 @@ class PeopleRLDBModel(RLDBBaseModel):
|
|||||||
deleted_at = Column(DateTime(timezone=True), nullable=True, index=True)
|
deleted_at = Column(DateTime(timezone=True), nullable=True, index=True)
|
||||||
|
|
||||||
|
|
||||||
class Comment:
|
|
||||||
# 评论内容
|
|
||||||
content: str
|
|
||||||
# 评论人
|
|
||||||
author: str
|
|
||||||
# 创建时间
|
|
||||||
created_at: datetime
|
|
||||||
# 更新时间
|
|
||||||
updated_at: datetime
|
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
|
||||||
self.content = kwargs.get('content', '')
|
|
||||||
self.author = kwargs.get('author', '')
|
|
||||||
self.created_at = kwargs.get('created_at', datetime.now())
|
|
||||||
self.updated_at = kwargs.get('updated_at', datetime.now())
|
|
||||||
|
|
||||||
def to_dict(self) -> dict:
|
|
||||||
return {
|
|
||||||
'content': self.content,
|
|
||||||
'author': self.author,
|
|
||||||
'created_at': int(self.created_at.timestamp()),
|
|
||||||
'updated_at': int(self.updated_at.timestamp()),
|
|
||||||
}
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_dict(cls, data: dict):
|
|
||||||
data['created_at'] = datetime.fromtimestamp(data['created_at'])
|
|
||||||
data['updated_at'] = datetime.fromtimestamp(data['updated_at'])
|
|
||||||
return cls(**data)
|
|
||||||
|
|
||||||
|
|
||||||
class People:
|
class People:
|
||||||
# 数据库 ID
|
# 数据库 ID
|
||||||
id: str
|
id: str
|
||||||
@@ -78,11 +46,9 @@ class People:
|
|||||||
# 个人介绍
|
# 个人介绍
|
||||||
introduction: Dict[str, str]
|
introduction: Dict[str, str]
|
||||||
# 总结评价
|
# 总结评价
|
||||||
comments: Dict[str, "Comment"]
|
comments: Dict[str, str]
|
||||||
# 封面
|
# 封面
|
||||||
cover: str = None
|
cover: str = None
|
||||||
# 创建时间
|
|
||||||
created_at: datetime = None
|
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
# 初始化所有属性,从kwargs中获取值,如果不存在则设置默认值
|
# 初始化所有属性,从kwargs中获取值,如果不存在则设置默认值
|
||||||
@@ -97,17 +63,19 @@ class People:
|
|||||||
self.introduction = kwargs.get('introduction', {}) if kwargs.get('introduction', {}) is not None else {}
|
self.introduction = kwargs.get('introduction', {}) if kwargs.get('introduction', {}) is not None else {}
|
||||||
self.comments = kwargs.get('comments', {}) if kwargs.get('comments', {}) is not None else {}
|
self.comments = kwargs.get('comments', {}) if kwargs.get('comments', {}) is not None else {}
|
||||||
self.cover = kwargs.get('cover', None) if kwargs.get('cover', None) is not None else None
|
self.cover = kwargs.get('cover', None) if kwargs.get('cover', None) is not None else None
|
||||||
self.created_at = kwargs.get('created_at', None)
|
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
# 返回对象的字符串表示,包含所有属性
|
# 返回对象的字符串表示,包含所有属性
|
||||||
return (f"People(id={self.id}, name={self.name}, contact={self.contact}, gender={self.gender}, "
|
return (f"People(id={self.id}, name={self.name}, contact={self.contact}, gender={self.gender}, "
|
||||||
f"age={self.age}, height={self.height}, marital_status={self.marital_status}, "
|
f"age={self.age}, height={self.height}, marital_status={self.marital_status}, "
|
||||||
f"match_requirement={self.match_requirement}, introduction={self.introduction}, "
|
f"match_requirement={self.match_requirement}, introduction={self.introduction}, "
|
||||||
f"comments={self.comments}, cover={self.cover}, created_at={self.created_at})")
|
f"comments={self.comments}, cover={self.cover})")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_dict(cls, data: dict):
|
def from_dict(cls, data: dict):
|
||||||
|
if 'created_at' in data:
|
||||||
|
# 移除 created_at 字段,避免类型错误
|
||||||
|
del data['created_at']
|
||||||
if 'updated_at' in data:
|
if 'updated_at' in data:
|
||||||
# 移除 updated_at 字段,避免类型错误
|
# 移除 updated_at 字段,避免类型错误
|
||||||
del data['updated_at']
|
del data['updated_at']
|
||||||
@@ -129,9 +97,8 @@ class People:
|
|||||||
marital_status=data.marital_status,
|
marital_status=data.marital_status,
|
||||||
match_requirement=data.match_requirement,
|
match_requirement=data.match_requirement,
|
||||||
introduction=json.loads(data.introduction) if data.introduction else {},
|
introduction=json.loads(data.introduction) if data.introduction else {},
|
||||||
comments={k: Comment.from_dict(v) for k, v in json.loads(data.comments).items()} if data.comments else {},
|
comments=json.loads(data.comments) if data.comments else {},
|
||||||
cover=data.cover,
|
cover=data.cover,
|
||||||
created_at=data.created_at,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def to_dict(self) -> dict:
|
def to_dict(self) -> dict:
|
||||||
@@ -146,9 +113,8 @@ class People:
|
|||||||
'marital_status': self.marital_status,
|
'marital_status': self.marital_status,
|
||||||
'match_requirement': self.match_requirement,
|
'match_requirement': self.match_requirement,
|
||||||
'introduction': self.introduction,
|
'introduction': self.introduction,
|
||||||
'comments': {k: v.to_dict() for k, v in self.comments.items()},
|
'comments': self.comments,
|
||||||
'cover': self.cover,
|
'cover': self.cover,
|
||||||
'created_at': int(self.created_at.timestamp()) if self.created_at else None,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def to_rldb_model(self) -> PeopleRLDBModel:
|
def to_rldb_model(self) -> PeopleRLDBModel:
|
||||||
@@ -163,22 +129,22 @@ class People:
|
|||||||
marital_status=self.marital_status,
|
marital_status=self.marital_status,
|
||||||
match_requirement=self.match_requirement,
|
match_requirement=self.match_requirement,
|
||||||
introduction=json.dumps(self.introduction, ensure_ascii=False),
|
introduction=json.dumps(self.introduction, ensure_ascii=False),
|
||||||
comments=json.dumps({k: v.to_dict() for k, v in self.comments.items()}, ensure_ascii=False),
|
comments=json.dumps(self.comments, ensure_ascii=False),
|
||||||
cover=self.cover,
|
cover=self.cover,
|
||||||
)
|
)
|
||||||
|
|
||||||
def validate(self) -> error:
|
def validate(self) -> error:
|
||||||
err = error(ErrorCode.SUCCESS, "")
|
err = error(ErrorCode.SUCCESS, "")
|
||||||
if not self.name:
|
if not self.name:
|
||||||
logging.error("Name is required, use default")
|
logging.error("Name is required")
|
||||||
self.name = ""
|
err = error(ErrorCode.MODEL_ERROR, "Name is required")
|
||||||
if not self.gender in ['男', '女', '未知']:
|
if not self.gender in ['男', '女', '未知']:
|
||||||
logging.error("Gender must be '男', '女', or '未知', use default")
|
logging.error("Gender must be '男', '女', or '未知'")
|
||||||
self.gender = "未知"
|
err = error(ErrorCode.MODEL_ERROR, "Gender must be '男', '女', or '未知'")
|
||||||
if not isinstance(self.age, int) or self.age < 0:
|
if not isinstance(self.age, int) or self.age <= 0:
|
||||||
logging.error("Age must be an integer and greater than 0, use default")
|
logging.error("Age must be an integer and greater than 0")
|
||||||
self.age = 0
|
err = error(ErrorCode.MODEL_ERROR, "Age must be an integer and greater than 0")
|
||||||
if not isinstance(self.height, int) or self.height < 0:
|
if not isinstance(self.height, int) or self.height <= 0:
|
||||||
logging.error("Height must be an integer and greater than 0, use default")
|
logging.error("Height must be an integer and greater than 0")
|
||||||
self.height = 0
|
err = error(ErrorCode.MODEL_ERROR, "Height must be an integer and greater than 0")
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
import logging
|
|
||||||
import uuid
|
import uuid
|
||||||
from models.people import People, PeopleRLDBModel, Comment
|
from models.people import People, PeopleRLDBModel
|
||||||
from datetime import datetime
|
|
||||||
from utils.error import ErrorCode, error
|
from utils.error import ErrorCode, error
|
||||||
from utils import rldb
|
from utils import rldb
|
||||||
|
|
||||||
@@ -68,50 +66,6 @@ class PeopleService:
|
|||||||
|
|
||||||
return peoples, error(ErrorCode.SUCCESS, "")
|
return peoples, error(ErrorCode.SUCCESS, "")
|
||||||
|
|
||||||
def save_remark(self, people_id: str, content: str) -> error:
|
|
||||||
"""
|
|
||||||
为人物添加或更新备注
|
|
||||||
|
|
||||||
:param people_id: 人物ID
|
|
||||||
:param content: 备注内容
|
|
||||||
:return: 错误对象
|
|
||||||
"""
|
|
||||||
people: People
|
|
||||||
err: error
|
|
||||||
people, err = self.get(people_id)
|
|
||||||
logging.info(f"get people before save remark: {people}")
|
|
||||||
if not err.success:
|
|
||||||
return err
|
|
||||||
remark = people.comments.get("remark", None)
|
|
||||||
if remark is not None:
|
|
||||||
remark.content = content
|
|
||||||
remark.updated_at = datetime.now()
|
|
||||||
else:
|
|
||||||
people.comments["remark"] = Comment(content=content)
|
|
||||||
logging.info(f"save remark for people {people}")
|
|
||||||
_, err = self.save(people)
|
|
||||||
return err
|
|
||||||
|
|
||||||
def delete_remark(self, people_id: str) -> error:
|
|
||||||
"""
|
|
||||||
删除人物备注
|
|
||||||
|
|
||||||
:param people_id: 人物ID
|
|
||||||
:return: 错误对象
|
|
||||||
"""
|
|
||||||
people: People
|
|
||||||
err: error
|
|
||||||
people, err = self.get(people_id)
|
|
||||||
if not err.success:
|
|
||||||
return err
|
|
||||||
|
|
||||||
if "remark" in people.comments:
|
|
||||||
del people.comments["remark"]
|
|
||||||
_, err = self.save(people)
|
|
||||||
return err
|
|
||||||
|
|
||||||
return error(ErrorCode.SUCCESS, "")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
people_service = None
|
people_service = None
|
||||||
|
|||||||
@@ -150,24 +150,3 @@ async def get_peoples(
|
|||||||
peoples = [people.to_dict() for people in results]
|
peoples = [people.to_dict() for people in results]
|
||||||
return BaseResponse(error_code=0, error_info="success", data=peoples)
|
return BaseResponse(error_code=0, error_info="success", data=peoples)
|
||||||
|
|
||||||
|
|
||||||
class RemarkRequest(BaseModel):
|
|
||||||
content: str
|
|
||||||
|
|
||||||
|
|
||||||
@api.post("/people/{people_id}/remark")
|
|
||||||
async def post_remark(people_id: str, request: RemarkRequest):
|
|
||||||
service = get_people_service()
|
|
||||||
error = service.save_remark(people_id, request.content)
|
|
||||||
if not error.success:
|
|
||||||
return BaseResponse(error_code=error.code, error_info=error.info)
|
|
||||||
return BaseResponse(error_code=0, error_info="success")
|
|
||||||
|
|
||||||
|
|
||||||
@api.delete("/people/{people_id}/remark")
|
|
||||||
async def delete_remark(people_id: str):
|
|
||||||
service = get_people_service()
|
|
||||||
error = service.delete_remark(people_id)
|
|
||||||
if not error.success:
|
|
||||||
return BaseResponse(error_code=error.code, error_info=error.info)
|
|
||||||
return BaseResponse(error_code=0, error_info="success")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user