Bladeren bron

use post_load to deserialise object

Guénaël Muller 6 jaren geleden
bovenliggende
commit
4719a871d8

+ 10 - 0
tracim/models/context_models.py Bestand weergeven

8
 from tracim.models.auth import Profile
8
 from tracim.models.auth import Profile
9
 
9
 
10
 
10
 
11
+class LoginCredentials(object):
12
+    """
13
+    Login credentials model for login
14
+    """
15
+
16
+    def __init__(self, email: str, password: str):
17
+        self.email = email
18
+        self.password = password
19
+
20
+
11
 class UserInContext(object):
21
 class UserInContext(object):
12
     """
22
     """
13
     Interface to get User data and User data related to context.
23
     Interface to get User data and User data related to context.

+ 9 - 0
tracim/views/core_api/schemas.py Bestand weergeven

1
 # coding=utf-8
1
 # coding=utf-8
2
 import marshmallow
2
 import marshmallow
3
+from marshmallow import post_load
4
+
5
+from tracim.models.context_models import LoginCredentials, UserInContext
3
 
6
 
4
 
7
 
5
 class ProfileSchema(marshmallow.Schema):
8
 class ProfileSchema(marshmallow.Schema):
8
 
11
 
9
 
12
 
10
 class UserSchema(marshmallow.Schema):
13
 class UserSchema(marshmallow.Schema):
14
+
11
     user_id = marshmallow.fields.Int(dump_only=True)
15
     user_id = marshmallow.fields.Int(dump_only=True)
12
     email = marshmallow.fields.Email(required=True)
16
     email = marshmallow.fields.Email(required=True)
13
     display_name = marshmallow.fields.String()
17
     display_name = marshmallow.fields.String()
29
 
33
 
30
 
34
 
31
 class BasicAuthSchema(marshmallow.Schema):
35
 class BasicAuthSchema(marshmallow.Schema):
36
+
32
     email = marshmallow.fields.Email(required=True)
37
     email = marshmallow.fields.Email(required=True)
33
     password = marshmallow.fields.String(required=True, load_only=True)
38
     password = marshmallow.fields.String(required=True, load_only=True)
34
 
39
 
40
+    @post_load
41
+    def make_login(self, data):
42
+        return LoginCredentials(**data)
43
+
35
 
44
 
36
 class LoginOutputHeaders(marshmallow.Schema):
45
 class LoginOutputHeaders(marshmallow.Schema):
37
     expire_after = marshmallow.fields.String()
46
     expire_after = marshmallow.fields.String()

+ 3 - 3
tracim/views/core_api/session_controller.py Bestand weergeven

30
         """
30
         """
31
         Logs user into the system
31
         Logs user into the system
32
         """
32
         """
33
-        email = request.json_body['email']
34
-        password = request.json_body['password']
33
+
34
+        login = hapic_data.body
35
         app_config = request.registry.settings['CFG']
35
         app_config = request.registry.settings['CFG']
36
         uapi = UserApi(
36
         uapi = UserApi(
37
             None,
37
             None,
38
             session=request.dbsession,
38
             session=request.dbsession,
39
             config=app_config,
39
             config=app_config,
40
         )
40
         )
41
-        return uapi.authenticate_user(email, password)
41
+        return uapi.authenticate_user(login.email, login.password)
42
 
42
 
43
     @hapic.with_api_doc()
43
     @hapic.with_api_doc()
44
     @hapic.output_body(NoContentSchema(), default_http_code=HTTPStatus.NO_CONTENT)  # nopep8
44
     @hapic.output_body(NoContentSchema(), default_http_code=HTTPStatus.NO_CONTENT)  # nopep8