瀏覽代碼

Use http status_code instead of json content for EventController

Guénaël Muller 7 年之前
父節點
當前提交
963da2c812
共有 2 個文件被更改,包括 26 次插入31 次删除
  1. 23 19
      tracim/tracim/controllers/events.py
  2. 3 12
      tracim/tracim/lib/email_fetcher.py

+ 23 - 19
tracim/tracim/controllers/events.py 查看文件

1
 import tg
1
 import tg
2
 from tg import request
2
 from tg import request
3
+from tg import abort
3
 from tg import RestController
4
 from tg import RestController
4
 from sqlalchemy.orm.exc import NoResultFound
5
 from sqlalchemy.orm.exc import NoResultFound
5
 
6
 
6
 from tracim.lib.content import ContentApi
7
 from tracim.lib.content import ContentApi
7
 from tracim.lib.user import UserApi
8
 from tracim.lib.user import UserApi
8
 from tracim.model.data import ContentType
9
 from tracim.model.data import ContentType
10
+from tracim.config.app_cfg import CFG
9
 
11
 
10
 
12
 
11
 class EventRestController(RestController):
13
 class EventRestController(RestController):
12
 
14
 
13
     @tg.expose('json')
15
     @tg.expose('json')
14
     def post(self):
16
     def post(self):
15
-        json = request.json_body
16
-
17
-        from tracim.config.app_cfg import CFG
17
+        try:
18
+            json = request.json_body
19
+        except:
20
+            abort(400,'Bad json')
18
         cfg = CFG.get_instance()
21
         cfg = CFG.get_instance()
19
-
20
         if 'token' in json and json['token'] == cfg.EMAIL_REPLY_TOKEN:
22
         if 'token' in json and json['token'] == cfg.EMAIL_REPLY_TOKEN:
21
-            if 'user_mail' not in json or 'content_id' not in json:
22
-                return {'status': 'error',
23
-                        'error': 'bad json', }
23
+            if 'user_mail' not in json:
24
+                abort(400,'Bad sson : user_mail is required.')
25
+            if 'content_id' not in json:
26
+                abort(400, 'Bad json : content_id is required.')
27
+            if  'payload' not in json:
28
+                abort(400, 'Bad json : payload is required.')
24
             uapi = UserApi(None)
29
             uapi = UserApi(None)
25
-            # TODO support Empty result error
26
             try:
30
             try:
27
                 user = uapi.get_one_by_email(json['user_mail'])
31
                 user = uapi.get_one_by_email(json['user_mail'])
28
             except NoResultFound:
32
             except NoResultFound:
29
-                return {'status': 'error',
30
-                        'error': 'bad user mail', }
33
+                abort(400,'Unknown user email.')
31
             api = ContentApi(user)
34
             api = ContentApi(user)
32
 
35
 
33
             try:
36
             try:
34
                 thread = api.get_one(json['content_id'],
37
                 thread = api.get_one(json['content_id'],
35
                                      content_type=ContentType.Any)
38
                                      content_type=ContentType.Any)
36
             except NoResultFound:
39
             except NoResultFound:
37
-                return {'status': 'error',
38
-                        'error': 'bad content id', }
40
+                abort(400,'Unknown content_id.')
39
             # INFO - G.M - 2017-11-17
41
             # INFO - G.M - 2017-11-17
40
             # When content_id is a sub-elem of a main content like Comment,
42
             # When content_id is a sub-elem of a main content like Comment,
41
             # Attach the thread to the main content.
43
             # Attach the thread to the main content.
42
             if thread.type == ContentType.Comment:
44
             if thread.type == ContentType.Comment:
43
                 thread = thread.parent
45
                 thread = thread.parent
44
             if thread.type == ContentType.Folder:
46
             if thread.type == ContentType.Folder:
45
-                return {'status': 'error',
46
-                        'error': 'comment for folder not allowed', }
47
-            api.create_comment(thread.workspace, thread,
48
-                               json['payload']['content'], True)
49
-            return {'status': 'ok', }
47
+                abort(400,'comment for folder not allowed')
48
+
49
+            if 'content' in json['payload']:
50
+                api.create_comment(thread.workspace, thread,
51
+                                   json['payload']['content'], True)
52
+                abort(204)
53
+            else:
54
+                abort(400,'No content to add new comment')
50
         else:
55
         else:
51
-            return {'status': 'error',
52
-                    'error': 'invalid token', }
56
+            abort(403)

+ 3 - 12
tracim/tracim/lib/email_fetcher.py 查看文件

282
                    }}
282
                    }}
283
             try:
283
             try:
284
                 r = requests.post(self.endpoint, json=msg)
284
                 r = requests.post(self.endpoint, json=msg)
285
-                response = r.json()
286
-                if 'status' not in response:
287
-                    log = 'bad response: {}'
288
-                    logger.error(self, log.format(str(response)))
289
-                else:
290
-                    if response['status'] == 'ok':
291
-                        pass
292
-                    elif response['status'] == 'error' and 'error' in response:
293
-                        log = 'error with email: {}'
294
-                        logger.error(self, log.format(str(response['error'])))
295
-                    else:
296
-                        log = 'Unknown error with email'
285
+                if r.status_code not in [200, 204]:
286
+                    log = 'bad status code response when sending mail to tracim: {}' # nopep8
287
+                    logger.error(self, log.format(str(r.status_code)))
297
             # TODO - G.M - Verify exception correctly works
288
             # TODO - G.M - Verify exception correctly works
298
             except requests.exceptions.Timeout:
289
             except requests.exceptions.Timeout:
299
                 log = 'Timeout error to transmit fetched mail to tracim : {}'
290
                 log = 'Timeout error to transmit fetched mail to tracim : {}'