Browse Source

fix user creation with command and email notification

Guénaël Muller 6 years ago
parent
commit
37d33febe1
4 changed files with 39 additions and 13 deletions
  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 View File

@@ -12,6 +12,7 @@ from tracim.command import Extender
12 12
 #from tracim.lib.daemons import RadicaleDaemon
13 13
 #from tracim.lib.email import get_email_manager
14 14
 from tracim.exceptions import AlreadyExistError
15
+from tracim.exceptions import NotificationNotSend
15 16
 from tracim.exceptions import CommandAbortedError
16 17
 from tracim.lib.core.group import GroupApi
17 18
 from tracim.lib.core.user import UserApi
@@ -106,7 +107,13 @@ class UserCommand(AppContextCommand):
106 107
             group.users.remove(user)
107 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 117
         if not password:
111 118
             if self._password_required():
112 119
                 raise CommandAbortedError(
@@ -115,18 +122,23 @@ class UserCommand(AppContextCommand):
115 122
             password = ''
116 123
 
117 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 131
             # TODO - G.M - 04-04-2018 - [Caldav] Check this code
122 132
             # # We need to enable radicale if it not already done
123 133
             # daemons = DaemonsManager()
124 134
             # daemons.run('radicale', RadicaleDaemon)
125
-
126 135
             self._user_api.execute_created_user_actions(user)
127 136
         except IntegrityError:
128 137
             self._session.rollback()
129 138
             raise AlreadyExistError()
139
+        except NotificationNotSend as exception:
140
+            self._session.rollback()
141
+            raise exception
130 142
 
131 143
         return user
132 144
 
@@ -166,10 +178,13 @@ class UserCommand(AppContextCommand):
166 178
             try:
167 179
                 user = self._create_user(
168 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 184
             except AlreadyExistError:
172 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 188
             # TODO - G.M - 04-04-2018 - [Email] Check this code
174 189
             # if parsed_args.send_email:
175 190
             #     email_manager = get_email_manager()

+ 4 - 0
tracim/exceptions.py View File

@@ -87,3 +87,7 @@ class DigestAuthNotImplemented(Exception):
87 87
 
88 88
 class LoginFailed(TracimException):
89 89
     pass
90
+
91
+
92
+class NotificationNotSend(TracimException):
93
+    pass

+ 1 - 1
tracim/lib/core/content.py View File

@@ -376,7 +376,7 @@ class ContentApi(object):
376 376
 
377 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 380
         assert content_type in ContentType.allowed_types()
381 381
 
382 382
         if content_type == ContentType.Folder and not label:

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

@@ -1,9 +1,11 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 import threading
3
+from smtplib import SMTPException
3 4
 
4 5
 import transaction
5 6
 import typing as typing
6 7
 
8
+from tracim.exceptions import NotificationNotSend
7 9
 from tracim.lib.mail_notifier.notifier import get_email_manager
8 10
 from tracim.models.auth import User
9 11
 
@@ -78,14 +80,19 @@ class UserApi(object):
78 80
             email=email,
79 81
             password=password,
80 82
             timezone=timezone,
81
-            do_save=do_save,
83
+            do_save=False,
82 84
         )
83 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 96
         return new_user
90 97
 
91 98
     def create_minimal_user(