1 | """ |
||
2 | byceps.services.email.service |
||
3 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
4 | |||
5 | :Copyright: 2006-2020 Jochen Kupperschmidt |
||
6 | :License: Modified BSD, see LICENSE for details. |
||
7 | """ |
||
8 | |||
9 | 1 | from typing import List, Optional |
|
10 | |||
11 | 1 | from ...database import upsert |
|
12 | 1 | from ... import email |
|
13 | 1 | from ...util.jobqueue import enqueue |
|
14 | |||
15 | 1 | from .models import EmailConfig as DbEmailConfig |
|
16 | 1 | from .transfer.models import EmailConfig, Message, Sender |
|
17 | |||
18 | |||
19 | 1 | class UnknownEmailConfigId(ValueError): |
|
20 | 1 | pass |
|
21 | |||
22 | |||
23 | 1 | class EmailError(Exception): |
|
24 | 1 | pass |
|
25 | |||
26 | |||
27 | 1 | def create_config( |
|
28 | config_id: str, |
||
29 | sender_address: str, |
||
30 | *, |
||
31 | sender_name: Optional[str] = None, |
||
32 | contact_address: Optional[str] = None, |
||
33 | ) -> EmailConfig: |
||
34 | """Create a configuration.""" |
||
35 | config = DbEmailConfig( |
||
36 | config_id, |
||
37 | sender_address, |
||
38 | sender_name=sender_name, |
||
39 | contact_address=contact_address, |
||
40 | ) |
||
41 | |||
42 | db.session.add(config) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
43 | db.session.commit() |
||
44 | |||
45 | return _db_entity_to_config(config) |
||
46 | |||
47 | |||
48 | 1 | def update_config( |
|
49 | config_id: str, |
||
50 | sender_address: str, |
||
51 | sender_name: Optional[str], |
||
52 | contact_address: Optional[str], |
||
53 | ) -> EmailConfig: |
||
54 | """Update a configuration.""" |
||
55 | config = DbEmailConfig.query.get(config_id) |
||
56 | |||
57 | if config is None: |
||
58 | raise UnknownEmailConfigId(config_id) |
||
59 | |||
60 | config.sender_address = sender_address |
||
61 | config.sender_name = sender_name |
||
62 | config.contact_address = contact_address |
||
63 | |||
64 | db.session.commit() |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
65 | |||
66 | return _db_entity_to_config(config) |
||
67 | |||
68 | |||
69 | 1 | def delete_config(config_id: str) -> None: |
|
70 | """Delete a configuration.""" |
||
71 | db.session.query(DbEmailConfig) \ |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
72 | .filter_by(id=config_id) \ |
||
73 | .delete() |
||
74 | |||
75 | db.session.commit() |
||
76 | |||
77 | |||
78 | 1 | def find_config(config_id: str) -> Optional[EmailConfig]: |
|
79 | """Return the configuration, or `None` if not found.""" |
||
80 | 1 | config = DbEmailConfig.query.get(config_id) |
|
81 | |||
82 | 1 | if config is None: |
|
83 | return None |
||
84 | |||
85 | 1 | return _db_entity_to_config(config) |
|
86 | |||
87 | |||
88 | 1 | def get_config(config_id: str) -> EmailConfig: |
|
89 | """Return the configuration, or raise an error if none is |
||
90 | configured for that ID. |
||
91 | """ |
||
92 | 1 | config = find_config(config_id) |
|
93 | |||
94 | 1 | if not config: |
|
95 | raise EmailError(f'No e-mail config for ID "{config_id}"') |
||
96 | |||
97 | 1 | return config |
|
98 | |||
99 | |||
100 | 1 | def set_config( |
|
101 | config_id: str, |
||
102 | sender_address: str, |
||
103 | *, |
||
104 | sender_name: Optional[str] = None, |
||
105 | contact_address: Optional[str] = None, |
||
106 | ) -> None: |
||
107 | """Add or update configuration for that ID.""" |
||
108 | 1 | table = DbEmailConfig.__table__ |
|
109 | 1 | identifier = { |
|
110 | 'id': config_id, |
||
111 | 'sender_address': sender_address, |
||
112 | } |
||
113 | 1 | replacement = { |
|
114 | 'sender_name': sender_name, |
||
115 | 'contact_address': contact_address, |
||
116 | } |
||
117 | |||
118 | 1 | upsert(table, identifier, replacement) |
|
119 | |||
120 | |||
121 | 1 | def get_all_configs() -> List[EmailConfig]: |
|
122 | """Return all configurations.""" |
||
123 | configs = DbEmailConfig.query.all() |
||
124 | |||
125 | return [_db_entity_to_config(config) for config in configs] |
||
126 | |||
127 | |||
128 | 1 | def enqueue_message(message: Message) -> None: |
|
129 | """Enqueue e-mail to be sent asynchronously.""" |
||
130 | 1 | enqueue_email( |
|
131 | message.sender, |
||
132 | message.recipients, |
||
133 | message.subject, |
||
134 | message.body) |
||
135 | |||
136 | |||
137 | 1 | def enqueue_email( |
|
138 | sender: Optional[Sender], recipients: List[str], subject: str, body: str |
||
139 | ) -> None: |
||
140 | """Enqueue e-mail to be sent asynchronously.""" |
||
141 | 1 | sender_str = sender.format() if (sender is not None) else None |
|
142 | |||
143 | 1 | enqueue(send_email, sender_str, recipients, subject, body) |
|
144 | |||
145 | |||
146 | 1 | def send_email( |
|
147 | sender: Optional[str], recipients: List[str], subject: str, body: str |
||
148 | ) -> None: |
||
149 | """Send e-mail.""" |
||
150 | 1 | email.send(sender, recipients, subject, body) |
|
151 | |||
152 | |||
153 | 1 | def _db_entity_to_config(config: DbEmailConfig) -> EmailConfig: |
|
154 | 1 | sender = Sender( |
|
155 | config.sender_address, |
||
156 | config.sender_name, |
||
157 | ) |
||
158 | |||
159 | 1 | return EmailConfig( |
|
160 | config.id, |
||
161 | sender, |
||
162 | config.contact_address, |
||
163 | ) |
||
164 |