[PATCH] ChangesetCommentsModel: refactor duplicate code

Jan Heylen heyleke at gmail.com
Wed Apr 29 16:09:49 EDT 2015


# HG changeset patch
# User Jan Heylen <heyleke at gmail.com>
# Date 1430323563 -7200
#      Wed Apr 29 18:06:03 2015 +0200
# Node ID 8ead3310a073d6453b926b891f1a59a0fcf6c6b5
# Parent  71140b7c9abfcb34db70792c39e09a4e4796efb1
ChangesetCommentsModel: refactor duplicate code

KALLITHEA_WHOOSH_TEST_DISABLE=1 KALLITHEA_NO_TMP_PATH=1 nosetests kallithea/tests/functional/test_changeset_comments.py
still passes all 4 tests

diff -r 71140b7c9abf -r 8ead3310a073 kallithea/model/comment.py
--- a/kallithea/model/comment.py	Wed Apr 29 04:25:53 2015 +0200
+++ b/kallithea/model/comment.py	Wed Apr 29 18:06:03 2015 +0200
@@ -257,27 +257,36 @@
         :param revision:
         :param pull_request:
         """
-
-        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):
+        """
+        Gets inline comments based on revision or pull_request_id
+
+        :param repo_id:
+        :param revision:
+        :param pull_request:
+        """
+        return self._get_comments(repo_id, revision=revision, pull_request=pull_request, inline=True)
+
+    def _get_comments(self, repo_id, revision=None, pull_request=None, inline=False):
+        """
+        Gets comments based on revision or pull_request_id
+
+        :param repo_id:
+        :param revision:
+        :param pull_request:
+        :param inline: boolean, get normal comments or inline comments
+        """
         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())\
+
+        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)
@@ -287,7 +296,11 @@
         else:
             raise Exception('Please specify revision or pull_request_id')
 
+        q = q.order_by(ChangesetComment.created_on)
+
         comments = q.all()
+        if not inline:
+            return comments
 
         paths = defaultdict(lambda: defaultdict(list))
 


More information about the kallithea-general mailing list