浏览代码

Context-related-model interface for user

Guénaël Muller 7 年前
父节点
当前提交
85027f5ed7

+ 1 - 1
tracim/models/auth.py 查看文件

159
             profile_id = max(group.group_id for group in self.groups)
159
             profile_id = max(group.group_id for group in self.groups)
160
         return Profile(profile_id)
160
         return Profile(profile_id)
161
 
161
 
162
-    # TODO - G-M - 27-03-2018 - [Calendar] Check about calendar code
162
+    # TODO - G-M - 20-04-2018 - [Calendar] Replace this in context model object
163
     # @property
163
     # @property
164
     # def calendar_url(self) -> str:
164
     # def calendar_url(self) -> str:
165
     #     # TODO - 20160531 - Bastien: Cyclic import if import in top of file
165
     #     # TODO - 20160531 - Bastien: Cyclic import if import in top of file

+ 66 - 0
tracim/models/context_models.py 查看文件

1
+# coding=utf-8
2
+import typing
3
+from datetime import datetime
4
+
5
+from sqlalchemy.orm import Session
6
+from tracim import CFG
7
+from tracim.models import User
8
+from tracim.models.auth import Profile
9
+
10
+
11
+class UserInContext(object):
12
+    """
13
+    Interface to get User data and User data related to context.
14
+    """
15
+
16
+    def __init__(self, user: User, dbsession: Session, config: CFG):
17
+        self.user = user
18
+        self.dbsession = dbsession
19
+        self.config = config
20
+
21
+    # Default
22
+
23
+    @property
24
+    def email(self) -> str:
25
+        return self.user.email
26
+
27
+    @property
28
+    def user_id(self) -> int:
29
+        return self.user.user_id
30
+
31
+    @property
32
+    def display_name(self) -> str:
33
+        return self.user.display_name
34
+
35
+    @property
36
+    def created(self) -> datetime:
37
+        return self.user.created
38
+
39
+    @property
40
+    def is_active(self) -> bool:
41
+        return self.user.is_active
42
+
43
+    @property
44
+    def timezone(self) -> str:
45
+        return self.user.timezone
46
+
47
+    @property
48
+    def profile(self) -> Profile:
49
+        return self.user.profile
50
+
51
+    # Context related
52
+
53
+    @property
54
+    def calendar_url(self) -> typing.Optional[str]:
55
+        # TODO - G-M - 20-04-2018 - [Calendar] Replace calendar code to get
56
+        # url calendar url.
57
+        #
58
+        # from tracim.lib.calendar import CalendarManager
59
+        # calendar_manager = CalendarManager(None)
60
+        # return calendar_manager.get_workspace_calendar_url(self.workspace_id)
61
+        return None
62
+
63
+    @property
64
+    def avatar_url(self) -> typing.Optional[str]:
65
+        # TODO - G-M - 20-04-2018 - [Avatar] Add user avatar feature
66
+        return None

+ 1 - 1
tracim/models/data.py 查看文件

74
 
74
 
75
         return contents
75
         return contents
76
 
76
 
77
-    # TODO - G-M - 27-03-2018 - [Calendar] Check about calendar code
77
+    # TODO - G-M - 27-03-2018 - [Calendar] Replace this in context model object
78
     # @property
78
     # @property
79
     # def calendar_url(self) -> str:
79
     # def calendar_url(self) -> str:
80
     #     # TODO - 20160531 - Bastien: Cyclic import if import in top of file
80
     #     # TODO - 20160531 - Bastien: Cyclic import if import in top of file

+ 2 - 0
tracim/tests/functional/test_session.py 查看文件

67
         assert res.json_body['profile']
67
         assert res.json_body['profile']
68
         assert isinstance(res.json_body['profile']['id'], int)
68
         assert isinstance(res.json_body['profile']['id'], int)
69
         assert res.json_body['profile']['slug'] == 'administrators'
69
         assert res.json_body['profile']['slug'] == 'administrators'
70
+        assert res.json_body['caldav_url'] is None
71
+        assert res.json_body['avatar_url'] is None
70
 
72
 
71
     def test_unauthenticated(self):
73
     def test_unauthenticated(self):
72
         self.testapp.authorization = (
74
         self.testapp.authorization = (

+ 1 - 2
tracim/views/core_api/schemas.py 查看文件

18
     # TODO - G.M - 17-04-2018 - check this, relative url allowed ?
18
     # TODO - G.M - 17-04-2018 - check this, relative url allowed ?
19
     caldav_url = marshmallow.fields.Url(
19
     caldav_url = marshmallow.fields.Url(
20
         allow_none=True,
20
         allow_none=True,
21
-        default=None,
22
         relative=True,
21
         relative=True,
23
         attribute='calendar_url'
22
         attribute='calendar_url'
24
     )
23
     )
25
-    avatar_url = marshmallow.fields.Url(allow_none=True, default=None)
24
+    avatar_url = marshmallow.fields.Url(allow_none=True)
26
     profile = marshmallow.fields.Nested(
25
     profile = marshmallow.fields.Nested(
27
         ProfileSchema,
26
         ProfileSchema,
28
         many=False,
27
         many=False,

+ 7 - 1
tracim/views/core_api/session_controller.py 查看文件

9
 from tracim import TracimRequest
9
 from tracim import TracimRequest
10
 from tracim.extensions import hapic
10
 from tracim.extensions import hapic
11
 from tracim.lib.core.user import UserApi
11
 from tracim.lib.core.user import UserApi
12
+from tracim.models.context_models import UserInContext
12
 from tracim.views.controllers import Controller
13
 from tracim.views.controllers import Controller
13
 from pyramid.config import Configurator
14
 from pyramid.config import Configurator
14
 
15
 
85
         """
86
         """
86
         Return current logged in user or 401
87
         Return current logged in user or 401
87
         """
88
         """
88
-        return request.current_user
89
+        app_config = request.registry.settings['CFG']
90
+        return UserInContext(
91
+            user=request.current_user,
92
+            dbsession=request.dbsession,
93
+            config=app_config,
94
+        )
89
 
95
 
90
     def bind(self, configurator: Configurator):
96
     def bind(self, configurator: Configurator):
91
 
97