Added simple interface to accept merge requests.
authorŁukasz Rekucki <lrekucki@gmail.com>
Thu, 8 Oct 2009 15:27:34 +0000 (17:27 +0200)
committerŁukasz Rekucki <lrekucki@gmail.com>
Thu, 8 Oct 2009 15:27:34 +0000 (17:27 +0200)
apps/api/handlers/library_handlers.py
apps/api/handlers/manage_handlers.py
apps/api/urls.py
apps/explorer/views.py
project/templates/manager/pull_request.html
project/urls.py

index e80d75c..d7438a1 100644 (file)
@@ -551,7 +551,7 @@ class MergeHandler(BaseHandler):
             # User is not permitted to make a merge, right away
             # So we instead create a pull request in the database
             try:
-                prq, created = PullRequest.get_or_create(                                        
+                prq, created = PullRequest.objects.get_or_create(
                     source_revision = str(udoc.revision),
                     defaults = {
                         'comitter': request.user,
index 9615958..b3e2760 100644 (file)
@@ -6,7 +6,9 @@ __doc__ = "Module documentation."
 
 from piston.handler import BaseHandler, AnonymousBaseHandler
 
+from api.utils import hglibrary
 from explorer.models import PullRequest
+from api.response import *
 
 class PullRequestListHandler(BaseHandler):
     allowed_methods = ('GET',)
@@ -19,7 +21,64 @@ class PullRequestListHandler(BaseHandler):
 
 
 class PullRequestHandler(BaseHandler):
-    allowed_methods = ('GET',)
+    allowed_methods = ('GET', 'PUT')
 
     def read(self, request, prq_id):
-        return PullRequest.objects.get(id=prq_id)    
\ No newline at end of file
+        return PullRequest.objects.get(id=prq_id)
+
+    def update(self, request, prq_id):
+        """Change the status of request"""
+
+        if not request.user.has_perm('explorer.document.can_share'):
+            return AccessDenied().django_response("Insufficient priviliges")
+        
+        prq = PullRequest.objects.get(id=prq_id)
+
+        if not prq:
+            return EntityNotFound().django_response()
+
+
+        action = request.PUT.get('action', None)
+
+        if action == 'accept' and prq.status == 'N':
+            return self.accept_merge(prq)
+        elif action == 'reject' and prq.status in ['N', 'R']:
+            return self.reject_merge(prq)
+        else:
+            return BadRequest().django_response()
+
+
+    @hglibrary
+    def accept_merge(self, prq, lib):        
+        doc = lib.document( prq.document )
+        udoc = doc.take( prq.comitter.username )
+        success, changed = udoc.share(prq.comment)
+
+        if not success:
+            return EntityConflict().django_response()
+
+        doc = doc.latest()
+
+        prq.status = 'A'
+        prq.merged_revisions = unicode(doc.revision)
+        prq.save()
+        
+        return SuccessAllOk().django_response({
+            'status': prq.status
+        })
+
+    
+    def reject_merge(self, prq, lib):
+        prq.status = 'R'
+        prq.save()
+
+        return SuccessAllOk().django_response({
+            'status': prq.status
+        })
+        
+
+        
+
+
+        
+
index deac319..d22d595 100644 (file)
@@ -33,8 +33,8 @@ urlpatterns = patterns('',
         {'emitter_format': 'json'} ),
         
     url(r"^pull-requests/(?P<prq_id>\d+)$", pullrequest_rsrc,
-        {'emitter_format': 'json'}, name="pullrequest_view" ),    
-    
+        {'emitter_format': 'json'}, name="pullrequest_view" ),
+        
     # Documents
     url(r'^documents$', library_resource,
         {'emitter_format': 'json'}, name="document_list_view"),
index 82376fc..124a574 100644 (file)
@@ -147,6 +147,14 @@ def _get_issues_for_file(fileid):
 # =================
 # = Pull requests =
 # =================
-#def pull_requests(request):
-#    return direct_to_template(request, 'manager/pull_request.html', extra_context = {
-#        'objects': models.PullRequest.objects.all()} )
+def pull_requests(request):
+    from explorer.models import PullRequest
+
+    objects = PullRequest.objects.order_by('status')
+
+    if not request.user.has_perm('explorer.book.can_share'):
+        objects = objects.filter(comitter=request.user)
+
+    
+    return direct_to_template(request, 'manager/pull_request.html', 
+        extra_context = {'objects': objects} )
index c03caab..9c23436 100644 (file)
@@ -1,7 +1,75 @@
+{% extends 'base.html' %}
+
+{% block extrahead %}
+<link rel="stylesheet" href="{{ STATIC_URL }}css/managment.css" type="text/css" />
+<script type="text/javascript">
+
+    
+    $(function() {
+
+        function refreshRow(id) {
+            var row = $('#request-' +id);
+
+            $.ajax({
+                url: "http://localhost:8000/api/pull-requests/" + id,                
+                dataType: 'json',
+                type: 'GET',
+                success: function(data) {
+                    row.removeClass('status-N');
+                    row.removeClass('status-R');
+                    row.removeClass('status-A');
+                    row.addClass('status-'+ data.status);
+
+                    $('.column-doc', row).html(data.document);
+                    $('.column-status', row).html(data.status);
+
+                    alert('Merge accepted.');
+                }
+            });            
+            
+        }
+        
+        $('.accept-button').click(function()
+        {
+            var id = parseInt($(this).attr('title'));
+            
+
+            $.ajax({
+                url: "http://localhost:8000/api/pull-requests/" + id,
+                data: {action: 'accept'},
+                dataType: 'json',
+                type: 'PUT',
+                success: function(data) {
+                    refreshRow(id);
+                }
+            });
+            
+        });
+        
+    });
+</script>
+{% endblock %}
+
+{% block maincontent %}
+<table class="request-report" cellspacing="0">
+    <tr>
+        <th>Utwór</th><th>Użytkownik</th><th>Komentarz</th><th>Stan</th>
+        <th>Akcje</th>
+    </tr>
 {% if objects %}
     {% for pullreq in objects %}
-    <p>{{ pullreq }}</p>
+    <tr class="status-{{pullreq.status}}" id="request-{{pullreq.id}}">
+        <td class="column-doc">{{ pullreq.document }}</td>
+        <td class="column-user">{{ pullreq.comitter }}</td>
+        <td class="column-comment">{{ pullreq.comment }}</td>
+        <td class="column-status"> {{ pullreq.status }}</td>
+        <td><button type="button" class="accept-button" title="{{pullreq.id}}">Akceptuj</button></td>
+    </tr>
+    
     {% endfor %}
 {% else %}
-    <p>Brak żądań</p>
+    <tr><td colspan="*">Brak żądań</td></tr>
 {% endif %}
+</table>
+
+{% endblock %}
index c38f3f6..e107635 100644 (file)
@@ -12,6 +12,9 @@ urlpatterns = patterns('',
     # Explorer:
     url(r'^$', 'explorer.views.file_list', name='file_list'),        
     url(r'^file/upload', 'explorer.views.file_upload', name='file_upload'),
+
+
+    url(r'^managment/pull-requests$', 'explorer.views.pull_requests'),
     
 #    url(r'^images/(?P<folder>[^/]+)/$', 'explorer.views.folder_images', name='folder_image'),
 #    url(r'^images/$', 'explorer.views.folder_images', {'folder': '.'}, name='folder_image_ajax'),
@@ -37,7 +40,7 @@ urlpatterns = patterns('',
     url(r'^admin/(.*)', admin.site.root),
 
     # Prototypes
-    url(r'^wysiwyg-proto/', include('wysiwyg.urls')),
+#    url(r'^wysiwyg-proto/', include('wysiwyg.urls')),
 
     # Our über-restful api
     url(r'^api/', include('api.urls') ),