refactor: 重整 schemas 作用域并统一用户上下文模型
This commit is contained in:
@@ -1,52 +1,43 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from typing import Literal, ClassVar, Union
|
||||
from typing import ClassVar
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, EmailStr, Field
|
||||
|
||||
from schemas.schedule.items import (
|
||||
AttachmentType,
|
||||
CalendarContent,
|
||||
CalendarDeleteContent,
|
||||
CalendarInviteContent,
|
||||
CalendarUpdateContent,
|
||||
ScheduleItemMetadata,
|
||||
ScheduleItemMetadataAttachment,
|
||||
ScheduleItemSourceType,
|
||||
ScheduleItemStatus,
|
||||
parse_calendar_content,
|
||||
)
|
||||
|
||||
class AttachmentType(str, Enum):
|
||||
DOCUMENT = "document"
|
||||
REMINDER = "reminder"
|
||||
|
||||
|
||||
class ScheduleItemMetadataAttachment(BaseModel):
|
||||
model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")
|
||||
|
||||
name: str
|
||||
type: AttachmentType
|
||||
visible_to: list[UUID] = Field(default_factory=list)
|
||||
url: str | None = None
|
||||
note: str | None = None
|
||||
content: str | None = None
|
||||
|
||||
|
||||
class ScheduleItemMetadata(BaseModel):
|
||||
model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")
|
||||
|
||||
color: str | None = Field(default=None, pattern=r"^#[0-9A-Fa-f]{6}$")
|
||||
location: str | None = None
|
||||
notes: str | None = None
|
||||
attachments: list[ScheduleItemMetadataAttachment] = Field(default_factory=list)
|
||||
reminder_minutes: int | None = Field(default=None, ge=0, le=10080)
|
||||
version: Literal[1] = 1
|
||||
|
||||
|
||||
class ScheduleItemStatus(str, Enum):
|
||||
ACTIVE = "active"
|
||||
COMPLETED = "completed"
|
||||
CANCELED = "canceled"
|
||||
ARCHIVED = "archived"
|
||||
|
||||
|
||||
class ScheduleItemSourceType(str, Enum):
|
||||
MANUAL = "manual"
|
||||
IMPORTED = "imported"
|
||||
AGENT_GENERATED = "agent_generated"
|
||||
__all__ = [
|
||||
"AttachmentType",
|
||||
"CalendarContent",
|
||||
"CalendarDeleteContent",
|
||||
"CalendarInviteContent",
|
||||
"CalendarUpdateContent",
|
||||
"ScheduleItemMetadata",
|
||||
"ScheduleItemMetadataAttachment",
|
||||
"ScheduleItemSourceType",
|
||||
"ScheduleItemStatus",
|
||||
"parse_calendar_content",
|
||||
"ScheduleItemCreateRequest",
|
||||
"ScheduleItemUpdateRequest",
|
||||
"ScheduleItemResponse",
|
||||
"ScheduleItemListItem",
|
||||
"ScheduleItemListRequest",
|
||||
"ScheduleItemShareRequest",
|
||||
"ScheduleItemShareResponse",
|
||||
]
|
||||
|
||||
|
||||
class ScheduleItemCreateRequest(BaseModel):
|
||||
@@ -107,10 +98,9 @@ class ScheduleItemListRequest(BaseModel):
|
||||
end_at: datetime
|
||||
|
||||
|
||||
# Permission bit constants (matching PermissionBits in inbox_messages/schemas.py)
|
||||
_PERMISSION_VIEW = 1 # 001
|
||||
_PERMISSION_INVITE = 2 # 010
|
||||
_PERMISSION_EDIT = 4 # 100
|
||||
_PERMISSION_VIEW = 1
|
||||
_PERMISSION_INVITE = 2
|
||||
_PERMISSION_EDIT = 4
|
||||
|
||||
|
||||
class ScheduleItemShareRequest(BaseModel):
|
||||
@@ -134,50 +124,3 @@ class ScheduleItemShareRequest(BaseModel):
|
||||
|
||||
class ScheduleItemShareResponse(BaseModel):
|
||||
message: str
|
||||
|
||||
|
||||
class CalendarInviteContent(BaseModel):
|
||||
model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")
|
||||
|
||||
type: Literal["invite"]
|
||||
permission: int = Field(..., description="权限: 1=view, 4=edit, 8=invite")
|
||||
action: Literal["pending"] = "pending"
|
||||
|
||||
|
||||
class CalendarUpdateContent(BaseModel):
|
||||
model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")
|
||||
|
||||
type: Literal["update"]
|
||||
title: str = Field(..., description="事件标题")
|
||||
action: Literal["updated"] = "updated"
|
||||
|
||||
|
||||
class CalendarDeleteContent(BaseModel):
|
||||
model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")
|
||||
|
||||
type: Literal["delete"]
|
||||
title: str = Field(..., description="事件标题")
|
||||
action: Literal["deleted"] = "deleted"
|
||||
|
||||
|
||||
CalendarContent = Union[
|
||||
CalendarInviteContent, CalendarUpdateContent, CalendarDeleteContent
|
||||
]
|
||||
|
||||
|
||||
def parse_calendar_content(content: str | None) -> CalendarContent | None:
|
||||
if not content:
|
||||
return None
|
||||
try:
|
||||
data = json.loads(content)
|
||||
content_type = data.get("type")
|
||||
if content_type == "invite":
|
||||
return CalendarInviteContent(**data)
|
||||
elif content_type == "update":
|
||||
return CalendarUpdateContent(**data)
|
||||
elif content_type == "delete":
|
||||
return CalendarDeleteContent(**data)
|
||||
else:
|
||||
raise ValueError(f"Unknown calendar content type: {content_type}")
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user