Browse Source

EventController always return json

Guénaël Muller 7 years ago
parent
commit
a020210cef
1 changed files with 44 additions and 11 deletions
  1. 44 11
      tracim/tracim/controllers/events.py

+ 44 - 11
tracim/tracim/controllers/events.py View File

1
 import tg
1
 import tg
2
 from tg import request
2
 from tg import request
3
+from tg import Response
3
 from tg import abort
4
 from tg import abort
4
 from tg import RestController
5
 from tg import RestController
5
 from sqlalchemy.orm.exc import NoResultFound
6
 from sqlalchemy.orm.exc import NoResultFound
17
         try:
18
         try:
18
             json = request.json_body
19
             json = request.json_body
19
         except:
20
         except:
20
-            abort(400,'Bad json')
21
+            return Response(
22
+                status=400,
23
+                json_body={'msg': 'Bad json'},
24
+            )
21
         cfg = CFG.get_instance()
25
         cfg = CFG.get_instance()
22
         if 'token' in json and json['token'] == cfg.EMAIL_REPLY_TOKEN:
26
         if 'token' in json and json['token'] == cfg.EMAIL_REPLY_TOKEN:
23
             if 'user_mail' not in json:
27
             if 'user_mail' not in json:
24
-                abort(400,'Bad sson : user_mail is required.')
28
+                return Response(
29
+                    status=400,
30
+                    json_body={'msg':'Bad json: user_mail is required'}
31
+                )
25
             if 'content_id' not in json:
32
             if 'content_id' not in json:
26
-                abort(400, 'Bad json : content_id is required.')
33
+                return Response(
34
+                    status=400,
35
+                    json_body={'msg':'Bad json: content_id is required'}
36
+                )
27
             if  'payload' not in json:
37
             if  'payload' not in json:
28
-                abort(400, 'Bad json : payload is required.')
38
+                return Response(
39
+                    status=400,
40
+                    json_body={'msg':'Bad json: payload is required'}
41
+                )
29
             uapi = UserApi(None)
42
             uapi = UserApi(None)
30
             try:
43
             try:
31
                 user = uapi.get_one_by_email(json['user_mail'])
44
                 user = uapi.get_one_by_email(json['user_mail'])
32
             except NoResultFound:
45
             except NoResultFound:
33
-                abort(400,'Unknown user email.')
46
+                return Response(
47
+                    status=400,
48
+                    json_body={'msg': 'Unknown user email'},
49
+                )
34
             api = ContentApi(user)
50
             api = ContentApi(user)
35
 
51
 
36
             try:
52
             try:
37
                 thread = api.get_one(json['content_id'],
53
                 thread = api.get_one(json['content_id'],
38
                                      content_type=ContentType.Any)
54
                                      content_type=ContentType.Any)
39
             except NoResultFound:
55
             except NoResultFound:
40
-                abort(400,'Unknown content_id.')
56
+                return Response(
57
+                    status=400,
58
+                    json_body={'msg': 'Unknown content_id'},
59
+                )
41
             # INFO - G.M - 2017-11-17
60
             # INFO - G.M - 2017-11-17
42
             # When content_id is a sub-elem of a main content like Comment,
61
             # When content_id is a sub-elem of a main content like Comment,
43
             # Attach the thread to the main content.
62
             # Attach the thread to the main content.
44
             if thread.type == ContentType.Comment:
63
             if thread.type == ContentType.Comment:
45
                 thread = thread.parent
64
                 thread = thread.parent
46
             if thread.type == ContentType.Folder:
65
             if thread.type == ContentType.Folder:
47
-                abort(400,'comment for folder not allowed')
48
-
66
+                return Response(
67
+                    status=400,
68
+                    json_body={'msg': 'comment for folder not allowed'},
69
+                )
49
             if 'content' in json['payload']:
70
             if 'content' in json['payload']:
50
                 api.create_comment(thread.workspace, thread,
71
                 api.create_comment(thread.workspace, thread,
51
                                    json['payload']['content'], True)
72
                                    json['payload']['content'], True)
52
-                abort(204)
73
+                return Response(
74
+                    status=204,
75
+                )
53
             else:
76
             else:
54
-                abort(400,'No content to add new comment')
77
+                return Response(
78
+                    status=400,
79
+                    json_body={'msg': 'No content to add new comment'},
80
+                )
55
         else:
81
         else:
56
-            abort(403)
82
+            # TODO - G.M - 2017-11-23 - Switch to status 403 ?
83
+            # 403 is a better status code in this case.
84
+            # 403 status response can't now return clean json, because they are
85
+            # handled somewhere else to return html.
86
+            return Response(
87
+                status=400,
88
+                json_body={'msg': 'Invalid token'}
89
+            )