From 752b0c80bed32a8449c6c1c92654d04294770ca7 Mon Sep 17 00:00:00 2001 From: qizhicheng Date: Thu, 14 Sep 2023 17:09:23 +0800 Subject: [PATCH 1/2] pydantic migration --- helpdesk/views/api/schemas.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/helpdesk/views/api/schemas.py b/helpdesk/views/api/schemas.py index 2599797e..26c1e9b0 100644 --- a/helpdesk/views/api/schemas.py +++ b/helpdesk/views/api/schemas.py @@ -26,19 +26,19 @@ class QeuryKey(str, Enum): class ParamRule(BaseModel): - id: Optional[int] - title: Optional[str] - provider_object: Optional[str] - rule: Optional[str] - is_auto_approval: Optional[bool] - approver: Optional[str] + id: Optional[int] = None + title: Optional[str] = None + provider_object: Optional[str] = None + rule: Optional[str] = None + is_auto_approval: Optional[bool] = None + approver: Optional[str] = None class OperateTicket(BaseModel): """ 操作工单的请求体 """ - reason: Optional[str] + reason: Optional[str] = None class PolicyFlowResp(BaseModel): @@ -48,12 +48,12 @@ class PolicyFlowResp(BaseModel): id: int name: str display: str - definition: Optional[dict] + definition: Optional[dict] = None created_at: datetime - created_by: Optional[str] + created_by: Optional[str] = None updated_at: datetime - updated_by: Optional[str] + updated_by: Optional[str] = None class Config: orm_mode = True @@ -120,9 +120,9 @@ class TicketPolicyResp(BaseModel): 工单和审批流关联的响应体 """ id: int - ticket_name: Optional[str] - policy_id: Optional[int] - link_condition: Optional[str] + ticket_name: Optional[str] = None + policy_id: Optional[int] = None + link_condition: Optional[str] = None class Config: orm_mode = True From 2b93573db4e4918916013896ad5075cb627640ee Mon Sep 17 00:00:00 2001 From: qizhicheng Date: Thu, 14 Sep 2023 17:33:26 +0800 Subject: [PATCH 2/2] fix notification render error --- helpdesk/libs/notification.py | 55 ++++++++++++++++++----------------- helpdesk/views/api/schemas.py | 12 ++++---- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/helpdesk/libs/notification.py b/helpdesk/libs/notification.py index abebde9c..c284e6c2 100644 --- a/helpdesk/libs/notification.py +++ b/helpdesk/libs/notification.py @@ -69,7 +69,8 @@ class MailNotification(Notification): method = 'mail' async def get_mail_addrs(self): - email_addrs = [ADMIN_EMAIL_ADDRS] + [get_user_email(cc) for cc in self.ticket.ccs] + self.ticket.annotation.get("approvers").split(',') + email_addrs = [ADMIN_EMAIL_ADDRS] + [get_user_email(cc) for cc in self.ticket.ccs] + self.ticket.annotation.get( + "approvers").split(',') email_addrs += [get_user_email(approver) for approver in await self.ticket.get_rule_actions('approver')] if self.phase.value in ('approval', 'mark'): email_addrs += [get_user_email(self.ticket.submitter)] @@ -146,10 +147,11 @@ def render(self): notify_people = approvers for index, node in enumerate(nodes): if self.ticket.annotation.get("current_node") == node.get("name"): - next_node = nodes[index+1].get("name") if (index != len(nodes)-1) else "" + next_node = nodes[index + 1].get("name") if (index != len(nodes) - 1) else "" notify_type = node.get("node_type") - if self.phase.value in (TicketPhase.APPROVAL.value, TicketPhase.MARK.value) or (self.phase.value == 'request' and self.ticket.status == "closed"): + if self.phase.value in (TicketPhase.APPROVAL.value, TicketPhase.MARK.value) or ( + self.phase.value == 'request' and self.ticket.status == "closed"): notify_type = NodeType.CC if notify_type == NodeType.CC: if self.phase.value == TicketPhase.MARK.value or approvers == "": @@ -160,34 +162,35 @@ def render(self): approval_log = copy.deepcopy(self.ticket.annotation.get("approval_log")) for log in approval_log: format = '%Y-%m-%d %H:%M:%S' - log["operated_at"] = timezone('Etc/UTC').localize(datetime.strptime(log.get("operated_at"), format)).astimezone(timezone(TIME_ZONE)).strftime(format) - - return NotifyMessage( - phase=self.phase.value, - title=self.ticket.title, - ticket_url=self.ticket.web_url, - status=self.ticket.status, - is_approved=self.ticket.is_approved or False, - submitter=self.ticket.submitter, - params=self.ticket.params, - request_time=timezone('Etc/UTC').localize(self.ticket.created_at).astimezone(timezone(TIME_ZONE)), - reason=self.ticket.reason or "", - approval_flow=self.ticket.annotation.get("policy"), - current_node=self.ticket.annotation.get("current_node"), - approvers=approvers, - next_node=next_node, - approval_log=approval_log, - notify_type=notify_type, - notify_people=notify_people, - comfirmed_by=self.ticket.confirmed_by or "" - ) + log["operated_at"] = timezone('Etc/UTC').localize( + datetime.strptime(log.get("operated_at"), format)).astimezone(timezone(TIME_ZONE)).strftime(format) + + return NotifyMessage.model_validate({ + "phase": self.phase.value, + "title": self.ticket.title, + "ticket_url": self.ticket.web_url, + "status": self.ticket.status, + "is_approved": self.ticket.is_approved or False, + "submitter": self.ticket.submitter, + "params": self.ticket.params, + "request_time": timezone('Etc/UTC').localize(self.ticket.created_at).astimezone(timezone(TIME_ZONE)), + "reason": self.ticket.reason or "", + "approval_flow": self.ticket.annotation.get("policy"), + "current_node": self.ticket.annotation.get("current_node"), + "approvers": approvers, + "next_node": next_node, + "approval_log": approval_log, + "notify_type": notify_type, + "notify_people": notify_people, + "comfirmed_by": self.ticket.confirmed_by or "", + }) async def send(self): if not WEBHOOK_EVENT_URL: return message = self.render() - r = requests.post(WEBHOOK_EVENT_URL, message.json()) + r = requests.post(WEBHOOK_EVENT_URL, message.model_dump_json()) if r.status_code == 200: return else: - report() \ No newline at end of file + report() diff --git a/helpdesk/views/api/schemas.py b/helpdesk/views/api/schemas.py index 26c1e9b0..a51aa8f1 100644 --- a/helpdesk/views/api/schemas.py +++ b/helpdesk/views/api/schemas.py @@ -48,12 +48,12 @@ class PolicyFlowResp(BaseModel): id: int name: str display: str - definition: Optional[dict] = None + definition: Optional[dict] created_at: datetime - created_by: Optional[str] = None + created_by: Optional[str] updated_at: datetime - updated_by: Optional[str] = None + updated_by: Optional[str] class Config: orm_mode = True @@ -120,9 +120,9 @@ class TicketPolicyResp(BaseModel): 工单和审批流关联的响应体 """ id: int - ticket_name: Optional[str] = None - policy_id: Optional[int] = None - link_condition: Optional[str] = None + ticket_name: Optional[str] + policy_id: Optional[int] + link_condition: Optional[str] class Config: orm_mode = True