diff --git a/rhodecode/controllers/login.py b/rhodecode/controllers/login.py --- a/rhodecode/controllers/login.py +++ b/rhodecode/controllers/login.py @@ -38,6 +38,7 @@ from rhodecode.lib.base import BaseContr from rhodecode.model.db import User from rhodecode.model.forms import LoginForm, RegisterForm, PasswordResetForm from rhodecode.model.user import UserModel +from rhodecode.model.meta import Session log = logging.getLogger(__name__) @@ -109,6 +110,7 @@ class LoginController(BaseController): user_model.create_registration(form_result) h.flash(_('You have successfully registered into rhodecode'), category='success') + Session().commit() return redirect(url('login_home')) except formencode.Invalid, errors: diff --git a/rhodecode/model/db.py b/rhodecode/model/db.py --- a/rhodecode/model/db.py +++ b/rhodecode/model/db.py @@ -283,6 +283,10 @@ class User(Base, BaseModel): notifications = relationship('UserNotification',) @property + def full_name(self): + return '%s %s' % (self.name, self.lastname) + + @property def full_contact(self): return '%s %s <%s>' % (self.name, self.lastname, self.email) @@ -1170,6 +1174,7 @@ class Notification(Base, BaseModel): TYPE_CHANGESET_COMMENT = u'cs_comment' TYPE_MESSAGE = u'message' TYPE_MENTION = u'mention' + TYPE_REGISTRATION = u'registration' notification_id = Column('notification_id', Integer(), nullable=False, primary_key=True) subject = Column('subject', Unicode(512), nullable=True) diff --git a/rhodecode/model/notification.py b/rhodecode/model/notification.py --- a/rhodecode/model/notification.py +++ b/rhodecode/model/notification.py @@ -57,8 +57,9 @@ class NotificationModel(BaseModel): raise Exception('notification must be int or Instance' ' of Notification got %s' % type(notification)) - def create(self, created_by, subject, body, recipients, - type_=Notification.TYPE_MESSAGE): + def create(self, created_by, subject, body, recipients=None, + type_=Notification.TYPE_MESSAGE, with_email=True, + email_kwargs={}): """ Creates notification of given type @@ -67,35 +68,46 @@ class NotificationModel(BaseModel): notification :param subject: :param body: - :param recipients: list of int, str or User objects + :param recipients: list of int, str or User objects, when None + is given send to all admins :param type_: type of notification + :param with_email: send email with this notification + :param email_kwargs: additional dict to pass as args to email template """ from rhodecode.lib.celerylib import tasks, run_task - if not getattr(recipients, '__iter__', False): + if recipients and not getattr(recipients, '__iter__', False): raise Exception('recipients must be a list of iterable') created_by_obj = self.__get_user(created_by) - recipients_objs = [] - for u in recipients: - obj = self.__get_user(u) - if obj: - recipients_objs.append(obj) - recipients_objs = set(recipients_objs) + if recipients: + recipients_objs = [] + for u in recipients: + obj = self.__get_user(u) + if obj: + recipients_objs.append(obj) + recipients_objs = set(recipients_objs) + else: + # empty recipients means to all admins + recipients_objs = User.query().filter(User.admin == True).all() notif = Notification.create(created_by=created_by_obj, subject=subject, body=body, recipients=recipients_objs, type_=type_) + if with_email is False: + return notif + # send email with notification for rec in recipients_objs: email_subject = NotificationModel().make_description(notif, False) - type_ = EmailNotificationModel.TYPE_CHANGESET_COMMENT + type_ = type_ email_body = body + kwargs = {'subject':subject, 'body':h.rst(body)} + kwargs.update(email_kwargs) email_body_html = EmailNotificationModel()\ - .get_email_tmpl(type_, **{'subject':subject, - 'body':h.rst(body)}) + .get_email_tmpl(type_, **kwargs) run_task(tasks.send_email, rec.email, email_subject, email_body, email_body_html) @@ -150,7 +162,9 @@ class NotificationModel(BaseModel): _map = {notification.TYPE_CHANGESET_COMMENT:_('commented on commit'), notification.TYPE_MESSAGE:_('sent message'), - notification.TYPE_MENTION:_('mentioned you')} + notification.TYPE_MENTION:_('mentioned you'), + notification.TYPE_REGISTRATION:_('registered in RhodeCode')} + DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" tmpl = "%(user)s %(action)s %(when)s" diff --git a/rhodecode/model/user.py b/rhodecode/model/user.py --- a/rhodecode/model/user.py +++ b/rhodecode/model/user.py @@ -26,6 +26,7 @@ import logging import traceback +from pylons import url from pylons.i18n.translation import _ from rhodecode.lib import safe_unicode @@ -33,7 +34,8 @@ from rhodecode.lib.caching_query import from rhodecode.model import BaseModel from rhodecode.model.db import User, UserRepoToPerm, Repository, Permission, \ - UserToPerm, UsersGroupRepoToPerm, UsersGroupToPerm, UsersGroupMember + UserToPerm, UsersGroupRepoToPerm, UsersGroupToPerm, UsersGroupMember, \ + Notification from rhodecode.lib.exceptions import DefaultUserException, \ UserOwnsReposException @@ -43,6 +45,7 @@ from sqlalchemy.orm import joinedload log = logging.getLogger(__name__) + PERM_WEIGHTS = {'repository.none': 0, 'repository.read': 1, 'repository.write': 3, @@ -211,7 +214,8 @@ class UserModel(BaseModel): return None def create_registration(self, form_data): - from rhodecode.lib.celerylib import tasks, run_task + from rhodecode.model.notification import NotificationModel + try: new_user = User() for k, v in form_data.items(): @@ -219,18 +223,26 @@ class UserModel(BaseModel): setattr(new_user, k, v) self.sa.add(new_user) - self.sa.commit() + self.sa.flush() + + # notification to admins + subject = _('new user registration') body = ('New user registration\n' - 'username: %s\n' - 'email: %s\n') - body = body % (form_data['username'], form_data['email']) + '---------------------\n' + '- Username: %s\n' + '- Full Name: %s\n' + '- Email: %s\n') + body = body % (new_user.username, new_user.full_name, + new_user.email) + edit_url = url('edit_user', id=new_user.user_id, qualified=True) + kw = {'registered_user_url':edit_url} + NotificationModel().create(created_by=new_user, subject=subject, + body=body, recipients=None, + type_=Notification.TYPE_REGISTRATION, + email_kwargs=kw) - run_task(tasks.send_email, None, - _('[RhodeCode] New User registration'), - body) except: log.error(traceback.format_exc()) - self.sa.rollback() raise def update(self, user_id, form_data): diff --git a/rhodecode/templates/email_templates/registration.html b/rhodecode/templates/email_templates/registration.html new file mode 100644 --- /dev/null +++ b/rhodecode/templates/email_templates/registration.html @@ -0,0 +1,9 @@ +## -*- coding: utf-8 -*- +<%inherit file="main.html"/> + +A new user have registered in RhodeCode + +${body} + + +View this user here :${registered_user_url} \ No newline at end of file