瀏覽代碼

reorganize code of tracim.controllers.events.EventRestController#post

Bastien Sevajol 7 年之前
父節點
當前提交
e4605de686
共有 1 個文件被更改,包括 68 次插入58 次删除
  1. 68 58
      tracim/tracim/controllers/events.py

+ 68 - 58
tracim/tracim/controllers/events.py 查看文件

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