-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemails.py
executable file
·152 lines (127 loc) · 5.39 KB
/
emails.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from core import error, print, req, mkdirs, write_files, Colors, confirm
import sys
import os
import json
def save_to_disk(dirname, id, name, subject, html, txt, new=True):
path = f'emailTemplates/{dirname}'
if new and os.path.isdir(path):
error(f"Email '{dirname}' already exists")
exit(9)
settings = {
'id': id,
'name': name,
'subject': subject,
}
files = {
f'{path}/settings.json': json.dumps(settings, indent=4, sort_keys=True),
f'{path}/template.html': html,
f'{path}/template.txt': txt
}
write_files(files)
def upload(id, name, subject, html, txt, create=False, save_non_id_dirname=None):
if id:
id = '/' + id
else:
create = True
res = req('POST' if create else 'PUT', '/api/email/template' + id, json={
'emailTemplate': {
'name': name,
'defaultSubject': subject,
'defaultHtmlTemplate': html,
'defaultTextTemplate': txt,
}
})
if save_non_id_dirname and not id:
save_to_disk(save_non_id_dirname, id, name, subject, html, txt)
return res.json()['emailTemplate']
def read_email_file(dirname, path):
with open(f'emailTemplates/{dirname}/{path}') as file:
return file.read()
def read_disk(dirname):
settings = json.loads(read_email_file(dirname, 'settings.json'))
settings['html'] = read_email_file(dirname, 'template.html')
settings['txt'] = read_email_file(dirname, 'template.txt')
return settings
def is_same(local, remote):
r = {'subject': 'defaultSubject', 'html': 'defaultHtmlTemplate', 'txt': 'defaultTextTemplate'}
diff = []
for k, v in local.items():
if v != remote[r.get(k, k)]:
diff.append(k)
return diff
def status():
local = {email: read_disk(email) for email in os.listdir('emailTemplates/') if os.path.isdir(f'emailTemplates/{email}')}
remote = list_emails(_print=False)
col = max(len(name)for name in local.keys())
def name_conflict(name): return name in [email['name'] for email in remote.values()]
for name, email in sorted(local.items()):
c, m = '', ''
if email['id'] not in remote:
if name_conflict(name):
if not email['id']:
c, m = Colors.NORMAL + Colors.RED, 'NAME CONFLICT (missing local id)'
else:
c, m = Colors.NORMAL + Colors.RED, 'NAME CONFLICT (local and remote id differ)'
elif not email['id']:
c, m = Colors.NORMAL + Colors.GREEN, 'CREATE (generate id)'
else:
c, m = Colors.NORMAL + Colors.GREEN, 'CREATE (missing remote)'
elif len(diff := is_same(email, remote[email['id']])) == 0:
m = 'SAME'
else:
c, m = Colors.NORMAL + Colors.YELLOW, f'UPDATED ({", ".join(diff)})'
print(f'{c}{name:<{col}} : {m}{Colors.END}')
return local, remote
def push():
local, remote = status()
print()
confirm('email push')
for dirname, email in local.items():
create = email['id'] not in remote
upload(**email, create=create, save_non_id_dirname=dirname)
print(f'{len(local)} email templates pushed.')
def download(id, name=''):
res = req('GET', f'/api/email/template/{id}')
email = res.json()['emailTemplate']
save_to_disk(name or email['name'], id, email['name'], email['defaultSubject'], email['defaultHtmlTemplate'], email['defaultTextTemplate'])
def create(name):
email = upload('', name, 'This is the subject', '<p>This is the html</p>', 'This is the plain text')
save_to_disk(name, email['id'], name, email['defaultSubject'], email['defaultHtmlTemplate'], email['defaultTextTemplate'])
def list_emails(_print=True):
res = req('GET', '/api/email/template')
emails = {}
for email in res.json()['emailTemplates']:
if _print:
print(email['id'], '-', email['name'])
emails[email['id']] = email
return emails
def delete(id):
res = req('DELETE', f'/api/email/template/{id}')
print(f'Successfully deleted email template with id {id}')
if __name__ == '__main__':
if len(sys.argv) <= 1:
error('Usage:')
error(f' {sys.argv[0]} status - Print email template statuses.')
error(f' {sys.argv[0]} push - Push email templates. Add -y or --yes to not confirm.')
error(f' {sys.argv[0]} pull - Pull all downloaded email templates.')
error(f' {sys.argv[0]} download <id> [name] - Pull an email template with id <id> from HOST to directory emailTemplates/<name>.')
error(f' {sys.argv[0]} create <name> - Create a new email template at HOST in directory emailTemplates/<name>.')
error(f' {sys.argv[0]} delete <id> - Delete a remote email template. Add -y or --yes to not confirm.')
error(f' {sys.argv[0]} list - List available email templates.')
exit(3)
if sys.argv[1] == 'status':
status()
elif sys.argv[1] == 'push':
push()
elif sys.argv[1] == 'pull':
pull()
elif sys.argv[1] == 'download':
download(*sys.argv[2:3+1])
elif sys.argv[1] == 'create':
create(sys.argv[2])
elif sys.argv[1] == 'delete':
delete(sys.argv[2])
elif sys.argv[1] == 'list':
list_emails()
else:
error(f'Unknown action "{sys.argv[1]}"')