changeset 2541:1c2ba03c42b4 beta

Added dynamic data loading for other repo we open pull request against - fixed notification emails, now with link to comment or status change in given pull request
author Marcin Kuzminski <marcin@python-works.com>
date Tue, 03 Jul 2012 03:07:15 +0200
parents b1975bb589c5
children 4496d3119627
files rhodecode/controllers/changeset.py rhodecode/controllers/pullrequests.py rhodecode/model/comment.py rhodecode/model/pull_request.py rhodecode/templates/pullrequests/pullrequest.html
diffstat 5 files changed, 73 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/controllers/changeset.py	Tue Jul 03 01:35:12 2012 +0200
+++ b/rhodecode/controllers/changeset.py	Tue Jul 03 03:07:15 2012 +0200
@@ -377,8 +377,8 @@
 
         comm = ChangesetCommentsModel().create(
             text=request.POST.get('text'),
-            repo_id=c.rhodecode_db_repo.repo_id,
-            user_id=c.rhodecode_user.user_id,
+            repo=c.rhodecode_db_repo.repo_id,
+            user=c.rhodecode_user.user_id,
             revision=revision,
             f_path=request.POST.get('f_path'),
             line_no=request.POST.get('line'),
--- a/rhodecode/controllers/pullrequests.py	Tue Jul 03 01:35:12 2012 +0200
+++ b/rhodecode/controllers/pullrequests.py	Tue Jul 03 03:07:15 2012 +0200
@@ -34,6 +34,7 @@
 from pylons.i18n.translation import _
 from pylons.decorators import jsonify
 
+from rhodecode.lib.compat import json
 from rhodecode.lib.base import BaseRepoController, render
 from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator
 from rhodecode.lib import helpers as h
@@ -86,6 +87,8 @@
             log.error('Review not available for GIT REPOS')
             raise HTTPNotFound
 
+        other_repos_info = {}
+
         c.org_refs = self._get_repo_refs(c.rhodecode_repo)
         c.org_repos = []
         c.other_repos = []
@@ -95,12 +98,23 @@
 
         c.other_refs = c.org_refs
         c.other_repos.extend(c.org_repos)
+
+        #add orginal repo
+        other_repos_info[org_repo.repo_name] = {
+            'gravatar': h.gravatar_url(org_repo.user.email, 24),
+            'description': org_repo.description
+        }
+
         c.default_pull_request = org_repo.repo_name
         #gather forks and add to this list
         for fork in org_repo.forks:
             c.other_repos.append((fork.repo_name, '%s/%s' % (
                                     fork.user.username, fork.repo_name))
                                  )
+            other_repos_info[fork.repo_name] = {
+                'gravatar': h.gravatar_url(fork.user.email, 24),
+                'description': fork.description
+            }
         #add parents of this fork also
         if org_repo.parent:
             c.default_pull_request = org_repo.parent.repo_name
@@ -108,7 +122,12 @@
                                         org_repo.parent.user.username,
                                         org_repo.parent.repo_name))
                                      )
+            other_repos_info[org_repo.parent.repo_name] = {
+                'gravatar': h.gravatar_url(org_repo.parent.user.email, 24),
+                'description': org_repo.parent.description
+            }
 
+        c.other_repos_info = json.dumps(other_repos_info)
         c.review_members = []
         c.available_members = []
         for u in User.query().filter(User.username != 'default').all():
@@ -134,17 +153,18 @@
         description = req_p['pullrequest_desc']
 
         try:
-            model = PullRequestModel()
-            pull_request = model.create(self.rhodecode_user.user_id, org_repo,
-                         org_ref, other_repo, other_ref, revisions,
-                         reviewers, title, description)
-            Session.commit()
+            pull_request = PullRequestModel().create(
+                self.rhodecode_user.user_id, org_repo, org_ref, other_repo,
+                other_ref, revisions, reviewers, title, description
+            )
+            Session().commit()
             h.flash(_('Successfully opened new pull request'),
                     category='success')
         except Exception:
             h.flash(_('Error occurred during sending pull request'),
                     category='error')
             log.error(traceback.format_exc())
+            return redirect(url('changelog_home', repo_name=org_repo,))
 
         return redirect(url('pullrequest_show', repo_name=other_repo,
                             pull_request_id=pull_request.pull_request_id))
@@ -257,8 +277,8 @@
 
         comm = ChangesetCommentsModel().create(
             text=request.POST.get('text'),
-            repo_id=c.rhodecode_db_repo.repo_id,
-            user_id=c.rhodecode_user.user_id,
+            repo=c.rhodecode_db_repo.repo_id,
+            user=c.rhodecode_user.user_id,
             pull_request=pull_request_id,
             f_path=request.POST.get('f_path'),
             line_no=request.POST.get('line'),
@@ -279,7 +299,7 @@
                       'user_commented_pull_request:%s' % pull_request_id,
                       c.rhodecode_db_repo, self.ip_addr, self.sa)
 
-        Session.commit()
+        Session().commit()
 
         if not request.environ.get('HTTP_X_PARTIAL_XHR'):
             return redirect(h.url('pullrequest_show', repo_name=repo_name,
@@ -302,7 +322,7 @@
         owner = lambda: co.author.user_id == c.rhodecode_user.user_id
         if h.HasPermissionAny('hg.admin', 'repository.admin')() or owner:
             ChangesetCommentsModel().delete(comment=co)
-            Session.commit()
+            Session().commit()
             return True
         else:
             raise HTTPForbidden()
\ No newline at end of file
--- a/rhodecode/model/comment.py	Tue Jul 03 01:35:12 2012 +0200
+++ b/rhodecode/model/comment.py	Tue Jul 03 03:07:15 2012 +0200
@@ -57,7 +57,7 @@
                 user_objects.append(user_obj)
         return user_objects
 
-    def create(self, text, repo_id, user_id, revision=None, pull_request=None,
+    def create(self, text, repo, user, revision=None, pull_request=None,
                f_path=None, line_no=None, status_change=None):
         """
         Creates new comment for changeset or pull request.
@@ -65,8 +65,8 @@
         status change of changeset or changesets associated with pull request
 
         :param text:
-        :param repo_id:
-        :param user_id:
+        :param repo:
+        :param user:
         :param revision:
         :param pull_request:
         :param f_path:
@@ -76,10 +76,11 @@
         if not text:
             return
 
-        repo = Repository.get(repo_id)
+        repo = self._get_repo(repo)
+        user = self._get_user(user)
         comment = ChangesetComment()
         comment.repo = repo
-        comment.user_id = user_id
+        comment.author = user
         comment.text = text
         comment.f_path = f_path
         comment.line_no = line_no
@@ -92,7 +93,7 @@
         elif pull_request:
             pull_request = self.__get_pull_request(pull_request)
             comment.pull_request = pull_request
-            desc = ''
+            desc = pull_request.pull_request_id
         else:
             raise Exception('Please specify revision or pull_request_id')
 
@@ -108,8 +109,8 @@
             if line_no:
                 line = _('on line %s') % line_no
             subj = safe_unicode(
-                h.link_to('Re commit: %(commit_desc)s %(line)s' % \
-                          {'commit_desc': desc, 'line': line},
+                h.link_to('Re commit: %(desc)s %(line)s' % \
+                          {'desc': desc, 'line': line},
                           h.url('changeset_home', repo_name=repo.repo_name,
                                 revision=revision,
                                 anchor='comment-%s' % comment.comment_id,
@@ -124,8 +125,18 @@
             recipients += [User.get_by_email(author_email)]
         #pull request
         elif pull_request:
-            #TODO: make this something usefull
-            subj = 'commented on pull request something...'
+            subj = safe_unicode(
+                h.link_to('Re pull request: %(desc)s %(line)s' % \
+                          {'desc': desc, 'line': line},
+                          h.url('pullrequest_show',
+                                repo_name=pull_request.other_repo.repo_name,
+                                pull_request_id=pull_request.pull_request_id,
+                                anchor='comment-%s' % comment.comment_id,
+                                qualified=True,
+                          )
+                )
+            )
+
             notification_type = Notification.TYPE_PULL_REQUEST_COMMENT
             # get the current participants of this pull request
             recipients = ChangesetComment.get_users(pull_request_id=
@@ -135,7 +146,7 @@
 
         # create notification objects, and emails
         NotificationModel().create(
-          created_by=user_id, subject=subj, body=body,
+          created_by=user, subject=subj, body=body,
           recipients=recipients, type_=notification_type,
           email_kwargs={'status_change': status_change}
         )
@@ -145,7 +156,7 @@
         if mention_recipients:
             subj = _('[Mention]') + ' ' + subj
             NotificationModel().create(
-                created_by=user_id, subject=subj, body=body,
+                created_by=user, subject=subj, body=body,
                 recipients=mention_recipients,
                 type_=notification_type,
                 email_kwargs={'status_change': status_change}
--- a/rhodecode/model/pull_request.py	Tue Jul 03 01:35:12 2012 +0200
+++ b/rhodecode/model/pull_request.py	Tue Jul 03 03:07:15 2012 +0200
@@ -29,11 +29,15 @@
 
 from rhodecode.lib import helpers as h
 from rhodecode.model import BaseModel
-from rhodecode.model.db import PullRequest, PullRequestReviewers, Notification
+from rhodecode.model.db import PullRequest, PullRequestReviewers, Notification,\
+    ChangesetStatus
 from rhodecode.model.notification import NotificationModel
 from rhodecode.lib.utils2 import safe_unicode
 
 from rhodecode.lib.vcs.utils.hgcompat import discovery
+from rhodecode.model.changeset_status import ChangesetStatusModel
+from rhodecode.model.comment import ChangesetCommentsModel
+from rhodecode.model.meta import Session
 
 log = logging.getLogger(__name__)
 
@@ -48,19 +52,22 @@
 
     def create(self, created_by, org_repo, org_ref, other_repo,
                other_ref, revisions, reviewers, title, description=None):
+
         created_by_user = self._get_user(created_by)
+        org_repo = self._get_repo(org_repo)
+        other_repo = self._get_repo(other_repo)
 
         new = PullRequest()
-        new.org_repo = self._get_repo(org_repo)
+        new.org_repo = org_repo
         new.org_ref = org_ref
-        new.other_repo = self._get_repo(other_repo)
+        new.other_repo = other_repo
         new.other_ref = other_ref
         new.revisions = revisions
         new.title = title
         new.description = description
         new.author = created_by_user
         self.sa.add(new)
-
+        Session().flush()
         #members
         for member in reviewers:
             _usr = self._get_user(member)
@@ -82,7 +89,7 @@
             )
         )
         body = description
-        notif.create(created_by=created_by, subject=subject, body=body,
+        notif.create(created_by=created_by_user, subject=subject, body=body,
                      recipients=reviewers,
                      type_=Notification.TYPE_PULL_REQUEST,)
 
--- a/rhodecode/templates/pullrequests/pullrequest.html	Tue Jul 03 01:35:12 2012 +0200
+++ b/rhodecode/templates/pullrequests/pullrequest.html	Tue Jul 03 03:07:15 2012 +0200
@@ -50,12 +50,12 @@
         <div style="float:left">
             <div class="fork_user">
                 <div class="gravatar">
-                    <img alt="gravatar" src="${h.gravatar_url(c.rhodecode_db_repo.user.email,24)}"/>
+                    <img id="other_repo_gravatar" alt="gravatar" src=""/>
                 </div>
                 <span style="font-size: 20px">
                 ${h.select('other_repo',c.default_pull_request ,c.other_repos,class_='refs')}:${h.select('other_ref','',c.other_refs,class_='refs')}
                 </span>
-                 <div style="padding:5px 3px 3px 42px;">${c.rhodecode_db_repo.description}</div>
+                 <div id="other_repo_desc" style="padding:5px 3px 3px 42px;"></div>
             </div>
             <div style="clear:both;padding-top: 10px"></div>
         </div>
@@ -142,7 +142,7 @@
 
 <script type="text/javascript">
   MultiSelectWidget('review_members','available_members','pull_request_form');
-
+  var other_repos_info = ${c.other_repos_info|n};
   var loadPreview = function(){
 	  YUD.setStyle(YUD.get('pull_request_overview_url').parentElement,'display','none');
       var url = "${h.url('compare_url',
@@ -176,8 +176,12 @@
       }
 
       ypjax(url,'pull_request_overview', function(data){
+    	  var sel_box = YUQ('#pull_request_form #other_repo')[0];
+    	  var repo_name = sel_box.options[sel_box.selectedIndex].value;
     	  YUD.get('pull_request_overview_url').href = url;
     	  YUD.setStyle(YUD.get('pull_request_overview_url').parentElement,'display','');
+    	  YUD.get('other_repo_gravatar').src = other_repos_info[repo_name]['gravatar'];
+    	  YUD.get('other_repo_desc').innerHTML = other_repos_info[repo_name]['description'];
       })
   }
   YUE.on('refresh','click',function(e){