Browse Source

Renaming,classmethod instead of staticmethod,'' instead of ""

Guénaël Muller 7 years ago
parent
commit
02bd4dd7db

+ 1 - 1
tracim/development.ini.base View File

@@ -224,7 +224,7 @@ email.reply.imap.folder = INBOX
224 224
 # Token for communication between mail fetcher and tracim controller
225 225
 email.reply.token = mysecuretoken
226 226
 # Delay in seconds between each check
227
-email.reply.delay = 60
227
+email.reply.check.heartbeat = 60
228 228
 
229 229
 ## Radical (CalDav server) configuration
230 230
 # radicale.server.host = 0.0.0.0

+ 2 - 2
tracim/tracim/config/app_cfg.py View File

@@ -373,8 +373,8 @@ class CFG(object):
373 373
         self.EMAIL_REPLY_IMAP_FOLDER = tg.config.get(
374 374
             'email.reply.imap.folder',
375 375
         )
376
-        self.EMAIL_REPLY_DELAY = int(tg.config.get(
377
-            'email.reply.delay',
376
+        self.EMAIL_REPLY_CHECK_HEARTBEAT = int(tg.config.get(
377
+            'email.reply.check.heartbeat',
378 378
         ))
379 379
         self.EMAIL_REPLY_TOKEN = tg.config.get(
380 380
             'email.reply.token',

+ 1 - 1
tracim/tracim/controllers/events.py View File

@@ -8,7 +8,7 @@ from tracim.lib.user import UserApi
8 8
 from tracim.model.data import ContentType
9 9
 
10 10
 
11
-class EventsRestController(RestController):
11
+class EventRestController(RestController):
12 12
 
13 13
     @tg.expose('json')
14 14
     def post(self):

+ 2 - 2
tracim/tracim/controllers/root.py View File

@@ -22,7 +22,7 @@ from tracim.controllers.help import HelpController
22 22
 from tracim.controllers.previews import PreviewsController
23 23
 from tracim.controllers.user import UserRestController
24 24
 from tracim.controllers.workspace import UserWorkspaceRestController
25
-from tracim.controllers.events import EventsRestController
25
+from tracim.controllers.events import EventRestController
26 26
 from tracim.lib import CST
27 27
 from tracim.lib.base import logger
28 28
 from tracim.lib.content import ContentApi
@@ -62,7 +62,7 @@ class RootController(StandardController):
62 62
     previews = PreviewsController()
63 63
 
64 64
     content = ContentController()
65
-    events = EventsRestController()
65
+    events = EventRestController()
66 66
     # api
67 67
     api = APIController()
68 68
 

+ 1 - 1
tracim/tracim/lib/daemons.py View File

@@ -172,7 +172,7 @@ class MailFetcherDaemon(Daemon):
172 172
             user=cfg.EMAIL_REPLY_IMAP_USER,
173 173
             password=cfg.EMAIL_REPLY_IMAP_PASSWORD,
174 174
             folder=cfg.EMAIL_REPLY_IMAP_FOLDER,
175
-            delay=cfg.EMAIL_REPLY_DELAY,
175
+            delay=cfg.EMAIL_REPLY_CHECK_HEARTBEAT,
176 176
             # FIXME - G.M - 2017-11-15 - proper tracim url formatting
177 177
             endpoint=cfg.WEBSITE_BASE_URL + "/events",
178 178
             token=cfg.EMAIL_REPLY_TOKEN,

+ 39 - 30
tracim/tracim/lib/email_fetcher.py View File

@@ -18,9 +18,9 @@ from email_reply_parser import EmailReplyParser
18 18
 
19 19
 from tracim.lib.base import logger
20 20
 
21
-TRACIM_SPECIAL_KEY_HEADER = "X-Tracim-Key"
22
-BS_HTML_BODY_PARSE_CONFIG = {
23
-    'tag_blacklist': ["script", "style", "blockquote"],
21
+TRACIM_SPECIAL_KEY_HEADER = 'X-Tracim-Key'
22
+BEAUTIFULSOUP_HTML_BODY_PARSE_CONFIG = {
23
+    'tag_blacklist': ['script', 'style', 'blockquote'],
24 24
     'class_blacklist': ['moz-cite-prefix', 'gmail_extra', 'gmail_quote',
25 25
                         'yahoo_quoted'],
26 26
     'id_blacklist': ['reply-intro'],
@@ -29,6 +29,8 @@ BS_HTML_BODY_PARSE_CONFIG = {
29 29
                       'thead', 'tr', 'td', 'tbody', 'table', 'p', 'pre'],
30 30
     'attrs_whitelist': ['href'],
31 31
 }
32
+CONTENT_TYPE_TEXT_PLAIN = 'text/plain'
33
+CONTENT_TYPE_TEXT_HTML = 'text/html'
32 34
 
33 35
 
34 36
 class DecodedMail(object):
@@ -63,30 +65,30 @@ class DecodedMail(object):
63 65
         body = None
64 66
         if body_part:
65 67
             charset = body_part.get_content_charset('iso-8859-1')
66
-            ctype = body_part.get_content_type()
67
-            if ctype == "text/plain":
68
+            content_type = body_part.get_content_type()
69
+            if content_type == CONTENT_TYPE_TEXT_PLAIN:
68 70
                 txt_body = body_part.get_payload(decode=True).decode(
69 71
                     charset)
70 72
                 body = DecodedMail._parse_txt_body(txt_body)
71 73
 
72
-            elif ctype == "text/html":
74
+            elif content_type == CONTENT_TYPE_TEXT_HTML:
73 75
                 html_body = body_part.get_payload(decode=True).decode(
74 76
                     charset)
75 77
                 body = DecodedMail._parse_html_body(html_body)
76 78
 
77 79
         return body
78 80
 
79
-    @staticmethod
80
-    def _parse_txt_body(txt_body: str):
81
+    @classmethod
82
+    def _parse_txt_body(cls, txt_body: str):
81 83
         txt_body = EmailReplyParser.parse_reply(txt_body)
82 84
         html_body = markdown.markdown(txt_body)
83 85
         body = DecodedMail._parse_html_body(html_body)
84 86
         return body
85 87
 
86
-    @staticmethod
87
-    def _parse_html_body(html_body: str):
88
+    @classmethod
89
+    def _parse_html_body(cls, html_body: str):
88 90
         soup = BeautifulSoup(html_body)
89
-        config = BS_HTML_BODY_PARSE_CONFIG
91
+        config = BEAUTIFULSOUP_HTML_BODY_PARSE_CONFIG
90 92
         for tag in soup.findAll():
91 93
             if DecodedMail._tag_to_extract(tag):
92 94
                 tag.extract()
@@ -99,9 +101,9 @@ class DecodedMail(object):
99 101
                 tag.unwrap()
100 102
         return str(soup)
101 103
 
102
-    @staticmethod
103
-    def _tag_to_extract(tag) -> bool:
104
-        config = BS_HTML_BODY_PARSE_CONFIG
104
+    @classmethod
105
+    def _tag_to_extract(cls, tag) -> bool:
106
+        config = BEAUTIFULSOUP_HTML_BODY_PARSE_CONFIG
105 107
         if tag.name.lower() in config['tag_blacklist']:
106 108
             return True
107 109
         if 'class' in tag.attrs:
@@ -114,22 +116,23 @@ class DecodedMail(object):
114 116
                     return True
115 117
         return False
116 118
 
117
-
118 119
     def _get_mime_body_message(self) -> typing.Optional[Message]:
119 120
         # FIXME - G.M - 2017-11-16 - Use stdlib msg.get_body feature for py3.6+
120 121
         # FIXME - G.M - 2017-11-16 - Check support for non-multipart mail
121 122
         part = None
122 123
         # Check for html
123 124
         for part in self._message.walk():
124
-            ctype = part.get_content_type()
125
-            cdispo = str(part.get('Content-Disposition'))
126
-            if ctype == 'text/html' and 'attachment' not in cdispo:
125
+            content_type = part.get_content_type()
126
+            content_dispo = str(part.get('Content-Disposition'))
127
+            if content_type == CONTENT_TYPE_TEXT_HTML \
128
+                    and 'attachment' not in content_dispo:
127 129
                 return part
128 130
         # check for plain text
129 131
         for part in self._message.walk():
130
-            ctype = part.get_content_type()
131
-            cdispo = str(part.get('Content-Disposition'))
132
-            if ctype == 'text/plain' and 'attachment' not in cdispo:
132
+            content_type = part.get_content_type()
133
+            content_dispo = str(part.get('Content-Disposition'))
134
+            if content_type == CONTENT_TYPE_TEXT_PLAIN and 'attachment' \
135
+                    not in content_dispo:
133 136
                 return part
134 137
         return part
135 138
 
@@ -153,8 +156,8 @@ class DecodedMail(object):
153 156
 
154 157
         return key
155 158
 
156
-    @staticmethod
157
-    def find_key_from_mail_address(mail_address: str) \
159
+    @classmethod
160
+    def find_key_from_mail_address(cls, mail_address: str) \
158 161
             -> typing.Optional[str]:
159 162
         """ Parse mail_adress-like string
160 163
         to retrieve key.
@@ -174,8 +177,14 @@ class DecodedMail(object):
174 177
 class MailFetcher(object):
175 178
 
176 179
     def __init__(self,
177
-                 host: str, port: str, user: str, password: str, folder: str,
178
-                 delay: int, endpoint: str, token:str) \
180
+                 host: str,
181
+                 port: str,
182
+                 user: str,
183
+                 password: str,
184
+                 folder: str,
185
+                 delay: int,
186
+                 endpoint: str,
187
+                 token: str) \
179 188
             -> None:
180 189
         """
181 190
         Fetch mail from a mailbox folder through IMAP and add their content to
@@ -269,11 +278,11 @@ class MailFetcher(object):
269 278
         unsended_mail = []
270 279
         while self._mails:
271 280
             mail = self._mails.pop()
272
-            msg = {"token": self.token,
273
-                   "user_mail": mail.get_from_address(),
274
-                   "content_id": mail.get_key(),
275
-                   "payload": {
276
-                       "content": mail.get_body(),
281
+            msg = {'token': self.token,
282
+                   'user_mail': mail.get_from_address(),
283
+                   'content_id': mail.get_key(),
284
+                   'payload': {
285
+                       'content': mail.get_body(),
277 286
                    }}
278 287
             try:
279 288
                 r = requests.post(self.endpoint, json=msg)