Mercurial > kallithea
annotate rhodecode/model/comment.py @ 2156:a27e4d44237e beta
Use __unicode__ instead of __repr__ in models.
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Thu, 22 Mar 2012 02:44:42 +0200 |
parents | a8c9c0094ddf |
children | 989c137f26eb |
rev | line source |
---|---|
1670 | 1 # -*- coding: utf-8 -*- |
2 """ | |
3 rhodecode.model.comment | |
4 ~~~~~~~~~~~~~~~~~~~~~~~ | |
5 | |
6 comments model for RhodeCode | |
1789
17caf4efe15c
implements #308 rewrote diffs to enable displaying full diff on each file
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
7 |
1670 | 8 :created_on: Nov 11, 2011 |
9 :author: marcink | |
1824
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1789
diff
changeset
|
10 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com> |
1670 | 11 :license: GPLv3, see COPYING for more details. |
12 """ | |
13 # This program is free software: you can redistribute it and/or modify | |
14 # it under the terms of the GNU General Public License as published by | |
15 # the Free Software Foundation, either version 3 of the License, or | |
16 # (at your option) any later version. | |
17 # | |
18 # This program is distributed in the hope that it will be useful, | |
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 # GNU General Public License for more details. | |
22 # | |
23 # You should have received a copy of the GNU General Public License | |
24 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 | |
26 import logging | |
27 import traceback | |
28 | |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
29 from pylons.i18n.translation import _ |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
30 from sqlalchemy.util.compat import defaultdict |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
31 |
2109 | 32 from rhodecode.lib.utils2 import extract_mentioned_users |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
33 from rhodecode.lib import helpers as h |
1670 | 34 from rhodecode.model import BaseModel |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
35 from rhodecode.model.db import ChangesetComment, User, Repository, Notification |
1703
f23828b00b21
notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1681
diff
changeset
|
36 from rhodecode.model.notification import NotificationModel |
1670 | 37 |
38 log = logging.getLogger(__name__) | |
39 | |
40 | |
41 class ChangesetCommentsModel(BaseModel): | |
42 | |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
43 def __get_changeset_comment(self, changeset_comment): |
1716
7d1fc253549e
notification to commit author + gardening
Marcin Kuzminski <marcin@python-works.com>
parents:
1713
diff
changeset
|
44 return self._get_instance(ChangesetComment, changeset_comment) |
1670 | 45 |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
46 def _extract_mentions(self, s): |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
47 user_objects = [] |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
48 for username in extract_mentioned_users(s): |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
49 user_obj = User.get_by_username(username, case_insensitive=True) |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
50 if user_obj: |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
51 user_objects.append(user_obj) |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
52 return user_objects |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
53 |
1675
7c487d2678c7
code refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1670
diff
changeset
|
54 def create(self, text, repo_id, user_id, revision, f_path=None, |
1670 | 55 line_no=None): |
56 """ | |
57 Creates new comment for changeset | |
1789
17caf4efe15c
implements #308 rewrote diffs to enable displaying full diff on each file
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
58 |
1670 | 59 :param text: |
60 :param repo_id: | |
61 :param user_id: | |
1675
7c487d2678c7
code refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1670
diff
changeset
|
62 :param revision: |
1670 | 63 :param f_path: |
64 :param line_no: | |
65 """ | |
2150
a8c9c0094ddf
White space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
66 |
1677
7276b170ce8b
#71 code-review
Marcin Kuzminski <marcin@python-works.com>
parents:
1675
diff
changeset
|
67 if text: |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
68 repo = Repository.get(repo_id) |
1716
7d1fc253549e
notification to commit author + gardening
Marcin Kuzminski <marcin@python-works.com>
parents:
1713
diff
changeset
|
69 cs = repo.scm_instance.get_changeset(revision) |
7d1fc253549e
notification to commit author + gardening
Marcin Kuzminski <marcin@python-works.com>
parents:
1713
diff
changeset
|
70 desc = cs.message |
2082 | 71 author_email = cs.author_email |
1677
7276b170ce8b
#71 code-review
Marcin Kuzminski <marcin@python-works.com>
parents:
1675
diff
changeset
|
72 comment = ChangesetComment() |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
73 comment.repo = repo |
1677
7276b170ce8b
#71 code-review
Marcin Kuzminski <marcin@python-works.com>
parents:
1675
diff
changeset
|
74 comment.user_id = user_id |
7276b170ce8b
#71 code-review
Marcin Kuzminski <marcin@python-works.com>
parents:
1675
diff
changeset
|
75 comment.revision = revision |
7276b170ce8b
#71 code-review
Marcin Kuzminski <marcin@python-works.com>
parents:
1675
diff
changeset
|
76 comment.text = text |
7276b170ce8b
#71 code-review
Marcin Kuzminski <marcin@python-works.com>
parents:
1675
diff
changeset
|
77 comment.f_path = f_path |
7276b170ce8b
#71 code-review
Marcin Kuzminski <marcin@python-works.com>
parents:
1675
diff
changeset
|
78 comment.line_no = line_no |
1670 | 79 |
1677
7276b170ce8b
#71 code-review
Marcin Kuzminski <marcin@python-works.com>
parents:
1675
diff
changeset
|
80 self.sa.add(comment) |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
81 self.sa.flush() |
1703
f23828b00b21
notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1681
diff
changeset
|
82 # make notification |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
83 line = '' |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
84 if line_no: |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
85 line = _('on line %s') % line_no |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
86 subj = h.link_to('Re commit: %(commit_desc)s %(line)s' % \ |
1789
17caf4efe15c
implements #308 rewrote diffs to enable displaying full diff on each file
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
87 {'commit_desc': desc, 'line': line}, |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
88 h.url('changeset_home', repo_name=repo.repo_name, |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
89 revision=revision, |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
90 anchor='comment-%s' % comment.comment_id, |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
91 qualified=True, |
1712
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
92 ) |
cac5109ac3b6
Notification system improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1703
diff
changeset
|
93 ) |
1703
f23828b00b21
notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1681
diff
changeset
|
94 body = text |
2077 | 95 |
96 # get the current participants of this changeset | |
1703
f23828b00b21
notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1681
diff
changeset
|
97 recipients = ChangesetComment.get_users(revision=revision) |
2077 | 98 |
2082 | 99 # add changeset author if it's in rhodecode system |
100 recipients += [User.get_by_email(author_email)] | |
1716
7d1fc253549e
notification to commit author + gardening
Marcin Kuzminski <marcin@python-works.com>
parents:
1713
diff
changeset
|
101 |
2077 | 102 NotificationModel().create( |
103 created_by=user_id, subject=subj, body=body, | |
104 recipients=recipients, type_=Notification.TYPE_CHANGESET_COMMENT | |
105 ) | |
1703
f23828b00b21
notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents:
1681
diff
changeset
|
106 |
1717
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
107 mention_recipients = set(self._extract_mentions(body))\ |
7ff304d3028f
Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
108 .difference(recipients) |
1716
7d1fc253549e
notification to commit author + gardening
Marcin Kuzminski <marcin@python-works.com>
parents:
1713
diff
changeset
|
109 if mention_recipients: |
7d1fc253549e
notification to commit author + gardening
Marcin Kuzminski <marcin@python-works.com>
parents:
1713
diff
changeset
|
110 subj = _('[Mention]') + ' ' + subj |
2077 | 111 NotificationModel().create( |
112 created_by=user_id, subject=subj, body=body, | |
113 recipients=mention_recipients, | |
114 type_=Notification.TYPE_CHANGESET_COMMENT | |
115 ) | |
1716
7d1fc253549e
notification to commit author + gardening
Marcin Kuzminski <marcin@python-works.com>
parents:
1713
diff
changeset
|
116 |
1677
7276b170ce8b
#71 code-review
Marcin Kuzminski <marcin@python-works.com>
parents:
1675
diff
changeset
|
117 return comment |
1670 | 118 |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
119 def delete(self, comment): |
1670 | 120 """ |
121 Deletes given comment | |
1789
17caf4efe15c
implements #308 rewrote diffs to enable displaying full diff on each file
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
122 |
1670 | 123 :param comment_id: |
124 """ | |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
125 comment = self.__get_changeset_comment(comment) |
1670 | 126 self.sa.delete(comment) |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1712
diff
changeset
|
127 |
1670 | 128 return comment |
1675
7c487d2678c7
code refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1670
diff
changeset
|
129 |
7c487d2678c7
code refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1670
diff
changeset
|
130 def get_comments(self, repo_id, revision): |
7c487d2678c7
code refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1670
diff
changeset
|
131 return ChangesetComment.query()\ |
7c487d2678c7
code refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1670
diff
changeset
|
132 .filter(ChangesetComment.repo_id == repo_id)\ |
7c487d2678c7
code refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1670
diff
changeset
|
133 .filter(ChangesetComment.revision == revision)\ |
7c487d2678c7
code refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1670
diff
changeset
|
134 .filter(ChangesetComment.line_no == None)\ |
7c487d2678c7
code refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1670
diff
changeset
|
135 .filter(ChangesetComment.f_path == None).all() |
7c487d2678c7
code refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1670
diff
changeset
|
136 |
1677
7276b170ce8b
#71 code-review
Marcin Kuzminski <marcin@python-works.com>
parents:
1675
diff
changeset
|
137 def get_inline_comments(self, repo_id, revision): |
1675
7c487d2678c7
code refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1670
diff
changeset
|
138 comments = self.sa.query(ChangesetComment)\ |
7c487d2678c7
code refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1670
diff
changeset
|
139 .filter(ChangesetComment.repo_id == repo_id)\ |
1681
1bf03daafaf0
fixes inline comments double entries
Marcin Kuzminski <marcin@python-works.com>
parents:
1677
diff
changeset
|
140 .filter(ChangesetComment.revision == revision)\ |
1bf03daafaf0
fixes inline comments double entries
Marcin Kuzminski <marcin@python-works.com>
parents:
1677
diff
changeset
|
141 .filter(ChangesetComment.line_no != None)\ |
1bf03daafaf0
fixes inline comments double entries
Marcin Kuzminski <marcin@python-works.com>
parents:
1677
diff
changeset
|
142 .filter(ChangesetComment.f_path != None).all() |
1677
7276b170ce8b
#71 code-review
Marcin Kuzminski <marcin@python-works.com>
parents:
1675
diff
changeset
|
143 |
1789
17caf4efe15c
implements #308 rewrote diffs to enable displaying full diff on each file
Marcin Kuzminski <marcin@python-works.com>
parents:
1734
diff
changeset
|
144 paths = defaultdict(lambda: defaultdict(list)) |
1675
7c487d2678c7
code refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1670
diff
changeset
|
145 |
7c487d2678c7
code refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1670
diff
changeset
|
146 for co in comments: |
1677
7276b170ce8b
#71 code-review
Marcin Kuzminski <marcin@python-works.com>
parents:
1675
diff
changeset
|
147 paths[co.f_path][co.line_no].append(co) |
7276b170ce8b
#71 code-review
Marcin Kuzminski <marcin@python-works.com>
parents:
1675
diff
changeset
|
148 return paths.items() |