changeset 5086:b5e399286ce5

ChangesetCommentsModel: refactor duplicate code Comments are now always ordered by creation date ... but that should not make any difference.
author Jan Heylen <heyleke@gmail.com>
date Thu, 30 Apr 2015 07:05:18 +0200
parents 34f2da0a2969
children d0d386e51d06
files kallithea/model/comment.py
diffstat 1 files changed, 24 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/model/comment.py	Thu Apr 30 07:05:18 2015 +0200
+++ b/kallithea/model/comment.py	Thu Apr 30 07:05:18 2015 +0200
@@ -236,19 +236,8 @@
 
         Returns a list, ordered by creation date.
         """
-        q = ChangesetComment.query()\
-                .filter(ChangesetComment.repo_id == repo_id)\
-                .filter(ChangesetComment.line_no == None)\
-                .filter(ChangesetComment.f_path == None)
-        if revision:
-            q = q.filter(ChangesetComment.revision == revision)
-        elif pull_request:
-            pull_request = self.__get_pull_request(pull_request)
-            q = q.filter(ChangesetComment.pull_request == pull_request)
-        else:
-            raise Exception('Please specify revision or pull_request')
-        q = q.order_by(ChangesetComment.created_on)
-        return q.all()
+        return self._get_comments(repo_id, revision=revision, pull_request=pull_request,
+                                  inline=False)
 
     def get_inline_comments(self, repo_id, revision=None, pull_request=None):
         """
@@ -256,11 +245,27 @@
 
         Returns a list of tuples with file path and list of comments per line number.
         """
+        comments = self._get_comments(repo_id, revision=revision, pull_request=pull_request,
+                                      inline=True)
+
+        paths = defaultdict(lambda: defaultdict(list))
+        for co in comments:
+            paths[co.f_path][co.line_no].append(co)
+        return paths.items()
+
+    def _get_comments(self, repo_id, revision=None, pull_request=None, inline=False):
+        """
+        Gets comments for either revision or pull_request_id, either inline or general.
+        """
         q = Session().query(ChangesetComment)\
-            .filter(ChangesetComment.repo_id == repo_id)\
-            .filter(ChangesetComment.line_no != None)\
-            .filter(ChangesetComment.f_path != None)\
-            .order_by(ChangesetComment.comment_id.asc())\
+            .filter(ChangesetComment.repo_id == repo_id)
+
+        if inline:
+            q = q.filter(ChangesetComment.line_no != None)\
+                .filter(ChangesetComment.f_path != None)
+        else:
+            q = q.filter(ChangesetComment.line_no == None)\
+                .filter(ChangesetComment.f_path == None)
 
         if revision:
             q = q.filter(ChangesetComment.revision == revision)
@@ -268,12 +273,6 @@
             pull_request = self.__get_pull_request(pull_request)
             q = q.filter(ChangesetComment.pull_request == pull_request)
         else:
-            raise Exception('Please specify revision or pull_request_id')
-
-        comments = q.all()
+            raise Exception('Please specify either revision or pull_request')
 
-        paths = defaultdict(lambda: defaultdict(list))
-
-        for co in comments:
-            paths[co.f_path][co.line_no].append(co)
-        return paths.items()
+        return q.order_by(ChangesetComment.created_on).all()