-class DocumentDublinCoreHandler(BaseHandler):
- allowed_methods = ('GET', 'PUT')
-
- @validate_form(forms.DocumentEntryRequest)
- def read(self, request, docid):
- """Read document as raw text"""
- lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
- try:
- if request.GET['revision'] == 'latest':
- document = lib.document(docid)
- else:
- document = lib.document_for_rev(request.GET['revision'])
-
- bookinfo = dcparser.BookInfo.from_string(doc.data('xml'))
- return bookinfo.serialize()
- except RevisionNotFound:
- return response.EntityNotFound().django_response()
-
- def update(self, request, docid):
- lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
- try:
- bi_json = request.PUT['contents']
- prev = request.PUT['revision']
- if request.PUT.has_key('message'):
- msg = u"$USER$ " + request.PUT['message']
- else:
- msg = u"$AUTO$ Dublin core update."
-
- current = lib.document(docid, request.user.username)
- orig = lib.document_for_rev(prev)
-
- if current != orig:
- return response.EntityConflict().django_response({
- "reason": "out-of-date",
- "provided": orig.revision,
- "latest": current.revision })
-
- xmldoc = parser.WLDocument.from_string(current.data('xml'))
- document.book_info = dcparser.BookInfo.from_json(bi_json)
-
- # zapisz
- ndoc = current.quickwrite('xml', \
- 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):
- return response.EntityNotFound().django_response()
+#class DocumentDublinCoreHandler(BaseHandler):
+# allowed_methods = ('GET', 'POST')
+#
+# @hglibrary
+# def read(self, request, docid, lib):
+# """Read document as raw text"""
+# try:
+# revision = request.GET.get('revision', 'latest')
+#
+# if revision == 'latest':
+# doc = lib.document(docid)
+# else:
+# doc = lib.document_for_rev(revision)
+#
+#
+# if document.id != docid:
+# return response.BadRequest().django_response({'reason': 'name-mismatch',
+# 'message': 'Provided revision is not valid for this document'})
+#
+# bookinfo = dcparser.BookInfo.from_string(doc.data('xml'))
+# return bookinfo.serialize()
+# except (EntryNotFound, RevisionNotFound), e:
+# return response.EntityNotFound().django_response({
+# 'exception': type(e), 'message': e.message})
+#
+# @hglibrary
+# def create(self, request, docid, lib):
+# try:
+# bi_json = request.POST['contents']
+# revision = request.POST['revision']
+#
+# if request.POST.has_key('message'):
+# msg = u"$USER$ " + request.PUT['message']
+# else:
+# msg = u"$AUTO$ Dublin core update."
+#
+# current = lib.document(docid, request.user.username)
+# orig = lib.document_for_rev(revision)
+#
+# if current != orig:
+# return response.EntityConflict().django_response({
+# "reason": "out-of-date",
+# "provided": orig.revision,
+# "latest": current.revision })
+#
+# xmldoc = parser.WLDocument.from_string(current.data('xml'))
+# document.book_info = dcparser.BookInfo.from_json(bi_json)
+#
+# # zapisz
+# ndoc = current.quickwrite('xml', \
+# document.serialize().encode('utf-8'),\
+# message=msg, user=request.user.username)
+#
+# try:
+# # return the new revision number
+# return {
+# "document": ndoc.id,
+# "subview": "dc",
+# "previous_revision": current.revision,
+# "revision": ndoc.revision,
+# 'timestamp': ndoc.revision.timestamp,
+# "url": reverse("docdc_view", args=[ndoc.id])
+# }
+# except Exception, e:
+# if ndoc: lib._rollback()
+# raise e
+# except RevisionNotFound:
+# return response.EntityNotFound().django_response()
+
+class MergeHandler(BaseHandler):
+ allowed_methods = ('POST',)
+
+ @validate_form(forms.MergeRequestForm, 'POST')
+ @hglibrary
+ def create(self, request, form, docid, lib):
+ """Create a new document revision from the information provided by user"""
+ revision = form.cleaned_data['revision']
+
+ # fetch the main branch document
+ doc = lib.document(docid)
+
+ # fetch the base document
+ user_doc = lib.document_for_rev(revision)
+ base_doc = user_doc.latest()
+
+ if base_doc != user_doc:
+ return response.EntityConflict().django_response({
+ "reason": "out-of-date",
+ "provided": str(user_doc.revision),
+ "latest": str(base_doc.revision)
+ })
+
+ if form.cleaned_data['type'] == 'update':
+ # update is always performed from the file branch
+ # to the user branch
+ changed, clean = base_doc.update(request.user.username)
+
+ # update user document
+ if changed:
+ user_doc_new = user_doc.latest()
+
+ # shared document is the same
+ doc_new = doc
+
+ if form.cleaned_data['type'] == 'share':
+ if not base_doc.up_to_date():
+ return response.BadRequest().django_response({
+ "reason": "not-fast-forward",
+ "message": "You must first update yout branch to the latest version."
+ })
+
+ # check for unresolved conflicts
+ if base_doc.has_conflict_marks():
+ return response.BadRequest().django_response({
+ "reason": "unresolved-conflicts",
+ "message": "There are unresolved conflicts in your file. Fix them, and try again."
+ })
+
+ if not request.user.has_perm('api.document.can_share'):
+ # User is not permitted to make a merge, right away
+ # So we instead create a pull request in the database
+ try:
+ prq, created = PullRequest.objects.get_or_create(
+ comitter = request.user,
+ document = docid,
+ status = "N",
+ defaults = {
+ 'source_revision': str(base_doc.revision),
+ 'comment': form.cleaned_data['message'] or '$AUTO$ Document shared.',
+ }
+ )
+
+ # there can't be 2 pending request from same user
+ # for the same document
+ if not created:
+ prq.source_revision = str(base_doc.revision)
+ prq.comment = prq.comment + 'u\n\n' + (form.cleaned_data['message'] or u'')
+ prq.save()
+
+ return response.RequestAccepted().django_response(\
+ ticket_status=prq.status, \
+ ticket_uri=reverse("pullrequest_view", args=[prq.id]) )
+ except IntegrityError:
+ return response.EntityConflict().django_response({
+ 'reason': 'request-already-exist'
+ })
+
+ changed = base_doc.share(form.cleaned_data['message'])
+
+ # update shared version if needed
+ if changed:
+ doc_new = doc.latest()
+
+ # the user wersion is the same
+ user_doc_new = base_doc
+
+ # The client can compare parent_revision to revision
+ # to see if he needs to update user's view
+ # Same goes for shared view
+
+ return response.SuccessAllOk().django_response({
+ "name": user_doc_new.id,
+ "user": user_doc_new.owner,
+ "parent_revision": user_doc_new.revision,
+ "parent_shared_revision": doc.revision,
+ "revision": user_doc_new.revision,
+ "shared_revision": doc_new.revision,
+ 'timestamp': user_doc_new.revision.timestamp,
+ })
\ No newline at end of file