changeset 1731:31e6eb2fb4b2 beta

implements #222 registration feedback - a notification message is created for admins - email template with registartion
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 26 Nov 2011 21:13:33 +0200
parents ce0b47534c36
children 8321b3d19b1f
files rhodecode/controllers/login.py rhodecode/model/db.py rhodecode/model/notification.py rhodecode/model/user.py rhodecode/templates/email_templates/registration.html
diffstat 5 files changed, 66 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/controllers/login.py	Sat Nov 26 19:26:24 2011 +0200
+++ b/rhodecode/controllers/login.py	Sat Nov 26 21:13:33 2011 +0200
@@ -38,6 +38,7 @@
 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 @@
                 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:
--- a/rhodecode/model/db.py	Sat Nov 26 19:26:24 2011 +0200
+++ b/rhodecode/model/db.py	Sat Nov 26 21:13:33 2011 +0200
@@ -283,6 +283,10 @@
     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 @@
     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)
--- a/rhodecode/model/notification.py	Sat Nov 26 19:26:24 2011 +0200
+++ b/rhodecode/model/notification.py	Sat Nov 26 21:13:33 2011 +0200
@@ -57,8 +57,9 @@
                 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 @@
             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 @@
 
         _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"
--- a/rhodecode/model/user.py	Sat Nov 26 19:26:24 2011 +0200
+++ b/rhodecode/model/user.py	Sat Nov 26 21:13:33 2011 +0200
@@ -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.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 @@
 
 log = logging.getLogger(__name__)
 
+
 PERM_WEIGHTS = {'repository.none': 0,
                 'repository.read': 1,
                 'repository.write': 3,
@@ -211,7 +214,8 @@
         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 @@
                     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):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rhodecode/templates/email_templates/registration.html	Sat Nov 26 21:13:33 2011 +0200
@@ -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