Show More
@@ -1,238 +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 | email_kwargs = { | |
|
127 | 'status_change': status_change, | |
|
128 | } | |
|
126 | 129 | #pull request |
|
127 | 130 | elif pull_request: |
|
128 | 131 | _url = h.url('pullrequest_show', |
|
129 | 132 | repo_name=pull_request.other_repo.repo_name, |
|
130 | 133 | pull_request_id=pull_request.pull_request_id, |
|
131 | 134 | anchor='comment-%s' % comment.comment_id, |
|
132 | 135 | qualified=True, |
|
133 | 136 | ) |
|
134 | 137 | subj = safe_unicode( |
|
135 | 138 | h.link_to('Re pull request: %(desc)s %(line)s' % \ |
|
136 | 139 | {'desc': desc, 'line': line}, _url) |
|
137 | 140 | ) |
|
138 | 141 | |
|
139 | 142 | notification_type = Notification.TYPE_PULL_REQUEST_COMMENT |
|
140 | 143 | # get the current participants of this pull request |
|
141 | 144 | recipients = ChangesetComment.get_users(pull_request_id= |
|
142 | 145 | pull_request.pull_request_id) |
|
143 | 146 | # add pull request author |
|
144 | 147 | recipients += [pull_request.author] |
|
145 | 148 | |
|
146 | 149 | # add the reviewers to notification |
|
147 | 150 | recipients += [x.user for x in pull_request.reviewers] |
|
148 | 151 | |
|
149 | 152 | #set some variables for email notification |
|
150 | kwargs = { | |
|
153 | email_kwargs = { | |
|
151 | 154 | 'pr_id': pull_request.pull_request_id, |
|
152 | 155 | 'status_change': status_change, |
|
153 | 156 | 'pr_comment_url': _url, |
|
154 | 157 | 'pr_comment_user': h.person(user.email), |
|
155 | 158 | 'pr_target_repo': h.url('summary_home', |
|
156 | 159 | repo_name=pull_request.other_repo.repo_name, |
|
157 | 160 | qualified=True) |
|
158 | 161 | } |
|
159 | 162 | # create notification objects, and emails |
|
160 | 163 | NotificationModel().create( |
|
161 | 164 | created_by=user, subject=subj, body=body, |
|
162 | 165 | recipients=recipients, type_=notification_type, |
|
163 | email_kwargs=kwargs | |
|
166 | email_kwargs=email_kwargs | |
|
164 | 167 | ) |
|
165 | 168 | |
|
166 | 169 | mention_recipients = set(self._extract_mentions(body))\ |
|
167 | 170 | .difference(recipients) |
|
168 | 171 | if mention_recipients: |
|
169 | 172 | kwargs.update({'pr_mention': True}) |
|
170 | 173 | subj = _('[Mention]') + ' ' + subj |
|
171 | 174 | NotificationModel().create( |
|
172 | 175 | created_by=user, subject=subj, body=body, |
|
173 | 176 | recipients=mention_recipients, |
|
174 | 177 | type_=notification_type, |
|
175 | 178 | email_kwargs=kwargs |
|
176 | 179 | ) |
|
177 | 180 | |
|
178 | 181 | return comment |
|
179 | 182 | |
|
180 | 183 | def delete(self, comment): |
|
181 | 184 | """ |
|
182 | 185 | Deletes given comment |
|
183 | 186 | |
|
184 | 187 | :param comment_id: |
|
185 | 188 | """ |
|
186 | 189 | comment = self.__get_changeset_comment(comment) |
|
187 | 190 | self.sa.delete(comment) |
|
188 | 191 | |
|
189 | 192 | return comment |
|
190 | 193 | |
|
191 | 194 | def get_comments(self, repo_id, revision=None, pull_request=None): |
|
192 | 195 | """ |
|
193 | 196 | Get's main comments based on revision or pull_request_id |
|
194 | 197 | |
|
195 | 198 | :param repo_id: |
|
196 | 199 | :type repo_id: |
|
197 | 200 | :param revision: |
|
198 | 201 | :type revision: |
|
199 | 202 | :param pull_request: |
|
200 | 203 | :type pull_request: |
|
201 | 204 | """ |
|
202 | 205 | |
|
203 | 206 | q = ChangesetComment.query()\ |
|
204 | 207 | .filter(ChangesetComment.repo_id == repo_id)\ |
|
205 | 208 | .filter(ChangesetComment.line_no == None)\ |
|
206 | 209 | .filter(ChangesetComment.f_path == None) |
|
207 | 210 | if revision: |
|
208 | 211 | q = q.filter(ChangesetComment.revision == revision) |
|
209 | 212 | elif pull_request: |
|
210 | 213 | pull_request = self.__get_pull_request(pull_request) |
|
211 | 214 | q = q.filter(ChangesetComment.pull_request == pull_request) |
|
212 | 215 | else: |
|
213 | 216 | raise Exception('Please specify revision or pull_request') |
|
214 | 217 | q = q.order_by(ChangesetComment.created_on) |
|
215 | 218 | return q.all() |
|
216 | 219 | |
|
217 | 220 | def get_inline_comments(self, repo_id, revision=None, pull_request=None): |
|
218 | 221 | q = self.sa.query(ChangesetComment)\ |
|
219 | 222 | .filter(ChangesetComment.repo_id == repo_id)\ |
|
220 | 223 | .filter(ChangesetComment.line_no != None)\ |
|
221 | 224 | .filter(ChangesetComment.f_path != None)\ |
|
222 | 225 | .order_by(ChangesetComment.comment_id.asc())\ |
|
223 | 226 | |
|
224 | 227 | if revision: |
|
225 | 228 | q = q.filter(ChangesetComment.revision == revision) |
|
226 | 229 | elif pull_request: |
|
227 | 230 | pull_request = self.__get_pull_request(pull_request) |
|
228 | 231 | q = q.filter(ChangesetComment.pull_request == pull_request) |
|
229 | 232 | else: |
|
230 | 233 | raise Exception('Please specify revision or pull_request_id') |
|
231 | 234 | |
|
232 | 235 | comments = q.all() |
|
233 | 236 | |
|
234 | 237 | paths = defaultdict(lambda: defaultdict(list)) |
|
235 | 238 | |
|
236 | 239 | for co in comments: |
|
237 | 240 | paths[co.f_path][co.line_no].append(co) |
|
238 | 241 | return paths.items() |
General Comments 0
You need to be logged in to leave comments.
Login now