Coverage for rfpy/mail/schemas.py: 100%
71 statements
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-31 16:00 +0000
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-31 16:00 +0000
1from typing import List, Union
2from datetime import datetime
4from pydantic import BaseModel, EmailStr, Field
7from rfpy import conf
10class OrmModel(BaseModel):
11 class Config:
12 orm_mode = True
15class Org(OrmModel):
16 id: str
17 name: str
20class User(OrmModel):
21 fullname: str
22 email: EmailStr
23 organisation: Org
26class Proj(OrmModel):
27 id: int
28 title: str
29 status: str
30 owner_organisation: Org = Field(..., alias='owner_org')
31 author: User = None
34class Issue(OrmModel):
35 id: int
36 status: str
37 deadline: datetime = Field(None, alias='deadline_date')
38 respondent: Org = None
39 respondent_email: str = None
42class ProjNote(OrmModel):
43 note_text: str
44 private: bool
45 distribution: str = Field(..., alias='pretty_distribution')
46 author_org: Org = Field(..., alias='organisation')
47 addressee: Org = Field(None, alias='target_organisation')
50class ScoreSchema(BaseModel):
51 score_value: int = None
52 comment: str = None
53 question_number: str
54 respondent_name: str
55 issue_id: float
58class EvtSchema(OrmModel):
59 id: int
60 timestamp: datetime # output format should be - '%-d %-b %Y %H:%M UTC'
61 event_type: str
62 user: User = None
63 organisation: Org = None
64 project: Proj = None
65 issue: Issue = None
66 note: ProjNote = None
67 score: ScoreSchema = None
70class TemplateModelSchema(OrmModel):
72 system_name: str
73 host_url_root: str
74 recipient: Union[User, EmailStr] = None
75 event: EvtSchema
78class BaseEmail(BaseModel):
79 To: EmailStr
80 From: EmailStr
81 Tag: str = None
84class PostmarkTemplateMessage(BaseEmail):
85 TemplateModel: TemplateModelSchema
86 TemplateAlias: str
89class PostmarkBatchWithTemplates(OrmModel):
90 Messages: List[PostmarkTemplateMessage]
93class PostmarkSimpleMessage(BaseEmail):
94 Subject: str
95 TextBody: str
98def build_template_model(event_model: EvtSchema,
99 recipient_user: User,
100 to_email: str) -> PostmarkTemplateMessage:
102 recipient_model = to_email
103 if recipient_user:
104 recipient_model = User.from_orm(recipient_user)
105 tms = TemplateModelSchema(
106 system_name=conf.CONF.system_name,
107 host_url_root=conf.CONF.webapp_hostname,
108 recipient=recipient_model,
109 event=event_model
110 )
111 return PostmarkTemplateMessage(
112 To=to_email,
113 From=conf.CONF.email_from_address,
114 TemplateModel=tms,
115 TemplateAlias=event_model.event_type
116 )