[PATCH 4 of 8 v2] comment: add draft flag for comments

Jan Heylen heyleke at gmail.com
Sat May 16 08:02:29 EDT 2015


# HG changeset patch
# User Jan Heylen <heyleke at gmail.com>
# Date 1431012327 -7200
#      Thu May 07 17:25:27 2015 +0200
# Node ID fa1872a9f955ac81cdd7a3bc3516ad05f1263400
# Parent  a761b2b50afdf387951bef282954a86650b6ee70
comment: add draft flag for comments

diff -r a761b2b50afd -r fa1872a9f955 kallithea/model/comment.py
--- a/kallithea/model/comment.py	Thu May 07 17:25:49 2015 +0200
+++ b/kallithea/model/comment.py	Thu May 07 17:25:27 2015 +0200
@@ -161,7 +161,7 @@
 
     def create(self, text, repo, user, revision=None, pull_request=None,
                f_path=None, line_no=None, status_change=None, closing_pr=False,
-               send_email=True):
+               send_email=True, draft=False):
         """
         Creates a new comment for either a changeset or a pull request.
         status_change and closing_pr is only for the optional email.
@@ -180,7 +180,7 @@
         comment.text = text
         comment.f_path = f_path
         comment.line_no = line_no
-        comment.draft = False
+        comment.draft = draft
 
         if revision:
             comment.revision = revision
@@ -193,7 +193,7 @@
         Session().add(comment)
         Session().flush()
 
-        if send_email:
+        if (not draft) and send_email:
             (subj, body, recipients, notification_type,
              email_kwargs) = self._get_notification_data(
                                 repo, comment, user,
@@ -231,14 +231,72 @@
 
         return comment
 
-    def get_comments(self, repo_id, revision=None, pull_request=None):
+    def draft_to_comment(self, repo, draft, user, status_change, closing_pr):
+        draft = self.__get_changeset_comment(draft)
+        draft.draft = False
+        (subj, body, recipients, notification_type,
+         email_kwargs) = self._get_notification_data(
+            repo, draft, user,
+            comment_text=draft.text,
+            line_no=draft.line_no,
+            revision=draft.revision,
+            pull_request=draft.pull_request,
+            status_change=status_change,
+            closing_pr=closing_pr)
+
+        mentions = set(self._extract_mentions(body)).difference(recipients)
+
+        return subj, body, recipients, notification_type, email_kwargs, mentions
+
+    def commit_drafts(self, repo_id, user_id, revision=None, pull_request=None, status_change=None):
+        repo = self._get_repo(repo_id)
+        user = self._get_user(user_id)
+        bodies = []
+        mention_recipients = set()
+
+        drafts = self.get_inline_drafts(repo_id, revision=revision, pull_request=pull_request, user_id=user_id)
+        for __,lines in drafts:
+            for draftlist in lines.values():
+                for draft in draftlist:
+                    (subj, body, recipients, notification_type,
+                     email_kwargs, mentions) = self.draft_to_comment(repo, draft, user, None, False)
+                    bodies.append(body)
+                    mention_recipients.update(mentions)
+        gen_drafts = self.get_comments(repo_id, revision=revision, pull_request=pull_request, user_id=user_id, draft=True)
+        for draft in gen_drafts:
+            (subj, body, recipients, notification_type,
+             email_kwargs, mentions) = self.draft_to_comment(repo, draft, user, status_change, False)
+            bodies.append(body)
+            mention_recipients.update(mentions)
+
+        if email_kwargs:
+            email_kwargs['is_mention'] = False
+            # create notification objects, and emails
+            NotificationModel().create(
+                created_by=user_id, subject=subj, bodies=bodies,
+                recipients=recipients, type_=notification_type,
+                email_kwargs=email_kwargs)
+
+        if mention_recipients:
+            email_kwargs['is_mention'] = True
+            subj = _('[Mention]') + ' ' + subj
+            NotificationModel().create(
+               created_by=user_id, subject=subj, bodies=bodies,
+               recipients=mention_recipients,
+               type_=notification_type,
+               email_kwargs=email_kwargs
+            )
+        return drafts
+
+
+    def get_comments(self, repo_id, revision=None, pull_request=None, user_id=None, draft=False):
         """
         Gets general comments for either revision or pull_request.
 
         Returns a list, ordered by creation date.
         """
         return self._get_comments(repo_id, revision=revision, pull_request=pull_request,
-                                  inline=False)
+                                  inline=False, draft=draft)
 
     def get_inline_comments(self, repo_id, revision=None, pull_request=None):
         """
@@ -246,15 +304,33 @@
 
         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)
+        return self._get_inline_comments(repo_id, revision=revision, pull_request=pull_request,
+                                         draft=False)
+
+    def get_inline_drafts(self, repo_id, revision=None, pull_request=None, user_id=None):
+        """
+        Gets inline draft comments for either revision or pull_request.
+
+        Returns a list of tuples with file path and list of comments per line number.
+        """
+        return self._get_inline_comments(repo_id, revision=revision, pull_request=pull_request,
+                                         user_id=user_id, draft=True)
+
+    def _get_inline_comments(self, repo_id, revision=None, pull_request=None, user_id=None, draft=False):
+        """
+        Gets (draft) inline comments for either revision or pull_request.
+
+        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, user_id=user_id,
+                                      inline=True, draft=draft)
 
         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):
+    def _get_comments(self, repo_id, revision=None, pull_request=None, user_id=None, inline=False, draft=False):
         """
         Gets comments for either revision or pull_request_id, either inline or general.
         """
@@ -276,4 +352,9 @@
         else:
             raise Exception('Please specify either revision or pull_request')
 
+        q = q.filter(ChangesetComment.draft == draft)
+
+        if user_id:
+            q = q.filter(ChangesetComment.user_id == user_id)
+
         return q.order_by(ChangesetComment.created_on).all()


More information about the kallithea-general mailing list