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,5 +1,6 @@
1 1
 import tg
2 2
 from tg import request
3
+from tg import Response
3 4
 from tg import abort
4 5
 from tg import RestController
5 6
 from sqlalchemy.orm.exc import NoResultFound
@@ -17,40 +18,72 @@ class EventRestController(RestController):
17 18
         try:
18 19
             json = request.json_body
19 20
         except:
20
-            abort(400,'Bad json')
21
+            return Response(
22
+                status=400,
23
+                json_body={'msg': 'Bad json'},
24
+            )
21 25
         cfg = CFG.get_instance()
22 26
         if 'token' in json and json['token'] == cfg.EMAIL_REPLY_TOKEN:
23 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 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 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 42
             uapi = UserApi(None)
30 43
             try:
31 44
                 user = uapi.get_one_by_email(json['user_mail'])
32 45
             except NoResultFound:
33
-                abort(400,'Unknown user email.')
46
+                return Response(
47
+                    status=400,
48
+                    json_body={'msg': 'Unknown user email'},
49
+                )
34 50
             api = ContentApi(user)
35 51
 
36 52
             try:
37 53
                 thread = api.get_one(json['content_id'],
38 54
                                      content_type=ContentType.Any)
39 55
             except NoResultFound:
40
-                abort(400,'Unknown content_id.')
56
+                return Response(
57
+                    status=400,
58
+                    json_body={'msg': 'Unknown content_id'},
59
+                )
41 60
             # INFO - G.M - 2017-11-17
42 61
             # When content_id is a sub-elem of a main content like Comment,
43 62
             # Attach the thread to the main content.
44 63
             if thread.type == ContentType.Comment:
45 64
                 thread = thread.parent
46 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 70
             if 'content' in json['payload']:
50 71
                 api.create_comment(thread.workspace, thread,
51 72
                                    json['payload']['content'], True)
52
-                abort(204)
73
+                return Response(
74
+                    status=204,
75
+                )
53 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 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
+            )