fix: auto-fix code issues (cron)

- 修复重复导入/字段
- 修复异常处理
- 修复PEP8格式问题
- 添加类型注解
This commit is contained in:
AutoFix Bot
2026-03-02 12:14:39 +08:00
parent e23f1fec08
commit 98527c4de4
39 changed files with 8109 additions and 8147 deletions

View File

@@ -15,29 +15,29 @@ from typing import Any
class SharePermission(Enum):
"""分享权限级别"""
READ_ONLY = "read_only" # 只读
COMMENT = "comment" # 可评论
EDIT = "edit" # 可编辑
ADMIN = "admin" # 管理员
READ_ONLY = "read_only" # 只读
COMMENT = "comment" # 可评论
EDIT = "edit" # 可编辑
ADMIN = "admin" # 管理员
class CommentTargetType(Enum):
"""评论目标类型"""
ENTITY = "entity" # 实体评论
RELATION = "relation" # 关系评论
TRANSCRIPT = "transcript" # 转录文本评论
PROJECT = "project" # 项目级评论
ENTITY = "entity" # 实体评论
RELATION = "relation" # 关系评论
TRANSCRIPT = "transcript" # 转录文本评论
PROJECT = "project" # 项目级评论
class ChangeType(Enum):
"""变更类型"""
CREATE = "create" # 创建
UPDATE = "update" # 更新
DELETE = "delete" # 删除
MERGE = "merge" # 合并
SPLIT = "split" # 拆分
CREATE = "create" # 创建
UPDATE = "update" # 更新
DELETE = "delete" # 删除
MERGE = "merge" # 合并
SPLIT = "split" # 拆分
@dataclass
@@ -136,10 +136,10 @@ class TeamSpace:
class CollaborationManager:
"""协作管理主类"""
def __init__(self, db_manager = None) -> None:
self.db = db_manager
self._shares_cache: dict[str, ProjectShare] = {}
self._comments_cache: dict[str, list[Comment]] = {}
def __init__(self, db_manager=None) -> None:
self.db = db_manager
self._shares_cache: dict[str, ProjectShare] = {}
self._comments_cache: dict[str, list[Comment]] = {}
# ============ 项目分享 ============
@@ -147,57 +147,57 @@ class CollaborationManager:
self,
project_id: str,
created_by: str,
permission: str = "read_only",
expires_in_days: int | None = None,
max_uses: int | None = None,
password: str | None = None,
allow_download: bool = False,
allow_export: bool = False,
permission: str = "read_only",
expires_in_days: int | None = None,
max_uses: int | None = None,
password: str | None = None,
allow_download: bool = False,
allow_export: bool = False,
) -> ProjectShare:
"""创建项目分享链接"""
share_id = str(uuid.uuid4())
token = self._generate_share_token(project_id)
share_id = str(uuid.uuid4())
token = self._generate_share_token(project_id)
now = datetime.now().isoformat()
expires_at = None
now = datetime.now().isoformat()
expires_at = None
if expires_in_days:
expires_at = (datetime.now() + timedelta(days = expires_in_days)).isoformat()
expires_at = (datetime.now() + timedelta(days=expires_in_days)).isoformat()
password_hash = None
password_hash = None
if password:
password_hash = hashlib.sha256(password.encode()).hexdigest()
password_hash = hashlib.sha256(password.encode()).hexdigest()
share = ProjectShare(
id = share_id,
project_id = project_id,
token = token,
permission = permission,
created_by = created_by,
created_at = now,
expires_at = expires_at,
max_uses = max_uses,
use_count = 0,
password_hash = password_hash,
is_active = True,
allow_download = allow_download,
allow_export = allow_export,
share = ProjectShare(
id=share_id,
project_id=project_id,
token=token,
permission=permission,
created_by=created_by,
created_at=now,
expires_at=expires_at,
max_uses=max_uses,
use_count=0,
password_hash=password_hash,
is_active=True,
allow_download=allow_download,
allow_export=allow_export,
)
# 保存到数据库
if self.db:
self._save_share_to_db(share)
self._shares_cache[token] = share
self._shares_cache[token] = share
return share
def _generate_share_token(self, project_id: str) -> str:
"""生成分享令牌"""
data = f"{project_id}:{datetime.now().timestamp()}:{uuid.uuid4()}"
data = f"{project_id}:{datetime.now().timestamp()}:{uuid.uuid4()}"
return hashlib.sha256(data.encode()).hexdigest()[:32]
def _save_share_to_db(self, share: ProjectShare) -> None:
"""保存分享记录到数据库"""
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
cursor.execute(
"""
INSERT INTO project_shares
@@ -224,12 +224,12 @@ class CollaborationManager:
)
self.db.conn.commit()
def validate_share_token(self, token: str, password: str | None = None) -> ProjectShare | None:
def validate_share_token(self, token: str, password: str | None = None) -> ProjectShare | None:
"""验证分享令牌"""
# 从缓存或数据库获取
share = self._shares_cache.get(token)
share = self._shares_cache.get(token)
if not share and self.db:
share = self._get_share_from_db(token)
share = self._get_share_from_db(token)
if not share:
return None
@@ -250,7 +250,7 @@ class CollaborationManager:
if share.password_hash:
if not password:
return None
password_hash = hashlib.sha256(password.encode()).hexdigest()
password_hash = hashlib.sha256(password.encode()).hexdigest()
if password_hash != share.password_hash:
return None
@@ -258,42 +258,42 @@ class CollaborationManager:
def _get_share_from_db(self, token: str) -> ProjectShare | None:
"""从数据库获取分享记录"""
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
cursor.execute(
"""
SELECT * FROM project_shares WHERE token = ?
""",
(token, ),
)
row = cursor.fetchone()
row = cursor.fetchone()
if not row:
return None
return ProjectShare(
id = row[0],
project_id = row[1],
token = row[2],
permission = row[3],
created_by = row[4],
created_at = row[5],
expires_at = row[6],
max_uses = row[7],
use_count = row[8],
password_hash = row[9],
is_active = bool(row[10]),
allow_download = bool(row[11]),
allow_export = bool(row[12]),
id=row[0],
project_id=row[1],
token=row[2],
permission=row[3],
created_by=row[4],
created_at=row[5],
expires_at=row[6],
max_uses=row[7],
use_count=row[8],
password_hash=row[9],
is_active=bool(row[10]),
allow_download=bool(row[11]),
allow_export=bool(row[12]),
)
def increment_share_usage(self, token: str) -> None:
"""增加分享链接使用次数"""
share = self._shares_cache.get(token)
share = self._shares_cache.get(token)
if share:
share.use_count += 1
if self.db:
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
cursor.execute(
"""
UPDATE project_shares
@@ -307,7 +307,7 @@ class CollaborationManager:
def revoke_share_link(self, share_id: str, revoked_by: str) -> bool:
"""撤销分享链接"""
if self.db:
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
cursor.execute(
"""
UPDATE project_shares
@@ -325,7 +325,7 @@ class CollaborationManager:
if not self.db:
return []
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
cursor.execute(
"""
SELECT * FROM project_shares
@@ -335,23 +335,23 @@ class CollaborationManager:
(project_id, ),
)
shares = []
shares = []
for row in cursor.fetchall():
shares.append(
ProjectShare(
id = row[0],
project_id = row[1],
token = row[2],
permission = row[3],
created_by = row[4],
created_at = row[5],
expires_at = row[6],
max_uses = row[7],
use_count = row[8],
password_hash = row[9],
is_active = bool(row[10]),
allow_download = bool(row[11]),
allow_export = bool(row[12]),
id=row[0],
project_id=row[1],
token=row[2],
permission=row[3],
created_by=row[4],
created_at=row[5],
expires_at=row[6],
max_uses=row[7],
use_count=row[8],
password_hash=row[9],
is_active=bool(row[10]),
allow_download=bool(row[11]),
allow_export=bool(row[12]),
)
)
return shares
@@ -366,46 +366,46 @@ class CollaborationManager:
author: str,
author_name: str,
content: str,
parent_id: str | None = None,
mentions: list[str] | None = None,
attachments: list[dict] | None = None,
parent_id: str | None = None,
mentions: list[str] | None = None,
attachments: list[dict] | None = None,
) -> Comment:
"""添加评论"""
comment_id = str(uuid.uuid4())
now = datetime.now().isoformat()
comment_id = str(uuid.uuid4())
now = datetime.now().isoformat()
comment = Comment(
id = comment_id,
project_id = project_id,
target_type = target_type,
target_id = target_id,
parent_id = parent_id,
author = author,
author_name = author_name,
content = content,
created_at = now,
updated_at = now,
resolved = False,
resolved_by = None,
resolved_at = None,
mentions = mentions or [],
attachments = attachments or [],
comment = Comment(
id=comment_id,
project_id=project_id,
target_type=target_type,
target_id=target_id,
parent_id=parent_id,
author=author,
author_name=author_name,
content=content,
created_at=now,
updated_at=now,
resolved=False,
resolved_by=None,
resolved_at=None,
mentions=mentions or [],
attachments=attachments or [],
)
if self.db:
self._save_comment_to_db(comment)
# 更新缓存
key = f"{target_type}:{target_id}"
key = f"{target_type}:{target_id}"
if key not in self._comments_cache:
self._comments_cache[key] = []
self._comments_cache[key] = []
self._comments_cache[key].append(comment)
return comment
def _save_comment_to_db(self, comment: Comment) -> None:
"""保存评论到数据库"""
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
cursor.execute(
"""
INSERT INTO comments
@@ -435,13 +435,13 @@ class CollaborationManager:
self.db.conn.commit()
def get_comments(
self, target_type: str, target_id: str, include_resolved: bool = True
self, target_type: str, target_id: str, include_resolved: bool = True
) -> list[Comment]:
"""获取评论列表"""
if not self.db:
return []
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
if include_resolved:
cursor.execute(
"""
@@ -461,7 +461,7 @@ class CollaborationManager:
(target_type, target_id),
)
comments = []
comments = []
for row in cursor.fetchall():
comments.append(self._row_to_comment(row))
return comments
@@ -469,21 +469,21 @@ class CollaborationManager:
def _row_to_comment(self, row) -> Comment:
"""将数据库行转换为Comment对象"""
return Comment(
id = row[0],
project_id = row[1],
target_type = row[2],
target_id = row[3],
parent_id = row[4],
author = row[5],
author_name = row[6],
content = row[7],
created_at = row[8],
updated_at = row[9],
resolved = bool(row[10]),
resolved_by = row[11],
resolved_at = row[12],
mentions = json.loads(row[13]) if row[13] else [],
attachments = json.loads(row[14]) if row[14] else [],
id=row[0],
project_id=row[1],
target_type=row[2],
target_id=row[3],
parent_id=row[4],
author=row[5],
author_name=row[6],
content=row[7],
created_at=row[8],
updated_at=row[9],
resolved=bool(row[10]),
resolved_by=row[11],
resolved_at=row[12],
mentions=json.loads(row[13]) if row[13] else [],
attachments=json.loads(row[14]) if row[14] else [],
)
def update_comment(self, comment_id: str, content: str, updated_by: str) -> Comment | None:
@@ -491,8 +491,8 @@ class CollaborationManager:
if not self.db:
return None
now = datetime.now().isoformat()
cursor = self.db.conn.cursor()
now = datetime.now().isoformat()
cursor = self.db.conn.cursor()
cursor.execute(
"""
UPDATE comments
@@ -509,9 +509,9 @@ class CollaborationManager:
def _get_comment_by_id(self, comment_id: str) -> Comment | None:
"""根据ID获取评论"""
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
cursor.execute("SELECT * FROM comments WHERE id = ?", (comment_id, ))
row = cursor.fetchone()
row = cursor.fetchone()
if row:
return self._row_to_comment(row)
return None
@@ -521,8 +521,8 @@ class CollaborationManager:
if not self.db:
return False
now = datetime.now().isoformat()
cursor = self.db.conn.cursor()
now = datetime.now().isoformat()
cursor = self.db.conn.cursor()
cursor.execute(
"""
UPDATE comments
@@ -539,7 +539,7 @@ class CollaborationManager:
if not self.db:
return False
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
# 只允许作者或管理员删除
cursor.execute(
"""
@@ -554,13 +554,13 @@ class CollaborationManager:
return cursor.rowcount > 0
def get_project_comments(
self, project_id: str, limit: int = 50, offset: int = 0
self, project_id: str, limit: int = 50, offset: int = 0
) -> list[Comment]:
"""获取项目下的所有评论"""
if not self.db:
return []
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
cursor.execute(
"""
SELECT * FROM comments
@@ -571,7 +571,7 @@ class CollaborationManager:
(project_id, limit, offset),
)
comments = []
comments = []
for row in cursor.fetchall():
comments.append(self._row_to_comment(row))
return comments
@@ -587,32 +587,32 @@ class CollaborationManager:
entity_name: str,
changed_by: str,
changed_by_name: str,
old_value: dict | None = None,
new_value: dict | None = None,
description: str = "",
session_id: str | None = None,
old_value: dict | None = None,
new_value: dict | None = None,
description: str = "",
session_id: str | None = None,
) -> ChangeRecord:
"""记录变更"""
record_id = str(uuid.uuid4())
now = datetime.now().isoformat()
record_id = str(uuid.uuid4())
now = datetime.now().isoformat()
record = ChangeRecord(
id = record_id,
project_id = project_id,
change_type = change_type,
entity_type = entity_type,
entity_id = entity_id,
entity_name = entity_name,
changed_by = changed_by,
changed_by_name = changed_by_name,
changed_at = now,
old_value = old_value,
new_value = new_value,
description = description,
session_id = session_id,
reverted = False,
reverted_at = None,
reverted_by = None,
record = ChangeRecord(
id=record_id,
project_id=project_id,
change_type=change_type,
entity_type=entity_type,
entity_id=entity_id,
entity_name=entity_name,
changed_by=changed_by,
changed_by_name=changed_by_name,
changed_at=now,
old_value=old_value,
new_value=new_value,
description=description,
session_id=session_id,
reverted=False,
reverted_at=None,
reverted_by=None,
)
if self.db:
@@ -622,7 +622,7 @@ class CollaborationManager:
def _save_change_to_db(self, record: ChangeRecord) -> None:
"""保存变更记录到数据库"""
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
cursor.execute(
"""
INSERT INTO change_history
@@ -655,16 +655,16 @@ class CollaborationManager:
def get_change_history(
self,
project_id: str,
entity_type: str | None = None,
entity_id: str | None = None,
limit: int = 50,
offset: int = 0,
entity_type: str | None = None,
entity_id: str | None = None,
limit: int = 50,
offset: int = 0,
) -> list[ChangeRecord]:
"""获取变更历史"""
if not self.db:
return []
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
if entity_type and entity_id:
cursor.execute(
@@ -697,7 +697,7 @@ class CollaborationManager:
(project_id, limit, offset),
)
records = []
records = []
for row in cursor.fetchall():
records.append(self._row_to_change_record(row))
return records
@@ -705,22 +705,22 @@ class CollaborationManager:
def _row_to_change_record(self, row) -> ChangeRecord:
"""将数据库行转换为ChangeRecord对象"""
return ChangeRecord(
id = row[0],
project_id = row[1],
change_type = row[2],
entity_type = row[3],
entity_id = row[4],
entity_name = row[5],
changed_by = row[6],
changed_by_name = row[7],
changed_at = row[8],
old_value = json.loads(row[9]) if row[9] else None,
new_value = json.loads(row[10]) if row[10] else None,
description = row[11],
session_id = row[12],
reverted = bool(row[13]),
reverted_at = row[14],
reverted_by = row[15],
id=row[0],
project_id=row[1],
change_type=row[2],
entity_type=row[3],
entity_id=row[4],
entity_name=row[5],
changed_by=row[6],
changed_by_name=row[7],
changed_at=row[8],
old_value=json.loads(row[9]) if row[9] else None,
new_value=json.loads(row[10]) if row[10] else None,
description=row[11],
session_id=row[12],
reverted=bool(row[13]),
reverted_at=row[14],
reverted_by=row[15],
)
def get_entity_version_history(self, entity_type: str, entity_id: str) -> list[ChangeRecord]:
@@ -728,7 +728,7 @@ class CollaborationManager:
if not self.db:
return []
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
cursor.execute(
"""
SELECT * FROM change_history
@@ -738,7 +738,7 @@ class CollaborationManager:
(entity_type, entity_id),
)
records = []
records = []
for row in cursor.fetchall():
records.append(self._row_to_change_record(row))
return records
@@ -748,8 +748,8 @@ class CollaborationManager:
if not self.db:
return False
now = datetime.now().isoformat()
cursor = self.db.conn.cursor()
now = datetime.now().isoformat()
cursor = self.db.conn.cursor()
cursor.execute(
"""
UPDATE change_history
@@ -766,7 +766,7 @@ class CollaborationManager:
if not self.db:
return {}
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
# 总变更数
cursor.execute(
@@ -775,7 +775,7 @@ class CollaborationManager:
""",
(project_id, ),
)
total_changes = cursor.fetchone()[0]
total_changes = cursor.fetchone()[0]
# 按类型统计
cursor.execute(
@@ -785,7 +785,7 @@ class CollaborationManager:
""",
(project_id, ),
)
type_counts = {row[0]: row[1] for row in cursor.fetchall()}
type_counts = {row[0]: row[1] for row in cursor.fetchall()}
# 按实体类型统计
cursor.execute(
@@ -795,7 +795,7 @@ class CollaborationManager:
""",
(project_id, ),
)
entity_type_counts = {row[0]: row[1] for row in cursor.fetchall()}
entity_type_counts = {row[0]: row[1] for row in cursor.fetchall()}
# 最近活跃的用户
cursor.execute(
@@ -808,7 +808,7 @@ class CollaborationManager:
""",
(project_id, ),
)
top_contributors = [{"name": row[0], "changes": row[1]} for row in cursor.fetchall()]
top_contributors = [{"name": row[0], "changes": row[1]} for row in cursor.fetchall()]
return {
"total_changes": total_changes,
@@ -827,27 +827,27 @@ class CollaborationManager:
user_email: str,
role: str,
invited_by: str,
permissions: list[str] | None = None,
permissions: list[str] | None = None,
) -> TeamMember:
"""添加团队成员"""
member_id = str(uuid.uuid4())
now = datetime.now().isoformat()
member_id = str(uuid.uuid4())
now = datetime.now().isoformat()
# 根据角色设置默认权限
if permissions is None:
permissions = self._get_default_permissions(role)
permissions = self._get_default_permissions(role)
member = TeamMember(
id = member_id,
project_id = project_id,
user_id = user_id,
user_name = user_name,
user_email = user_email,
role = role,
joined_at = now,
invited_by = invited_by,
last_active_at = None,
permissions = permissions,
member = TeamMember(
id=member_id,
project_id=project_id,
user_id=user_id,
user_name=user_name,
user_email=user_email,
role=role,
joined_at=now,
invited_by=invited_by,
last_active_at=None,
permissions=permissions,
)
if self.db:
@@ -857,7 +857,7 @@ class CollaborationManager:
def _get_default_permissions(self, role: str) -> list[str]:
"""获取角色的默认权限"""
permissions_map = {
permissions_map = {
"owner": ["read", "write", "delete", "share", "admin", "export"],
"admin": ["read", "write", "delete", "share", "export"],
"editor": ["read", "write", "export"],
@@ -868,7 +868,7 @@ class CollaborationManager:
def _save_member_to_db(self, member: TeamMember) -> None:
"""保存成员到数据库"""
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
cursor.execute(
"""
INSERT INTO team_members
@@ -896,7 +896,7 @@ class CollaborationManager:
if not self.db:
return []
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
cursor.execute(
"""
SELECT * FROM team_members WHERE project_id = ?
@@ -905,7 +905,7 @@ class CollaborationManager:
(project_id, ),
)
members = []
members = []
for row in cursor.fetchall():
members.append(self._row_to_team_member(row))
return members
@@ -913,16 +913,16 @@ class CollaborationManager:
def _row_to_team_member(self, row) -> TeamMember:
"""将数据库行转换为TeamMember对象"""
return TeamMember(
id = row[0],
project_id = row[1],
user_id = row[2],
user_name = row[3],
user_email = row[4],
role = row[5],
joined_at = row[6],
invited_by = row[7],
last_active_at = row[8],
permissions = json.loads(row[9]) if row[9] else [],
id=row[0],
project_id=row[1],
user_id=row[2],
user_name=row[3],
user_email=row[4],
role=row[5],
joined_at=row[6],
invited_by=row[7],
last_active_at=row[8],
permissions=json.loads(row[9]) if row[9] else [],
)
def update_member_role(self, member_id: str, new_role: str, updated_by: str) -> bool:
@@ -930,8 +930,8 @@ class CollaborationManager:
if not self.db:
return False
permissions = self._get_default_permissions(new_role)
cursor = self.db.conn.cursor()
permissions = self._get_default_permissions(new_role)
cursor = self.db.conn.cursor()
cursor.execute(
"""
UPDATE team_members
@@ -948,7 +948,7 @@ class CollaborationManager:
if not self.db:
return False
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
cursor.execute("DELETE FROM team_members WHERE id = ?", (member_id, ))
self.db.conn.commit()
return cursor.rowcount > 0
@@ -958,7 +958,7 @@ class CollaborationManager:
if not self.db:
return False
cursor = self.db.conn.cursor()
cursor = self.db.conn.cursor()
cursor.execute(
"""
SELECT permissions FROM team_members
@@ -967,11 +967,11 @@ class CollaborationManager:
(project_id, user_id),
)
row = cursor.fetchone()
row = cursor.fetchone()
if not row:
return False
permissions = json.loads(row[0]) if row[0] else []
permissions = json.loads(row[0]) if row[0] else []
return permission in permissions or "admin" in permissions
def update_last_active(self, project_id: str, user_id: str) -> None:
@@ -979,8 +979,8 @@ class CollaborationManager:
if not self.db:
return
now = datetime.now().isoformat()
cursor = self.db.conn.cursor()
now = datetime.now().isoformat()
cursor = self.db.conn.cursor()
cursor.execute(
"""
UPDATE team_members
@@ -993,12 +993,12 @@ class CollaborationManager:
# 全局协作管理器实例
_collaboration_manager = None
_collaboration_manager = None
def get_collaboration_manager(db_manager = None) -> None:
def get_collaboration_manager(db_manager=None) -> None:
"""获取协作管理器单例"""
global _collaboration_manager
if _collaboration_manager is None:
_collaboration_manager = CollaborationManager(db_manager)
_collaboration_manager = CollaborationManager(db_manager)
return _collaboration_manager