django-codenerix-email 4.0.34__py2.py3-none-any.whl → 4.0.36__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (23) hide show
  1. codenerix_email/__init__.py +1 -1
  2. codenerix_email/__pycache__/__init__.cpython-310.pyc +0 -0
  3. codenerix_email/__pycache__/__init__.cpython-311.pyc +0 -0
  4. codenerix_email/__pycache__/models.cpython-310.pyc +0 -0
  5. codenerix_email/__pycache__/models.cpython-311.pyc +0 -0
  6. codenerix_email/management/commands/__pycache__/email_test.cpython-311.pyc +0 -0
  7. codenerix_email/management/commands/__pycache__/emails_recv.cpython-311.pyc +0 -0
  8. codenerix_email/management/commands/__pycache__/emails_send.cpython-311.pyc +0 -0
  9. codenerix_email/management/commands/__pycache__/recv_emails.cpython-311.pyc +0 -0
  10. codenerix_email/management/commands/__pycache__/send_emails.cpython-311.pyc +0 -0
  11. codenerix_email/management/commands/__pycache__/test_email.cpython-311.pyc +0 -0
  12. codenerix_email/management/commands/email_test.py +174 -0
  13. codenerix_email/management/commands/emails_recv.py +601 -0
  14. codenerix_email/management/commands/emails_send.py +238 -0
  15. codenerix_email/management/commands/recv_emails.py +8 -598
  16. codenerix_email/management/commands/send_emails.py +8 -241
  17. codenerix_email/management/commands/test_email.py +8 -171
  18. codenerix_email/models.py +16 -18
  19. {django_codenerix_email-4.0.34.dist-info → django_codenerix_email-4.0.36.dist-info}/METADATA +1 -1
  20. {django_codenerix_email-4.0.34.dist-info → django_codenerix_email-4.0.36.dist-info}/RECORD +23 -16
  21. {django_codenerix_email-4.0.34.dist-info → django_codenerix_email-4.0.36.dist-info}/LICENSE +0 -0
  22. {django_codenerix_email-4.0.34.dist-info → django_codenerix_email-4.0.36.dist-info}/WHEEL +0 -0
  23. {django_codenerix_email-4.0.34.dist-info → django_codenerix_email-4.0.36.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,238 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # django-codenerix-email
4
+ #
5
+ # Codenerix GNU
6
+ #
7
+ # Project URL : http://www.codenerix.com
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+
21
+ import time
22
+
23
+ from django.core.management.base import BaseCommand
24
+ from django.conf import settings
25
+ from django.utils import timezone
26
+
27
+ from codenerix_lib.debugger import Debugger
28
+ from codenerix_email.models import EmailMessage
29
+
30
+
31
+ class Command(BaseCommand, Debugger):
32
+ # Show this when the user types help
33
+ help = "Try to send all emails in the queue"
34
+
35
+ def add_arguments(self, parser):
36
+ # Named (optional) arguments
37
+ parser.add_argument(
38
+ "-d",
39
+ action="store_true",
40
+ dest="d",
41
+ default=False,
42
+ help="Keep the command working forever as a daemon",
43
+ )
44
+
45
+ # Named (optional) arguments
46
+ parser.add_argument(
47
+ "--daemon",
48
+ action="store_true",
49
+ dest="daemon",
50
+ default=False,
51
+ help="Keep the command working forever as a daemon",
52
+ )
53
+
54
+ # Named (optional) arguments
55
+ parser.add_argument(
56
+ "-c",
57
+ action="store_true",
58
+ dest="c",
59
+ default=False,
60
+ help="Clear the sending status to all the Queue",
61
+ )
62
+
63
+ # Named (optional) arguments
64
+ parser.add_argument(
65
+ "--clear",
66
+ action="store_true",
67
+ dest="clear",
68
+ default=False,
69
+ help="Clear the sending status to all the Queue",
70
+ )
71
+ # Named (optional) arguments
72
+ parser.add_argument(
73
+ "--verbose",
74
+ action="store_true",
75
+ dest="verbose",
76
+ default=False,
77
+ help="Enable verbose mode",
78
+ )
79
+ # Named (optional) arguments
80
+ parser.add_argument(
81
+ "--now",
82
+ action="store_true",
83
+ dest="now",
84
+ default=False,
85
+ help="Send now, do not wait the retry time",
86
+ )
87
+ # Named (optional) arguments
88
+ parser.add_argument(
89
+ "--all",
90
+ action="store_true",
91
+ dest="all",
92
+ default=False,
93
+ help="Send all, do not do on buckets",
94
+ )
95
+
96
+ def handle(self, *args, **options):
97
+ # Get user configuration
98
+ daemon = bool(options["daemon"] or options["d"])
99
+ clear = bool(options["clear"] or options["c"])
100
+ bucket_size = getattr(settings, "CLIENT_EMAIL_BUCKETS", 10)
101
+ verbose = bool(options.get("verbose", False))
102
+ sendnow = bool(options.get("now", False))
103
+ doall = bool(options.get("all", False))
104
+
105
+ # Autoconfigure Debugger
106
+ self.set_name("CODENERIX-EMAIL")
107
+ self.set_debug()
108
+
109
+ # Daemon
110
+ if verbose:
111
+ if daemon:
112
+ self.debug(
113
+ "Starting command in DAEMON mode with a "
114
+ f"queue of {bucket_size} emails",
115
+ color="cyan",
116
+ )
117
+ else:
118
+ self.debug(
119
+ "Starting a queue of {} emails".format(bucket_size),
120
+ color="blue",
121
+ )
122
+
123
+ # In if requested set sending status for all the list to False
124
+ if clear:
125
+ EmailMessage.objects.filter(sending=True).update(sending=False)
126
+
127
+ # Get a bunch of emails in the queue
128
+ connection = None
129
+
130
+ # If daemon mode is requested
131
+ first = True
132
+ while first or daemon:
133
+ # Get a bucket of emails
134
+ emails = EmailMessage.objects.filter(
135
+ sent=False,
136
+ sending=False,
137
+ error=False,
138
+ )
139
+
140
+ # If we do not have to send now we have to wait for the next retry
141
+ if not sendnow:
142
+ emails = emails.filter(
143
+ next_retry__lte=timezone.now(),
144
+ )
145
+
146
+ # Order emails by priority and next retry
147
+ emails = emails.order_by("priority", "next_retry")
148
+
149
+ # Send in buckets if we are not doing them all
150
+ if not doall:
151
+ emails = emails[0 : bucket_size + 1]
152
+
153
+ # Check if there are emails to process
154
+ if emails:
155
+ # Convert to list
156
+ list_emails = [x.pk for x in emails]
157
+
158
+ # Set sending status for all the list
159
+ EmailMessage.objects.filter(pk__in=list_emails).update(
160
+ sending=True
161
+ )
162
+
163
+ # For each email
164
+ for email in emails:
165
+ if verbose:
166
+ self.debug(
167
+ "Sending to {}".format(email.eto),
168
+ color="white",
169
+ tail=False,
170
+ )
171
+
172
+ # Check if we have connection
173
+ if not connection:
174
+ if verbose:
175
+ self.debug(
176
+ " - Connecting",
177
+ color="yellow",
178
+ head=False,
179
+ tail=False,
180
+ )
181
+ connection = email.connect()
182
+
183
+ # Send the email
184
+ try:
185
+ email.send(connection, debug=False)
186
+ except Exception as e:
187
+ email.sending = False
188
+ error = "Exception: {}\n".format(e)
189
+ if email.log:
190
+ email.log += error
191
+ else:
192
+ email.log = error
193
+ email.save()
194
+ self.error(error)
195
+ if verbose:
196
+ if email.sent:
197
+ self.debug(" -> SENT", color="green", head=False)
198
+ else:
199
+ self.debug(
200
+ " -> ERROR",
201
+ color="red",
202
+ head=False,
203
+ tail=False,
204
+ )
205
+ self.debug(
206
+ " ({} retries left)".format(
207
+ getattr(
208
+ settings, "CLIENT_EMAIL_RETRIES", 10
209
+ )
210
+ - email.retries
211
+ ),
212
+ color="cyan",
213
+ head=False,
214
+ )
215
+
216
+ # Delete all that have been sent
217
+ if not getattr(settings, "CLIENT_EMAIL_HISTORY", True):
218
+ EmailMessage.objects.filter(
219
+ pk__in=list_emails, sent=True
220
+ ).delete()
221
+
222
+ elif daemon:
223
+ # Sleep for a while
224
+ try:
225
+ time.sleep(10)
226
+ except KeyboardInterrupt:
227
+ self.debug("Exited by user request!", color="green")
228
+ break
229
+
230
+ elif verbose:
231
+ # No emails to send
232
+ self.debug(
233
+ "No emails to be sent at this moment in the queue!",
234
+ color="green",
235
+ )
236
+
237
+ # This was the first time
238
+ first = False