Browse Source

allow to return UserInContext object from user_lib + some cleaning/doc

Guénaël Muller 6 years ago
parent
commit
66cc93ffcf
1 changed files with 55 additions and 6 deletions
  1. 55 6
      tracim/lib/core/user.py

+ 55 - 6
tracim/lib/core/user.py View File

@@ -8,6 +8,7 @@ from tracim.models.auth import User
8 8
 from sqlalchemy.orm.exc import NoResultFound
9 9
 from tracim.exceptions import BadUserPassword
10 10
 from tracim.exceptions import AuthenticationFailed
11
+from tracim.models.context_models import UserInContext
11 12
 
12 13
 
13 14
 class UserApi(object):
@@ -20,18 +21,64 @@ class UserApi(object):
20 21
     def _base_query(self):
21 22
         return self._session.query(User)
22 23
 
23
-    def get_one(self, user_id: int) -> User:
24
-        return self._base_query().filter(User.user_id==user_id).one()
24
+    def _get_correct_user_type(
25
+            self,
26
+            user: User,
27
+            in_context: bool,
28
+    ) -> typing.Union[User, UserInContext]:
29
+        """
30
+        Choose user type object depending on in_context bool.
31
+        :param user:
32
+        :param in_context:
33
+        :return: user as User or UserInContext if in_context is True
34
+        """
35
+        if in_context:
36
+            user = UserInContext(
37
+                user=user,
38
+                dbsession=self._session,
39
+                config=self._config,
40
+            )
41
+        return user
25 42
 
26
-    def get_one_by_email(self, email: str) -> User:
27
-        return self._base_query().filter(User.email==email).one()
43
+    # Getters
28 44
 
29
-    def get_one_by_id(self, id: int) -> User:
30
-        return self._base_query().filter(User.user_id==id).one()
45
+    def get_one(
46
+            self,
47
+            user_id: int,
48
+            in_context: bool=False,
49
+    ) -> typing.Union[UserInContext, User]:
50
+        """
51
+        Get one user by user id
52
+        :param user_id:
53
+        :param in_context: Return User or UserInContext Object
54
+        :return: one user
55
+        """
56
+        user = self._base_query().filter(User.user_id == user_id).one()
57
+        return self._get_correct_user_type(user, in_context)
58
+
59
+    def get_one_by_email(
60
+            self,
61
+            email: str,
62
+            in_context: bool=False,
63
+    ) -> User:
64
+        """
65
+        Get one user by email
66
+        :param email: Email of the user
67
+        :param in_context: Return User or UserInContext Object
68
+        :return: one user
69
+        """
70
+        user = self._base_query().filter(User.email == email).one()
71
+        return self._get_correct_user_type(user, in_context)
72
+
73
+    # FIXME - G.M - 24-04-2018 - Duplicate method with get_one.
74
+    def get_one_by_id(self, id: int, in_context=False) -> User:
75
+        return self.get_one(user_id=id, in_context=in_context)
31 76
 
32 77
     def get_all(self) -> typing.Iterable[User]:
33 78
         return self._session.query(User).order_by(User.display_name).all()
34 79
 
80
+    # Check methods
81
+
35 82
     def user_with_email_exists(self, email: str):
36 83
         try:
37 84
             self.get_one_by_email(email)
@@ -57,6 +104,8 @@ class UserApi(object):
57 104
         except (BadUserPassword, NoResultFound):
58 105
             raise AuthenticationFailed
59 106
 
107
+    # Actions
108
+
60 109
     def update(
61 110
             self,
62 111
             user: User,