##// END OF EJS Templates
fixed kwargs ->email_kwargs
marcink -
r2814:5acab314 beta
parent child Browse files
Show More
@@ -1,241 +1,241 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 rhodecode.model.comment
4 4 ~~~~~~~~~~~~~~~~~~~~~~~
5 5
6 6 comments model for RhodeCode
7 7
8 8 :created_on: Nov 11, 2011
9 9 :author: marcink
10 10 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com>
11 11 :license: GPLv3, see COPYING for more details.
12 12 """
13 13 # This program is free software: you can redistribute it and/or modify
14 14 # it under the terms of the GNU General Public License as published by
15 15 # the Free Software Foundation, either version 3 of the License, or
16 16 # (at your option) any later version.
17 17 #
18 18 # This program is distributed in the hope that it will be useful,
19 19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 21 # GNU General Public License for more details.
22 22 #
23 23 # You should have received a copy of the GNU General Public License
24 24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 25
26 26 import logging
27 27 import traceback
28 28
29 29 from pylons.i18n.translation import _
30 30 from sqlalchemy.util.compat import defaultdict
31 31
32 32 from rhodecode.lib.utils2 import extract_mentioned_users, safe_unicode
33 33 from rhodecode.lib import helpers as h
34 34 from rhodecode.model import BaseModel
35 35 from rhodecode.model.db import ChangesetComment, User, Repository, \
36 36 Notification, PullRequest
37 37 from rhodecode.model.notification import NotificationModel
38 38
39 39 log = logging.getLogger(__name__)
40 40
41 41
42 42 class ChangesetCommentsModel(BaseModel):
43 43
44 44 cls = ChangesetComment
45 45
46 46 def __get_changeset_comment(self, changeset_comment):
47 47 return self._get_instance(ChangesetComment, changeset_comment)
48 48
49 49 def __get_pull_request(self, pull_request):
50 50 return self._get_instance(PullRequest, pull_request)
51 51
52 52 def _extract_mentions(self, s):
53 53 user_objects = []
54 54 for username in extract_mentioned_users(s):
55 55 user_obj = User.get_by_username(username, case_insensitive=True)
56 56 if user_obj:
57 57 user_objects.append(user_obj)
58 58 return user_objects
59 59
60 60 def create(self, text, repo, user, revision=None, pull_request=None,
61 61 f_path=None, line_no=None, status_change=None):
62 62 """
63 63 Creates new comment for changeset or pull request.
64 64 IF status_change is not none this comment is associated with a
65 65 status change of changeset or changesets associated with pull request
66 66
67 67 :param text:
68 68 :param repo:
69 69 :param user:
70 70 :param revision:
71 71 :param pull_request:
72 72 :param f_path:
73 73 :param line_no:
74 74 :param status_change:
75 75 """
76 76 if not text:
77 77 return
78 78
79 79 repo = self._get_repo(repo)
80 80 user = self._get_user(user)
81 81 comment = ChangesetComment()
82 82 comment.repo = repo
83 83 comment.author = user
84 84 comment.text = text
85 85 comment.f_path = f_path
86 86 comment.line_no = line_no
87 87
88 88 if revision:
89 89 cs = repo.scm_instance.get_changeset(revision)
90 90 desc = "%s - %s" % (cs.short_id, h.shorter(cs.message, 256))
91 91 author_email = cs.author_email
92 92 comment.revision = revision
93 93 elif pull_request:
94 94 pull_request = self.__get_pull_request(pull_request)
95 95 comment.pull_request = pull_request
96 96 desc = pull_request.pull_request_id
97 97 else:
98 98 raise Exception('Please specify revision or pull_request_id')
99 99
100 100 self.sa.add(comment)
101 101 self.sa.flush()
102 102
103 103 # make notification
104 104 line = ''
105 105 body = text
106 106
107 107 #changeset
108 108 if revision:
109 109 if line_no:
110 110 line = _('on line %s') % line_no
111 111 subj = safe_unicode(
112 112 h.link_to('Re commit: %(desc)s %(line)s' % \
113 113 {'desc': desc, 'line': line},
114 114 h.url('changeset_home', repo_name=repo.repo_name,
115 115 revision=revision,
116 116 anchor='comment-%s' % comment.comment_id,
117 117 qualified=True,
118 118 )
119 119 )
120 120 )
121 121 notification_type = Notification.TYPE_CHANGESET_COMMENT
122 122 # get the current participants of this changeset
123 123 recipients = ChangesetComment.get_users(revision=revision)
124 124 # add changeset author if it's in rhodecode system
125 125 recipients += [User.get_by_email(author_email)]
126 126 email_kwargs = {
127 127 'status_change': status_change,
128 128 }
129 129 #pull request
130 130 elif pull_request:
131 131 _url = h.url('pullrequest_show',
132 132 repo_name=pull_request.other_repo.repo_name,
133 133 pull_request_id=pull_request.pull_request_id,
134 134 anchor='comment-%s' % comment.comment_id,
135 135 qualified=True,
136 136 )
137 137 subj = safe_unicode(
138 138 h.link_to('Re pull request: %(desc)s %(line)s' % \
139 139 {'desc': desc, 'line': line}, _url)
140 140 )
141 141
142 142 notification_type = Notification.TYPE_PULL_REQUEST_COMMENT
143 143 # get the current participants of this pull request
144 144 recipients = ChangesetComment.get_users(pull_request_id=
145 145 pull_request.pull_request_id)
146 146 # add pull request author
147 147 recipients += [pull_request.author]
148 148
149 149 # add the reviewers to notification
150 150 recipients += [x.user for x in pull_request.reviewers]
151 151
152 152 #set some variables for email notification
153 153 email_kwargs = {
154 154 'pr_id': pull_request.pull_request_id,
155 155 'status_change': status_change,
156 156 'pr_comment_url': _url,
157 157 'pr_comment_user': h.person(user.email),
158 158 'pr_target_repo': h.url('summary_home',
159 159 repo_name=pull_request.other_repo.repo_name,
160 160 qualified=True)
161 161 }
162 162 # create notification objects, and emails
163 163 NotificationModel().create(
164 164 created_by=user, subject=subj, body=body,
165 165 recipients=recipients, type_=notification_type,
166 166 email_kwargs=email_kwargs
167 167 )
168 168
169 169 mention_recipients = set(self._extract_mentions(body))\
170 170 .difference(recipients)
171 171 if mention_recipients:
172 kwargs.update({'pr_mention': True})
172 email_kwargs.update({'pr_mention': True})
173 173 subj = _('[Mention]') + ' ' + subj
174 174 NotificationModel().create(
175 175 created_by=user, subject=subj, body=body,
176 176 recipients=mention_recipients,
177 177 type_=notification_type,
178 email_kwargs=kwargs
178 email_kwargs=email_kwargs
179 179 )
180 180
181 181 return comment
182 182
183 183 def delete(self, comment):
184 184 """
185 185 Deletes given comment
186 186
187 187 :param comment_id:
188 188 """
189 189 comment = self.__get_changeset_comment(comment)
190 190 self.sa.delete(comment)
191 191
192 192 return comment
193 193
194 194 def get_comments(self, repo_id, revision=None, pull_request=None):
195 195 """
196 196 Get's main comments based on revision or pull_request_id
197 197
198 198 :param repo_id:
199 199 :type repo_id:
200 200 :param revision:
201 201 :type revision:
202 202 :param pull_request:
203 203 :type pull_request:
204 204 """
205 205
206 206 q = ChangesetComment.query()\
207 207 .filter(ChangesetComment.repo_id == repo_id)\
208 208 .filter(ChangesetComment.line_no == None)\
209 209 .filter(ChangesetComment.f_path == None)
210 210 if revision:
211 211 q = q.filter(ChangesetComment.revision == revision)
212 212 elif pull_request:
213 213 pull_request = self.__get_pull_request(pull_request)
214 214 q = q.filter(ChangesetComment.pull_request == pull_request)
215 215 else:
216 216 raise Exception('Please specify revision or pull_request')
217 217 q = q.order_by(ChangesetComment.created_on)
218 218 return q.all()
219 219
220 220 def get_inline_comments(self, repo_id, revision=None, pull_request=None):
221 221 q = self.sa.query(ChangesetComment)\
222 222 .filter(ChangesetComment.repo_id == repo_id)\
223 223 .filter(ChangesetComment.line_no != None)\
224 224 .filter(ChangesetComment.f_path != None)\
225 225 .order_by(ChangesetComment.comment_id.asc())\
226 226
227 227 if revision:
228 228 q = q.filter(ChangesetComment.revision == revision)
229 229 elif pull_request:
230 230 pull_request = self.__get_pull_request(pull_request)
231 231 q = q.filter(ChangesetComment.pull_request == pull_request)
232 232 else:
233 233 raise Exception('Please specify revision or pull_request_id')
234 234
235 235 comments = q.all()
236 236
237 237 paths = defaultdict(lambda: defaultdict(list))
238 238
239 239 for co in comments:
240 240 paths[co.f_path][co.line_no].append(co)
241 241 return paths.items()
General Comments 0
You need to be logged in to leave comments. Login now