Pull requests.
authorŁukasz Rekucki <lrekucki@gmail.com>
Sat, 26 Sep 2009 18:57:01 +0000 (20:57 +0200)
committerŁukasz Rekucki <lrekucki@gmail.com>
Sat, 26 Sep 2009 18:57:01 +0000 (20:57 +0200)
apps/api/forms.py
apps/api/handlers/library_handlers.py
apps/api/management/commands/upload_document.py [deleted file]
apps/api/resources.py
apps/api/urls.py
apps/api/utils.py
apps/explorer/models.py
lib/wlrepo/mercurial_backend/document.py

index 3c393a8..af1b6b5 100644 (file)
@@ -10,7 +10,7 @@ from django import forms
 
 class MergeRequestForm(forms.Form):
     # should the target document revision be updated or shared
-    type = forms.ChoiceField(choices=('update', 'share'))
+    type = forms.ChoiceField(choices=(('update', 'Update'), ('share', 'Share')) )
 
     # which revision to update/share
     target_revision = forms.RegexField('[0-9a-f]{40}')
index 5f844db..6fd7786 100644 (file)
@@ -70,6 +70,8 @@ class LibraryHandler(BaseHandler):
         try:
             lock = lib.lock()            
             try:
+                print "DOCID", docid
+                
                 doc = lib.document_create(docid)
                 # document created, but no content yet
 
@@ -226,11 +228,10 @@ class DocumentTextHandler(BaseHandler):
                     "updated_revision": ndoc.revision
                 }
             except Exception, e:
-                lib.rollback()
-                raise e
-        
-        except (RevisionNotFound, KeyError):
-            return response.EntityNotFound().django_response()
+                lib._rollback()
+                raise e        
+        except RevisionNotFound, e:
+            return response.EntityNotFound().django_response(e)
 
 #
 # Dublin Core handlers
@@ -245,9 +246,9 @@ class DocumentDublinCoreHandler(BaseHandler):
         """Read document as raw text"""        
         try:
             if revision == 'latest':
-                document = lib.document(docid)
+                doc = lib.document(docid)
             else:
-                document = lib.document_for_rev(revision)
+                doc = lib.document_for_rev(revision)
             
             bookinfo = dcparser.BookInfo.from_string(doc.data('xml'))
             return bookinfo.serialize()
@@ -280,20 +281,24 @@ class DocumentDublinCoreHandler(BaseHandler):
                 document.serialize().encode('utf-8'),\
                 message=msg, user=request.user.username)
 
-            return {
-                "document": ndoc.id,
-                "subview": "xml",
-                "previous_revision": prev,
-                "updated_revision": ndoc.revision
-            }
-        except (RevisionNotFound, KeyError):
+            try:
+                # return the new revision number
+                return {
+                    "document": ndoc.id,
+                    "subview": "dc",
+                    "previous_revision": current.revision,
+                    "updated_revision": ndoc.revision
+                }
+            except Exception, e:
+                lib._rollback()
+                raise e
+        except RevisionNotFound:
             return response.EntityNotFound().django_response()
 
-
 class MergeHandler(BaseHandler):
     allowed_methods = ('POST',)
 
-    @validate_form(forms.MergeRequestForm)
+    @validate_form(forms.MergeRequestForm, 'POST')
     @hglibrary
     def create(self, request, form, docid, lib):
         """Create a new document revision from the information provided by user"""
@@ -306,7 +311,7 @@ class MergeHandler(BaseHandler):
         if target_rev == 'latest':
             target_rev = udoc.revision
 
-        if udoc.revision != target_rev:
+        if str(udoc.revision) != target_rev:
             # user think doesn't know he has an old version
             # of his own branch.
             
@@ -328,22 +333,24 @@ class MergeHandler(BaseHandler):
             # update his internal state.
             return response.EntityConflict().django_response({
                     "reason": "out-of-date",
-                    "provided": target_revision,
+                    "provided": target_rev,
                     "latest": udoc.revision })
 
-        if not request.user.has_permission('explorer.pull_request.can_add'):
+        if not request.user.has_perm('explorer.book.can_share'):
             # User is not permitted to make a merge, right away
             # So we instead create a pull request in the database
             prq = PullRequest(
-                commiter=request.uset.username,
+                comitter=request.user,
                 document=docid,
-                source_revision = udoc.revision,
+                source_revision = str(udoc.revision),
                 status="N",
-                comment = form.cleaned_data['comment']
+                comment = form.cleaned_data['comment'] or '$AUTO$ Document shared.'
             )
 
             prq.save()
-            return response.RequestAccepted()
+            return response.RequestAccepted().django_response(\
+                ticket_status=prq.status, \
+                ticket_uri=reverse("pullrequest_view", args=[prq.id]) )
 
         if form.cleanded_data['type'] == 'update':
             # update is always performed from the file branch
diff --git a/apps/api/management/commands/upload_document.py b/apps/api/management/commands/upload_document.py
deleted file mode 100644 (file)
index 66ef924..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-#!/usr/bin/env python
-# -*- conding: utf-8 -*-
-__author__="lreqc"
-__date__ ="$2009-09-08 14:31:26$"
-
-from django.core.management.base import BaseCommand
-from django.utils import simplejson as json
-from django.test.client import Client
-from django.core.urlresolvers import reverse
-
-from optparse import make_option
-
-class Command(BaseCommand):
-    
-    option_list = BaseCommand.option_list + (
-        make_option('-u', '--user', action='store', dest='username'),
-        make_option('-p', '--password', action='store', dest='password'),
-        make_option('-d', '--dublin-core', action='store_true', dest='dc'),
-    )
-    
-    def handle(self, *args, **options):
-        client = Client()
-        if not options['username'] or not options['password']:
-            raise CommandError("You must provide login data")
-
-        client.login(username=options['username'], \
-            password=options['password'])
-
-        print options['username'], options['password']
-        
-        filename = args[0]
-        bookname = args[1]
-
-        print "Uploading '%s' as document '%s'" % (filename, bookname)
-        print "Wth DC template" if options['dc'] else ""
-
-        print client.post( reverse("document_list_view"),\
-        {
-            'bookname': bookname,
-            'ocr_file': open(filename),
-            'generate_dc': options['dc'] } )
-                  
index 113d4b6..2d09787 100644 (file)
@@ -7,9 +7,6 @@ __doc__ = "Module documentation."
 from piston.resource import Resource
 from api.utils import DjangoAuth
 
-
-
-
 authdata = {'authentication': DjangoAuth()}
 
 #
@@ -24,6 +21,11 @@ document_html_resource = Resource(dh.DocumentHTMLHandler, **authdata)
 document_dc_resource = Resource(dh.DocumentDublinCoreHandler, **authdata)
 document_merge = Resource(dh.MergeHandler, **authdata)
 
+import api.handlers.manage_handlers as mh
+
+pullrequest_collection = Resource(mh.PullRequestListHandler, **authdata)
+pullrequest_rsrc = Resource(mh.PullRequestHandler, **authdata)
+
 #
 # Toolbar resources
 #
@@ -31,6 +33,8 @@ import api.handlers.toolbar_handlers as th
 toolbar_buttons = Resource(th.ToolbarHandler, **authdata)
 scriptlets = Resource(th.ScriptletsHandler, **authdata)
 
+
+
 __all__ = [
     'library_resource',
     'document_resource',
@@ -38,5 +42,7 @@ __all__ = [
     'document_dc_resource',
     'document_merge',
     'toolbar_buttons',
-    'scriptlets'
+    'scriptlets',
+    'pullrequest_collection',
+    'pullrequest_rsrc',
 ]
\ No newline at end of file
index b36cbe3..8c47e83 100644 (file)
@@ -18,10 +18,15 @@ urlpatterns = patterns('',
 #    url(r'^hello\.(?P<emitter_format>.+)$', hello_resource),
 
     # Toolbar
-    url(r'^toolbar/buttons$', toolbar_buttons, {'emitter_format': 'json'}),
-
-    # Toolbar
+    url(r'^toolbar/buttons$', toolbar_buttons, {'emitter_format': 'json'}),    
     url(r'^toolbar/scriptlets$', scriptlets, {'emitter_format': 'json'}),
+
+    # Pull requests
+    url(r"^pull-requests$", pullrequest_collection,
+        {'emitter_format': 'json'} ),
+        
+    url(r"^pull-requests/(?P<prq_id>\d+)$", pullrequest_rsrc,
+        {'emitter_format': 'json'}, name="pullrequest_view" ),    
     
     # Documents
     url(r'^documents$', library_resource,
@@ -53,7 +58,7 @@ urlpatterns = patterns('',
         document_dc_resource, {'emitter_format': 'json'},
         name="docdc_view"),
 
-    url(urlpath(r'documents', DOC, 'revision'),
+    url(urlpath(r'documents', DOC, 'revision', format=False),
         document_merge, {'emitter_format': 'json'}, name="docmerge_view")
 
 #    url(r'^documents/(?P<docid>[^/]+)/parts$',
index d8177ab..a52e555 100644 (file)
@@ -39,7 +39,7 @@ def validate_form(formclass, source='GET'):
             form = formclass(getattr(request, source), request.FILES)
 
             if not form.is_valid():
-                errorlist = [{'field': k, 'errors': e} for k, e in form.errors.items()]
+                errorlist = [{'field': k, 'errors': str(e)} for k, e in form.errors.items()]
                 return api.response.BadRequest().django_response(errorlist)
 
             kwargs['form'] = form
index 0a3a252..f2dc8f4 100644 (file)
@@ -56,8 +56,8 @@ class EditorPanel(models.Model):
     
 class Book(models.Model):
     class Meta:
-        permissions = (
-            ("can_add_files", "Can do hg add."),
+        permissions = (            
+            ("can_share", "Can share documents without pull requests."),
         )
         abstract=True
     pass
index 6cf8a5b..523e330 100644 (file)
@@ -32,7 +32,7 @@ class MercurialDocument(wlrepo.Document):
             f.close()
             l._fileadd(r(entry))            
 
-        return self.invoke_and_commit(write, lambda d: (msg, user))
+        return self.invoke_and_commit(write, lambda d: (msg, self.owner))
 
     def invoke_and_commit(self, ops,
             before_commit, rollback=False):
@@ -50,7 +50,7 @@ class MercurialDocument(wlrepo.Document):
                 return self._library.document(docid=self.id, user=user)
             except Exception, e:
                 # rollback the last commit
-                self._library.rollback()
+                self._library._rollback()
                 raise e
         finally:
             lock.release()