1
1
import sys
2
2
from pathlib import Path
3
- from pprint import pprint
4
3
5
4
import structlog
6
5
from django .conf import settings
@@ -49,15 +48,18 @@ class Command(BaseCommand):
49
48
to be included in the notification.
50
49
* ``email.md`` is a Markdown file with the first line as the subject,
51
50
and the rest is the content.
52
- Note that ``user`` and ``domain`` are available in the context.
51
+
52
+ The context available is:
53
+ * ``user``
54
+ * ``production_uri``
53
55
54
56
.. code:: markdown
55
57
56
58
Read the Docs deprecated option, action required
57
59
58
60
Dear {{ user.firstname }},
59
61
60
- Greetings from [Read the Docs]({{ domain }}).
62
+ Greetings from [Read the Docs]({{ production_uri }}).
61
63
62
64
.. note::
63
65
@@ -77,30 +79,30 @@ class Command(BaseCommand):
77
79
78
80
def add_arguments (self , parser ):
79
81
parser .add_argument (
80
- ' --production' ,
81
- action = ' store_true' ,
82
- dest = ' production' ,
82
+ " --production" ,
83
+ action = " store_true" ,
84
+ dest = " production" ,
83
85
default = False ,
84
86
help = (
85
- ' Send the email/notification for real, '
86
- ' otherwise we only print the notification in the console (dryrun).'
87
- )
87
+ " Send the email/notification for real, "
88
+ " otherwise we only logs the notification in the console (dryrun)."
89
+ ),
88
90
)
89
91
parser .add_argument (
90
- ' --email' ,
92
+ " --email" ,
91
93
help = (
92
- ' Path to a file with the email content in markdown. '
93
- ' The first line would be the subject.'
94
+ " Path to a file with the email content in markdown. "
95
+ " The first line would be the subject."
94
96
),
95
97
)
96
98
parser .add_argument (
97
- ' --notification' ,
98
- help = ' Path to a file with the notification content in markdown.' ,
99
+ " --notification" ,
100
+ help = " Path to a file with the notification content in markdown." ,
99
101
)
100
102
parser .add_argument (
101
- ' --sticky' ,
102
- action = ' store_true' ,
103
- dest = ' sticky' ,
103
+ " --sticky" ,
104
+ action = " store_true" ,
105
+ dest = " sticky" ,
104
106
default = False ,
105
107
help = (
106
108
'Make the notification sticky '
@@ -143,7 +145,7 @@ def handle(self, *args, **options):
143
145
users = AdminPermission .owners (organization )
144
146
elif usernames :
145
147
file = Path (usernames )
146
- with file .open () as f :
148
+ with file .open (encoding = "utf8" ) as f :
147
149
usernames = f .readlines ()
148
150
149
151
# remove "\n" from lines
@@ -155,62 +157,59 @@ def handle(self, *args, **options):
155
157
organizationowner__organization__disabled = False
156
158
).distinct ()
157
159
else :
158
- users = (
159
- User .objects
160
- .filter (projects__skip = False )
161
- .distinct ()
162
- )
163
-
164
- print (
165
- "len(owners)={} production={} email={} notification={} sticky={}" .format (
166
- users .count (),
167
- bool (options ["production" ]),
168
- options ["email" ],
169
- options ["notification" ],
170
- options ["sticky" ],
171
- )
160
+ users = User .objects .filter (projects__skip = False ).distinct ()
161
+
162
+ log .info (
163
+ "Command arguments." ,
164
+ n_owners = users .count (),
165
+ production = bool (options ["production" ]),
166
+ email_filepath = options ["email" ],
167
+ notification_filepath = options ["notification" ],
168
+ sticky = options ["sticky" ],
172
169
)
173
170
174
171
if input ("Continue? y/N: " ) != "y" :
175
172
print ("Aborting run." )
176
173
return
177
174
178
- notification_content = ''
179
- if options [' notification' ]:
180
- file = Path (options [' notification' ])
181
- with file .open () as f :
175
+ notification_content = ""
176
+ if options [" notification" ]:
177
+ file = Path (options [" notification" ])
178
+ with file .open (encoding = "utf8" ) as f :
182
179
notification_content = f .read ()
183
180
184
- email_subject = ''
185
- email_content = ''
186
- if options [' email' ]:
187
- file = Path (options [' email' ])
188
- with file .open () as f :
189
- content = f .read ().split (' \n ' )
181
+ email_subject = ""
182
+ email_content = ""
183
+ if options [" email" ]:
184
+ file = Path (options [" email" ])
185
+ with file .open (encoding = "utf8" ) as f :
186
+ content = f .read ().split (" \n " )
190
187
email_subject = content [0 ].strip ()
191
- email_content = ' \n ' .join (content [1 :]).strip ()
188
+ email_content = " \n " .join (content [1 :]).strip ()
192
189
193
190
resp = contact_users (
194
191
users = users ,
195
192
email_subject = email_subject ,
196
193
email_content = email_content ,
197
194
notification_content = notification_content ,
198
- sticky_notification = options ['sticky' ],
199
- dryrun = not options ['production' ],
195
+ sticky_notification = options ["sticky" ],
196
+ dryrun = not options ["production" ],
197
+ )
198
+
199
+ email = resp ["email" ]
200
+ log .info (
201
+ "Sending emails finished." ,
202
+ total = len (email ["sent" ]),
203
+ total_failed = len (email ["failed" ]),
204
+ sent_emails = email ["sent" ],
205
+ failed_emails = email ["failed" ],
200
206
)
201
207
202
- email = resp ['email' ]
203
- total = len (email ['sent' ])
204
- total_failed = len (email ['failed' ])
205
- print (f'Emails sent ({ total } ):' )
206
- pprint (email ['sent' ])
207
- print (f'Failed emails ({ total_failed } ):' )
208
- pprint (email ['failed' ])
209
-
210
- notification = resp ['notification' ]
211
- total = len (notification ['sent' ])
212
- total_failed = len (notification ['failed' ])
213
- print (f'Notifications sent ({ total } )' )
214
- pprint (notification ['sent' ])
215
- print (f'Failed notifications ({ total_failed } )' )
216
- pprint (notification ['failed' ])
208
+ notification = resp ["notification" ]
209
+ log .info (
210
+ "Sending notifications finished." ,
211
+ total = len (notification ["sent" ]),
212
+ total_failed = len (notification ["failed" ]),
213
+ sent_notifications = notification ["sent" ],
214
+ failed_notifications = notification ["failed" ],
215
+ )
0 commit comments