Преглед на файлове

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

Guénaël Muller преди 6 години
родител
ревизия
66cc93ffcf
променени са 1 файла, в които са добавени 55 реда и са изтрити 6 реда
  1. 55 6
      tracim/lib/core/user.py

+ 55 - 6
tracim/lib/core/user.py Целия файл

8
 from sqlalchemy.orm.exc import NoResultFound
8
 from sqlalchemy.orm.exc import NoResultFound
9
 from tracim.exceptions import BadUserPassword
9
 from tracim.exceptions import BadUserPassword
10
 from tracim.exceptions import AuthenticationFailed
10
 from tracim.exceptions import AuthenticationFailed
11
+from tracim.models.context_models import UserInContext
11
 
12
 
12
 
13
 
13
 class UserApi(object):
14
 class UserApi(object):
20
     def _base_query(self):
21
     def _base_query(self):
21
         return self._session.query(User)
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
     def get_all(self) -> typing.Iterable[User]:
77
     def get_all(self) -> typing.Iterable[User]:
33
         return self._session.query(User).order_by(User.display_name).all()
78
         return self._session.query(User).order_by(User.display_name).all()
34
 
79
 
80
+    # Check methods
81
+
35
     def user_with_email_exists(self, email: str):
82
     def user_with_email_exists(self, email: str):
36
         try:
83
         try:
37
             self.get_one_by_email(email)
84
             self.get_one_by_email(email)
57
         except (BadUserPassword, NoResultFound):
104
         except (BadUserPassword, NoResultFound):
58
             raise AuthenticationFailed
105
             raise AuthenticationFailed
59
 
106
 
107
+    # Actions
108
+
60
     def update(
109
     def update(
61
             self,
110
             self,
62
             user: User,
111
             user: User,