Browse Source

Context-related-model interface for user

Guénaël Muller 6 years ago
parent
commit
85027f5ed7

+ 1 - 1
tracim/models/auth.py View File

@@ -159,7 +159,7 @@ class User(DeclarativeBase):
159 159
             profile_id = max(group.group_id for group in self.groups)
160 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 163
     # @property
164 164
     # def calendar_url(self) -> str:
165 165
     #     # TODO - 20160531 - Bastien: Cyclic import if import in top of file

+ 66 - 0
tracim/models/context_models.py View File

@@ -0,0 +1,66 @@
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 View File

@@ -74,7 +74,7 @@ class Workspace(DeclarativeBase):
74 74
 
75 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 78
     # @property
79 79
     # def calendar_url(self) -> str:
80 80
     #     # TODO - 20160531 - Bastien: Cyclic import if import in top of file

+ 2 - 0
tracim/tests/functional/test_session.py View File

@@ -67,6 +67,8 @@ class TestWhoamiEndpoint(FunctionalTest):
67 67
         assert res.json_body['profile']
68 68
         assert isinstance(res.json_body['profile']['id'], int)
69 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 73
     def test_unauthenticated(self):
72 74
         self.testapp.authorization = (

+ 1 - 2
tracim/views/core_api/schemas.py View File

@@ -18,11 +18,10 @@ class UserSchema(marshmallow.Schema):
18 18
     # TODO - G.M - 17-04-2018 - check this, relative url allowed ?
19 19
     caldav_url = marshmallow.fields.Url(
20 20
         allow_none=True,
21
-        default=None,
22 21
         relative=True,
23 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 25
     profile = marshmallow.fields.Nested(
27 26
         ProfileSchema,
28 27
         many=False,

+ 7 - 1
tracim/views/core_api/session_controller.py View File

@@ -9,6 +9,7 @@ from sqlalchemy.orm.exc import NoResultFound
9 9
 from tracim import TracimRequest
10 10
 from tracim.extensions import hapic
11 11
 from tracim.lib.core.user import UserApi
12
+from tracim.models.context_models import UserInContext
12 13
 from tracim.views.controllers import Controller
13 14
 from pyramid.config import Configurator
14 15
 
@@ -85,7 +86,12 @@ class SessionController(Controller):
85 86
         """
86 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 96
     def bind(self, configurator: Configurator):
91 97