diff rhodecode/model/comment.py @ 2439:ad19dfcdb1cc codereview

Refactoring of changeset_file_comments for more generic usage. In both It enables sharing code between changeset, and pull requests discussions
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 10 Jun 2012 16:44:06 +0200
parents f29469677319
children 1bc579bcd67a
line wrap: on
line diff
--- a/rhodecode/model/comment.py	Sun Jun 10 16:39:52 2012 +0200
+++ b/rhodecode/model/comment.py	Sun Jun 10 16:44:06 2012 +0200
@@ -135,21 +135,44 @@
 
         return comment
 
-    def get_comments(self, repo_id, revision):
-        return ChangesetComment.query()\
+    def get_comments(self, repo_id, revision=None, pull_request_id=None):
+        """
+        Get's main comments based on revision or pull_request_id
+
+        :param repo_id:
+        :type repo_id:
+        :param revision:
+        :type revision:
+        :param pull_request_id:
+        :type pull_request_id:
+        """
+        q = ChangesetComment.query()\
                 .filter(ChangesetComment.repo_id == repo_id)\
-                .filter(ChangesetComment.revision == revision)\
                 .filter(ChangesetComment.line_no == None)\
-                .filter(ChangesetComment.f_path == None).all()
+                .filter(ChangesetComment.f_path == None)
+        if revision:
+            q = q.filter(ChangesetComment.revision == revision)
+        elif pull_request_id:
+            q = q.filter(ChangesetComment.pull_request_id == pull_request_id)
+        else:
+            raise Exception('Please specify revision or pull_request_id')
+        return q.all()
 
-    def get_inline_comments(self, repo_id, revision):
-        comments = self.sa.query(ChangesetComment)\
+    def get_inline_comments(self, repo_id, revision=None, pull_request_id=None):
+        q = self.sa.query(ChangesetComment)\
             .filter(ChangesetComment.repo_id == repo_id)\
-            .filter(ChangesetComment.revision == revision)\
             .filter(ChangesetComment.line_no != None)\
             .filter(ChangesetComment.f_path != None)\
             .order_by(ChangesetComment.comment_id.asc())\
-            .all()
+
+        if revision:
+            q = q.filter(ChangesetComment.revision == revision)
+        elif pull_request_id:
+            q = q.filter(ChangesetComment.pull_request_id == pull_request_id)
+        else:
+            raise Exception('Please specify revision or pull_request_id')
+
+        comments = q.all()
 
         paths = defaultdict(lambda: defaultdict(list))