changeset 1675:7c487d2678c7 beta

code refactoring
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 12 Nov 2011 17:30:12 +0200
parents 6f0143e5efe5
children e86191684f4b
files rhodecode/controllers/changeset.py rhodecode/model/comment.py rhodecode/model/db.py rhodecode/templates/changeset/changeset_file_comment.html
diffstat 4 files changed, 30 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/controllers/changeset.py	Sat Nov 12 17:09:17 2011 +0200
+++ b/rhodecode/controllers/changeset.py	Sat Nov 12 17:30:12 2011 +0200
@@ -98,15 +98,13 @@
         c.cut_off = False  # defines if cut off limit is reached
 
         c.comments = []
-        for cs in c.cs_ranges:
-            c.comments.extend(ChangesetComment.query()\
-                              .filter(ChangesetComment.repo_id == c.rhodecode_db_repo.repo_id)\
-                              .filter(ChangesetComment.commit_id == cs.raw_id)\
-                              .filter(ChangesetComment.line_no == None)\
-                              .filter(ChangesetComment.f_path == None).all())
 
         # Iterate over ranges (default changeset view is always one changeset)
         for changeset in c.cs_ranges:
+            c.comments.extend(ChangesetCommentsModel()\
+                              .get_comments(c.rhodecode_db_repo.repo_id,
+                                            changeset.raw_id))
+
             c.changes[changeset.raw_id] = []
             try:
                 changeset_parent = changeset.parents[0]
@@ -272,7 +270,7 @@
         ccmodel.create(text=request.POST.get('text'),
                        repo_id=c.rhodecode_db_repo.repo_id, 
                        user_id=c.rhodecode_user.user_id, 
-                       commit_id=revision, f_path=request.POST.get('f_path'), 
+                       revision=revision, f_path=request.POST.get('f_path'), 
                        line_no = request.POST.get('line'))
 
         return redirect(h.url('changeset_home', repo_name=repo_name,
--- a/rhodecode/model/comment.py	Sat Nov 12 17:09:17 2011 +0200
+++ b/rhodecode/model/comment.py	Sat Nov 12 17:30:12 2011 +0200
@@ -29,6 +29,7 @@
 
 from rhodecode.model import BaseModel
 from rhodecode.model.db import ChangesetComment
+from sqlalchemy.util.compat import defaultdict
 
 log = logging.getLogger(__name__)
 
@@ -36,7 +37,7 @@
 class ChangesetCommentsModel(BaseModel):
 
 
-    def create(self, text, repo_id, user_id, commit_id, f_path=None,
+    def create(self, text, repo_id, user_id, revision, f_path=None,
                line_no=None):
         """
         Creates new comment for changeset
@@ -44,7 +45,7 @@
         :param text:
         :param repo_id:
         :param user_id:
-        :param commit_id:
+        :param revision:
         :param f_path:
         :param line_no:
         """
@@ -52,7 +53,7 @@
         comment = ChangesetComment()
         comment.repo_id = repo_id
         comment.user_id = user_id
-        comment.commit_id = commit_id
+        comment.revision = revision
         comment.text = text
         comment.f_path = f_path
         comment.line_no = line_no
@@ -71,3 +72,22 @@
         self.sa.delete(comment)
         self.sa.commit()
         return comment
+
+
+    def get_comments(self, repo_id, revision):
+        return ChangesetComment.query()\
+                .filter(ChangesetComment.repo_id == repo_id)\
+                .filter(ChangesetComment.revision == revision)\
+                .filter(ChangesetComment.line_no == None)\
+                .filter(ChangesetComment.f_path == None).all()
+
+    def get_comments_for_file(self, repo_id, f_path, raw_id):
+        comments = self.sa.query(ChangesetComment)\
+            .filter(ChangesetComment.repo_id == repo_id)\
+            .filter(ChangesetComment.commit_id == raw_id)\
+            .filter(ChangesetComment.f_path == f_path).all()
+
+        d = defaultdict(list)
+        for co in comments:
+            d[co.line_no].append(co)
+        return d.items()
--- a/rhodecode/model/db.py	Sat Nov 12 17:09:17 2011 +0200
+++ b/rhodecode/model/db.py	Sat Nov 12 17:30:12 2011 +0200
@@ -1100,7 +1100,7 @@
     __table_args__ = ({'extend_existing':True},)
     comment_id = Column('comment_id', Integer(), nullable=False, primary_key=True)
     repo_id = Column('repo_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False)
-    commit_id = Column('commit_id', String(100), nullable=False)
+    revision = Column('revision', String(40), nullable=False)
     line_no = Column('line_no', Integer(), nullable=True)
     f_path = Column('f_path', String(1000), nullable=True)
     user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False)
--- a/rhodecode/templates/changeset/changeset_file_comment.html	Sat Nov 12 17:09:17 2011 +0200
+++ b/rhodecode/templates/changeset/changeset_file_comment.html	Sat Nov 12 17:30:12 2011 +0200
@@ -10,7 +10,7 @@
   			${co.author.username}
   		</span>
   		<a href="${h.url.current(anchor='comment-%s' % co.comment_id)}"> ${_('commented on')} </a>
-  		${h.short_id(co.commit_id)}
+  		${h.short_id(co.revision)}
   		%if co.f_path:
   			${_(' in file ')}
   			${co.f_path}:L${co.line_no}