aboutsummaryrefslogtreecommitdiff
path: root/rhodecode/model/comment.py
diff options
context:
space:
mode:
authorMarcin Kuzminski <marcin@python-works.com>2013-03-02 21:26:38 +0100
committerMarcin Kuzminski <marcin@python-works.com>2013-03-02 21:26:38 +0100
commit00253ed593ec9fdaec57c2fc79e9e7fe1dd914c1 (patch)
tree9b0acfb6552a5d12a79755adc208ee202039ba9e /rhodecode/model/comment.py
parentf044fd67d509bc4ebd32afa79810348f5fe3ea8d (diff)
notifications changes
- closing pull requests will give notification about it - more detailed notifications about commenting on changesets and pull requests --HG-- branch : beta
Diffstat (limited to 'rhodecode/model/comment.py')
-rw-r--r--rhodecode/model/comment.py153
1 files changed, 94 insertions, 59 deletions
diff --git a/rhodecode/model/comment.py b/rhodecode/model/comment.py
index 0bf1b40b..ed814180 100644
--- a/rhodecode/model/comment.py
+++ b/rhodecode/model/comment.py
@@ -35,6 +35,7 @@ from rhodecode.model import BaseModel
from rhodecode.model.db import ChangesetComment, User, Repository, \
Notification, PullRequest
from rhodecode.model.notification import NotificationModel
+from rhodecode.model.meta import Session
log = logging.getLogger(__name__)
@@ -57,67 +58,41 @@ class ChangesetCommentsModel(BaseModel):
user_objects.append(user_obj)
return user_objects
- def create(self, text, repo, user, revision=None, pull_request=None,
- f_path=None, line_no=None, status_change=None, send_email=True):
+ def _get_notification_data(self, repo, comment, user, comment_text,
+ line_no=None, revision=None, pull_request=None,
+ status_change=None, closing_pr=False):
"""
- Creates new comment for changeset or pull request.
- IF status_change is not none this comment is associated with a
- status change of changeset or changesets associated with pull request
+ Get notification data
- :param text:
- :param repo:
- :param user:
- :param revision:
- :param pull_request:
- :param f_path:
- :param line_no:
- :param status_change:
- :param send_email:
+ :param comment_text:
+ :param line:
+ :returns: tuple (subj,body,recipients,notification_type,email_kwargs)
"""
- if not text:
- return
-
- repo = self._get_repo(repo)
- user = self._get_user(user)
- comment = ChangesetComment()
- comment.repo = repo
- comment.author = user
- comment.text = text
- comment.f_path = f_path
- comment.line_no = line_no
-
- if revision:
- cs = repo.scm_instance.get_changeset(revision)
- desc = "%s - %s" % (cs.short_id, h.shorter(cs.message, 256))
- comment.revision = revision
- elif pull_request:
- pull_request = self.__get_pull_request(pull_request)
- comment.pull_request = pull_request
- else:
- raise Exception('Please specify revision or pull_request_id')
-
- self.sa.add(comment)
- self.sa.flush()
-
# make notification
+ body = comment_text # text of the comment
line = ''
- body = text
+ if line_no:
+ line = _('on line %s') % line_no
#changeset
if revision:
- if line_no:
- line = _('on line %s') % line_no
+ notification_type = Notification.TYPE_CHANGESET_COMMENT
+ cs = repo.scm_instance.get_changeset(revision)
+ desc = "%s" % (cs.short_id)
+
+ _url = h.url('changeset_home',
+ repo_name=repo.repo_name,
+ revision=revision,
+ anchor='comment-%s' % comment.comment_id,
+ qualified=True,
+ )
subj = safe_unicode(
- h.link_to('Re commit: %(desc)s %(line)s' % \
+ h.link_to('Re changeset: %(desc)s %(line)s' % \
{'desc': desc, 'line': line},
- h.url('changeset_home', repo_name=repo.repo_name,
- revision=revision,
- anchor='comment-%s' % comment.comment_id,
- qualified=True,
- )
- )
+ _url)
)
- notification_type = Notification.TYPE_CHANGESET_COMMENT
+ email_subject = 'User %s commented on changeset %s' % \
+ (user.username, h.short_id(revision))
# get the current participants of this changeset
recipients = ChangesetComment.get_users(revision=revision)
# add changeset author if it's in rhodecode system
@@ -128,9 +103,17 @@ class ChangesetCommentsModel(BaseModel):
recipients += [cs_author]
email_kwargs = {
'status_change': status_change,
+ 'cs_comment_user': h.person(user.email),
+ 'cs_target_repo': h.url('summary_home', repo_name=repo.repo_name,
+ qualified=True),
+ 'cs_comment_url': _url,
+ 'raw_id': revision,
+ 'message': cs.message
}
#pull request
elif pull_request:
+ notification_type = Notification.TYPE_PULL_REQUEST_COMMENT
+ desc = comment.pull_request.title
_url = h.url('pullrequest_show',
repo_name=pull_request.other_repo.repo_name,
pull_request_id=pull_request.pull_request_id,
@@ -139,13 +122,13 @@ class ChangesetCommentsModel(BaseModel):
)
subj = safe_unicode(
h.link_to('Re pull request #%(pr_id)s: %(desc)s %(line)s' % \
- {'desc': comment.pull_request.title,
+ {'desc': desc,
'pr_id': comment.pull_request.pull_request_id,
'line': line},
_url)
)
-
- notification_type = Notification.TYPE_PULL_REQUEST_COMMENT
+ email_subject = 'User %s commented on pull request #%s' % \
+ (user.username, comment.pull_request.pull_request_id)
# get the current participants of this pull request
recipients = ChangesetComment.get_users(pull_request_id=
pull_request.pull_request_id)
@@ -159,6 +142,7 @@ class ChangesetCommentsModel(BaseModel):
email_kwargs = {
'pr_id': pull_request.pull_request_id,
'status_change': status_change,
+ 'closing_pr': closing_pr,
'pr_comment_url': _url,
'pr_comment_user': h.person(user.email),
'pr_target_repo': h.url('summary_home',
@@ -166,12 +150,66 @@ class ChangesetCommentsModel(BaseModel):
qualified=True)
}
+ return subj, body, recipients, notification_type, email_kwargs, email_subject
+
+ def create(self, text, repo, user, revision=None, pull_request=None,
+ f_path=None, line_no=None, status_change=None, closing_pr=False,
+ send_email=True):
+ """
+ Creates new comment for changeset or pull request.
+ IF status_change is not none this comment is associated with a
+ status change of changeset or changesets associated with pull request
+
+ :param text:
+ :param repo:
+ :param user:
+ :param revision:
+ :param pull_request:
+ :param f_path:
+ :param line_no:
+ :param status_change:
+ :param closing_pr:
+ :param send_email:
+ """
+ if not text:
+ log.warning('Missing text for comment, skipping...')
+ return
+
+ repo = self._get_repo(repo)
+ user = self._get_user(user)
+ comment = ChangesetComment()
+ comment.repo = repo
+ comment.author = user
+ comment.text = text
+ comment.f_path = f_path
+ comment.line_no = line_no
+
+ if revision:
+ comment.revision = revision
+ elif pull_request:
+ pull_request = self.__get_pull_request(pull_request)
+ comment.pull_request = pull_request
+ else:
+ raise Exception('Please specify revision or pull_request_id')
+
+ Session().add(comment)
+ Session().flush()
+
if send_email:
+ (subj, body, recipients, notification_type,
+ email_kwargs, email_subject) = self._get_notification_data(
+ repo, comment, user,
+ comment_text=text,
+ line_no=line_no,
+ revision=revision,
+ pull_request=pull_request,
+ status_change=status_change,
+ closing_pr=closing_pr)
# create notification objects, and emails
NotificationModel().create(
created_by=user, subject=subj, body=body,
recipients=recipients, type_=notification_type,
- email_kwargs=email_kwargs
+ email_kwargs=email_kwargs, email_subject=email_subject
)
mention_recipients = set(self._extract_mentions(body))\
@@ -195,7 +233,7 @@ class ChangesetCommentsModel(BaseModel):
:param comment_id:
"""
comment = self.__get_changeset_comment(comment)
- self.sa.delete(comment)
+ Session().delete(comment)
return comment
@@ -204,11 +242,8 @@ class ChangesetCommentsModel(BaseModel):
Get's main comments based on revision or pull_request_id
:param repo_id:
- :type repo_id:
:param revision:
- :type revision:
:param pull_request:
- :type pull_request:
"""
q = ChangesetComment.query()\
@@ -226,7 +261,7 @@ class ChangesetCommentsModel(BaseModel):
return q.all()
def get_inline_comments(self, repo_id, revision=None, pull_request=None):
- q = self.sa.query(ChangesetComment)\
+ q = Session().query(ChangesetComment)\
.filter(ChangesetComment.repo_id == repo_id)\
.filter(ChangesetComment.line_no != None)\
.filter(ChangesetComment.f_path != None)\