aboutsummaryrefslogtreecommitdiff
path: root/rhodecode/model
diff options
context:
space:
mode:
authorMarcin Kuzminski <marcin@python-works.com>2011-11-26 21:13:33 +0200
committerMarcin Kuzminski <marcin@python-works.com>2011-11-26 21:13:33 +0200
commit0aa3f80c1cfde0317d21e7bd8e7380b159cf7f16 (patch)
tree7b38ae504b034386bc65fc6e982528d9e9902b3b /rhodecode/model
parentff256e8b8af07a64f71f38fa38b1cb50ed26584e (diff)
implements #222 registration feedback
- a notification message is created for admins - email template with registartion --HG-- branch : beta
Diffstat (limited to 'rhodecode/model')
-rwxr-xr-xrhodecode/model/db.py5
-rw-r--r--rhodecode/model/notification.py42
-rw-r--r--rhodecode/model/user.py32
3 files changed, 55 insertions, 24 deletions
diff --git a/rhodecode/model/db.py b/rhodecode/model/db.py
index ee4eb929..c31046a4 100755
--- 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
index 56fef09d..6f53a58e 100644
--- 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
index e86f0a98..a48127bd 100644
--- 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 FromCache
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):