[PATCH v2] ChangesetCommentsModel: refactor duplicate code

Jan Heylen heyleke at gmail.com
Thu Apr 30 01:06:40 EDT 2015


# HG changeset patch
# User Jan Heylen <heyleke at gmail.com>
# Date 1430370318 -7200
#      Thu Apr 30 07:05:18 2015 +0200
# Node ID 406c95a068096edec2b8ffdedf2a0d1caced14a4
# 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 406c95a06809 kallithea/model/comment.py
--- a/kallithea/model/comment.py	Wed Apr 29 04:25:53 2015 +0200
+++ b/kallithea/model/comment.py	Thu Apr 30 07:05:18 2015 +0200
@@ -65,8 +65,6 @@
         """
         Get notification data
 
-        :param comment_text:
-        :param line:
         :returns: tuple (subj,body,recipients,notification_type,email_kwargs)
         """
         # make notification
@@ -171,16 +169,7 @@
         If status_change is not None this comment is associated with a
         status change of changeset or changesets associated with pull request
 
-        :param text:
-        :param repo:
-        :param user:
-        :param revision:
-        :param pull_request: (for emails, not for comments)
-        :param f_path:
-        :param line_no:
-        :param status_change: (for emails, not for comments)
-        :param closing_pr: (for emails, not for comments)
-        :param send_email: also send email
+        Returns the created comment
         """
         if not status_change and not text:
             log.warning('Missing text for comment, skipping...')
@@ -241,8 +230,6 @@
     def delete(self, comment):
         """
         Deletes given comment
-
-        :param comment_id:
         """
         comment = self.__get_changeset_comment(comment)
         Session().delete(comment)
@@ -253,31 +240,36 @@
         """
         Gets main comments based on revision or pull_request_id
 
-        :param repo_id:
-        :param revision:
-        :param pull_request:
+        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):
+        """
+        Gets inline comments based on revision or pull_request_id
+          
+        Returns a dictionary with extra information on file path and line number
+        """
+        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
+        Could be normal comments or inline Comments.
+        
+        Retrun value depends on the type of comments returned:
+          * for normal comments, a list, ordered by creation date
+          * for inline commetns, a dictionary with extra information on file path and line number
+        """
         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 +279,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