Browse Source

Completes notification logging

Adrien Panay 7 years ago
parent
commit
d3915bd391

+ 9 - 6
tracim/tracim/lib/email.py View File

1
 # -*- coding: utf-8 -*-
1
 # -*- coding: utf-8 -*-
2
 import smtplib
2
 import smtplib
3
+import typing
3
 from email.message import Message
4
 from email.message import Message
4
 from email.mime.multipart import MIMEMultipart
5
 from email.mime.multipart import MIMEMultipart
5
 from email.mime.text import MIMEText
6
 from email.mime.text import MIMEText
6
 
7
 
7
-import typing
8
 from mako.template import Template
8
 from mako.template import Template
9
 from tg.i18n import ugettext as _
9
 from tg.i18n import ugettext as _
10
 
10
 
11
 from tracim.lib.base import logger
11
 from tracim.lib.base import logger
12
-from tracim.model import User
13
-
14
 from tracim.lib.utils import get_rq_queue
12
 from tracim.lib.utils import get_rq_queue
13
+from tracim.model import User
15
 
14
 
16
 
15
 
17
 def send_email_through(
16
 def send_email_through(
53
         self.password = password
52
         self.password = password
54
 
53
 
55
 
54
 
56
-
57
 class EmailSender(object):
55
 class EmailSender(object):
58
     """
56
     """
59
     this class allow to send emails and has no relations with SQLAlchemy and other tg HTTP request environment
57
     this class allow to send emails and has no relations with SQLAlchemy and other tg HTTP request environment
86
             self._smtp_connection.quit()
84
             self._smtp_connection.quit()
87
             logger.info(self, 'Connection closed.')
85
             logger.info(self, 'Connection closed.')
88
 
86
 
89
-
90
     def send_mail(self, message: MIMEMultipart):
87
     def send_mail(self, message: MIMEMultipart):
91
         if not self._is_active:
88
         if not self._is_active:
92
             logger.info(self, 'Not sending email to {} (service desactivated)'.format(message['To']))
89
             logger.info(self, 'Not sending email to {} (service desactivated)'.format(message['To']))
93
         else:
90
         else:
94
-            self.connect() # Acutally, this connects to SMTP only if required
91
+            self.connect()  # Acutally, this connects to SMTP only if required
95
             logger.info(self, 'Sending email to {}'.format(message['To']))
92
             logger.info(self, 'Sending email to {}'.format(message['To']))
96
             self._smtp_connection.send_message(message)
93
             self._smtp_connection.send_message(message)
94
+            from tracim.lib.notifications import EmailNotifier
95
+            EmailNotifier.log_notification(
96
+                action='   SENT',
97
+                recipient=message['To'],
98
+                subject=message['Subject'],
99
+            )
97
 
100
 
98
 
101
 
99
 class EmailManager(object):
102
 class EmailManager(object):

+ 20 - 13
tracim/tracim/lib/notifications.py View File

1
 # -*- coding: utf-8 -*-
1
 # -*- coding: utf-8 -*-
2
 import datetime
2
 import datetime
3
+import typing
3
 
4
 
4
 from email.header import Header
5
 from email.header import Header
5
 from email.mime.multipart import MIMEMultipart
6
 from email.mime.multipart import MIMEMultipart
6
 from email.mime.text import MIMEText
7
 from email.mime.text import MIMEText
7
 
8
 
8
-import lxml
9
 from lxml.html.diff import htmldiff
9
 from lxml.html.diff import htmldiff
10
 
10
 
11
 from mako.template import Template
11
 from mako.template import Template
12
 
12
 
13
 from tracim.lib.base import logger
13
 from tracim.lib.base import logger
14
+from tracim.lib.email import EmailSender
14
 from tracim.lib.email import SmtpConfiguration
15
 from tracim.lib.email import SmtpConfiguration
15
 from tracim.lib.email import send_email_through
16
 from tracim.lib.email import send_email_through
16
-from tracim.lib.email import EmailSender
17
 from tracim.lib.user import UserApi
17
 from tracim.lib.user import UserApi
18
-from tracim.lib.workspace import WorkspaceApi
19
 from tracim.lib.utils import lazy_ugettext as l_
18
 from tracim.lib.utils import lazy_ugettext as l_
20
-from tracim.model.serializers import Context
19
+from tracim.lib.workspace import WorkspaceApi
20
+from tracim.model.auth import User
21
+from tracim.model.data import ActionDescription
22
+from tracim.model.data import Content
23
+from tracim.model.data import ContentType
24
+from tracim.model.data import UserRoleInWorkspace
21
 from tracim.model.serializers import CTX
25
 from tracim.model.serializers import CTX
26
+from tracim.model.serializers import Context
22
 from tracim.model.serializers import DictLikeClass
27
 from tracim.model.serializers import DictLikeClass
23
 
28
 
24
-from tracim.model.data import Content, UserRoleInWorkspace, ContentType, \
25
-    ActionDescription
26
-from tracim.model.auth import User
27
-
28
 
29
 
29
 class INotifier(object):
30
 class INotifier(object):
30
     """
31
     """
212
         )
213
         )
213
 
214
 
214
     @staticmethod
215
     @staticmethod
215
-    def _log_notification(
216
-            recipient: str,
217
-            subject: str
216
+    def log_notification(
217
+            action: str,
218
+            recipient: typing.Optional[str],
219
+            subject: typing.Optional[str],
218
     ) -> None:
220
     ) -> None:
219
         """Log notification metadata."""
221
         """Log notification metadata."""
220
         from tracim.config.app_cfg import CFG
222
         from tracim.config.app_cfg import CFG
221
         log_path = CFG.get_instance().EMAIL_NOTIFICATION_LOG_FILE_PATH
223
         log_path = CFG.get_instance().EMAIL_NOTIFICATION_LOG_FILE_PATH
222
         if log_path:
224
         if log_path:
225
+            # TODO - A.P - 2017-09-06 - file logging inefficiency
226
+            # Updating a document with 100 users to notify will leads to open
227
+            # and close the file 100 times.
223
             with open(log_path, 'a') as log_file:
228
             with open(log_path, 'a') as log_file:
224
                 print(
229
                 print(
225
                     datetime.datetime.now(),
230
                     datetime.datetime.now(),
231
+                    action,
226
                     recipient,
232
                     recipient,
227
                     subject,
233
                     subject,
228
                     sep='|',
234
                     sep='|',
296
             message.attach(part1)
302
             message.attach(part1)
297
             message.attach(part2)
303
             message.attach(part2)
298
 
304
 
299
-            self._log_notification(
300
-                recipient=message['for'],
305
+            self.log_notification(
306
+                action='CREATED',
307
+                recipient=message['To'],
301
                 subject=message['Subject'],
308
                 subject=message['Subject'],
302
             )
309
             )
303
             send_email_through(async_email_sender.send_mail, message)
310
             send_email_through(async_email_sender.send_mail, message)

+ 18 - 5
tracim/tracim/tests/library/test_notification.py View File

49
 class TestEmailNotifier(TestStandard):
49
 class TestEmailNotifier(TestStandard):
50
 
50
 
51
     def test_unit__log_notification(self):
51
     def test_unit__log_notification(self):
52
+        """Check file and format of notification log."""
52
         log_path = CFG.get_instance().EMAIL_NOTIFICATION_LOG_FILE_PATH
53
         log_path = CFG.get_instance().EMAIL_NOTIFICATION_LOG_FILE_PATH
53
-        pattern = '\|{rec}\|{subj}$\\n'
54
+        pattern = '\|{act}\|{rec}\|{subj}$\\n'
55
+        line_1_act = 'CREATED'
54
         line_1_rec = 'user 1 <us.er@o.ne>'
56
         line_1_rec = 'user 1 <us.er@o.ne>'
55
         line_1_subj = 'notification 1'
57
         line_1_subj = 'notification 1'
56
-        line_1_pattern = pattern.format(rec=line_1_rec, subj=line_1_subj)
58
+        line_1_pattern = pattern.format(
59
+            act=line_1_act,
60
+            rec=line_1_rec,
61
+            subj=line_1_subj,
62
+        )
63
+        line_2_act = '   SENT'
57
         line_2_rec = 'user 2 <us.er@t.wo>'
64
         line_2_rec = 'user 2 <us.er@t.wo>'
58
         line_2_subj = 'notification 2'
65
         line_2_subj = 'notification 2'
59
-        line_2_pattern = pattern.format(rec=line_2_rec, subj=line_2_subj)
60
-        EmailNotifier._log_notification(
66
+        line_2_pattern = pattern.format(
67
+            act=line_2_act,
68
+            rec=line_2_rec,
69
+            subj=line_2_subj,
70
+        )
71
+        EmailNotifier.log_notification(
72
+            action=line_1_act,
61
             recipient=line_1_rec,
73
             recipient=line_1_rec,
62
             subject=line_1_subj,
74
             subject=line_1_subj,
63
         )
75
         )
64
-        EmailNotifier._log_notification(
76
+        EmailNotifier.log_notification(
77
+            action=line_2_act,
65
             recipient=line_2_rec,
78
             recipient=line_2_rec,
66
             subject=line_2_subj,
79
             subject=line_2_subj,
67
         )
80
         )