Ver código fonte

fix user creation with command and email notification

Guénaël Muller 6 anos atrás
pai
commit
37d33febe1
4 arquivos alterados com 39 adições e 13 exclusões
  1. 21 6
      tracim/command/user.py
  2. 4 0
      tracim/exceptions.py
  3. 1 1
      tracim/lib/core/content.py
  4. 13 6
      tracim/lib/core/user.py

+ 21 - 6
tracim/command/user.py Ver arquivo

12
 #from tracim.lib.daemons import RadicaleDaemon
12
 #from tracim.lib.daemons import RadicaleDaemon
13
 #from tracim.lib.email import get_email_manager
13
 #from tracim.lib.email import get_email_manager
14
 from tracim.exceptions import AlreadyExistError
14
 from tracim.exceptions import AlreadyExistError
15
+from tracim.exceptions import NotificationNotSend
15
 from tracim.exceptions import CommandAbortedError
16
 from tracim.exceptions import CommandAbortedError
16
 from tracim.lib.core.group import GroupApi
17
 from tracim.lib.core.group import GroupApi
17
 from tracim.lib.core.user import UserApi
18
 from tracim.lib.core.user import UserApi
106
             group.users.remove(user)
107
             group.users.remove(user)
107
         self._session.flush()
108
         self._session.flush()
108
 
109
 
109
-    def _create_user(self, login: str, password: str, **kwargs) -> User:
110
+    def _create_user(
111
+            self,
112
+            login: str,
113
+            password: str,
114
+            do_notify: bool,
115
+            **kwargs
116
+    ) -> User:
110
         if not password:
117
         if not password:
111
             if self._password_required():
118
             if self._password_required():
112
                 raise CommandAbortedError(
119
                 raise CommandAbortedError(
115
             password = ''
122
             password = ''
116
 
123
 
117
         try:
124
         try:
118
-            user = self._user_api.create_minimal_user(email=login)
119
-            user.password = password
120
-            self._user_api.save(user)
125
+            user = self._user_api.create_user(
126
+                email=login,
127
+                password=password,
128
+                do_save=True,
129
+                do_notify=do_notify,
130
+            )
121
             # TODO - G.M - 04-04-2018 - [Caldav] Check this code
131
             # TODO - G.M - 04-04-2018 - [Caldav] Check this code
122
             # # We need to enable radicale if it not already done
132
             # # We need to enable radicale if it not already done
123
             # daemons = DaemonsManager()
133
             # daemons = DaemonsManager()
124
             # daemons.run('radicale', RadicaleDaemon)
134
             # daemons.run('radicale', RadicaleDaemon)
125
-
126
             self._user_api.execute_created_user_actions(user)
135
             self._user_api.execute_created_user_actions(user)
127
         except IntegrityError:
136
         except IntegrityError:
128
             self._session.rollback()
137
             self._session.rollback()
129
             raise AlreadyExistError()
138
             raise AlreadyExistError()
139
+        except NotificationNotSend as exception:
140
+            self._session.rollback()
141
+            raise exception
130
 
142
 
131
         return user
143
         return user
132
 
144
 
166
             try:
178
             try:
167
                 user = self._create_user(
179
                 user = self._create_user(
168
                     login=parsed_args.login,
180
                     login=parsed_args.login,
169
-                    password=parsed_args.password
181
+                    password=parsed_args.password,
182
+                    do_notify=parsed_args.send_email,
170
                 )
183
                 )
171
             except AlreadyExistError:
184
             except AlreadyExistError:
172
                 raise CommandAbortedError("Error: User already exist (use `user update` command instead)")
185
                 raise CommandAbortedError("Error: User already exist (use `user update` command instead)")
186
+            except NotificationNotSend:
187
+                raise CommandAbortedError("Error: Cannot send email notification, user not created.")
173
             # TODO - G.M - 04-04-2018 - [Email] Check this code
188
             # TODO - G.M - 04-04-2018 - [Email] Check this code
174
             # if parsed_args.send_email:
189
             # if parsed_args.send_email:
175
             #     email_manager = get_email_manager()
190
             #     email_manager = get_email_manager()

+ 4 - 0
tracim/exceptions.py Ver arquivo

87
 
87
 
88
 class LoginFailed(TracimException):
88
 class LoginFailed(TracimException):
89
     pass
89
     pass
90
+
91
+
92
+class NotificationNotSend(TracimException):
93
+    pass

+ 1 - 1
tracim/lib/core/content.py Ver arquivo

376
 
376
 
377
         return result
377
         return result
378
 
378
 
379
-    def create(self, content_type: str, workspace: Workspace, parent: Content=None, label:str ='', do_save=False, is_temporary: bool=False, do_notify=False) -> Content:
379
+    def create(self, content_type: str, workspace: Workspace, parent: Content=None, label:str ='', do_save=False, is_temporary: bool=False, do_notify=True) -> Content:
380
         assert content_type in ContentType.allowed_types()
380
         assert content_type in ContentType.allowed_types()
381
 
381
 
382
         if content_type == ContentType.Folder and not label:
382
         if content_type == ContentType.Folder and not label:

+ 13 - 6
tracim/lib/core/user.py Ver arquivo

1
 # -*- coding: utf-8 -*-
1
 # -*- coding: utf-8 -*-
2
 import threading
2
 import threading
3
+from smtplib import SMTPException
3
 
4
 
4
 import transaction
5
 import transaction
5
 import typing as typing
6
 import typing as typing
6
 
7
 
8
+from tracim.exceptions import NotificationNotSend
7
 from tracim.lib.mail_notifier.notifier import get_email_manager
9
 from tracim.lib.mail_notifier.notifier import get_email_manager
8
 from tracim.models.auth import User
10
 from tracim.models.auth import User
9
 
11
 
78
             email=email,
80
             email=email,
79
             password=password,
81
             password=password,
80
             timezone=timezone,
82
             timezone=timezone,
81
-            do_save=do_save,
83
+            do_save=False,
82
         )
84
         )
83
         if do_notify:
85
         if do_notify:
84
-            email_manager = get_email_manager(self._config, self._session)
85
-            email_manager.notify_created_account(
86
-                new_user,
87
-                password=password
88
-            )
86
+            try:
87
+                email_manager = get_email_manager(self._config, self._session)
88
+                email_manager.notify_created_account(
89
+                    new_user,
90
+                    password=password
91
+                )
92
+            except SMTPException as e:
93
+                raise NotificationNotSend()
94
+        if do_save:
95
+            self.save(new_user)
89
         return new_user
96
         return new_user
90
 
97
 
91
     def create_minimal_user(
98
     def create_minimal_user(