##// END OF EJS Templates
White space cleanup
marcink -
r2150:a8c9c009 beta
parent child Browse files
Show More
@@ -1,148 +1,148 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
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, Notification
36 36 from rhodecode.model.notification import NotificationModel
37 37
38 38 log = logging.getLogger(__name__)
39 39
40 40
41 41 class ChangesetCommentsModel(BaseModel):
42 42
43 43 def __get_changeset_comment(self, changeset_comment):
44 44 return self._get_instance(ChangesetComment, changeset_comment)
45 45
46 46 def _extract_mentions(self, s):
47 47 user_objects = []
48 48 for username in extract_mentioned_users(s):
49 49 user_obj = User.get_by_username(username, case_insensitive=True)
50 50 if user_obj:
51 51 user_objects.append(user_obj)
52 52 return user_objects
53 53
54 54 def create(self, text, repo_id, user_id, revision, f_path=None,
55 55 line_no=None):
56 56 """
57 57 Creates new comment for changeset
58 58
59 59 :param text:
60 60 :param repo_id:
61 61 :param user_id:
62 62 :param revision:
63 63 :param f_path:
64 64 :param line_no:
65 65 """
66
66 67 if text:
67 68 repo = Repository.get(repo_id)
68 69 cs = repo.scm_instance.get_changeset(revision)
69 70 desc = cs.message
70 71 author_email = cs.author_email
71 72 comment = ChangesetComment()
72 73 comment.repo = repo
73 74 comment.user_id = user_id
74 75 comment.revision = revision
75 76 comment.text = text
76 77 comment.f_path = f_path
77 78 comment.line_no = line_no
78 79
79 80 self.sa.add(comment)
80 81 self.sa.flush()
81
82 82 # make notification
83 83 line = ''
84 84 if line_no:
85 85 line = _('on line %s') % line_no
86 86 subj = h.link_to('Re commit: %(commit_desc)s %(line)s' % \
87 87 {'commit_desc': desc, 'line': line},
88 88 h.url('changeset_home', repo_name=repo.repo_name,
89 89 revision=revision,
90 90 anchor='comment-%s' % comment.comment_id,
91 91 qualified=True,
92 92 )
93 93 )
94 94 body = text
95 95
96 96 # get the current participants of this changeset
97 97 recipients = ChangesetComment.get_users(revision=revision)
98 98
99 99 # add changeset author if it's in rhodecode system
100 100 recipients += [User.get_by_email(author_email)]
101 101
102 102 NotificationModel().create(
103 103 created_by=user_id, subject=subj, body=body,
104 104 recipients=recipients, type_=Notification.TYPE_CHANGESET_COMMENT
105 105 )
106 106
107 107 mention_recipients = set(self._extract_mentions(body))\
108 108 .difference(recipients)
109 109 if mention_recipients:
110 110 subj = _('[Mention]') + ' ' + subj
111 111 NotificationModel().create(
112 112 created_by=user_id, subject=subj, body=body,
113 113 recipients=mention_recipients,
114 114 type_=Notification.TYPE_CHANGESET_COMMENT
115 115 )
116 116
117 117 return comment
118 118
119 119 def delete(self, comment):
120 120 """
121 121 Deletes given comment
122 122
123 123 :param comment_id:
124 124 """
125 125 comment = self.__get_changeset_comment(comment)
126 126 self.sa.delete(comment)
127 127
128 128 return comment
129 129
130 130 def get_comments(self, repo_id, revision):
131 131 return ChangesetComment.query()\
132 132 .filter(ChangesetComment.repo_id == repo_id)\
133 133 .filter(ChangesetComment.revision == revision)\
134 134 .filter(ChangesetComment.line_no == None)\
135 135 .filter(ChangesetComment.f_path == None).all()
136 136
137 137 def get_inline_comments(self, repo_id, revision):
138 138 comments = self.sa.query(ChangesetComment)\
139 139 .filter(ChangesetComment.repo_id == repo_id)\
140 140 .filter(ChangesetComment.revision == revision)\
141 141 .filter(ChangesetComment.line_no != None)\
142 142 .filter(ChangesetComment.f_path != None).all()
143 143
144 144 paths = defaultdict(lambda: defaultdict(list))
145 145
146 146 for co in comments:
147 147 paths[co.f_path][co.line_no].append(co)
148 148 return paths.items()
General Comments 0
You need to be logged in to leave comments. Login now