diff --git a/rhodecode/model/comment.py b/rhodecode/model/comment.py --- a/rhodecode/model/comment.py +++ b/rhodecode/model/comment.py @@ -459,9 +459,6 @@ class CommentsModel(BaseModel): if send_email: recipients += [self._get_user(u) for u in (extra_recipients or [])] - # pre-generate the subject for notification itself - (subject, _e, body_plaintext) = EmailNotificationModel().render_email( - notification_type, **kwargs) mention_recipients = set( self._extract_mentions(text)).difference(recipients) @@ -469,8 +466,8 @@ class CommentsModel(BaseModel): # create notification objects, and emails NotificationModel().create( created_by=user, - notification_subject=subject, - notification_body=body_plaintext, + notification_subject='', # Filled in based on the notification_type + notification_body='', # Filled in based on the notification_type notification_type=notification_type, recipients=recipients, mention_recipients=mention_recipients, diff --git a/rhodecode/model/notification.py b/rhodecode/model/notification.py --- a/rhodecode/model/notification.py +++ b/rhodecode/model/notification.py @@ -55,7 +55,7 @@ class NotificationModel(BaseModel): ' of Notification got %s' % type(notification)) def create( - self, created_by, notification_subject, notification_body, + self, created_by, notification_subject='', notification_body='', notification_type=Notification.TYPE_MESSAGE, recipients=None, mention_recipients=None, with_email=True, email_kwargs=None): """ @@ -64,11 +64,12 @@ class NotificationModel(BaseModel): :param created_by: int, str or User instance. User who created this notification - :param notification_subject: subject of notification itself + :param notification_subject: subject of notification itself, + it will be generated automatically from notification_type if not specified :param notification_body: body of notification text + it will be generated automatically from notification_type if not specified :param notification_type: type of notification, based on that we pick templates - :param recipients: list of int, str or User objects, when None is given send to all admins :param mention_recipients: list of int, str or User objects, @@ -82,14 +83,19 @@ class NotificationModel(BaseModel): if recipients and not getattr(recipients, '__iter__', False): raise Exception('recipients must be an iterable object') + if not (notification_subject and notification_body) and not notification_type: + raise ValueError('notification_subject, and notification_body ' + 'cannot be empty when notification_type is not specified') + created_by_obj = self._get_user(created_by) + + if not created_by_obj: + raise Exception('unknown user %s' % created_by) + # default MAIN body if not given email_kwargs = email_kwargs or {'body': notification_body} mention_recipients = mention_recipients or set() - if not created_by_obj: - raise Exception('unknown user %s' % created_by) - if recipients is None: # recipients is None means to all admins recipients_objs = User.query().filter(User.admin == true()).all() @@ -113,6 +119,15 @@ class NotificationModel(BaseModel): # add mentioned users into recipients final_recipients = set(recipients_objs).union(mention_recipients) + (subject, email_body, email_body_plaintext) = \ + EmailNotificationModel().render_email(notification_type, **email_kwargs) + + if not notification_subject: + notification_subject = subject + + if not notification_body: + notification_body = email_body_plaintext + notification = Notification.create( created_by=created_by_obj, subject=notification_subject, body=notification_body, recipients=final_recipients, diff --git a/rhodecode/model/pull_request.py b/rhodecode/model/pull_request.py --- a/rhodecode/model/pull_request.py +++ b/rhodecode/model/pull_request.py @@ -1502,15 +1502,11 @@ class PullRequestModel(BaseModel): 'user_role': role } - # pre-generate the subject for notification itself - (subject, _e, body_plaintext) = EmailNotificationModel().render_email( - notification_type, **kwargs) - # create notification objects, and emails NotificationModel().create( created_by=current_rhodecode_user, - notification_subject=subject, - notification_body=body_plaintext, + notification_subject='', # Filled in based on the notification_type + notification_body='', # Filled in based on the notification_type notification_type=notification_type, recipients=recipients, email_kwargs=kwargs, @@ -1579,14 +1575,11 @@ class PullRequestModel(BaseModel): 'thread_ids': [pr_url], } - (subject, _e, body_plaintext) = EmailNotificationModel().render_email( - EmailNotificationModel.TYPE_PULL_REQUEST_UPDATE, **email_kwargs) - # create notification objects, and emails NotificationModel().create( created_by=updating_user, - notification_subject=subject, - notification_body=body_plaintext, + notification_subject='', # Filled in based on the notification_type + notification_body='', # Filled in based on the notification_type notification_type=EmailNotificationModel.TYPE_PULL_REQUEST_UPDATE, recipients=recipients, email_kwargs=email_kwargs, diff --git a/rhodecode/model/user.py b/rhodecode/model/user.py --- a/rhodecode/model/user.py +++ b/rhodecode/model/user.py @@ -425,15 +425,12 @@ class UserModel(BaseModel): 'date': datetime.datetime.now() } notification_type = EmailNotificationModel.TYPE_REGISTRATION - # pre-generate the subject for notification itself - (subject, _e, body_plaintext) = EmailNotificationModel().render_email( - notification_type, **kwargs) # create notification objects, and emails NotificationModel().create( created_by=new_user, - notification_subject=subject, - notification_body=body_plaintext, + notification_subject='', # Filled in based on the notification_type + notification_body='', # Filled in based on the notification_type notification_type=notification_type, recipients=None, # all admins email_kwargs=kwargs,